Lesson 12 of 14 · 20m
Webhooks: Signing Outbound, Verifying Inbound
HMAC signatures over raw bodies, timestamp tolerances, and at-least-once delivery that receivers dedupe by event ID.
Your API needs to tell consumers "invoice paid," "deploy finished," "key rotated." The mechanism is an HTTP POST to a URL the consumer registered — which makes your API the client now, and the consumer's endpoint an unauthenticated-by-design server that anyone on the internet can POST to. Everything in this lesson is about the two directions of trust that creates.
Outbound: you sign, they verify
The payload is signed with HMAC-SHA256 over timestamp + "." + body, with the timestamp in the signature header. Stripe's Stripe-Signature: t=...,v1=... is the reference format; GitHub sends X-Hub-Signature-256: sha256=...; Slack sends X-Slack-Signature. The timestamp does two jobs: it lets the receiver reject stale deliveries (a five-minute tolerance kills replay), and it keys the HMAC input so a captured signature can't be re-minted on a different body. Rotate the signing secret on a schedule and version the signatures — v1 in Stripe's header is how multiple secrets coexist during rollover without breaking consumers mid-rotation.
Deliveries are at-least-once: retries with exponential backoff on any non-2xx, dedupe by a unique event ID in the body (id: "evt_01JZY8..."), ordering explicitly not guaranteed — the consumer's job is to be idempotent, which is the same Idempotency-Key lesson run backwards. Document it or they'll build on a lie.
Inbound: the receiver's side
Verify on the raw body. The HMAC covers bytes, not a parsed object — express.json(), json.loads, anything that re-serializes breaks the signature because JSON key order and whitespace aren't canonical. The correct shape:
sig = request.headers["X-Signature"]
expected = "v1=" + hmac.new(secret, f"{ts}.{raw_body}".encode(), hashlib.sha256).hexdigest()
if not hmac.compare_digest(sig, expected):
return 401compare_digest/timingSafeEqual — constant-time comparison, because == leaks the prefix through response timing. Check t against a tolerance window before the HMAC check so old captures fail cheap. Then the event ID goes into your dedupe store (the idempotency lesson's transaction trick, one table over), and only then do you process.
One subtlety: signature verification is the authentication. TLS authenticates the transport — it proves nobody read the wire, not that the sender held your secret. Anyone can open a TLS connection to your endpoint; only the secret-holder produces a valid HMAC. This is also why the secret can't live in the URL path: registered webhook URLs ending in ?key=... land in logs and referrers, the same reason Bearer tokens don't go in query strings (the tokens lesson).
The registration endpoint is your SSRF surface
Whoever registers a webhook URL picks where your server sends POSTs — that endpoint is the "fetch this URL" feature from the SSRF lesson wearing a different hat. Validate at registration: resolve-and-pin, block internal ranges, re-check on every delivery because DNS changes after registration. And rate-limit the registration path itself — webhook-as-a-service exists partly because running this correctly is work; Svix and friends sell the whole subsystem.
The honest contract
Tell consumers what you'll do: which events, which signature scheme, what t tolerance, how many retries, what dedupe key, how you rotate secrets, what the failure mode is when they're down for a week. Then their side has its own list — and if they skip verification because "it works in dev," their endpoint is a forged-event intake. That's the bug you'll see in their postmortem.
Try this
Implement both ends for a invoice.paid event: the signing side (HMAC over t.body, versioned secret), the verifying side (raw body, timestamp tolerance, compare_digest, event-ID dedupe), and the registration endpoint (URL validation, internal-range block, re-check per delivery). Then replay a captured delivery and see whether your dedupe catches it.
Pitfalls
- Verifying parsed JSON — the HMAC covers bytes; re-serialization breaks it.
==on signatures — timing oracles leak prefixes; constant-time compare.- No timestamp in the HMAC — replayed signatures stay valid forever.
- Secret in the URL — it lands in logs; the header is the credential.
- Register-once-trust-forever — DNS moves; validate at delivery, not just at setup.
Was this lesson helpful?
Keep reading
Related articles from the SudoSecurity blog.
Building a Production REST API With Node.js and Express
Build a REST API in Node.js and Express the way production systems need: input validation, JWT auth, refresh token rotation, status codes, rate limiting, idempotency keys, and centralized error handling.
GraphQL Over HTTP Is Finally a Spec. Your Server Probably Still Says 200.
The GraphQL over HTTP specification shipped its first edition on November 12, ending the decade of '200 Not OK.' Validation errors get 422, partial results get a new 294 status, and a dedicated media type tells clients the body is safe to parse.
Strings Are Lying to You: Unicode Length, Case and Normalization in PHP, Python and JavaScript
One family emoji is 18 bytes, 8 JavaScript code units, 5 Python characters and 1 thing a human sees. I ran the same Unicode tests through PHP 8.6, Python 3.15 and Node 26: each gave a different answer for length, case and truncation, a KELVIN SIGN lowercases into a real username, and PHP 8.6's...
PHP and WordPress Patched a Day Apart. Your Front End Decides Which PHP Bugs Reach You.
PHP 8.5.13, 8.4.28, 8.3.37 and 8.2.36 fix 18 CVEs, seven rated 9.2, and WordPress 7.1.5 fixes 20 more issues. Most of the PHP bugs only fire under specific settings, so I checked the official Docker images for each condition. The images were patched the same day, and the patch quietly turns off...