Securing Webhooks: HMAC Signatures, Timestamps, Replay Protection and Retries
Build and consume webhooks securely: verify HMAC signatures with constant-time comparison, reject replays, deduplicate events, handle retries.
Published December 6, 2026
Webhooks deliver events from one system to another over HTTP: a payment succeeded, a repository was pushed, a form was submitted. Because webhook endpoints are public URLs, anyone can send requests to them. Without verification, an attacker can fake a "payment succeeded" event. Without idempotency, legitimate retries can process the same event twice.
The concept
The standard approach is an HMAC signature. The sender and receiver share a secret; the sender computes HMAC-SHA256 over the raw request body (usually combined with a timestamp) and sends it in a header. The receiver recomputes the HMAC over the exact raw bytes it received and compares in constant time. A timestamp check (reject events older than a few minutes) blocks replay of captured requests.
Delivery is at-least-once: senders retry on timeouts and errors, so receivers must deduplicate by event ID and respond quickly (acknowledge with 2xx, then process asynchronously). Senders have their own security concerns: customer-supplied webhook URLs are an SSRF risk, so validate destinations, block internal addresses and limit redirects.
Receiving a webhook safely
Threats and controls
| Threat | Control |
|---|---|
| Forged events | HMAC signature with shared secret (or asymmetric signatures) |
| Replay of captured request | Signed timestamp, reject if older than ~5 minutes |
| Duplicate deliveries | Store processed event IDs; idempotent handlers |
| Slow processing → sender retries | Acknowledge quickly; process in background |
| Secret leakage | Per-endpoint secrets; support rotation with two active secrets |
| SSRF via destination URL (sender side) | Validate URLs, block private IPs, limit redirects |
Verification checklist
- Read the raw body (before JSON parsing)
- Parse timestamp and signature from header
- Reject if timestamp outside tolerance
- Compute HMAC over "timestamp.body" and compare in constant time
- Deduplicate by event ID, enqueue, return 2xx
Practical examples
Verify an HMAC signature in Node.js
Use the raw body and timingSafeEqual.
import crypto from 'node:crypto'
app.post('/webhooks/payments', express.raw({ type: 'application/json' }), (req, res) => {
const [tPart, sigPart] = req.get('X-Signature').split(',')
const t = Number(tPart.split('=')[1]); const sig = sigPart.split('=')[1]
if (Math.abs(Date.now() / 1000 - t) > 300) return res.sendStatus(400)
const expected = crypto.createHmac('sha256', process.env.WEBHOOK_SECRET).update(`${t}.${req.body}`).digest('hex')
const ok = expected.length === sig.length && crypto.timingSafeEqual(Buffer.from(expected), Buffer.from(sig))
if (!ok) return res.sendStatus(401)
const event = JSON.parse(req.body)
queue.add('payment-event', event, { jobId: event.id }) // jobId dedupes
res.sendStatus(200)
})Signing outgoing webhooks (Python)
What a sender should do.
import hmac, hashlib, time, json
body = json.dumps(event, separators=(",", ":")).encode()
t = str(int(time.time()))
sig = hmac.new(secret.encode(), f"{t}.".encode() + body, hashlib.sha256).hexdigest()
headers = {"Content-Type": "application/json", "X-Signature": f"t={t},v1={sig}", "X-Event-Id": event["id"]}Deduplicate by event ID
A unique constraint makes processing idempotent.
INSERT INTO processed_webhooks (event_id, received_at) VALUES ($1, now())
ON CONFLICT (event_id) DO NOTHING
RETURNING event_id; -- no row → already processedCommon mistakes
- Verifying the signature over re-serialized JSON instead of the raw body.
- Comparing signatures with == (timing attacks).
- Doing slow work before responding, causing timeouts and duplicate retries.
- One shared secret for all customers with no rotation.
Key takeaways
- Verify HMAC signatures over the raw body in constant time.
- Include and check a timestamp to block replays.
- Deduplicate by event ID; process asynchronously.
- As a sender, protect against SSRF and support secret rotation.
FAQ
Are IP allow-lists enough?
They help as an extra layer, but signatures are the primary control because provider IPs change and can be shared.
Should I use asymmetric signatures?
Some providers sign with private keys so receivers only need a public key; it avoids distributing shared secrets.
Is there a standard format?
The Standard Webhooks specification defines common headers and signing; many providers use similar schemes.