Identify which bytes were actually signed

Your endpoint receives a legitimate webhook but rejects its signature. Logging the parsed JSON appears to show the same values as the provider's example. The missing detail is that a signature usually covers the request's exact byte sequence. Whitespace, property order, Unicode representation, or an added newline can change that sequence without changing the apparent JSON object.

Capture the raw body before a JSON parser consumes it. Do not reconstruct it with JSON.stringify and assume the result is identical. Check that a reverse proxy or middleware has not transformed the body. Compare harmless diagnostic facts such as byte length and content type, rather than putting secrets or full customer payloads into logs.

Verify the provider's signature format explicitly

GitHub's SHA-256 webhook signature uses an sha256= prefix followed by hexadecimal characters. Validate that format before converting it to bytes. A missing header is not the same as an empty signature, and malformed hex should not reach the comparison step. Fetch the secret from private configuration and fail closed when it is absent.

This framework-independent helper accepts a Buffer containing the original body. It is a GitHub-style example, not a universal webhook verifier: other providers may sign a timestamp plus the body, use a different encoding, or require a distinct header. Follow that provider's specification rather than adapting a signature by guesswork.

javascript example
import { createHmac, timingSafeEqual } from 'node:crypto';

export function validSignature(rawBody, header, secret) {
  if (!Buffer.isBuffer(rawBody) ||
      typeof header !== 'string' ||
      typeof secret !== 'string' || secret.length === 0 ||
      !/^sha256=[a-f0-9]{64}$/i.test(header)) {
    return false;
  }
  const expected = createHmac('sha256', secret)
    .update(rawBody).digest();
  const supplied = Buffer.from(header.slice(7), 'hex');
  return supplied.length === expected.length &&
    timingSafeEqual(supplied, expected);
}

Keep parsing and replay protection separate

After the signature succeeds, parse JSON and validate the event shape. The signature establishes that someone with the shared secret produced those bytes; it does not prove that every field is valid for your workflow. Unsupported event types should not accidentally trigger a default action. Bound request size before buffering the body to avoid unnecessary memory pressure.

A valid signature also does not stop a previously valid delivery from being replayed. Persist the provider's delivery identifier and apply idempotency. If the provider signs a timestamp, enforce its documented tolerance as well. Do not invent a timestamp header for providers that do not define one, and do not log the shared secret while investigating mismatches.

Build a useful failure matrix

Generate a test signature using a development secret and a fixed body containing punctuation and non-ASCII text. Verify that the untouched Buffer passes, while changing one byte fails. Test missing, truncated, and non-hex headers; each should be rejected without an unhandled exception.

Run the same tests through the actual HTTP endpoint, not only the helper. This catches middleware order problems that a unit test cannot see. When rotating secrets, coordinate the sender and receiver according to the provider's supported process. Remove temporary body logging after diagnosis and leave only a non-sensitive failure reason.

  1. Test an exact known body and expected signature.
  2. Change a space or newline and confirm rejection.
  3. Send malformed headers and verify a controlled error response.
  4. Replay a valid delivery and verify duplicate handling.

Sources and further reading

Related engineering notes