Highlights

Contents

Part VII
07

Webhooks

We receive events that other companies push to us over HTTP, and we send our own to restaurants. A webhook endpoint is a public door that anyone on the internet can knock on, so every request must prove where it came from. We will cover the receiving flow, signatures, timestamps, replays and secret rotation, and duplicates, ordering and sending webhooks reliably.

CHAPTER ATLAS / FOLLOW THE READING PATHCHAPTER ATLAS / FOLLOW THE READING PATH1Rate limiting2More limiting algorithms3Distributed and securitylimits4Timeouts5Retries6Circuit breakers andbulkheads7Webhooks8Third-party APIs9Email10The payment call end toend

#Section 19Receiving a webhook

When a payment succeeds, Wren’s payment provider does not wait to be asked. It sends an HTTP POST to https://api.wren.example/webhooks/stripe with a JSON body describing the event, payment_intent.succeeded for payment intent pi_3N…. This is a webhook, an HTTP callback from one system to another when something happens. Payment providers, version control hosts, messaging platforms and shipping companies all use them, and receiving them correctly is a standard backend skill.

The design that survives production has five steps, and the order matters. First, verify that the request really came from the provider, by checking its signature, section 20. Second, persist the raw event, keyed by the provider’s event id with a unique constraint, in an inbox table, Unit VIII. Third, acknowledge quickly, returning 200 within a second or two. Fourth, process asynchronously, with a worker reading the inbox and doing the actual work, marking the order paid, notifying the kitchen, sending the receipt. Fifth, handle failures in that processing with the retries and dead-letter queues of Unit VIII.

RECEIVING A WEBHOOK: VERIFY, STORE, ANSWER FAST, WORK LATERRECEIVING A WEBHOOK: VERIFY, STORE, ANSWER FAST, WORK LATERStripeverifyHMAC + timepersistevent id unique200 OKunder 2 sinboxworker: processslow processing inside the request causes timeouts, and the provider retries, so duplicates multiply
Figure 10.25Verify, persist, answer within a couple of seconds, and let a worker do the real work.

Acknowledging before processing is the counterintuitive part. Providers wait only a short time for a response, a few seconds to tens of seconds depending on the provider, and treat anything other than a 2xx within that time as a failure to retry. If Wren processed the event inside the request, a slow database or a slow kitchen call would make the response late, the provider would retry, and Wren would receive the same event again while still processing the first. Under load, retries pile on retries. Persisting and acknowledging takes milliseconds and removes the problem, and the inbox makes the eventual processing idempotent.

The endpoint itself needs the usual defences. It accepts only POST with the expected content type, limits the body size, is excluded from CSRF protection and session middleware that do not apply to it, and has its own rate limit and bulkhead, so a flood of webhooks, genuine or not, cannot starve the rest of the API. It also returns 2xx for events it does not care about, after verifying them, since a provider that keeps getting errors for an event type will keep retrying it and may eventually disable the endpoint.

Providers retry failed deliveries over long periods. Stripe retries for up to three days with exponential backoff, and GitHub and others have their own schedules. A short outage at Wren therefore means a burst of delayed webhooks when it recovers, which the inbox and asynchronous processing absorb.

#Section 20Signatures, timestamps, replays and rotation

Anyone can send a POST to Wren’s webhook URL. An attacker who sends a fake payment_intent.succeeded event for order 124 would get food without paying, unless Wren checks that the event is genuine. Providers sign each webhook with a shared secret, and the receiver must verify the signature before trusting anything in the body.

The common scheme uses an HMAC, a hash-based message authentication code, usually HMAC-SHA256. The provider and Wren share a secret, whsec_…, set up when the endpoint was registered. For each delivery, the provider computes the HMAC of the timestamp, a dot and the raw request body, and sends it in a header, as in Stripe’s Stripe-Signature: t=1791200000,v1=5257a869…. Wren recomputes the HMAC over the same bytes with its copy of the secret and compares the two. Only someone who knows the secret could have produced a matching signature, and any change to the body or timestamp changes it.

CHECKING A SIGNATURE: RECOMPUTE THE HMAC OVER TIMESTAMP AND RAW BODYCHECKING A SIGNATURE: RECOMPUTE THE HMAC OVER TIMESTAMP AND RAW BODYStripe-Signature: t=1791200000, v1=5257a869e7ec…signed payload = "1791200000" + "." + raw bodywhsec_… secretHMAC-SHA256compare in constant timeuse the raw bytes: re-serialized JSON changes spacing and breaks the signature
Figure 10.26Recompute the HMAC over the timestamp and the exact raw body, and compare in constant time.

Three implementation details are where receivers go wrong. Use the raw bytes of the body, exactly as received, not JSON that has been parsed and re-serialized, which changes spacing and key order and breaks the signature. Compare in constant time, with a function such as hmac.compare_digest or crypto.timingSafeEqual, because an ordinary string comparison that stops at the first differing byte leaks how many leading bytes matched, which can let an attacker forge a signature byte by byte. And reject requests with no signature or an unknown scheme rather than skipping the check.

