# How do webhook security mechanisms verify the authenticity of incoming HTTP notifications from external systems?

**Topic:** Integration · **Difficulty:** Medium · **Tags:** Integration, Webhook Security, HMAC Signatures

Read it on the site: https://www.forcechallenges.com/interview-questions/how-do-webhook-security-mechanisms-verify-the-authenticity-of-incoming-http-notifications-from-external

## Answer

**Basics:** A webhook is an inbound HTTP call. Anyone who knows the URL can post to it, so the receiver must verify the sender. In an HMAC scheme, the sender signs the body with a secret key and sends the result, for example in `X-Signature`. The receiver does the same calculation and compares.

**How it works:**
1. Share a secret out of band and store it securely (Named Credential, protected Custom Metadata, or a secrets manager).
2. The sender computes HMAC-SHA256 of the raw body, often with a timestamp, and sends it in a header.
3. The receiver reads the raw body bytes (not a re-serialised object), recomputes the HMAC, and compares.
4. Reject mismatches with 401 or 403 and do no work.
5. Check the timestamp and a unique delivery ID to stop replays.

**Advanced / deep dive:** In Salesforce, an `@RestResource` class or an Experience Cloud/Site endpoint can receive webhooks. Use `RestContext.request.requestBody` so you sign exactly what was sent. Compare the strings carefully. Apex has no built-in constant-time compare, so keep the compare simple and rate-limit failures (verify whether `Crypto.verifyHmac` is available in your API version for a safer check). Guest user endpoints need minimal permissions, since the guest profile is public. Add IP allowlists if the sender publishes ranges, use HTTPS only, and respond fast with a 2xx while processing asynchronously, for example by publishing a platform event.

**Code / config example:**
```apex
@RestResource(urlMapping='/webhook/v1/*')
global with sharing class WebhookReceiver {
    @HttpPost
    global static void receive() {
        RestRequest req = RestContext.request;
        String sent = req.headers.get('X-Signature');
        Blob mac = Crypto.generateMac('HmacSHA256', req.requestBody, Blob.valueOf(Secrets__mdt.getInstance('Partner').Key__c));
        String expected = EncodingUtil.convertToHex(mac);
        if (sent == null || sent != expected) {
            RestContext.response.statusCode = 401;
            return;
        }
        EventBus.publish(new Inbound_Webhook__e(Body__c = req.requestBody.toString()));
        RestContext.response.statusCode = 202;
    }
}
```

**Hidden gotchas & must-remember points:**
- Sign the raw body. Re-encoded JSON produces a different hash.
- Hex versus Base64 encoding of the signature differs by provider. Match theirs exactly.
- Without a timestamp or nonce check, a captured request can be replayed.
- Never store the secret in a plain custom label or custom setting readable by users.
- Guest user access requires careful object and field permissions.
- Return quickly. Senders retry on slow responses, causing duplicates.

**Best practice:** Verify the HMAC first, enforce timestamp and idempotency checks, then hand off to asynchronous processing.

---
Source: ForceChallenges · last updated 2026-11-02
