# Webhook security

> Verify `X-Vitral-WhatsApp-Signature` against the raw body.

| Header | Value |
| --- | --- |
| `Content-Type` | `application/json` |
| `X-Vitral-WhatsApp-Signature` | Hex HMAC-SHA256 of `JSON.stringify(payload)`, using the tenant webhook secret. Empty string when no secret is stored. |
| `X-Vitral-WhatsApp-Event` | Same string as `event`. |
| `X-Vitral-WhatsApp-Delivery-Id` | `WebhookDelivery` id. Absent on the synchronous test POST. |

There is no `sha256=` prefix. Meta’s `X-Hub-Signature-256` uses that prefix. This header does not.

Meta’s own callback, `POST /api/v1/webhooks/meta`, is separate. A valid `X-Hub-Signature-256` is stored, then `200` `EVENT_RECEIVED`. If it cannot be stored the reply is `503` and Meta retries. A missing or invalid signature is not stored. `true` returns `401`. The log includes `looksLikeMeta`, `metaObject`, `userAgent`, `reason`, and `bodyBytes`. It does not include the raw body or the signature.

Rotating a tenant webhook secret does not happen on deploy. The previous secret stops verifying on the next delivery. There is no grace period. Update that env to the secret returned by the rotate call in the same step, or WAsndr rejects deliveries. Do not rotate the tenant WAsndr verifies as part of deploying this change.

The signer and the HTTP client stringify the same object. Verify the raw request body. Parsing and re-serializing can change key order and fail the HMAC.

```javascript
import { createHmac, timingSafeEqual } from "node:crypto";

export function verifyWhatsAppSignature(rawBody, signatureHeader, secret) {
  if (!secret || !signatureHeader) return false;
  const expected = createHmac("sha256", secret).update(rawBody).digest("hex");
  const a = Buffer.from(expected);
  const b = Buffer.from(signatureHeader);
  if (a.length !== b.length) return false;
  return timingSafeEqual(a, b);
}
```

Respond `2xx` after you persist the event. Anything else, including a timeout, is a failure. The client waits 10 seconds.
