Blog

Webhook Signature Verification: Authenticate The Bytes Before JSON

the parsed object is already too late
A sealed webhook payload passing HMAC verification before reaching a JSON parser

If you parse and reserialize JSON before checking its signature, you may verify different bytes from those the sender signed. Whitespace, key order, and encoding are enough to break the comparison.

Authenticate raw body first.

Compute The Expected MAC

Provider contracts differ. GitHub, for example, documents an HMAC-SHA256 signature in X-Hub-Signature-256 over the payload contents. Its validation guide also recommends a timing-safe comparison.

Mac mac = Mac.getInstance("HmacSHA256");
mac.init(new SecretKeySpec(secret, "HmacSHA256"));
byte[] expected = mac.doFinal(rawBody);

if (!MessageDigest.isEqual(expected, suppliedDigest)) {
    throw new InvalidWebhookSignature();
}

Parse algorithm prefixes and hex encoding exactly as provider specifies. Never log the secret or full signature on failure.

Authentic Does Not Mean New

A captured valid request can be replayed. Where provider sends a timestamp, enforce a narrow age window and include that timestamp in the signed material as specified. Store a delivery ID under a unique constraint so duplicate deliveries become no-ops.

Rotate Without A Blind Window

During rotation, accept the current and previous secret for a short controlled window, then remove the old one. Metrics should identify which key version verified without revealing key data.

Read body once, verify, parse, claim delivery identity, then perform idempotent work. Every step has a different security or reliability purpose.