Public: anyone can read this; the crew can change it.
POST /hooks/stripe verifies the signature on the raw body and turns each event into an entitlement #11219
POST /hooks/stripe now verifies the signature on the raw body and turns each event into an entitlement. bun scripts/gate.ts, tsc, design-lint and 267 tests across 28 files pass, including the new test/stripe-hook.test.ts (13 tests).
stopped parked by the crew deploy edbe868
3 earlier comments
POST /hooks/stripe now verifies the signature on the raw body and turns each event into an entitlement.
bun scripts/gate.ts, tsc, design-lint and 267 tests across 28 files pass, including the new test/stripe-hook.test.ts (13 tests). scripts/fast.ts named about 1,800 files, so I ran the billing, stripe, csp-report, schema, route and security-floor suites instead. I did not run the forge's full check, and I did not try the route on the deployed preview.
- Signature (
src/billing/signature.ts): the HMAC-SHA256 runs over${t}.plus the raw request bytes, keyed by thewhsec_…string as UTF-8. Everyv1is compared in constant time, other header keys are ignored, and a timestamp more than 300 s off is refused. The tests cover a good signature, a bad one, a stale timestamp, twov1values and a body whose bytes differ from its re-serialized JSON. - Route (
src/http/stripe_hook.ts):- A refused delivery answers 400 and counts toward an alarm that shows on Billing from 5 a day.
- Each event id is kept once; a repeat answers 200 and does nothing.
- The route is rate limited per client and for the whole forge (60 and 600 a minute), reads no cookie and sets none.
- The answer is sent after local work only.
checkout.session.completedmarks the order (client_reference_id) paid and binds its customer and subscription.
- Reconcile (
src/billing/reconcile.ts): any subscription or invoice event re-reads the subscription from Stripe after the answer and writes what Stripe says now, so out-of-order events end on the latest truth.activeandtrialingbecomeactive,past_duebecomesgrace, and anything else becomesnone. The plan comes from the paid order. - Sweep: an hourly timer in
src/main.tsruns a reconcile of every paid subscription once a day. - Probe: Settings → Billing → Provider shows the last event and when, refusals today, and the last daily check.
- Migration and docs: the migration
20261111120000-stripe-hookis pinned.docs/features/billing.mdhas the how-to.
Not done:
- Order creation: nothing creates orders yet. Checkout is a separate piece, so
createOrderinsrc/billing/orders.tsis called only by tests until it lands. - Reconcile failures: a reconcile that fails after the answer is not retried; the daily sweep heals it.
- Secret-read audit: reading the signing secret writes an unattributed
secret.readevent per delivery. I left the actor empty because the event table's actor column is a foreign key to users.
To try: on the preview, open Settings → Billing → Provider with Stripe connected and a signing secret saved. Sign ${t}.${body} with that secret and POST it to /hooks/stripe with Stripe-Signature: t=…,v1=…; the probe then shows the event.
I fixed the reserved-names failure and pushed. The /map and / budget failure I could not reproduce, so I left it alone.
- Reserved names:
POST /hooks/stripemadehooksa top-level path, andhookswas not inSYSTEM_NAMES, so a user could have claimed it as a username. I added it tosrc/core/reserved.ts.test/reserved-names.test.tsnow passes, andbun x tsc --noEmitis clean. - Budget test: "budgets > /map and / answer under their p95 and statement budgets" took 1650 ms on shard 44 in the check, and it passes locally. This pull's change touches neither
/nor/map. The only code it adds to the request path is the one extra route insrc/http/server.ts. I read this as a slow shard, not a regression. If it fails again on the same assertion, it needs a closer look at what/does under load.
I only ran test/reserved-names.test.ts and test/snappy.test.ts locally, not the full suite. The forge's check runs everything on this push.
The last check was stopped by the forge's CPU limit, not by a failing test, so I changed no code and pushed an empty commit to re-run it.
- What the log shows: the run used 16.7 cores for 30 s against a limit of 16. The
egress refusedlines come from a browser-based test hitting Google and DuckDuckGo update hosts. They are probably not what broke the run, and that traffic is not from this pull. - This pull:
test/stripe-hook.test.tspasses 13 of 13 in about 2.6 s, so the new tests are not a heavy CPU user. The one new background job is an hourly billing-sweep timer, which does no work when the tests run. - Not run locally: the forge's full 96-shard check. If the re-run is stopped by the CPU limit again, it will need a person to look at the check's capacity, not this code.
The earlier fix (reserving the name hooks) is already in this push.
POST /hooks/stripenow verifies the signature on the raw body and turns each event into an entitlement.bun scripts/gate.ts,tsc, design-lint and 267 tests across 28 files pass, including the newtest/stripe-hook.test.ts(13 tests).scripts/fast.tsnamed about 1,800 files, so I ran the billing, stripe, csp-report, schema, route and security-floor suites instead. I did not run the forge's full check, and I did not try the route on the deployed preview.src/billing/signature.ts): the HMAC-SHA256 runs over${t}.plus the raw request bytes, keyed by thewhsec_…string as UTF-8. Everyv1is compared in constant time, other header keys are ignored, and a timestamp more than 300 s off is refused. The tests cover a good signature, a bad one, a stale timestamp, twov1values and a body whose bytes differ from its re-serialized JSON.src/http/stripe_hook.ts):checkout.session.completedmarks the order (client_reference_id) paid and binds its customer and subscription.src/billing/reconcile.ts): any subscription or invoice event re-reads the subscription from Stripe after the answer and writes what Stripe says now, so out-of-order events end on the latest truth.activeandtrialingbecomeactive,past_duebecomesgrace, and anything else becomesnone. The plan comes from the paid order.src/main.tsruns a reconcile of every paid subscription once a day.20261111120000-stripe-hookis pinned.docs/features/billing.mdhas the how-to.Not done:
createOrderinsrc/billing/orders.tsis called only by tests until it lands.secret.readevent per delivery. I left the actor empty because the event table's actor column is a foreign key to users.To try: on the preview, open Settings → Billing → Provider with Stripe connected and a signing secret saved. Sign
${t}.${body}with that secret and POST it to/hooks/stripewithStripe-Signature: t=…,v1=…; the probe then shows the event.Closes #11137