ServicesSecurity BlueprintInsightsAboutTalk to us

Webhook handlers that survive forgeries, replays and retries

A webhook endpoint is an unauthenticated POST request from the internet. Anyone can send one, and even honest providers will send the same event more than once. A handler that trusts what arrives will eventually be forged, replayed or double-charged.

Five steps cover most of the risk.

1. Verify the signature on the raw body

Most providers sign each request with HMAC-SHA256 and a shared secret. Compute the signature over the exact bytes you received. If you parse the JSON and serialise it again, spacing and key order change and the signature will not match. In Express, use a raw-body parser for this route.

import { createHmac, timingSafeEqual } from 'node:crypto';

function verify(rawBody, signatureHeader, secret) {
  const expected = createHmac('sha256', secret).update(rawBody).digest('hex');
  const a = Buffer.from(expected);
  const b = Buffer.from(signatureHeader ?? '');
  return a.length === b.length && timingSafeEqual(a, b);
}

Use timingSafeEqual, not ===, so response timing does not leak how much of a guess was right. It throws when lengths differ, which is why the length check comes first.

2. Reject old requests

A valid signature proves who sent a request, not when. Include a timestamp in the signed data and reject anything more than about five minutes old. Without that, someone who captures one genuine request can replay it forever.

3. Make processing idempotent

Providers retry when your server is slow or returns an error, so duplicates are normal. Record each event ID with a uniqueness guarantee:

CREATE TABLE processed_events (
  event_id    text PRIMARY KEY,
  received_at timestamptz NOT NULL DEFAULT now()
);

INSERT INTO processed_events (event_id) VALUES ($1) ON CONFLICT DO NOTHING;
-- 0 rows inserted = already handled: answer 200 and stop

Do the insert and the actual work in one transaction. If the process crashes halfway, both roll back and the retry starts clean.

4. Answer fast, work later

Once the event is verified and stored, return a 2xx and hand the slow work to a queue and a worker. A handler that takes too long makes the provider time out and retry, which creates the duplicates and extra load you are trying to avoid.

5. Dead-letter the failures

Retry failed jobs with exponential backoff. After a fixed number of attempts, move the event to a dead-letter store with the error attached and alert a person. A failed event that disappears silently is the worst outcome: money, orders or access changes that never happened and nobody knows.

The checklist

  • Signature checked on the raw body, in constant time.
  • Timestamp within a short window.
  • Event ID stored once, enforced by the database.
  • 2xx returned quickly, work done in a worker.
  • Retries with backoff, then a dead-letter queue and an alert.

We built our Relay Event Engine to practice exactly these five steps together.

Newsletter

Get new insights by email.

Short, practical notes on security and technology risk, sent only when there’s something worth reading. Never more than one a month.