The webhook nobody signs
A webhook endpoint is a public door that accepts instructions from strangers who say they are your vendor. Two real ones from a production app: a shared secret that has to survive log files, and an HMAC that breaks if you parse the body first. Plus a two-minute test you can aim at your own.
- security
- appsec
- webdev
- nestjs

Here is a request that a lot of production apps will answer with a cheerful 200:
curl -s -X POST https://your-api.example/webhooks/payments \
-H "Content-Type: application/json" \
-d '{"event":"payment.succeeded","amount":5000,"customer":"cus_123"}'Nobody at the payment provider sent that. You did, from a laptop, with no key of any kind. If your endpoint processes it, anyone who can guess the URL can tell your app that a payment succeeded, a vehicle moved, a message was delivered or a user confirmed their email. The endpoint exists so a vendor can reach your system without logging in, which makes it a login-free door by design, and the only thing standing in that door is whatever check you wrote.
I maintain an app with two such doors, and they use two different checks. The differences between them are most of what there is to know.
The first one: a shared secret#
The app's GPS server posts position updates to it. The sender is one machine I control, so the simplest thing that works is a shared secret in a header, compared on arrival. A first version of that check is five lines, and the interesting part is everything around the comparison.
The comparison itself is constant-time, with a length check ahead of it because the function throws on buffers of different sizes:
const secretBuf = Buffer.from(incomingSecret);
const expectedBuf = Buffer.from(expectedSecret);
if (
secretBuf.length !== expectedBuf.length ||
!crypto.timingSafeEqual(secretBuf, expectedBuf)
) {
throw new UnauthorizedException("Invalid webhook secret");
}The app also refuses to start if the secret is not configured. A missing environment variable is the easiest way for an authentication check to turn itself off without anyone deciding to, so on boot the controller checks for the secret and exits the process if it is absent. Failing loudly on deploy is better than accepting everything quietly for a month.
And the secret must never be logged. This is in the code as a comment, and it is there because of what happened. During debugging, a statement logged both the secret the request presented and the secret the server expected. Log output was captured to a file, the file was committed, and the secret sat in version control history until a secrets scanner found it, eight times in one commit. It was rotated, the log statements were removed, and the comment now says that the header carries the secret in plaintext and no log line may touch either side of the comparison. A shared secret is only as private as the least careful place it gets printed.
The second one: a signature over the exact bytes#
The other door belongs to Meta's WhatsApp Cloud API, and here the sender is a third party, so a shared secret in a header would be the wrong shape. Meta signs each delivery instead. The request carries X-Hub-Signature-256, an HMAC-SHA256 of the body, keyed with an app secret that only Meta and I know. If the signature matches, the body is the one Meta sent, unchanged.
The trap is in the word body. It means the exact bytes that arrived. Most web frameworks parse a JSON body before your handler sees it, and if you take the parsed object, serialise it again and hash that, the whitespace and key order may differ from what Meta signed. The signatures will not match, and the failure looks like an attack on every single request. The fix in this app is to keep the raw bytes around, and the handler hashes those:
const expected = crypto
.createHmac("sha256", appSecret)
.update(req.rawBody)
.digest("hex");Three rules sit around that call. A missing app secret is a hard error and never a skipped check. A signature header without the sha256= prefix is rejected before any hashing. And if the raw body is unavailable, which means the app was deployed without the setting that keeps it, the request fails too, because that is a deployment fault and the safe response to a fault in a security check is to refuse.

Each of those rules has a test: a correctly signed body is accepted, a tampered body is rejected, a body signed with the wrong secret is rejected, a missing header is rejected, a non-prefixed signature is rejected, and an app with no secret configured refuses every request. The tests generate their own secrets, so no real one ever sits in the repository.

A rate limit that knows who is asking#
One more detail is specific to webhooks. Every delivery from Meta arrives from Meta's addresses, so a rate limiter keyed on client IP sees one very busy caller and throttles all of your users at once. The limit has to be keyed on something inside the payload, in this case the sender's phone number, which means it can only run after the signature has been verified and the body parsed. Limits per sender sit inside the handler, in order: a number of inbound messages per minute, a stricter limit on replies to numbers the app does not know, and a per-hour cap on commands from linked numbers.
What this does not cover#
I have described verifying the sender. I have not described replay protection, which stops an attacker re-sending a genuine signed request later. Meta's scheme does not include a timestamp in the signed content in the way some providers' do, so any replay defence has to come from making the handler idempotent. This app acknowledges a delivery quickly and handles the work afterwards, and I have not shown that the handler is safe to run twice on the same event.
Run it against your own#
Take a real event body for your endpoint, the kind your provider actually sends (its documentation has examples, and so do your logs), and keep it in a variable. Use the same body in every request below, so that a rejection can only come from the check you are testing, not from a body your code fails to parse. With no credentials:
BODY='{"event":"payment.succeeded","amount":5000,"customer":"cus_123"}' # your provider's real event shape
curl -s -o /dev/null -w "%{http_code}\n" -X POST https://your-api.example/webhooks/your-endpoint \
-H "Content-Type: application/json" -d "$BODY"You want a 401 or a 403. A 200 means the door is open, or may be: some endpoints answer 200 to everything and quietly drop what fails, and some acknowledge first and check later. So after a 200, look in the logs or the data for whether the event was acted on. A 400 proves nothing either way, because it usually means the body failed validation before any check ran.
If you do get a rejection, send the same body with a valid signature to make sure the good path works:
SIG=$(printf '%s' "$BODY" | openssl dgst -sha256 -hmac "$YOUR_APP_SECRET" | sed 's/^.* //')
curl -s -o /dev/null -w "%{http_code}\n" -X POST https://your-api.example/webhooks/your-endpoint \
-H "Content-Type: application/json" -H "X-Hub-Signature-256: sha256=$SIG" -d "$BODY"Then change one value in the body, keep the old signature, and send it again:
TAMPERED='{"event":"payment.succeeded","amount":9000,"customer":"cus_123"}' # the same body, one value changed
curl -s -o /dev/null -w "%{http_code}\n" -X POST https://your-api.example/webhooks/your-endpoint \
-H "Content-Type: application/json" -H "X-Hub-Signature-256: sha256=$SIG" -d "$TAMPERED"The signed request should pass and be processed, and the tampered one should fail and leave no trace in the data. The tampered body is still valid JSON in the same shape, so if it is rejected, the signature check rejected it. If both pass, the signature is being read and then ignored.
If your endpoint uses a shared secret in a header instead, like the first door above, run the same pair with that header: the right secret should pass, and the same request with one character of the secret changed should fail.

Likes are saved in your browser only.
This post is also published on dev.to.
Keep reading
- 27 November 20266 minEleven fixes from one code reviewNo scanner reported any of them. A coding agent ran a security-review skill I wrote over my own multi-tenant app, and I reviewed every patch. The result: eleven small security commits, including an XSS sink, a token compared with ===, and a sign-in that never checked the issuer.
- 18 November 20268 minThree controls that looked switched onA deploy gate that checked one commit and shipped another, a secrets scanner that never read a merge commit, and a rate limiter that saw every visitor as the same address. All three passed every check I had. Here is how each one failed quietly, and a two-minute test for each.
- 3 November 20266 minThe AI endpoint that charges you money when somebody abuses itAbuse of a normal endpoint costs you capacity. Abuse of an endpoint that calls a model costs you money per request while everything looks healthy. Four controls, and the quota bug that charges you for every rejection.