diff --git a/apps/web/__tests__/unit/rate-limit.test.ts b/apps/web/__tests__/unit/rate-limit.test.ts index 80e8ae37bb3..540bcb6328a 100644 --- a/apps/web/__tests__/unit/rate-limit.test.ts +++ b/apps/web/__tests__/unit/rate-limit.test.ts @@ -324,11 +324,56 @@ describe("isRateLimited outside production", () => { }); }); -describe("isRateLimited in production", () => { +describe("isRateLimited in self-hosted production", () => { beforeEach(() => { vi.stubEnv("NODE_ENV", "production"); }); + it.each([undefined, "", "0"])( + "uses the local authorization limit without probing protected ingress (VERCEL=%s)", + async (vercel) => { + vi.stubEnv("VERCEL", vercel); + mocks.checkRateLimit.mockResolvedValue({ + rateLimited: true, + error: "blocked", + }); + const { isRateLimited, RATE_LIMIT_IDS } = await loadRateLimit(); + const approve = (userId: string) => + isRateLimited(RATE_LIMIT_IDS.AGENT_AUTHORIZATION, { + key: `agent-authorization:${userId}`, + }); + + expect(await callTimes(10, () => approve("user-1"))).toEqual( + Array(10).fill(false), + ); + expect(await approve("user-1")).toBe(true); + expect(await approve("user-2")).toBe(false); + expect(mocks.checkRateLimit).not.toHaveBeenCalled(); + }, + ); + + it("retains per-address limits when no explicit key is supplied", async () => { + const { isRateLimited, RATE_LIMIT_IDS } = await loadRateLimit(); + const fromAddress = (ip: string) => + isRateLimited(RATE_LIMIT_IDS.TRANSLATE_TRANSCRIPT, { + headers: new Headers({ "x-forwarded-for": ip }), + }); + + expect(await callTimes(30, () => fromAddress("198.51.100.7"))).toEqual( + Array(30).fill(false), + ); + expect(await fromAddress("198.51.100.7")).toBe(true); + expect(await fromAddress("198.51.100.8")).toBe(false); + expect(mocks.checkRateLimit).not.toHaveBeenCalled(); + }); +}); + +describe("isRateLimited on Vercel in production", () => { + beforeEach(() => { + vi.stubEnv("NODE_ENV", "production"); + vi.stubEnv("VERCEL", "1"); + }); + it("follows the firewall when its rule answers", async () => { const { isRateLimited, RATE_LIMIT_IDS } = await loadRateLimit(); mocks.checkRateLimit.mockResolvedValue({ rateLimited: false }); diff --git a/apps/web/lib/rate-limit.ts b/apps/web/lib/rate-limit.ts index c3e478228ad..82b75f2abc7 100644 --- a/apps/web/lib/rate-limit.ts +++ b/apps/web/lib/rate-limit.ts @@ -23,9 +23,11 @@ export function firewallRequestHeaders(source: Headers): Headers { * * Each `ruleId` should also exist as a Rate Limit rule in the Vercel Firewall * dashboard with a `@vercel/firewall` condition matching the id. When the - * firewall answers (allowed, limited or blocked) its decision is final. When it - * cannot answer (non-production, rule not configured, missing client address - * or any firewall error) the request is counted against a fixed-window limit + * firewall answers on Vercel (allowed, limited or blocked) its decision is final. + * Self-hosted ingress may return 403 for the Vercel-only endpoint, which the SDK + * treats as a block. Off Vercel, or when the firewall cannot answer (non-production, + * rule not configured, missing client address or any firewall error), the request + * is counted against a fixed-window limit * kept in this process (`FALLBACK_RATE_LIMITS`). That fallback is per instance, * so it is a backstop, not a replacement for the dashboard rule. * @@ -72,7 +74,9 @@ async function checkFirewallRateLimit( headersList: Headers, key: string | undefined, ): Promise { - if (process.env.NODE_ENV !== "production") return undefined; + if (process.env.NODE_ENV !== "production" || process.env.VERCEL !== "1") { + return undefined; + } try { const request = new Request("https://cap.so/api/rate-limit", {