Verifying Webhook Unix Timestamps to Stop Replay Attacks
Checking only a cryptographic signature on an incoming payment webhook leaves your server exposed to replay attacks. If an attacker captures a valid signed HTTP request and resends it two days later, your endpoint could unlock orders or issue credits twice unless you verify the signed Unix timestamp.
How the Stripe-Signature Timestamp Header Works
Payment platforms like Stripe include a Stripe-Signature header formatted like t=1764892800,v1=5257a869..., where t is a 10-digit Unix timestamp in seconds. As explained in the Stripe Webhook Signature documentation, the sender signs timestamp + "." + raw_request_body using HMAC-SHA256.
Verification Steps to Block Replay Attacks
- Extract
tandv1from the signature header. - Compute the expected HMAC-SHA256 using your raw unparsed HTTP body bytes and webhook secret. Never parse and re-stringify JSON first, or whitespace changes will break the hash.
- Compare signatures in constant time using
crypto.timingSafeEqual()to prevent timing attacks. - Enforce a timestamp tolerance window: Official Stripe SDKs reject webhooks older than 300 seconds (5 minutes) by comparing
Math.abs(nowSeconds - t) <= 300.
const nowSec = Math.floor(Date.now() / 1000);
if (Math.abs(nowSec - Number(headerTimestamp)) > 300) {
throw new Error("Webhook timestamp outside 5-minute tolerance");
}