|
| 1 | +import { describe, it, expect, beforeEach, afterEach, vi } from "vitest"; |
| 2 | +import { DEMO_LIMITS, acquireStream, checkWebhookCooldown, clientIp } from "@/lib/demo-limits"; |
| 3 | + |
| 4 | +function ipHeaders(ip: string): Request { |
| 5 | + return new Request("https://orbital.example/api/events/G", { |
| 6 | + headers: { "x-vercel-forwarded-for": ip }, |
| 7 | + }); |
| 8 | +} |
| 9 | + |
| 10 | +/** Each test gets a distinct IP - the limiter state is module-level. */ |
| 11 | +let counter = 0; |
| 12 | +const freshIp = () => `198.51.100.${++counter % 250}${Math.floor(counter / 250)}`; |
| 13 | + |
| 14 | +describe("acquireStream", () => { |
| 15 | + it("permits the configured number of concurrent streams per IP", () => { |
| 16 | + const ip = freshIp(); |
| 17 | + const first = acquireStream(ip); |
| 18 | + expect(first.ok).toBe(true); |
| 19 | + |
| 20 | + const second = acquireStream(ip); |
| 21 | + expect(second.ok).toBe(false); |
| 22 | + if (!second.ok) { |
| 23 | + expect(second.body.reason).toBe("per_ip_stream_limit"); |
| 24 | + expect(second.body.upgradeUrl).toBe(DEMO_LIMITS.upgradeUrl); |
| 25 | + } |
| 26 | + }); |
| 27 | + |
| 28 | + it("frees the slot on release", () => { |
| 29 | + const ip = freshIp(); |
| 30 | + const slot = acquireStream(ip); |
| 31 | + expect(slot.ok).toBe(true); |
| 32 | + if (!slot.ok) return; |
| 33 | + |
| 34 | + slot.release(); |
| 35 | + expect(acquireStream(ip).ok).toBe(true); |
| 36 | + }); |
| 37 | + |
| 38 | + it("is idempotent on repeated release, so a double teardown cannot mint slots", () => { |
| 39 | + const ip = freshIp(); |
| 40 | + const slot = acquireStream(ip); |
| 41 | + if (!slot.ok) throw new Error("expected slot"); |
| 42 | + |
| 43 | + // Both the abort listener and the session timer call close() in the SSE |
| 44 | + // routes; the guard there is belt, this is braces. |
| 45 | + slot.release(); |
| 46 | + slot.release(); |
| 47 | + slot.release(); |
| 48 | + |
| 49 | + const a = acquireStream(ip); |
| 50 | + expect(a.ok).toBe(true); |
| 51 | + expect(acquireStream(ip).ok).toBe(false); |
| 52 | + }); |
| 53 | + |
| 54 | + it("tracks IPs independently", () => { |
| 55 | + const a = freshIp(); |
| 56 | + const b = freshIp(); |
| 57 | + expect(acquireStream(a).ok).toBe(true); |
| 58 | + expect(acquireStream(b).ok).toBe(true); |
| 59 | + }); |
| 60 | +}); |
| 61 | + |
| 62 | +describe("checkWebhookCooldown", () => { |
| 63 | + afterEach(() => vi.useRealTimers()); |
| 64 | + |
| 65 | + it("allows the first call and rejects an immediate second", () => { |
| 66 | + const ip = freshIp(); |
| 67 | + expect(checkWebhookCooldown(ip).ok).toBe(true); |
| 68 | + |
| 69 | + const second = checkWebhookCooldown(ip); |
| 70 | + expect(second.ok).toBe(false); |
| 71 | + if (!second.ok) { |
| 72 | + expect(second.body.reason).toBe("rate_limit"); |
| 73 | + expect(second.body.retryAfterMs).toBeGreaterThan(0); |
| 74 | + expect(second.body.retryAfterMs).toBeLessThanOrEqual(DEMO_LIMITS.webhookCooldownMs); |
| 75 | + } |
| 76 | + }); |
| 77 | + |
| 78 | + it("allows again once the cooldown has elapsed", () => { |
| 79 | + vi.useFakeTimers(); |
| 80 | + const ip = freshIp(); |
| 81 | + expect(checkWebhookCooldown(ip).ok).toBe(true); |
| 82 | + expect(checkWebhookCooldown(ip).ok).toBe(false); |
| 83 | + |
| 84 | + vi.advanceTimersByTime(DEMO_LIMITS.webhookCooldownMs + 1); |
| 85 | + expect(checkWebhookCooldown(ip).ok).toBe(true); |
| 86 | + }); |
| 87 | +}); |
| 88 | + |
| 89 | +describe("limits are keyed on the trusted identity", () => { |
| 90 | + it("a rotating forged XFF cannot escape the webhook cooldown", () => { |
| 91 | + const real = freshIp(); |
| 92 | + // Every request comes from the same real peer; only the forged prefix moves. |
| 93 | + const requests = ["1.1.1.1", "2.2.2.2", "3.3.3.3"].map( |
| 94 | + (forged) => |
| 95 | + new Request("https://orbital.example/api/webhook-sample", { |
| 96 | + headers: { "x-vercel-forwarded-for": real, "x-forwarded-for": forged }, |
| 97 | + }), |
| 98 | + ); |
| 99 | + |
| 100 | + const results = requests.map((req) => checkWebhookCooldown(clientIp(req)).ok); |
| 101 | + expect(results).toEqual([true, false, false]); |
| 102 | + }); |
| 103 | + |
| 104 | + it("resolves the same bucket for the same peer regardless of forged headers", () => { |
| 105 | + const real = freshIp(); |
| 106 | + expect(clientIp(ipHeaders(real))).toBe(real); |
| 107 | + }); |
| 108 | +}); |
0 commit comments