The short answer. A valid signature establishes who knew the signing secret and what bytes they signed. Your application must still decide whether the event is fresh, relevant and safe to process.
Start with the boundary
A delivery arrives twice. The first request times out after the database commits; the second reaches another instance. Both signatures are valid. If your handler treats authentication as permission to repeat the action, a perfectly legitimate retry can become a duplicate credit or notification.
Design the receiver as four separate decisions: accept the transport, authenticate the bytes, classify the event, then commit work. Each decision should have a different failure reason in your logs. That makes a wrong secret distinguishable from a delayed delivery and a broken database distinguishable from a malformed request.
This guide proposes a receiver design. The small verification function below is a study fragment for GitHub's documented signature format, not a universal webhook protocol or a complete HTTP server.
Keep the signed representation intact
Capture a bounded raw body before JSON middleware. Parsing and serializing can change whitespace or representation while preserving the apparent object. A signature authenticates a representation, not your interpretation of it. GitHub and Stripe document their own validation contracts; use the contract belonging to your sender.
Set a body-size limit at both the edge and application. Read through a byte counter rather than trusting Content-Length. Decide how compressed requests are handled before deployment: a proxy that decompresses the body can change which bytes reach your verifier. Record configuration errors without recording secrets or entire customer payloads.
After authentication, parse JSON and validate its shape. Signature success is not schema validation, tenant authorization or protection against unexpectedly large nested objects.
Make the comparison small and explicit
For GitHub's sha256= format, validate the encoding before decoding it. Node's timing-safe comparison requires equal-length inputs; its presence does not make the entire request handler timing-safe.
import { createHmac, timingSafeEqual } from 'node:crypto';
export function verifyGitHub(raw, signature, secret) {
if (!Buffer.isBuffer(raw)) return false;
if (typeof signature !== 'string' ||
!/^sha256=[0-9a-fA-F]{64}$/.test(signature)) return false;
const received = Buffer.from(signature.slice(7), 'hex');
const expected = createHmac('sha256', secret).update(raw).digest();
return timingSafeEqual(expected, received);
}
Validate that the configured secret exists at startup. Keep this helper independent of the framework so tests can pass exact byte buffers. For other providers, change the signed message and header parser deliberately. Do not remove a timestamp from the message merely to make a local test pass.
This GitHub example signs the payload only. It has no authenticated timestamp field. The timestamp-based signature lab uses a separate, illustrative contract; do not substitute one verifier for the other.
Freshness and duplicates are different questions
A timestamp window limits how old an authenticated delivery may be. It only helps if the timestamp is itself covered by the signature. Use the provider's documented tolerance, account for clock skew, and distinguish delivery time from the time the business event happened. A legitimate event may describe something much older.
A delivery ID answers another question: have we accepted this event already? Store a provider, account and event identifier together, with a unique database constraint. An in-memory set disappears on restart and cannot coordinate replicas. Design retention around documented retry and replay behavior, not an arbitrary five-minute timer.
When a duplicate arrives, return the acknowledgment required by the sender after confirming that the previous durable acceptance exists. Do not rerun the side effect to demonstrate that the event is valid.
Acknowledge the handoff you actually made
One workable design writes an authenticated event record and a pending job in one transaction. A worker claims that job, applies the business operation and records completion. The HTTP path acknowledges only after the transaction commits. If the database is unavailable, a success response would falsely tell the sender that the handoff worked.
The worker still needs idempotency: it can fail after applying an external side effect and before saving its result. Tie the outgoing action to a stable business key and reconcile uncertain outcomes. “We use a queue” is not evidence of exactly-once execution.
Keep delivery state separate from business state. For example, a verified payment event can be accepted for processing while the order remains under review. Document which component owns each transition so operations staff know where a stuck event belongs.
Test the failure paths, not only the example
- Change one byte in a valid payload: validation must fail.
- Change JSON spacing while keeping the original signature: validation must fail.
- Send a truncated digest, duplicate signature header or unsupported format: reject it consistently.
- Deliver the same authenticated event concurrently to two instances: only one durable acceptance should be created.
- Crash after commit and before acknowledgment: the retry must find the existing record.
- Rotate secrets with a defined overlap, then remove the old secret and verify rejection.
Use synthetic fixtures and separate secrets for these exercises. A browser lab is useful for seeing byte changes, but it does not certify a deployed receiver, inspect a proxy or test a database race. Reproduce those boundaries in your own integration environment and record the runtime and configuration used.
Primary sources
Consult the current documentation when implementing. Links checked during preparation of this edition.