Public: anyone can read this; the crew can change it.
Unsigned Stripe deliveries cannot spend the bucket genuine ones need #14983
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/14964-unsigned-stripe-deliveries-cannot-spend → main 2 commits fork
2 files
changed +38
−8
src/http/stripe_hook.ts +13 −5
| @@ -1,7 +1,9 @@ | ||
| 1 | 1 | /** |
| 2 | 2 | * POST /hooks/stripe (#11244): where Stripe tells the forge what happened. |
| 3 | 3 | * Public and cookie-free: the handler never reads a cookie or sets one, and |
| 4 | * the signature is the only credential. The raw body is read first (bounded), | |
| 4 | * the signature is the only credential. Each sender is rate limited by address | |
| 5 | * before any work; the whole-forge cap counts only verified deliveries, so | |
| 6 | * unsigned traffic cannot crowd out Stripe's. The raw body is read (bounded), | |
| 5 | 7 | * checked with `verifyStripeSignature` against the sealed webhook secret, and |
| 6 | 8 | * only then parsed. A bad or stale signature is 400 and counted per UTC day |
| 7 | 9 | * (`stripe_hook_refused`). A verified event's id is stored once; a repeated id |
| @@ -57,6 +59,8 @@ export function setStripeHookCaps(app: App, perClient: number, wholeForge: numbe | ||
| 57 | 59 | const answer = (status: number, headers?: Record<string, string>, body: string | null = null): Response => |
| 58 | 60 | new Response(body, { status, headers: { "cache-control": "no-store", ...headers } }); |
| 59 | 61 | |
| 62 | const tooMany = (retryAfter: number): Response => answer(429, { "retry-after": String(Math.max(1, retryAfter)) }); | |
| 63 | ||
| 60 | 64 | /** |
| 61 | 65 | * What a redelivered event id is told: a 200, so Stripe stops, and these words, so a person reading |
| 62 | 66 | * the log knows. |
| @@ -101,10 +105,12 @@ function countRefused(app: App, kind: "bad" | "stale", now: Date): void { | ||
| 101 | 105 | export async function stripeHook(ctx: Ctx): Promise<Response> { |
| 102 | 106 | const { request, app } = ctx; |
| 103 | 107 | const { client, forge } = bucketsOf(app); |
| 104 | for (const [bucket, key] of [[client, clientBucket(request)], [forge, "forge"]] as const) { | |
| 105 | const verdict = bucket.take(key, "ro"); | |
| 106 | if (!verdict.allowed) return answer(429, { "retry-after": String(Math.max(1, verdict.retryAfter)) }); | |
| 107 | } | |
| 108 | // Every request, signed or not, is charged to its address first: that bounds the hashing one | |
| 109 | // sender can cause, and the body is capped before it is hashed. The whole-forge bucket is charged | |
| 110 | // only after the signature verifies, so a flood of unsigned requests from many addresses cannot | |
| 111 | // spend the room genuine deliveries need (#14964). | |
| 112 | const own = client.take(clientBucket(request), "ro"); | |
| 113 | if (!own.allowed) return tooMany(own.retryAfter); | |
| 108 | 114 | |
| 109 | 115 | const secret = openWebhookSecret(app.db, app.config.dataDir, app.bus, undefined, true); |
| 110 | 116 | if (secret === undefined) return answer(404); |
| @@ -117,6 +123,8 @@ export async function stripeHook(ctx: Ctx): Promise<Response> { | ||
| 117 | 123 | countRefused(app, checked, now); |
| 118 | 124 | return answer(400); |
| 119 | 125 | } |
| 126 | const whole = forge.take("forge", "ro"); | |
| 127 | if (!whole.allowed) return tooMany(whole.retryAfter); | |
| 120 | 128 | // The secret was opened quietly above; the audit row says it opened a verified event, once. |
| 121 | 129 | app.bus.append({ kind: "secret.read", payload: { name: "STRIPE_WEBHOOK_SECRET", scope: "forge", run: "stripe-hook" } }); |
| 122 | 130 |
test/stripe-hook.test.ts +25 −3
| @@ -119,11 +119,33 @@ describe("POST /hooks/stripe", () => { | ||
| 119 | 119 | expect(reads()).toBe(before + 1); |
| 120 | 120 | }); |
| 121 | 121 | |
| 122 | test("a whole-forge cap answers 429 across clients", async () => { | |
| 122 | test("a whole-forge cap answers 429 across clients, counting only signed deliveries", async () => { | |
| 123 | 123 | setStripeHookCaps(app, 120, 3); |
| 124 | 124 | const codes: number[] = []; |
| 125 | for (let i = 0; i < 5; i++) codes.push((await post("{}", "t=1,v1=00")).status); // each from a fresh address | |
| 126 | expect(codes).toEqual([400, 400, 400, 429, 429]); | |
| 125 | for (let i = 0; i < 5; i++) { | |
| 126 | const body = evt(`evt_cap_${i}`, "ping.neutral"); | |
| 127 | codes.push((await post(body, await sign(body))).status); // each from a fresh address | |
| 128 | } | |
| 129 | expect(codes).toEqual([200, 200, 200, 429, 429]); | |
| 130 | setStripeHookCaps(app, 120, 1200); | |
| 131 | }); | |
| 132 | ||
| 133 | test("a flood of unsigned requests from many addresses does not crowd out a signed delivery", async () => { | |
| 134 | setStripeHookCaps(app, 120, 3); | |
| 135 | for (let i = 0; i < 20; i++) expect((await post("{}", "t=1,v1=00")).status).toBe(400); // each from a fresh address | |
| 136 | const body = evt("evt_after_flood", "ping.neutral"); | |
| 137 | expect((await post(body, await sign(body))).status).toBe(200); | |
| 138 | expect(events()).toContainEqual({ id: "evt_after_flood", type: "ping.neutral" }); | |
| 139 | setStripeHookCaps(app, 120, 1200); | |
| 140 | }); | |
| 141 | ||
| 142 | test("one address flooding unsigned requests is held by its own bucket", async () => { | |
| 143 | setStripeHookCaps(app, 2, 1200); | |
| 144 | const from = { "x-forwarded-for": "10.98.0.1" }; | |
| 145 | const hit = () => fetch(`${base}/hooks/stripe`, { method: "POST", headers: { ...from, "stripe-signature": "t=1,v1=00" }, body: "{}" }); | |
| 146 | expect([(await hit()).status, (await hit()).status, (await hit()).status]).toEqual([400, 400, 429]); | |
| 147 | const body = evt("evt_other_address", "ping.neutral"); | |
| 148 | expect((await post(body, await sign(body))).status).toBe(200); // another address is untouched | |
| 127 | 149 | setStripeHookCaps(app, 120, 1200); |
| 128 | 150 | }); |
| 129 | 151 |
new above
offline