Verify webhook signatures: reject forged requests using the raw body and secret
A webhook signature is a value used to check that an HTTP request came from a provider that holds a shared secret and that the signed data was not changed in transit. Many providers use HMAC-SHA-256, but signature headers and signed input differ by product. Follow the provider’s official library or verification procedure, and verify the original request body before parsing or reserializing its JSON.
A webhook signature is a value used to check that an HTTP request came from a provider that holds a shared secret and that the signed data was not changed in transit. Many providers use HMAC-SHA-256, but signature headers and signed input differ by product. Follow the provider’s official library or verification procedure, and verify the original request body before parsing or reserializing its JSON.
Verification is not encryption: it does not hide the body, and a valid signature alone does not prevent replay or duplicate processing. A receiver still needs separate checks for event type, durable acceptance, and duplicate handling.
What a signature does and does not establish
HMAC is a symmetric method that creates a message authentication code from a secret key and message. The sender and receiver share the same key, so the receiver recomputes the value and compares it with the supplied signature. It provides evidence of message integrity and possession of the shared key. Because a receiver holding the key can also create a valid HMAC, it is not equivalent to a public-key digital signature that can show which party signed a message to an outside verifier.
| Check | What the signature provides |
|---|---|
| Body integrity | Verification normally fails if signed bytes change |
| Shared-key possession | A party holding the correct key can create a valid HMAC |
| Confidentiality | None; HTTPS protects data in transit |
| Freshness or one-time processing | Only partly, if the provider signs a timestamp or delivery ID and the receiver checks it; signature verification alone does not guarantee either |
| Non-repudiation by the sender | Shared-key HMAC does not provide it because multiple parties may hold the secret |
Webhook requests arrive at a public HTTP endpoint. Before signature verification, do not treat the body as a trusted event or perform side effects such as recording a payment, changing an account, or writing business data.
Check the provider’s signature contract
Header names, algorithms, key formats, signed input, encoding, and version prefixes are not interchangeable. First identify the exact product and webhook type, then implement the procedure documented by that provider or use its SDK.
| Provider | Core contract | Implementation note |
|---|---|---|
| GitHub | X-Hub-Signature-256 carries an HMAC-SHA-256 hex digest prefixed with sha256=; input is the request payload body | The legacy X-Hub-Signature uses SHA-1. Handle the body consistently as UTF-8 and use a constant-time comparison. |
| Stripe | Pass Stripe-Signature and the endpoint signing secret to the Stripe SDK. Snapshot events use constructEvent; thin events use parseEventNotification. | The verifier needs the original HTTP body. Do not mix the CLI-forwarding secret with the Dashboard endpoint secret. |
| Standard Webhooks 1.0.0 | Signs the message ID, attempt timestamp, and raw payload. Symmetric signing uses HMAC-SHA-256 over msg_id.timestamp.payload; asymmetric signing also includes endpoint ID and uses Ed25519. | This is the specification’s contract. Do not assume a product implements it unless its docs confirm that. |
| GitLab signing token | In GitLab 19.1, signing tokens became generally available. It uses HMAC-SHA-256 over webhook-id.webhook-timestamp.raw JSON body. | GitLab recommends signing tokens for new webhooks. Check version support and do not confuse this with the weaker plaintext secret-token header. |
For GitHub, the receiver calculates HMAC-SHA-256 from the secret and request body, formats the result as hex with the sha256= prefix, then compares it with X-Hub-Signature-256. GitHub publishes this verification fixture: secret “It's a Secret to Everybody”, payload “Hello, World!”, expected signature sha256=757107ea0eb2509fc211221cce984b8a37570b6d7586c22c46f4379c8b043e17. This is a documented test vector, not a verification test run in a reader’s system.
Preserve the original request body
Signatures are usually calculated over the bytes sent by the provider, or an explicitly defined encoding of those bytes, rather than a JSON object after parsing. Parsing and serializing JSON again can change whitespace, key order, Unicode representation, or line endings. Even if the meaning looks identical, changed bytes produce a different HMAC.
Build the receiving path in this order:
Read or preserve the raw request body before the framework’s automatic JSON parser changes it.
Verify using the exact body encoding, signature header, key, and signed string specified by the provider.
Reject a failed comparison and perform no business side effects. Missing headers, malformed values, and invalid bodies should also fail closed.
Only after verification, parse JSON and check the event type/action and expected account or endpoint scope.
Record the delivery ID and processing state for duplicate control, acknowledge promptly, and perform heavier work in the background.
For example, Stripe’s Express guidance uses express.raw({type: 'application/json'}) on the webhook route and places that route before a JSON parser such as express.json(). Do not copy framework settings blindly; find the supported way to obtain the raw body in your environment.
Compare safely and protect the secret
Comparing HMAC strings with ordinary equality can make execution time depend on how many leading characters match. GitHub recommends a constant-time comparison such as secure_compare or Node.js crypto.timingSafeEqual. Prefer the provider SDK’s verifier over reimplementing cryptographic operations. If a comparison function requires equal-length inputs, validate format and length first and follow its API requirements.
Use a high-entropy random secret. Never put it in source code, a client bundle, URL, repository, or logs. Provide it to the server through a secret manager or access-controlled environment variable, and restrict read access. Keep endpoint secrets separate across test and production environments or providers. If exposure is suspected, rotate the secret through the provider and check whether the contract supports a transition period with old and new keys. Standard Webhooks specifies a way to send multiple signatures during zero-downtime rotation, but not every provider supports it.
Diagnose signature failures
A failed check may indicate an implementation or environment mismatch, or a forged request; do not assume it is automatically an attack. Do not log the full body or secret. Check:
Are you reading the correct signature header? Did you confuse GitHub’s SHA-256 header with its legacy SHA-1 header?
Is this the exact secret for this endpoint? Stripe CLI and Dashboard endpoint secrets differ.
Did JSON middleware transform the body before verification?
Are you using the exact bytes, encoding, signed string, hex/base64 format, and prefix required by the provider?
Is the algorithm and signature version explicitly allowed? Do not blindly accept an arbitrary algorithm named by an incoming header.
Does the test request belong to the same environment as the endpoint secret?
Log only a constrained error code and request trace ID. Never log the secret, full signature input, or sensitive body. Do not process an unverified request. Even after verification, use delivery IDs to suppress duplicates and validate timestamp tolerance when the provider contract requires it. Detailed replay defense is covered separately.
Sources
[GitHub Docs: Validating webhook deliveries](https://docs.github.com/en/webhooks/using-webhooks/validating-webhook-deliveries)
[GitHub Docs: Best practices for using webhooks](https://docs.github.com/en/webhooks/using-webhooks/best-practices-for-using-webhooks)
[Stripe Docs: Set up events and verify signatures](https://docs.stripe.com/events/set-up-events.md#signature-checking)
[Stripe Docs: Resolve webhook signature verification errors](https://docs.stripe.com/events/manage-webhook-endpoints.md#signature-errors)
[Standard Webhooks specification 1.0.0](https://github.com/standard-webhooks/standard-webhooks/blob/main/spec/standard-webhooks.md)
[GitLab Docs: Webhooks](https://docs.gitlab.com/user/project/integrations/webhooks/)
[MDN: HMAC](https://developer.mozilla.org/en-US/docs/Web/Glossary/HMAC)