← Back to writing
Cyber Security Oct 12, 2026 6 min read

Hardening Webhook Receivers Against Timing Side-Channels and Replay Attacks

Why standard string comparison operators in webhook receivers leak HMAC secrets through microsecond timing variations, and how constant-time comparisons with timestamp tolerance windows eliminate exploitation vectors.

Hardening Webhook Receivers Against Timing Side-Channels and Replay Attacks

The Vulnerability Behind Webhook Verification

Most modern webhooks authenticate inbound HTTP payloads using an HMAC SHA-256 signature passed in request headers. A surprising number of production webhooks verify this signature using standard language string comparison:

// VULNERABLE: Short-circuiting string comparison
if (signatureHeader === expectedSignature) {
  processPayload(req.body);
}

In JavaScript, C, Python, and Go, standard string equality operators bail out on the very first non-matching byte. This creates a statistical timing variation—an attacker measuring response times over thousands of automated requests can infer which initial bytes matched, progressively reconstructing valid signatures byte by byte.


1. Constant-Time Signature Comparison

To prevent timing leaks, cryptographic comparisons must evaluate all bytes in identical CPU cycle intervals regardless of where differences occur.

Node.js provides crypto.timingSafeEqual, but it will throw an unhandled exception if buffer lengths differ:

import crypto from 'node:crypto';

export function verifyWebhookSignature(
  payload: string | Buffer,
  signatureHeader: string,
  secret: string
): boolean {
  const hmac = crypto.createHmac('sha256', secret);
  const computedSignature = hmac.update(payload).digest('hex');

  const a = Buffer.from(signatureHeader, 'utf8');
  const b = Buffer.from(computedSignature, 'utf8');

  // Prevent length mismatch exceptions from leaking timing info
  if (a.length !== b.length) {
    return false;
  }

  return crypto.timingSafeEqual(a, b);
}

2. Defeating Replay Attacks with Tolerance Windows

Even with constant-time verification, an eavesdropped or intercepted payload can be resent indefinitely by a malicious actor unless your receiver enforces replay defense:

  1. Timestamp Headers: Require suppliers to include an epoch timestamp in signature calculation (e.g. t=1728741000,v1=...).
  2. Tolerance Bounds: Reject any requests older than 300 seconds (5 minutes) or skewed into the future.
  3. Idempotency Keys: Cache processed webhook message IDs in a fast distributed store like Redis with a 24-hour TTL:
export async function assertFreshPayload(
  eventId: string,
  timestampSeconds: number,
  redisClient: any
): Promise<void> {
  const now = Math.floor(Date.now() / 1000);
  const maxTolerance = 300; // 5 minutes

  if (Math.abs(now - timestampSeconds) > maxTolerance) {
    throw new Error('Webhook rejected: timestamp outside tolerance threshold.');
  }

  // Atomically set with NX (Not eXists)
  const isUnique = await redisClient.set(`webhook:seen:${eventId}`, '1', 'EX', 86400, 'NX');
  if (!isUnique) {
    throw new Error('Webhook rejected: duplicate payload detected.');
  }
}

Summary Architecture

  • Step 1: Raw binary buffer ingestion (never parse JSON before signature check).
  • Step 2: Split header components (t timestamp, v1 digest).
  • Step 3: Recompute HMAC with shared secret and execute crypto.timingSafeEqual.
  • Step 4: Enforce replay cutoff window and record message idempotency key.

Following these practices eliminates 100% of webhook timing attack surfaces in production.

Primidac
Engineering notes
← More notes
Notification