Loading
Code Deb

Webhooks That Survive Retries, Secret Rotation and Replay Attempts

Build secure webhook delivery with raw-body signatures, HMAC, timestamp tolerance, durable deduplication, retry policies and secret rotation.

A sealed message travels between endpoints with retry paths and verification boundaries.

A webhook signature proves that a payload matches a trusted signing key. It does not establish that this is the first delivery or that applying the same event twice is safe. The receiver needs a separate durable processing identity.

Two hundred events become 303 deliveries

We simulated 200 logical event IDs. Every fifth index produces three deliveries; other multiples of seven produce two; the rest produce one. No random workload or network timing is involved.

Processing ruleBusiness writes in the model
Apply every valid delivery303
Commit once per event identity200

Without deduplication, 103 additional writes occur. This is a synthetic retry workload, not a measured Stripe or Code Deb retry distribution. Stripe's webhook documentation separately warns about retries and duplicate/out-of-order events.

Verify the original bytes, not a parsed object

import { verifyWebhook } from "@codedeb/webhook-kit";
const proof = verifyWebhook({
  headers, body: rawRequestBytes,
  secrets: [currentSecret, previousSecret],
  now: Math.floor(Date.now() / 1000)
});
// Only parse and validate the payload after verification.

The package implements HMAC-SHA256 over ID, timestamp and original bytes, with bounded key and signature lists and constant-time comparison of equal-length buffers. The default time tolerance is 300 seconds. See Standard Webhooks specification and Webhook Kit. Reformatting JSON can change the signed bytes even when the parsed object represents the same values.

A unique constraint is necessary but not sufficient

-- Insert and enqueue in one database transaction.
INSERT INTO webhook_inbox (tenant_id, sender_id, event_id, payload)
VALUES ($1, $2, $3, $4)
ON CONFLICT (tenant_id, sender_id, event_id) DO NOTHING
RETURNING event_id;

The first accepted event must durably create its processing record. A worker then commits its business effects and completion record transactionally or through a carefully designed outbox. If an event ID is marked processed before its effect is written, a crash can cause permanent loss. If an external payment operation cannot join the database transaction, propagate a stable idempotency key to the provider.

Rotation, replay and ordering solve different problems

Allow a bounded active and previous key set during an intentional migration. Remove the previous key at a known time after sender cutover. Timestamp validation limits replay age; durable deduplication protects against replay within the valid window. Event ordering should depend on an actual business revision or state transition, not delivery arrival order. The most recently arrived message is not necessarily the newest transaction.

Test failures at the transaction boundary

Send the same event repeatedly, mutate one byte, use the wrong tenant secret, simulate a timeout after commit and deliver a refund before its associated capture. Keep valid signature verification separate from authorization and business transition acceptance. Report delivery attempts, accepted logical events and committed effects as separate counters. Stock reservation invariants, Invoice issuance and Log redaction are direct consumers of those guarantees. The 200-event model proves the arithmetic of duplicate delivery, not that a production adapter is crash-safe.

Method and reproducibility: Controlled synthetic model. See Code Deb's recorded experiment results and reproduction script in the repository. Figures are reproducible from declared inputs, not undisclosed client outcomes. The cited external material is independent of Code Deb's experiment.

More from Code Deb

Read about our agency, explore available services, or contact the team for a project discussion.