Webhook security best practices begin with a simple rule: never trust an incoming event just because it arrived at a URL that your application controls. Treat every delivery as an external request. Verify the provider's signature, validate the event, make processing safe to repeat and keep the endpoint separated from sensitive internal services.
What is webhook security?
Webhook security is the set of controls that verifies incoming event notifications and limits what they can do. It helps prevent forged requests, tampering, replay, duplicate processing, secret exposure and unsafe outbound requests when an application sends events to customer-managed URLs.
A webhook normally lets one system send an HTTP request to another when something happens, such as a payment update or a repository change. This is useful because the receiving system does not need to poll constantly. The trade-off is that an internet-facing endpoint receives requests that must be authenticated and handled carefully.
Verify the sender before acting on an event
Use the signature format documented by the provider. Many webhook systems use a message authentication code such as HMAC with SHA-256. The receiver calculates the expected signature using the shared secret and the exact message bytes, then compares it using a constant-time comparison supplied by a reputable library.
Do not invent a universal signature parser. GitHub, Stripe and other providers use different header formats and signing inputs. Some sign the raw request body; others also authenticate a timestamp. Use the provider's maintained verification library where available and pass it the unmodified request body before JSON parsing or other middleware changes it.
- Keep the signing secret outside source code, public repositories and client-side applications.
- Use HTTPS and keep certificate verification enabled.
- Use a separate secret for each endpoint where the provider supports it.
- Reject missing or invalid signatures before triggering business actions.
How do you stop webhook replay and duplicate processing?
Replay protection prevents a captured valid delivery from being accepted again outside the provider's allowed process, while idempotency prevents the same event from causing the same business action twice. Use authenticated timestamps and event identifiers when the provider signs them, and store processed event IDs durably.
Signature verification alone does not guarantee replay protection. A captured request may still carry a valid signature. Where the provider's signature protocol includes an authenticated timestamp, enforce its documented freshness window and keep the server clock synchronized. Store an authenticated event ID after verification so normal retries or duplicate deliveries do not repeat a payment, order or notification.
Provider differences matter. GitHub's X-GitHub-Delivery header identifies a delivery and remains the same on redelivery, but its body-only signature does not make that header an authenticated replay timestamp. Follow each provider's specification rather than assuming every event ID or header is cryptographically protected.
Design safe retries and fast acknowledgements
Webhook providers commonly retry deliveries after timeouts or server errors. Your handler should validate the request, record or queue the event durably, and return a successful response promptly. Process slower business work asynchronously when appropriate, then monitor failed jobs and provide a controlled recovery path.
Do not acknowledge an event before it is safely recorded. If a provider retries because it received no timely response, the receiver should safely recognise the repeated event. Use bounded retry policies, exponential backoff with jitter for your own downstream calls, and a dead-letter or review queue for events that repeatedly fail.
Protect webhook endpoints from unsafe payloads and network access
A valid signature proves that a request matches a provider's signing process; it does not prove that every field is suitable for every operation. Validate the event type, action, schema, identifiers and allowed state transitions. Apply request-size limits, accept only the required HTTP method and avoid using raw payload fields to build database commands or shell instructions.
If your product sends webhooks to customer-supplied callback URLs, protect the sending side against server-side request forgery (SSRF). Restrict destinations to approved public HTTPS addresses, block loopback, private, link-local and cloud metadata ranges, and re-check resolved IP addresses at connection time. Redirects and DNS changes must not bypass those checks.
Webhook control checklist
| Control | Receiver responsibility | Publisher responsibility |
|---|---|---|
| Transport | Serve the endpoint over HTTPS | Verify TLS certificates when delivering |
| Authenticity | Verify the provider's exact signature format | Sign each delivery with a protected secret |
| Replay and duplicates | Use supported freshness checks and durable deduplication | Provide documented event identifiers and retry behaviour |
| Payload handling | Validate size, schema, event type and allowed actions | Send only required event data |
| Operations | Queue safely, log outcomes and alert on failures | Provide delivery history and a redelivery process |
Example: processing an order-paid event safely
Imagine an online store receives an order.paid event. The handler first verifies the provider signature using the raw body. It checks the event type and order identifier, then inserts the event ID into a durable processing table with a uniqueness constraint. If that ID already exists, it acknowledges the duplicate without issuing another shipment request.
For a new event, the handler queues an order-state update and returns a success response after the queue confirms the record. A worker then verifies that the order is still awaiting payment, records the payment state and sends the fulfilment request using its own idempotency key. If fulfilment fails, the worker retries under a controlled policy rather than asking the webhook sender to repeat the whole workflow.
Test before release and monitor after launch
- Send a request with no signature and confirm that it is rejected.
- Change one byte of a signed body and confirm signature verification fails.
- Deliver the same valid event twice and confirm the business action happens once.
- Test an old timestamp only when the provider's protocol authenticates timestamps.
- Test oversized payloads, unknown event types, repeated failures and secret rotation.
- Confirm logs do not contain signing secrets, authorization headers or unnecessary personal data.
Track signature failures, duplicate counts, queue age, delivery latency and dead-letter events. Logs should help operators diagnose a problem without copying full customer payloads or sensitive headers into a general-purpose log store.
Frequently asked questions
Should every webhook use a signing secret?
For an internet-facing webhook, use the strongest authentication method supported by the provider, commonly a shared secret and signed payload. A secret should be unique where possible, stored outside source code, rotated after suspected exposure and verified before the application performs any business action.
Is HTTPS enough to secure a webhook?
No. HTTPS protects data in transit and helps prevent interception, but it does not prove that the sender is authorised to trigger your endpoint. Combine transport security with provider-specific signature verification, payload validation, replay and duplicate controls, access restrictions and monitoring.
Can a webhook provider send the same event more than once?
Yes. Providers may retry after timeouts, temporary server errors or failed acknowledgements, and operators may request redelivery. Design handlers to be idempotent by storing verified event identifiers and checking business state before repeating payments, fulfilment, account changes, notifications or other critical customer actions.
Should a webhook endpoint process the whole event before responding?
Not always. A handler can verify the signature, validate the event and durably queue it before returning success, then let a worker perform slower tasks. The key is not to acknowledge an event that has not been safely recorded or to lose work after a server restart.
How often should webhook secrets be rotated?
Follow the provider's rotation facilities and your organisation's secret-management policy. Rotate immediately after suspected exposure, staff or system changes that compromise access, or a provider-recommended event. For planned rotation, use a documented overlap only when both sides can safely support it.
Key takeaway
A secure webhook is more than a secret and an HTTPS URL. Verify the provider's exact signing scheme, prevent repeat side effects, validate the event, acknowledge only after durable recording and monitor failures. Test the publisher and receiver responsibilities separately because their security controls are not identical.