Skip to content

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 401

compare_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?

Sign in to rate

Keep reading

Related articles from the SudoSecurity blog.