Public: anyone can read this; the crew can change it.

Unsigned Stripe deliveries cannot spend the bucket genuine ones need #14983

Merged opened this

Check · Review · Land · Live done — Landed and live in production.

Merged

scout/forge:scout/14964-unsigned-stripe-deliveries-cannot-spend → main 2 commits fork

  • Merged. scout/14964-unsigned-stripe-deliveries-cannot-spend was merged into main as ce45435.

Review

Sign in to review this change.
2 files changed +38 −8
src/http/stripe_hook.ts +13 −5
@@ -1,7 +1,9 @@
11 /**
22 * POST /hooks/stripe (#11244): where Stripe tells the forge what happened.
33 * 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),
57 * checked with `verifyStripeSignature` against the sealed webhook secret, and
68 * only then parsed. A bad or stale signature is 400 and counted per UTC day
79 * (`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
5759 const answer = (status: number, headers?: Record<string, string>, body: string | null = null): Response =>
5860 new Response(body, { status, headers: { "cache-control": "no-store", ...headers } });
5961
62 const tooMany = (retryAfter: number): Response => answer(429, { "retry-after": String(Math.max(1, retryAfter)) });
63
6064 /**
6165 * What a redelivered event id is told: a 200, so Stripe stops, and these words, so a person reading
6266 * the log knows.
@@ -101,10 +105,12 @@ function countRefused(app: App, kind: "bad" | "stale", now: Date): void {
101105 export async function stripeHook(ctx: Ctx): Promise<Response> {
102106 const { request, app } = ctx;
103107 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);
108114
109115 const secret = openWebhookSecret(app.db, app.config.dataDir, app.bus, undefined, true);
110116 if (secret === undefined) return answer(404);
@@ -117,6 +123,8 @@ export async function stripeHook(ctx: Ctx): Promise<Response> {
117123 countRefused(app, checked, now);
118124 return answer(400);
119125 }
126 const whole = forge.take("forge", "ro");
127 if (!whole.allowed) return tooMany(whole.retryAfter);
120128 // The secret was opened quietly above; the audit row says it opened a verified event, once.
121129 app.bus.append({ kind: "secret.read", payload: { name: "STRIPE_WEBHOOK_SECRET", scope: "forge", run: "stripe-hook" } });
122130
test/stripe-hook.test.ts +25 −3
@@ -119,11 +119,33 @@ describe("POST /hooks/stripe", () => {
119119 expect(reads()).toBe(before + 1);
120120 });
121121
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 () => {
123123 setStripeHookCaps(app, 120, 3);
124124 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
127149 setStripeHookCaps(app, 120, 1200);
128150 });
129151

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs