Securing Webhook Consumers with Signature Verification and Replay Protection
Verify the exact request body with the provider’s library, reject stale signatures, and process duplicate deliveries idempotently.
A webhook endpoint receives an HTTP request from an external system and must establish that the payload came from the expected provider before acting on it. Signature verification, timestamp tolerance, and idempotent processing address different parts of that trust boundary.
THE PRODUCTION PROBLEM STATEMENT
A public endpoint that trusts JSON fields can be forged by anyone who can reach it. A valid old request can also be replayed, and providers commonly retry deliveries when they do not receive a timely response.
Verify the raw body before interpreting payload fields
Signature schemes often cover the exact request bytes. Parsing JSON and serializing it again can change whitespace, ordering, or encoding and invalidate verification. Use the provider SDK and preserve the raw body as the integration requires.
Keep signing secrets in protected configuration, support deliberate rotation, and use the provider’s documented verification library. Do not invent a homegrown signature parser for a production integration.
app.post('/webhooks/provider', rawBodyParser, (req, res) => {
let event;
try {
event = provider.webhooks.constructEvent(
req.body,
req.headers['provider-signature'],
process.env.PROVIDER_WEBHOOK_SECRET
);
} catch {
return res.status(400).send('Invalid signature');
}
enqueueVerifiedEvent(event);
return res.sendStatus(202);
});Reject stale messages and tolerate safe retries
When a signature includes a timestamp, enforce the provider’s documented tolerance window to reduce replay risk. Keep server clocks monitored and understand how retries interact with the accepted window.
A valid event may still arrive more than once. Store its provider event ID with a uniqueness constraint and make processing idempotent, so retrying a delivery does not repeat a charge, email, or fulfillment action.
Authenticity and idempotency are separate checks
A valid signature proves a party holding the secret signed a message. It does not prove that the event has never been processed before.
Acknowledge quickly and process with durable state
Verify the request, persist or enqueue the event, and return a timely acknowledgment. Long business workflows should run asynchronously after durable acceptance, with retries and dead-letter handling around the worker.
Record verification failures, event IDs, retry counts, and outcomes without logging secrets or sensitive payload fields. This makes delivery incidents diagnosable without turning logs into a second data breach.
Frequently Asked Questions & Technical Inquiries
Direct, practical answers to common architectural and debugging questions.
Follow the provider’s integration path. Many signatures cover the exact raw body, so parse only after verification unless the provider documents otherwise.
Faisal Askari
Verified AuthorSoftware Engineer & Full-Stack Developer
Faisal Askari is a software engineer with deep expertise in web systems architecture, the JavaScript runtime ecosystem, and client-side performance engineering. Over years of hands-on production engineering, he has designed and shipped enterprise dashboards, high-converting digital interfaces, and specialized full-stack applications.
Resilient Full-Stack Architecture: Idempotent APIs, Optimistic UI, and Cache Invalidation
Engineering robust distributed state between client and server: idempotency keys, safe optimistic rollbacks, exponential backoff with jitter, and cache coherence.
Definitive Web Authentication: Secure HttpOnly Session Cookies vs LocalStorage JWT Storage
Why storing access tokens in localStorage exposes users to token exfiltration, and how to structure secure, revocable HttpOnly cookie sessions.