The timestamp prevents replay attacks. An attacker who captures one genuine webhook, from a log or a misconfigured proxy, could send it again later. The signature would still verify, since the body and timestamp are unchanged. Including the timestamp in the signed payload and rejecting deliveries whose timestamp is more than 300 s from Wren’s clock makes old captures useless. Within the 5-minute window, the inbox’s unique event id rejects a replay as a duplicate.

A REPLAYED WEBHOOK: VALID SIGNATURE, STALE TIMESTAMPA REPLAYED WEBHOOK: VALID SIGNATURE, STALE TIMESTAMPsigned 20:00tolerance 5 minreplayed 20:10reject if |now − t| > 300 s; the event id check catches replays inside the window
Figure 10.27A captured webhook replayed ten minutes later. The signature is valid and the timestamp is too old.

Secrets must be rotated, after a leak, when an engineer with access leaves, or on a schedule. Rotation without dropping events needs an overlap. Wren generates a new secret, configures its receiver to accept signatures from either the old or the new one, tells the provider to start signing with the new one, and some time later removes the old one. Stripe supports exactly this by sending signatures from both secrets during a rotation window.

ROTATING A WEBHOOK SECRET WITHOUT DROPPING EVENTSROTATING A WEBHOOK SECRET WITHOUT DROPPING EVENTSold secret validnew secret validoverlap: provider signs with both; receiver accepts eitheradd the new secret, verify against both, switch the provider, then drop the old one
Figure 10.28For a while both secrets are valid. The receiver accepts either until the old one is retired.

#Section 21Duplicates, ordering and sending webhooks

Webhooks are delivered at least once, so duplicates are normal. A provider that does not receive Wren’s 200, because of a network blip or a slow response, sends the event again with the same event id. The inbox’s unique constraint on the event id turns the second delivery into a no-op that still returns 200.

Webhooks are also not delivered in order. The provider may send charge.refunded before payment_intent.succeeded arrives, if the first delivery failed and was retried. A receiver that applies events as state changes in arrival order can end up wrong. The robust approach treats each webhook as a hint that something changed. The worker uses the event to look up the current state of the object from the provider’s API, “what is the status of payment intent pi_3N… now?”, and acts on that. Order no longer matters, and a missed webhook is caught by the next one or by a periodic reconciliation job that compares Wren’s records with the provider’s.

WEBHOOKS ARRIVE TWICE AND OUT OF ORDER; TREAT THEM AS HINTSWEBHOOKS ARRIVE TWICE AND OUT OF ORDER; TREAT THEM AS HINTSevt_1 payment_intent.succeededevt_2 charge.refundedevt_1 payment_intent.succeededarrival order: 1, 2, 1 againevent_idseenevt_1yesevt_2yesduplicate evt_1: skipfor state, fetch the object from the provider's API and act on its current statusthen the order in which hints arrive stops mattering
Figure 10.29Event 1 arrives twice and after event 2. Deduplicate by id, and fetch the current state.

Wren also sends webhooks, notifying restaurants’ point-of-sale systems about new orders. Sending well mirrors receiving well. Each delivery carries an event id, a timestamp and an HMAC signature over both and the body, with a per-restaurant secret. Deliveries go out from a queue, not from the request path, with a 10 s timeout. Failures are retried with jittered exponential backoff, after 1 minute, 5, 30, 2 hours, 8 and 24, continuing for up to three days.

WREN SENDING WEBHOOKS TO RESTAURANTS: SIGN, RETRY, AND EVENTUALLY STOPWREN SENDING WEBHOOKS TO RESTAURANTS: SIGN, RETRY, AND EVENTUALLY STOPWrennow1 m5 m30 m2 h8 h24 hretry schedule with jitterafter 3 days of failures: disable the endpoint, email the restaurant, keep events for replayevery delivery has an event id, a timestamp and an HMAC signature, and a 10 s timeout
Figure 10.30Wren’s retry schedule for a restaurant’s endpoint, stretching from minutes to a day.

An endpoint that keeps failing must not consume capacity forever. After three days of failures, Wren disables the endpoint, emails the restaurant, and keeps the undelivered events so they can be replayed when the restaurant fixes their system. A dashboard shows each restaurant their recent deliveries, responses and retries, which saves countless support tickets. And because receivers may be slow, Wren’s sending workers have their own bulkhead, so one restaurant’s slow endpoint cannot delay everyone else’s notifications.

Webhook URLs are supplied by customers, which makes them a server-side request forgery risk. A restaurant could register http://169.254.169.254/ or an internal address and make Wren’s servers call it. Unit XIII covers the defences, resolving the address, refusing private ranges and sending from an isolated egress proxy.