Lesson 2 of 9 · 26m
Signing Webhooks Correctly
Canonicalize the exact bytes being authenticated, choose durable signing metadata, rotate keys safely, and avoid the implementation mistakes that make HMAC decorative.
Most webhook signatures fail in boring ways. Not because HMAC is broken, but because one side signs a pretty-printed JSON document while the other verifies raw bytes, because the timestamp is optional, because old keys never rotate, or because verification uses string equality that leaks timing. A signature scheme is a protocol. Write it like one.
Sign bytes, not intentions
JSON has no single canonical representation in ordinary web frameworks. Whitespace, key order, Unicode escaping, number formatting, and duplicate fields can all change while semantic content appears constant. The robust producer rule is simple: serialize the body once, transmit exactly those bytes, and sign exactly those bytes.
body = json.dumps(payload, separators=(",", ":"), ensure_ascii=False).encode()
timestamp = str(int(time.time()))
signed = timestamp.encode() + b"." + body
signature = hmac.new(secret.encode(), signed, hashlib.sha256).hexdigest()
headers = {
"Acme-Webhook-Id": event_id,
"Acme-Webhook-Timestamp": timestamp,
"Acme-Webhook-Signature": f"v1={signature}",
}Including the timestamp in the signed input prevents an attacker from replaying an old body with a fresh-looking timestamp or stripping the timestamp header entirely. The event ID can also be signed; either way, document the exact string construction.
Make the scheme explicit enough to reimplement
Your documentation should specify:
signature input = UTF-8(timestamp) + "." + raw request body
digest = HMAC-SHA256 using the endpoint signing secret
header format = v1=<lowercase hex digest>
comparison = constant-time equality over decoded digest bytes
timestamp window = reject outside +/- 300 seconds, accounting for clock skew
key discovery = endpoint-specific secret shown once at creationDo not invent a bespoke cryptographic construction. HMAC-SHA256 is sufficient for authentication when both parties share a secret. Asymmetric signatures are useful for public verification by many parties, but most private webhook integrations do not need that complexity.
Key rotation is part of the protocol
Consumers need overlap. If you rotate from secret A to secret B instantly, every in-flight retry signed with A fails. Send signatures for both keys during the rotation window, or send a key identifier and let the consumer select the correct secret:
Acme-Webhook-Signature: v1=oldhex,v1b=newhex
Acme-Webhook-Key-Id: whsec_2026_11The first format is simple but changes parsing rules. The second is cleaner when consumers can store multiple active keys. Whatever you choose, version the scheme. Never silently change the meaning of an existing header.
Common implementation traps
A signing proxy that re-serializes JSON invalidates signatures. Compression is normally safe because signatures verify after decompression, but middleware that mutates whitespace is not. Some frameworks consume request bodies before handlers can read them. Test through the real ingress chain, not just the application function.
Field notes
- Publish one canonical verification example with a fixed secret, timestamp, body, and expected digest; it saves integrators days.
- Rotate secrets on suspicion, on schedule, and after staff or vendor access changes. Rotation without a rollover plan creates outages.
- Do not include the secret in a query string. URLs leak into logs, browser history, analytics, and exception reports.
- Avoid signing only selected fields. Attackers can then replay or mutate unsigned parts depending on consumer behavior.
- Log whether verification failed because of timestamp, missing signature, unknown key, or digest mismatch — but never log the secret or full signature comparison values.
Was this lesson helpful?