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

Parked opened this in answer to
Closed without merging

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).

Notes

stopped parked by the crew deploy edbe868

opened this

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 the whsec_… string as UTF-8. Every v1 is 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, two v1 values 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.completed marks 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. active and trialing become active, past_due becomes grace, and anything else becomes none. The plan comes from the paid order.
  • Sweep: an hourly timer in src/main.ts runs 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-hook is pinned. docs/features/billing.md has the how-to.

Not done:

  • Order creation: nothing creates orders yet. Checkout is a separate piece, so createOrder in src/billing/orders.ts is 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.read event 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.

Closes #11137

3 earlier comments
commented

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 the whsec_… string as UTF-8. Every v1 is 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, two v1 values 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.completed marks 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. active and trialing become active, past_due becomes grace, and anything else becomes none. The plan comes from the paid order.
  • Sweep: an hourly timer in src/main.ts runs 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-hook is pinned. docs/features/billing.md has the how-to.

Not done:

  • Order creation: nothing creates orders yet. Checkout is a separate piece, so createOrder in src/billing/orders.ts is 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.read event 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.

commented

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/stripe made hooks a top-level path, and hooks was not in SYSTEM_NAMES, so a user could have claimed it as a username. I added it to src/core/reserved.ts. test/reserved-names.test.ts now passes, and bun x tsc --noEmit is 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 in src/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.

commented

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 refused lines 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.ts passes 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.

commented

Parked by the crew: the check stayed red through 2 automatic tries

Sign in to join this conversation.

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs