Webhook Replay Protection: RFC & Rollout
Context
Our billing webhook receiver verifies HMAC-SHA256 signatures, but previously ignored the signed timestamp header. A captured delivery could be replayed indefinitely and still pass verification.
Root Cause
Signature checking compared the raw digest without checking delivery age or using constant-time equality:
verifyWebhookSignature()hashed the raw payload body onlyX-Webhook-Timestampwas never bound into the signed message- Replayed payloads older than 5 minutes returned
200
Implementation
The verifier now signs ${timestamp}.${rawBody}, rejects timestamps outside a 300-second skew window, and compares digests with timingSafeEqual.
- Replay-safe — stale deliveries fail fast with
401 - Timing-safe — constant-time buffer comparison avoids side-channel leaks
- Idempotent — delivery IDs are cached in KV for 10 minutes
Verification Matrix
Every replay and signature edge case is tested:
| Case | Status |
|---|---|
| Valid signature within 5 min window | 200 |
| Valid signature older than 5 min | 401 |
| Duplicate delivery ID | 200 (no-op) |
| Tampered payload or bad digest | 401 |
Rollout Plan
- Enable
webhook_replay_guardin dry-run logging mode for 48 hours - Track
webhooks.verify.staleandwebhooks.verify.invalidcounters - Enforce hard rejections once clock-skew false positives stay below 0.1%
- Remove the legacy body-only verifier after one billing cycle

