Webhooks
debt(d7/e5/b5/t5)
Closest to 'only careful code review or runtime testing' (d7). The detection_hints note that semgrep can flag some patterns (e.g. no HMAC verification, synchronous heavy processing), but automated detection is explicitly marked 'no' in the metadata. Missing signature verification and non-idempotent handlers are invisible until a real webhook sender triggers duplicate or forged payloads in production — semgrep gives partial coverage but won't catch all logical mistakes like missing idempotency, pushing this toward d7.
Closest to 'touches multiple files / significant refactor in one component' (e5). The quick_fix requires: adding HMAC signature verification middleware, refactoring synchronous handlers to enqueue work asynchronously (introducing a queue worker), and adding idempotency checks. This spans the endpoint handler, a queue system, and potentially a logging layer — more than a one-liner but contained within the integration component rather than a full cross-cutting refactor.
Closest to 'persistent productivity tax' (b5). Webhook handling applies to web and api contexts and introduces ongoing obligations: every new webhook integration must re-implement signature verification, idempotency, async dispatch, and raw payload logging. The pattern doesn't pervade the entire codebase but imposes a recurring structural cost on every integration work stream, consistent with b5.
Closest to 'notable trap (a documented gotcha most devs eventually learn)' (t5). The misconception field explicitly states developers assume webhooks are always more efficient than polling, and the common_mistakes list shows several well-documented but non-obvious gotchas (missing signature verification, synchronous processing causing retries, lack of idempotency). These are documented traps that most developers learn only after hitting them — fitting t5 rather than t7 because the issues are well-covered in provider documentation.
Also Known As
TL;DR
Explanation
Webhooks enable event-driven integrations: instead of your application polling an API every minute, the service POSTs a JSON payload to your endpoint the moment an event fires (payment.completed, push.received). Implementing a receiver: expose a public HTTPS endpoint, verify the payload signature (HMAC-SHA256 of the body against a shared secret, constant-time comparison with hash_equals()), respond with 200 immediately, and queue actual processing asynchronously to avoid delivery timeouts. Sending webhooks: store endpoint URLs per customer, deliver with retry and exponential backoff, record all delivery attempts, and support manual replay for debugging.
Common Misconception
Why It Matters
Common Mistakes
- Not validating the webhook signature — any HTTP client can send a fake payload to your endpoint.
- Not returning HTTP 200 quickly and processing asynchronously — slow handlers cause the sender to retry, duplicating events.
- Not handling duplicate delivery — webhook senders retry on timeout or non-2xx; your handler must be idempotent.
- Not logging raw payloads — debugging webhook failures requires the original body.
Avoid When
- The consumer cannot guarantee idempotent processing — duplicate deliveries will cause duplicate side effects.
- The payload contains sensitive data sent to an unverified URL — always validate signatures and use HTTPS.
- The receiving server is unreliable — webhooks can be lost; use a message queue for guaranteed delivery.
- You need request-reply semantics — webhooks are fire-and-forget; use polling or SSE for two-way flow.
When To Use
- Notifying external systems of events in real time without them needing to poll.
- Integrating with third-party platforms (Stripe, GitHub, Shopify) that push events on state changes.
- Triggering CI pipelines, cache purges, or downstream workflows in response to your own events.
- Replacing polling in scenarios where events are infrequent and latency matters.
Code Examples
// No signature validation — accepts any POST as legitimate:
app()->post('/webhook', function(Request $req): Response {
processEvent($req->json()); // No HMAC verification
return response('ok', 200);
});
// Add: hash_equals(hash_hmac('sha256', $req->rawBody(), $secret), $req->header('X-Signature'))
// Verify Stripe webhook signature
$payload = file_get_contents('php://input');
$sig = $_SERVER['HTTP_STRIPE_SIGNATURE'];
$event = \Stripe\Webhook::constructEvent($payload, $sig, $secret);