Most webhook bugs are not receiving bugs. The request arrived. The handler ran. Something after that point quietly dropped the event, and nobody found out until a customer asked where their order was.
These are the three failure modes I see most often, each with a test you can run in a few minutes.
1. The sender gives up after one failure
The target returns 503 because a deploy is rolling, or the database is briefly locked. The sender retries twice in ten seconds, gives up, and marks the event delivered. The event is gone.
The fix is not "retry more". It is to acknowledge the event, persist it, and retry from a queue with backoff:
- Return
2xxonly after the event is stored. - Treat
408,425,429and5xxas transient; retry with exponential backoff and jitter. - Treat other
4xxas permanent; move the event to a dead-letter queue instead of retrying forever. - Set a maximum attempt count and make the last error visible.
Test it: run a target that returns 503 for the first two requests and 200 afterwards. Send one signed event, then inspect the stored event. You should see attempts: 2 and last_error mentioning the 503 before it reaches done.
from http.server import BaseHTTPRequestHandler, HTTPServer import json calls = 0 class H(BaseHTTPRequestHandler): def do_POST(self): global calls calls += 1 self.rfile.read(int(self.headers.get("Content-Length", "0"))) if calls <= 2: self.send_response(503) self.end_headers() self.wfile.write(b"down") return self.send_response(200) self.send_header("Content-Type", "application/json") self.end_headers() self.wfile.write(json.dumps({"ok": True}).encode()) def log_message(self, *args): pass HTTPServer(("127.0.0.1", 8788), H).serve_forever() If your integration never retries, this test fails on the first request and the event disappears. That is the bug.
2. The same event runs twice
Senders retry. Networks duplicate. A timeout on the sender side does not mean the target did not process the request. If the handler is not idempotent, a retry becomes a second order, a second email, or a second charge.
Deduplication has to happen before the side effect, using a stable key supplied by the sender:
- Prefer the sender's event id header, for example
X-Event-Id. - Fall back to a hash of the raw body when the sender has no id.
- Store the key with a unique constraint, and return success on a duplicate without running the work again.
- Keep the raw body, because the signature is computed over the exact bytes, not the parsed JSON.
Test it: send the same event twice with the same X-Event-Id. The first response should be 202 accepted; the second should be 200 duplicate, and the target should see exactly one delivery.
A handler that parses JSON, re-serialises it and then verifies the signature will fail this test for a different reason: the bytes changed. Verify against the raw body.
3. The signature check is not replay-safe
Verifying an HMAC proves the request was signed with the shared secret. It does not prove the request is new. A captured request can be replayed later, and a correct signature will still match.
Two things are needed:
- Sign a timestamp together with the body, for example
HMAC(secret, "{timestamp}.{body}"). - Reject timestamps outside a short window, five minutes is typical.
Test it: sign a request with a timestamp ten minutes in the past. A correct implementation returns 401. Then replay a fresh, correctly signed request twice. The signature passes both times; the dedupe layer is what stops the second one from running.
These are separate controls. Signature verification answers "who sent this". Replay protection answers "when was this sent". Idempotency answers "has this already been handled".
The order that matters
The sequence inside the handler decides whether failures are recoverable:
- Read the raw body.
- Verify signature and timestamp window.
- Check the dedupe key.
- Persist the event.
- Return
202. - Do the work asynchronously.
- On failure, retry with backoff or move to the dead-letter queue.
If step 5 happens before step 4, a crash between them loses the event. If step 6 happens before step 5, a slow target turns into sender timeouts and duplicate deliveries.
What I built from this
I packaged the pattern as a small self-hosted gateway, HookDesk. It is a FastAPI service with SQLite as the queue, so there is no broker to operate. It verifies HMAC-SHA256 signatures with a five-minute window, deduplicates by X-Event-Id, retries transient failures with backoff and jitter, and exposes a dead-letter queue plus an admin API for stats and manual retries. There is a Dockerfile, tests, and a sender CLI for the tests above.
It runs with:
uv run uvicorn hookdesk.main:create_app --factory --port 8000 Source licence is $29: https://takbirzaman.gumroad.com/l/hookdesk
If you only have one endpoint and one retry, you may not need a gateway. The three tests above are still worth running against whatever you have, because each one fails silently today.
