On replay: since the WhatsApp signature has no timestamp in the signed content, the defence is dedupe on event identity. Each message in the payload carries its own id (the wamid value), so insert that id into a table with a unique constraint before doing any work, and on a conflict return 200 and skip. That also makes acknowledge-first, handle-later safe, because a provider retry or a replayed capture hits the conflict instead of running the handler twice. One catch is atomicity: if the id insert and the job enqueue are separate steps, a crash between them leaves a recorded id with no job, and the retry is then silently dropped. Writing both in one transaction, with a jobs table as the queue, closes that gap. Keep the ids for as long as a replay is plausible, and since they are small, indefinitely is cheap.
This is a clear write-up of the fix, and the atomicity catch is the part most people miss: a recorded id with no job is a silent drop, which is worse than a duplicate. One small nuance on timestamps. Meta's signature header has no timestamp of its own, but each message in the body carries one, and the body is what's signed. So a freshness check is possible. It just can't replace the dedupe you describe, because Meta retries failed deliveries, and a retry arrives late on purpose. Insert the id and the job in one transaction is the pattern I'd use for the next change.
Agreed on the layering. If the body timestamp is used, the window has to be wide, since retries can land days later, so I would use it to flag and alert on old deliveries and not to reject them. Where it earns its place is ordering: status updates (sent, delivered, read) can arrive out of order, so compare the event timestamp with the stored one before overwriting a message's state. Otherwise a late "sent" can roll back a "read".
I test what ships and publish what I find, failures included.
Kubernetes, AWS, supply chain & AI agent security.
Docker Captain, CNCF Ambassador, IBM Champion. 1.8M+ Docker Hub pulls.
Dedupe holds only while the key sits inside what the signature covers. In WhatsApp's case the wamid is in the signed body, so the unique constraint is sound. A key read from a header is not.
Cypress Cloud's webhooks show the split: X-Cypress-Signature covers X-Cypress-Timestamp and the body, and X-Cypress-Event-Id is outside it. In run 43513818839 I sent one real delivery back at the public URL with a fresh X-Cypress-Event-Id. It got 409, the same as the unchanged copy. Unsigned, or with one byte added to the body, it got 401, and none of the four started a job.
A dedupe table keyed on that header would have run the handler twice. On the GPS door, with only the shared secret checked, what would you key on?
With only a shared secret and nothing signed beyond the body, I would key on a hash of the raw body plus the device id read from inside the body, since that is the only input the secret covers. If the payload carries a device sequence number or a reading timestamp, put it in the key too, otherwise two identical readings collapse into one. Keep the dedupe rows at least as long as the longest retry window, and treat every header outside the key as untrusted metadata.
Good point, and the current code doesn't do it: it takes one secret. Both ends are on one box, so the gap during a routine rotation is short, but accepting the current and previous secret for a brief overlap is cleaner. The one exception is a leak: then the old secret should die at once, even if a few requests fail until the sender is updated. Noting the overlap for the next pass.
Oyinlola, the "two doors" framing is what makes this stick — the same endpoint shape, two completely different trust models, and the check has to match the sender, not the route. The detail I would steal is the fail-closed defaults: refusing to start without the secret, and treating a missing app secret as a hard error instead of a skipped check. Most webhook guides show the HMAC and stop there; yours shows what happens when the pieces around it are missing.
The trap I have seen most is the one you name in the second door: hashing the parsed body instead of the exact bytes. A JSON parser that reorders keys or normalises whitespace quietly breaks every signature, and the failure looks like the provider is wrong.
One question on the part you deliberately left out: since you name replay protection as not covered, do you check a timestamp on the signed payload anywhere in your own setup, or is that the next control you would add after the signature and the per-sender rate limit?
Thanks Abdullah. Honest answer: no, not yet. There's no timestamp check and no dedupe in the handler today, which is why replay sits under what the post doesn't cover. Two things blunt it a little. A tap on a menu that has since been replaced is ignored, because the reply has to point at the current prompt, and the per-sender limits cap how fast anyone can repeat a message. Neither stops a replay of the current message. The next control is dedupe on the message id, and Hritik's reply below describes it better than I would have.
The signature half is right, but there's a third door you get for free once you verify: replay. A captured request with a valid X-Hub-Signature-256 stays valid forever, so an attacker who sees one delivery (proxy log, a stolen backup, whatever the secret leak was) can POST the same bytes again and your handler processes the same payment twice. Meta's own payload leaves you the fix — keep a short TTL table keyed on the event id in entry[].changes[].value.messages[].id (or the X-Hub-Signature-256 plus a timestamp if you control the sender) and reject anything you have already accepted inside the window, with the insert as the lock so two concurrent retries can't both win. Same applies to the GPS door: a shared secret plus a timestamp header, reject older than a couple of minutes.
One more on the secret: timingSafeEqual protects the comparison, but the throw-on-length-mismatch means you leak length by the different exception path anyway. Hashing both sides to a fixed width first (sha256(incoming) vs sha256(expected)) closes that without the branch.
Does the WhatsApp handler do anything at all before the signature check, like writing a log row or touching the DB? If yes and it's before, a replayed request hits the side effect even if you later return 401.
I research AI tools, APIs & agents — comparing specs, pricing and real user reports — then publish honest rankings so you don't waste money. Research-first, never sponsored fluff.
The secret sitting in git history until a scanner found it eight times in one commit — yeah, that one hurt to read, I've done the dumber version of that with a log line. The raw-body trap is the part I'd have tripped on too, re-serialising feels like the obvious thing to do. For the GPS door, do you rotate that shared secret on any schedule, or is it set-and-forget since it's one machine you control?
Closer to set-and-forget, honestly. It has been rotated once, after that scanner find, and there's no schedule yet. Both ends sit on one server I run, so a rotation is a single coordinated change, which is probably why it never got a calendar. Kevin's comment below is the better answer to how it should work.
The cheerful 200 to an unsigned webhook is scary because it looks like everything works. Verifying the signature and the timestamp before anything else should be the first line in every handler.
Agreed on the signature first. Meta's scheme doesn't sign a timestamp, though, so for that door replay protection has to come from the handler being safe to run twice. That's the gap I flagged at the end of the post.
GMT strategy and custom B2B outreach lists on request: people and companies by ICP, domain, profile URL, email or name. 1B+ profiles. Claude Code plugins, API, MCP. kevin@outricher.com
Good point, and the current code doesn't do it: it takes one secret. Both ends are on one box, so the gap during a rotation is short, but accepting the current and previous secret for a brief overlap is cleaner, and it means a rotation after a leak never waits for a quiet moment. Noting it for the next pass.
We're a place where coders share, stay up-to-date and grow their careers.
On replay: since the WhatsApp signature has no timestamp in the signed content, the defence is dedupe on event identity. Each message in the payload carries its own id (the wamid value), so insert that id into a table with a unique constraint before doing any work, and on a conflict return 200 and skip. That also makes acknowledge-first, handle-later safe, because a provider retry or a replayed capture hits the conflict instead of running the handler twice. One catch is atomicity: if the id insert and the job enqueue are separate steps, a crash between them leaves a recorded id with no job, and the retry is then silently dropped. Writing both in one transaction, with a jobs table as the queue, closes that gap. Keep the ids for as long as a replay is plausible, and since they are small, indefinitely is cheap.
This is a clear write-up of the fix, and the atomicity catch is the part most people miss: a recorded id with no job is a silent drop, which is worse than a duplicate. One small nuance on timestamps. Meta's signature header has no timestamp of its own, but each message in the body carries one, and the body is what's signed. So a freshness check is possible. It just can't replace the dedupe you describe, because Meta retries failed deliveries, and a retry arrives late on purpose. Insert the id and the job in one transaction is the pattern I'd use for the next change.
Agreed on the layering. If the body timestamp is used, the window has to be wide, since retries can land days later, so I would use it to flag and alert on old deliveries and not to reject them. Where it earns its place is ordering: status updates (sent, delivered, read) can arrive out of order, so compare the event timestamp with the stored one before overwriting a message's state. Otherwise a late "sent" can roll back a "read".
Dedupe holds only while the key sits inside what the signature covers. In WhatsApp's case the
wamidis in the signed body, so the unique constraint is sound. A key read from a header is not.Cypress Cloud's webhooks show the split:
X-Cypress-SignaturecoversX-Cypress-Timestampand the body, andX-Cypress-Event-Idis outside it. In run 43513818839 I sent one real delivery back at the public URL with a freshX-Cypress-Event-Id. It got 409, the same as the unchanged copy. Unsigned, or with one byte added to the body, it got 401, and none of the four started a job.A dedupe table keyed on that header would have run the handler twice. On the GPS door, with only the shared secret checked, what would you key on?
With only a shared secret and nothing signed beyond the body, I would key on a hash of the raw body plus the device id read from inside the body, since that is the only input the secret covers. If the payload carries a device sequence number or a reading timestamp, put it in the key too, otherwise two identical readings collapse into one. Keep the dedupe rows at least as long as the longest retry window, and treat every header outside the key as untrusted metadata.
Good point, and the current code doesn't do it: it takes one secret. Both ends are on one box, so the gap during a routine rotation is short, but accepting the current and previous secret for a brief overlap is cleaner. The one exception is a leak: then the old secret should die at once, even if a few requests fail until the sender is updated. Noting the overlap for the next pass.
Oyinlola, the "two doors" framing is what makes this stick — the same endpoint shape, two completely different trust models, and the check has to match the sender, not the route. The detail I would steal is the fail-closed defaults: refusing to start without the secret, and treating a missing app secret as a hard error instead of a skipped check. Most webhook guides show the HMAC and stop there; yours shows what happens when the pieces around it are missing.
The trap I have seen most is the one you name in the second door: hashing the parsed body instead of the exact bytes. A JSON parser that reorders keys or normalises whitespace quietly breaks every signature, and the failure looks like the provider is wrong.
One question on the part you deliberately left out: since you name replay protection as not covered, do you check a timestamp on the signed payload anywhere in your own setup, or is that the next control you would add after the signature and the per-sender rate limit?
Thanks Abdullah. Honest answer: no, not yet. There's no timestamp check and no dedupe in the handler today, which is why replay sits under what the post doesn't cover. Two things blunt it a little. A tap on a menu that has since been replaced is ignored, because the reply has to point at the current prompt, and the per-sender limits cap how fast anyone can repeat a message. Neither stops a replay of the current message. The next control is dedupe on the message id, and Hritik's reply below describes it better than I would have.
The signature half is right, but there's a third door you get for free once you verify: replay. A captured request with a valid
X-Hub-Signature-256stays valid forever, so an attacker who sees one delivery (proxy log, a stolen backup, whatever the secret leak was) can POST the same bytes again and your handler processes the same payment twice. Meta's own payload leaves you the fix — keep a short TTL table keyed on the event id inentry[].changes[].value.messages[].id(or theX-Hub-Signature-256plus a timestamp if you control the sender) and reject anything you have already accepted inside the window, with the insert as the lock so two concurrent retries can't both win. Same applies to the GPS door: a shared secret plus a timestamp header, reject older than a couple of minutes.One more on the secret:
timingSafeEqualprotects the comparison, but thethrow-on-length-mismatch means you leak length by the different exception path anyway. Hashing both sides to a fixed width first (sha256(incoming)vssha256(expected)) closes that without the branch.Does the WhatsApp handler do anything at all before the signature check, like writing a log row or touching the DB? If yes and it's before, a replayed request hits the side effect even if you later return 401.
The secret sitting in git history until a scanner found it eight times in one commit — yeah, that one hurt to read, I've done the dumber version of that with a log line. The raw-body trap is the part I'd have tripped on too, re-serialising feels like the obvious thing to do. For the GPS door, do you rotate that shared secret on any schedule, or is it set-and-forget since it's one machine you control?
Closer to set-and-forget, honestly. It has been rotated once, after that scanner find, and there's no schedule yet. Both ends sit on one server I run, so a rotation is a single coordinated change, which is probably why it never got a calendar. Kevin's comment below is the better answer to how it should work.
refusing to start when the secret is missing is the part i would steal. failing loudly on deploy beats accepting everything quietly for a month.
Steal away. It came from asking how the check could switch itself off without anyone deciding to, and a missing env var was the easiest answer.
The cheerful 200 to an unsigned webhook is scary because it looks like everything works. Verifying the signature and the timestamp before anything else should be the first line in every handler.
Agreed on the signature first. Meta's scheme doesn't sign a timestamp, though, so for that door replay protection has to come from the handler being safe to run twice. That's the gap I flagged at the end of the post.
worth letting the first door accept two secrets for a short window, otherwise rotating after a leak either drops requests or keeps getting put off
Good point, and the current code doesn't do it: it takes one secret. Both ends are on one box, so the gap during a rotation is short, but accepting the current and previous secret for a brief overlap is cleaner, and it means a rotation after a leak never waits for a quiet moment. Noting it for the next pass.