opyeb hmaclab

hmaclab · GitHub

Verifying GitHub webhooks with X-Hub-Signature-256

sha256= prefix, legacy sha1=, preserving the raw body

When a secret is set on a repository webhook, GitHub signs every delivery. The current standard header is X-Hub-Signature-256, whose value is sha256= followed by 64 lowercase hexadecimal characters.

X-Hub-Signature-256: sha256=3f2a9c... (64 hex characters)

Verification steps

  1. Read the request body exactly as received. If your framework parsed JSON automatically, check whether the raw body is still available.
  2. Use the secret exactly as entered in GitHub, without stray spaces or newlines.
  3. Compute HMAC-SHA256 over the body with the secret.
  4. Strip the sha256= prefix and compare with the computed value in constant time.

Handling the prefix

Comparing the whole header always fails because of the prefix. Either strip it and compare the hexadecimal, or prepend the same prefix to your computed value. Normalise case either way: GitHub sends lowercase hex.

Header names are case-insensitive, but normalising the prefix and hex case before comparing avoids needless mismatches.

The legacy X-Hub-Signature

Older integrations receive X-Hub-Signature with a sha1= prefix and 40 hex characters. GitHub recommends SHA-256 for new webhooks, and when both headers arrive, prefer SHA-256. Choosing SHA-1 in hmaclab shows this legacy header format.

Common mistakes

Response codes

Reject a bad signature with 401 and do not process the body. If the signature is valid but processing fails, return 5xx so GitHub retries. Retries redeliver the same event, so deduplicate by event or delivery ID.

Questions

Which header wins when both arrive?

Verify X-Hub-Signature-256 first and fall back to the legacy X-Hub-Signature only when it is absent.

Why not re-serialise the body after parsing?

JSON serialisation can change whitespace and key order, so the bytes differ from what GitHub signed and the signature fails.

What happens when the secret changes?

New deliveries are signed with the new secret. During a rollout, consider accepting the previous secret for a short window.

Build and verify a signature in the exact GitHub format.

Open the tool

This guide follows the HMAC definition in RFC 2104 and the public webhook documentation of GitHub, Slack and Stripe. Every example value is synthetic.