What a signature proves
Your webhook URL is public. Anyone who finds it can post a fake payment.captured and, if your code believes it, get an order for free. A signature proves two things about a request: it came from someone who knows the shared secret, and the body wasn't changed on the way.
Almost every provider uses HMAC-SHA256: the sender computes a keyed hash of the body with a secret only you and it know, and sends it in a header. You compute the same hash over the bytes you received and compare.
How popular providers sign their webhooks
| Provider | Header | What is signed | Encoding |
|---|---|---|---|
| Stripe | Stripe-Signature | <timestamp>.<body>, timestamp in the header | hex |
| Razorpay | X-Razorpay-Signature | the body | hex |
| Shopify | X-Shopify-Hmac-Sha256 | the body | base64 |
| Cashfree | x-webhook-signature | <timestamp><body>, timestamp in milliseconds | base64 |
| GitHub | X-Hub-Signature-256 | the body, prefixed sha256= | hex |
| Standard Webhooks / Svix | webhook-signature | <id>.<timestamp>.<body> | base64 |
The details differ in exactly the places that cause bugs: which bytes are signed, hex or base64, and whether a timestamp is included.
Verifying a signature correctly
The same four steps work for every HMAC scheme:
- 01Read the raw request body as bytes, before any JSON parsing.
- 02Build the signed string exactly as the provider documents it (body alone, or timestamp plus body).
- 03Compute HMAC-SHA256 with your secret and encode it the same way (hex or base64).
- 04Compare with a constant-time function, and check the timestamp if there is one.
import crypto from 'node:crypto'
export function verifyStripeStyle(rawBody, header, secret, toleranceSec = 300) {
const parts = Object.fromEntries(header.split(',').map((p) => p.split('=')))
const t = Number(parts.t)
if (!t || Math.abs(Date.now() / 1000 - t) > toleranceSec) return false // stale or future
const expected = crypto.createHmac('sha256', secret).update(`${t}.`).update(rawBody).digest('hex')
const given = Buffer.from(parts.v1 ?? '', 'hex')
return given.length === 32 && crypto.timingSafeEqual(given, Buffer.from(expected, 'hex'))
}Five mistakes that break verification
- 01Hashing parsed JSON.
JSON.stringify(JSON.parse(body))changes spacing, key order and number formats. Always hash the raw bytes. - 02The wrong secret. Test and live secrets differ, Stripe has one per endpoint, and Razorpay's webhook secret is not your API key secret.
- 03Comparing with
==. An ordinary string comparison stops at the first different character, which leaks timing. Usecrypto.timingSafeEqual,hmac.compare_digestorhash_equals. - 04Mixing up encodings. Comparing a hex digest with a base64 header never matches. Decode both to bytes first.
- 05Ignoring the timestamp. A valid signature on an old request is still an old request. Reject timestamps more than about five minutes away, and drop repeats by event ID.
Replays, duplicates and rotating secrets
A signature stops forgery, not repetition. An attacker who captures one valid request can send it again, and providers themselves resend events after timeouts. Keep the IDs of events you've processed and ignore repeats. Providers without a signed timestamp, like Razorpay and Shopify, send an event ID header for exactly this.
When you rotate a secret, accept both the old and the new one for a short overlap. Stripe makes this easy by sending several v1 signatures during a roll; check each one.
Or let Relaya verify at the edge
Relaya implements each provider's scheme (Razorpay, Cashfree, PayU, PhonePe, Stripe, Shopify, GitHub, Slack, HubSpot, Twilio, Square, Segment, SendGrid, Notion, Jira, Standard Webhooks and generic HMAC), checks timestamps and drops duplicates. Forged requests are rejected and stored as evidence. Your code then verifies one scheme for every provider:
// The same function as above works: Relaya signs forwarded events the Stripe way, // with your destination's secret (rsec_…), and adds an Idempotency-Key header // that stays the same across retries. verifyStripeStyle(rawBody, req.headers['relaya-signature'], process.env.RELAYA_SECRET)
See the docs for SDKs in Node, Python, PHP, Go and Java.