Webhook security 101: five attacks and the checks that stop them
A webhook endpoint faces five attacks: forged requests, tampered payloads, replayed deliveries, timing attacks on the signature comparison, and, if you send webhooks, server-side request forgery through customer-supplied URLs. An HMAC-SHA256 signature over the raw body stops the first two, a signed timestamp plus deduplication stops replays, constant-time comparison stops timing leaks, and egress filtering stops SSRF.
This lesson takes each attack in turn: what the attacker sends, the check that rejects it, and a request you can fire at your own staging endpoint to prove the check works. For the definitions, see what is webhook security. For the sender-side rules in short form, see webhook security best practices.
Webhook security checklist
- Verify an HMAC-SHA256 signature before parsing the payload, and reject anything that fails.
- Compute the HMAC over the raw request bytes, not re-serialized JSON.
- Compare signatures with
hmac.compare_digestorcrypto.timingSafeEqual, never==. - Sign a timestamp along with the payload and reject anything outside a 300-second window. Under Standard Webhooks that timestamp arrives in the
webhook-timestampheader and is signed asid.timestamp.payload. - Accept requests over HTTPS only, on port 443, with no plaintext fallback.
- Store the secret in a secrets manager, and support two active secrets during rotation.
- Cap the body size, validate the payload schema after verification, and rate limit the endpoint.
- Deduplicate on the event ID, sent as
webhook-id, so a replayed or retried delivery cannot be processed twice. - Log event IDs, source IPs, and verification results, with sensitive fields redacted.
- If you send webhooks, filter customer-supplied URLs for private IP ranges through a DNS-resolving proxy.
Forged requests: anyone can POST to your URL
Webhook URLs are not secrets. They sit in dashboards, config files, and proxy logs, and an attacker who finds one can send whatever body they like. A forged invoice.paid that marks an unpaid order as paid is the classic case. Test for it with one request that carries no signature and one with a made-up signature:
curl -i https://staging.example.com/webhooks \
-H 'content-type: application/json' \
-d '{"type":"invoice.paid","data":{"id":"in_1042"}}'
curl -i https://staging.example.com/webhooks \
-H 'content-type: application/json' \
-H 'webhook-id: msg_forged' \
-H "webhook-timestamp: $(date +%s)" \
-H 'webhook-signature: v1,dGhpcyBpcyBub3QgYSBzaWduYXR1cmU=' \
-d '{"type":"invoice.paid","data":{"id":"in_1042"}}'
Both must return a 401 with no side effect. A 2xx means the handler trusts unauthenticated input, and a 500 usually means it parsed the body before checking the signature. The fix is HMAC verification on every request, before parsing; the webhook signature page has a complete verification function for the Standard Webhooks scheme. Keep the 401 bare. An error message that says which header was wrong tells an attacker how far they got.
Tampered payloads: the signature covers the exact bytes
An attacker who captures a real delivery, from a log line or a misconfigured proxy, can edit it and send it on. Take a captured body, change "amount": 4900 to "amount": 1, keep the original headers, and resend it. The request must fail, because the signature was computed over the original bytes and one changed character produces a different HMAC.
The more common failure here is self-inflicted. A handler that verifies a re-serialized copy of parsed JSON instead of the raw body rejects legitimate requests whenever key order or whitespace differs, and teams “fix” that by switching verification off. Read the raw bytes, verify them, and parse only after verification passes. Cap the body size before any of that, 1 MB is generous for most events, so a forged 500 MB body gets a 413 instead of being read into memory.
Replayed deliveries: a valid signature at the wrong time
A replay is a captured delivery sent again unchanged, so its signature still verifies. If the event credits an account, every replay credits it again. Two checks stop it. Timestamp tolerance rejects anything signed more than 300 seconds ago, and because the timestamp is part of the signed string id.timestamp.body, an attacker cannot freshen it without breaking the signature. Inside that five-minute window, deduplication on webhook-id turns the replay into a no-op.
Here is that order of checks in a Flask handler. The Svix library’s verify call covers the signature and the timestamp window, and Redis holds the IDs already seen:
import os
import redis
from flask import Flask, abort, request
from svix.webhooks import Webhook, WebhookVerificationError
app = Flask(__name__)
app.config["MAX_CONTENT_LENGTH"] = 1024 * 1024
wh = Webhook(os.environ["WEBHOOK_SECRET"])
seen = redis.Redis()
@app.post("/webhooks")
def receive():
try:
event = wh.verify(request.get_data(), dict(request.headers))
except WebhookVerificationError:
abort(401)
key = f"webhook:{request.headers['webhook-id']}"
if not seen.set(key, 1, nx=True, ex=7 * 24 * 3600):
return "", 204
try:
enqueue(event)
except Exception:
seen.delete(key)
raise
return "", 202
enqueue stands for whatever puts the event on your job queue. The key lives for seven days, longer than most senders’ retry windows (Stripe retries for up to three days), so the same check also absorbs ordinary retries. Deleting the key when the enqueue fails matters: otherwise the sender’s retry of an event you never processed would be dropped as a duplicate.
To test it, capture a real signed delivery with Svix Play or your request log and replay it with curl. Within five minutes of the original timestamp, the first copy returns 202 and the second 204. After five minutes, both return 401.
Timing attacks: compare in constant time
Ordinary string equality returns at the first byte that differs, so a guess that is wrong at byte 1 is rejected slightly faster than one wrong at byte 30. With enough requests an attacker can measure that difference and recover a valid signature one byte at a time, the same class of bug as the Jenkins webhook plugin vulnerability we covered in securely verifying signature hashes.
Every mainstream language ships a constant-time comparison: hmac.compare_digest in Python, crypto.timingSafeEqual in Node.js (which throws if the lengths differ, so check length first), hmac.Equal in Go, MessageDigest.isEqual in Java, hash_equals in PHP, and Rack::Utils.secure_compare in Ruby. Timing leaks are hard to demonstrate from a laptop over the internet, so treat this one as a code review check: search the verification path for == or === next to anything named signature.
Leaked secrets: rotate without dropping deliveries
If the signing secret leaks, every check above passes for the attacker until you rotate. Keep it in a secrets manager, readable only by the service that verifies, and use a separate webhook secret per endpoint so one leak does not expose every integration. Rotation works without downtime because the signature header can carry more than one value. During the overlap window the sender signs with both secrets:
webhook-signature: v1,JaeL82bGPAJWiRxAZ7M6gUb4doAHhYSVYr0j9puXWnk= v1,btZ77ELikB4Bc1YjHfRcujDqg5pZkGQ9jIxKzJ+kuZY=
A receiver that checks every v1, value against its current secret keeps verifying whichever secret it holds. Deploy the new one, confirm your 401 rate stays flat, and then retire the old one. Secret rotation covers the sender side of the overlap.
SSRF: when you are the one sending
If your product sends webhooks, the URLs customers register are attacker input, and your delivery workers will request whatever they point at. An endpoint of http://169.254.169.254/latest/meta-data/ asks the cloud metadata service for credentials. Blocking that string is not enough, because each of these reaches internal addresses too:
http://127.0.0.1.nip.io/, a public DNS name that resolves to loopbackhttp://2130706433/, the decimal form of127.0.0.1http://[::ffff:169.254.169.254]/, an IPv4-mapped IPv6 address- a public URL that answers with a 302 redirect to an internal address
- a hostname whose DNS answer changes between your validation and the request, known as DNS rebinding
The fix is to check the IP address you actually connect to, not the URL string. Resolve at connection time, refuse private and reserved ranges (10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16, 127.0.0.0/8, 169.254.0.0/16, 100.64.0.0/10, 0.0.0.0/8, ::1, fc00::/7, fe80::/10), and either disable redirects or re-check every hop. A proxy such as Smokescreen applies that check to every outbound request, and running delivery workers in a subnet with no route to internal services covers anything the filter misses. Test it by registering each URL above as an endpoint in staging: every one should be refused at registration or fail delivery without reaching the internal service. The SSRF glossary entry covers the attack class in general.
None of these checks is hard alone. The work is applying all of them on every endpoint for as long as the integration lives. Svix signs every message under the Standard Webhooks scheme and ships verification libraries for the receiving side, and Svix Ingest verifies provider webhooks before they reach your code.