Security

Webhook signature verification, explained

How webhook signatures work, how Stripe, Razorpay, Shopify and Cashfree sign their requests, and the five mistakes that make verification fail.

By the Relaya team · · 8 min read

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

ProviderHeaderWhat is signedEncoding
StripeStripe-Signature<timestamp>.<body>, timestamp in the headerhex
RazorpayX-Razorpay-Signaturethe bodyhex
ShopifyX-Shopify-Hmac-Sha256the bodybase64
Cashfreex-webhook-signature<timestamp><body>, timestamp in millisecondsbase64
GitHubX-Hub-Signature-256the body, prefixed sha256=hex
Standard Webhooks / Svixwebhook-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:

  1. 01
    Read the raw request body as bytes, before any JSON parsing.
  2. 02
    Build the signed string exactly as the provider documents it (body alone, or timestamp plus body).
  3. 03
    Compute HMAC-SHA256 with your secret and encode it the same way (hex or base64).
  4. 04
    Compare with a constant-time function, and check the timestamp if there is one.
Verify a Stripe-style signature (Node.js)
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

  1. 01
    Hashing parsed JSON. JSON.stringify(JSON.parse(body)) changes spacing, key order and number formats. Always hash the raw bytes.
  2. 02
    The wrong secret. Test and live secrets differ, Stripe has one per endpoint, and Razorpay's webhook secret is not your API key secret.
  3. 03
    Comparing with ==. An ordinary string comparison stops at the first different character, which leaks timing. Use crypto.timingSafeEqual, hmac.compare_digest or hash_equals.
  4. 04
    Mixing up encodings. Comparing a hex digest with a base64 header never matches. Decode both to bytes first.
  5. 05
    Ignoring 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:

Relaya-Signature: t=<unix seconds>,v1=<hex HMAC-SHA256 of '<t>.<body>'>
// 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.

Stop finding out from your customers.

Point your first webhook at Relaya today. Try the Team plan free for 7 days.