How Webhook HMAC Signatures Prevent Tampering
When external SaaS platforms (like Stripe, GitHub, or Shopify) push HTTP webhook events to your application endpoint, verifying the HMAC SHA-256 signature header is critical. It guarantees that the incoming request actually originated from the authentic vendor and was not intercepted, modified, or forged by an attacker.
Common Webhook Verification Pitfalls
- Parsing JSON Before Verification: Re-serializing an already parsed JSON object alters whitespace, newline characters, and key ordering, causing signature validation to fail. Always verify the raw, unparsed request
Buffer. - Timing Attacks: Standard string equality checks (
sig === computed) exit early on the first mismatched character, creating a side-channel timing vulnerability. Always use constant-time comparisons like Node.jscrypto.timingSafeEqualor Pythonhmac.compare_digest. - Timestamp Replay Attacks: Stripe prefixes signatures with a Unix timestamp (
t=169...). Applications should reject webhooks with timestamps older than 5 minutes to prevent replay attacks.