Verifying signatures
The signature scheme on webhook deliveries and how to verify it correctly
Every delivery carries a Ticketing-Signature header, signed with the secret
shown once when you create the endpoint:
Ticketing-Signature: t=1793885263,v1=5257a869e7ecebeda32affa62cdca3fa51cad7e77a0e56ff536d0ce8e108d8bdtis the Unix timestamp, in seconds, at which the delivery was signed.v1is a hex HMAC-SHA256 over the string`${t}.${rawBody}`— the timestamp, a literal full stop, then the raw request body — keyed with your endpoint's signing secret.
This mirrors Stripe's verification pattern, deliberately. If you have verified a Stripe webhook before, the shape will be familiar, and libraries that already handle that format need little adapting — which is worth more to you than anything a bespoke scheme could offer.
Verifying
Read the raw body as bytes, before any JSON parsing, and keep the bytes — not a decoded string — exactly as received.
Parse the header into t and every v1 value it carries. There can be more
than one — see rotation below.
Reject a stale or improbable timestamp. Refuse the delivery if t is more
than five minutes away from your own clock, in either direction. Old
protects you from a replay; far-future protects you from a clock you cannot
trust.
Recompute HMAC-SHA256(secret, "{t}.{rawBody}") and compare it against each
v1 value with a constant-time comparison. Accept if any one matches.
import { createHmac, timingSafeEqual } from "node:crypto";
/** `rawBody` is a Buffer of the bytes as received — never a decoded string. */
export function verify(rawBody, header, secret, toleranceSeconds = 300) {
const parts = header.split(",").map((part) => part.trim());
const timestampPart = parts.find((part) => part.startsWith("t="));
const timestamp = Number(timestampPart?.slice(2));
if (!Number.isFinite(timestamp)) return false;
const skew = Math.abs(Math.floor(Date.now() / 1000) - timestamp);
if (skew > toleranceSeconds) return false;
// Built from bytes: the timestamp, a literal ".", then the body verbatim.
// Interpolating the body into a template string would decode it first.
const expected = createHmac("sha256", secret)
.update(String(timestamp), "utf8")
.update(".", "utf8")
.update(rawBody)
.digest();
// Every v1 in the header — during a rotation overlap there are two.
return parts
.filter((part) => part.startsWith("v1="))
.some((part) => {
const encoded = part.slice(3);
// Buffer accepts malformed/truncated hex. Reject it before the
// length-sensitive constant-time primitive sees attacker input.
if (!/^[0-9a-f]{64}$/iu.test(encoded)) return false;
const candidate = Buffer.from(encoded, "hex");
return (
candidate.length === expected.length &&
timingSafeEqual(candidate, expected)
);
});
}Get those bytes from your framework's raw-body facility. In Express that is
express.raw({ type: "application/json" }), which hands you a Buffer. In a
Fetch-style handler it is Buffer.from(await request.arrayBuffer()) — not
await request.text(), which decodes the bytes and takes you straight back to
the problem below.
Sign over the bytes you received, never over a re-serialized object.
JSON.stringify(JSON.parse(body)) is not the same string: key order, unicode
escaping and number formatting all move. A verifier built on a parsed body
works in development and fails on the first payload that happens to serialize
differently — which is the worst possible failure mode, because it looks like
an attack.
Rotation
Rotating an endpoint's secret mints a new one while the old secret keeps
signing for 24 hours. During that overlap a delivery carries more than one
v1 value:
Ticketing-Signature: t=1793885263,v1=5257a869…,v1=b9f10c33…Accept the delivery if any v1 matches a secret you hold. That is what makes
rotation zero-downtime: deploy the new secret whenever you like inside the
window, and no delivery fails while you do.
Write your verifier to loop over v1 values from the start, even before you
have ever rotated. A verifier that reads only the first one passes every test
you can write today and fails the first time you rotate.
Payloads are secret-bearing
The body is the activity envelope, byte-for-byte identical to what GET/v1/activities/{id} returns for the same activity. That identity is deliberate: it is what makes a replica built from deliveries and one built from a sweep the same replica.
It also means the body can carry an admission code — the secret a ticket's QR encodes and the only thing the door reads.
- Never log the raw body. Not at debug level, not "temporarily". Log the
activity
id,typeandcreated, which is enough to trace anything. - Terminate TLS at your own endpoint, and do not forward deliveries onward through anything that logs bodies.
- Treat your signing secret like an API key. It is shown once at creation and rotated, never recovered.
Responding
- Any 2xx is success. Respond within about ten seconds.
- Answer first, work second. Verify the signature, enqueue the activity, return 2xx. Doing the work inline is how a slow database turns into a retry storm.
- Deduplicate on the
act_id. Delivery is at-least-once, there is no ordering promise, and a retry of something you already applied is normal rather than exceptional. - A non-2xx is retried on a backoff with jitter — roughly a minute, five minutes, thirty minutes, two hours, six hours, then every twelve — and given up on after three days.
An endpoint that fails every delivery for three consecutive days is disabled. Recovery is to fix it, re-enable it through the API, and close the gap with a sweep — there is no per-activity redelivery button, because the pull rail already is one and it cannot itself be missed.
Gotchas
- Verify before you parse. A body you have parsed is a body you can no longer verify.
- Compare in constant time. A byte-by-byte early return leaks the signature one byte at a time.
- Do not skip the timestamp check. Without it a captured delivery can be replayed forever, signature and all.
- Do not assume one
v1. - Do not assume order. Two deliveries about one resource can arrive either
way round; prefer the snapshot with the later
created. - Do not treat a signature failure as a bug in the sender. It is either a raw-body problem, a stale secret, or something you genuinely want to reject.