Webhook security: accept an event without performing the action twice
A webhook tells your server about an event in another service: a changed booking, a document becoming ready or a subscription update. A network failure can cause redelivery. Even a correctly signed notification should not trigger the same action on every arrival. Here is how to connect source verification, durable receipt and a single intended data change.
Start with one clearly defined business action
Imagine a fictional calendar that creates an administrator task when a training provider sends a notification. Two copies should leave one task. An older booking change should not overwrite the current record. This is a teaching scenario, not a finding from a real product audit.
Write down the expected outcome before choosing a library: which event changes what, which object it affects and what counts as a duplicate. A delivery, an event and a booking may each have different identifiers. The signature establishes the notification’s source; the calendar’s rules determine what that source may change.
Verify the original body before changing data
HTTPS protects transport, but does not by itself identify whoever sends a POST to your endpoint. Use the provider’s authentication mechanism and maintained library. Bound request size, permitted methods and content format before parsing; validate the event schema after authenticating the source.
In Stripe’s documented example, verification needs the original body, Stripe-Signature and the correct endpoint secret. Parsing and serializing JSON again can change the signed bytes. The local CLI secret differs from a Dashboard endpoint secret. Diagnose configuration and middleware order while keeping secrets out of logs.
Do not trust an event ID to modify records before verification succeeds. Next, check the event type, organization and mapping between external and internal objects. A genuine notification still does not authorize an arbitrary state transition. OWASP’s REST guidance treats these as separate checks.
Separate delivery freshness from event age
Some protocols authenticate a delivery timestamp. Stripe generates a new signature and timestamp for each retry; the event’s creation time stays the same. Keep signature freshness checks enabled and synchronize the server clock. An old created value does not automatically make a delivery unauthentic.
Another provider may have a different contract. Establish exactly what is signed and how retries work. An event timestamp is not a universal unique key: multiple changes may share it. Stripe’s Event reference gives id, created and livemode distinct purposes.
Persist receipt before acknowledging it
For the fictional calendar, a reasonable design is to save the verified event in an inbox that survives restarts and let a worker process it. A 2xx response means receipt is already durable. If persistence fails, a success response conceals the loss from the sender.
Protect the record with a unique key that accounts for the source, account, mode and event ID. A non-atomic check for an existing row before insertion allows two handlers to race. Coordinate local data changes and processing state through a transaction or another verified consistency protocol.
Analyze external effects separately. If another service creates the task, a crash immediately afterward leaves an unknown outcome. A recipient-supported idempotency key, an outgoing-action journal and reconciliation can help; an outbox alone cannot prevent a duplicate at the recipient. Stripe’s Idempotency-Key applies to outbound API calls and does not replace inbound event tracking.
Plan for reversed order and recovery
Stripe does not guarantee delivery order. A late notification may require the calendar to fetch the provider’s current state. Use an object version for ordering only when the provider documents that guarantee. Avoid unconditionally overwriting a record with every arriving status.
Distinguish received, processing, completed and failed work. Define who requeues stalled work and how they distinguish it from a handler that is still running. Stripe’s recovery guidance specifically warns that manual processing may overlap with automatic retries.
Exercise failure scenarios in your own environment
The following is a proposed plan for your own sandbox using fictional bookings. These tests were not performed here. Record the HTTP response, inbox state and final task count for each scenario; a successful status code alone proves too little.
- Missing signature or modified body: business data remains unchanged.
- Old delivery signature: freshness validation follows the provider’s protocol.
- Legitimate redelivery of an older event: validate the new signature without duplicating the result.
- Two sequential and two concurrent copies: one intended business effect remains.
- Persistence failure and a restart after receipt: the event is retained or retried and processing recovers.
- Failure after an external action and reversed event order: reconcile the outcome and preserve current data.
Keep evidence of receipt and processing
Connect delivery attempts through the event identifier, time, verification outcome and processing state. Logs should explain failures without signing secrets or complete personal payloads. Monitor stalled jobs and recurring errors, and assign responsibility for recovery.
Useful evidence consists of a saved scenario matrix, a clear recovery procedure and confirmation that retries do not multiply actions. Repeat the matrix after framework or SDK upgrades: a change in how the request body is read can break signature verification while the business logic stays unchanged.