Skip to content
HooktierDocs

GitHub webhook signature does not match (X-Hub-Signature-256)

Your handler rejects a GitHub delivery because its computed HMAC differs from X-Hub-Signature-256. GitHub’s Ruby example reports:

Signatures didn't match!

Verification depends on three inputs: the raw request body, the complete signature header and the secret configured for that webhook. Check them in that order.

X-Hub-Signature-256 contains sha256= followed by a hex HMAC-SHA256 of the request body, keyed with the webhook secret. GitHub sends this header only when a secret is configured. The older X-Hub-Signature uses HMAC-SHA1 for legacy compatibility; GitHub recommends X-Hub-Signature-256 instead. Do not fall back to SHA-1 when SHA-256 verification fails.

Before changing your live configuration, pass these exact values through your verifier:

Secret: It's a Secret to Everybody
Body: Hello, World!
X-Hub-Signature-256: sha256=757107ea0eb2509fc211221cce984b8a37570b6d7586c22c46f4379c8b043e17

These are GitHub’s documented test values, not a production secret. The body has no trailing newline. Each verifier below passes this case. If yours fails, fix the algorithm or header handling before investigating a real delivery.

1. A parser or JSON encoder changed the body

Section titled “1. A parser or JSON encoder changed the body”

GitHub signs the request body. Parsing and re-encoding JSON can change whitespace or Unicode escapes without changing the JSON values. The tested json-reencoded and unicode-escaped cases fail verification, although their decoded JSON values stay the same.

Keep the original bytes until verification finishes. Do not hash JSON.stringify(req.body) or a parsed object. In Express, register the route with express.raw() before any global express.json() or form parser. The route below tests that order.

2. The payload was decoded with the wrong encoding

Section titled “2. The payload was decoded with the wrong encoding”

GitHub says to handle payloads as UTF-8 because they can contain Unicode characters. Prefer a Buffer or byte array for the manual verifier. If an API requires a string, decode the original bytes as UTF-8 once. Do not change line endings, normalize Unicode or add a newline. The non-ASCII test body contains 世界 and 🌍; all samples verify its original bytes.

GitHub’s signature always starts with sha256=. Compare the complete header with a complete expected signature, or remove exactly that prefix and compare digest bytes. Do not compare a bare hex digest with the complete header. Do not decode the digest as base64.

Read header names case-insensitively. The Express sample uses req.get("X-Hub-Signature-256"); its mixed-case and lowercase header tests both pass. Reject a missing header or a malformed value before decoding hex. If the header is absent, also check whether a secret is configured.

These samples reject uppercase and mixed-case hex. GitHub’s documentation does not authorize those variants; the pinned Octokit helper compares the header with its generated lowercase hex. They also reject SHA256=, a correctly sized non-hex digest and an oversized digest.

GitHub’s troubleshooting instructions say to use the correct webhook secret and update it if its value is unknown. Repository and organization webhook settings have a Secret field; a GitHub App has a Webhook secret field.

Check the configuration of the webhook that made this request, rather than a different repository, organization or GitHub App. After changing that secret, update the receiving process to the same value. Inspect secret loading for an unintended trailing newline from an environment file. The secret-newline test fails with the unchanged signature.

Do not automatically trim a secret: spaces can be intentional key bytes. Fix the stored value instead. Reject an empty or missing secret; the tests include a forged signature computed with an empty key, which every verifier rejects. Never log the secret or the body.

GitHub’s Content type setting has two formats. application/json sends JSON directly in the POST body. application/x-www-form-urlencoded sends JSON in a form parameter named payload.

If your later code expects JSON directly, select application/json in the webhook settings. If you keep the form format, verify the entire raw form body before decoding its payload field. The form vector passes; using its decoded JSON with the same signature fails. The Express route accepts both content types and verifies their raw bytes.

6. A proxy changed the body or dropped the header

Section titled “6. A proxy changed the body or dropped the header”

GitHub warns that a proxy or load balancer must not modify the payload or headers before verification. Locate the suspected layer with measurements from one test request:

  1. Record the body length and SHA-256 immediately before and after that layer.
  2. Compare those measurements for the same request, using its request identifier.
  3. If they differ, configure that layer to preserve the original bytes.
  4. Check that the complete X-Hub-Signature-256 value reaches the verifier unchanged.

Do not compare a live delivery with a separately generated sample; they are different requests. Record measurements rather than bodies or secrets.

A plain == can produce the correct verdict, but GitHub warns against it because of timing attacks. Use a constant-time comparison for the digest: timingSafeEqual in Node, hmac.Equal in Go, or hmac.compare_digest in Python. Validate the header shape first so malformed input cannot trigger a length error.

These manual verifiers accept raw body bytes and the full sha256=<hex> header. They return false for missing or malformed inputs. Their adapters report a missing header separately as missing. No sample logs the body or secret.

Save this as verify-github.mjs:

verify-github.mjs
import { createHmac, timingSafeEqual } from "node:crypto";
export function verifyGitHub(rawBody, header, secret) {
// An empty key would let anyone forge a matching signature.
if (typeof secret !== "string" || secret.length === 0) return false;
if (!Buffer.isBuffer(rawBody)) return false;
if (typeof header !== "string" || header.length !== 71 || !/^sha256=[0-9a-f]{64}$/.test(header)) return false;
const expected = createHmac("sha256", secret).update(rawBody).digest();
const given = Buffer.from(header.slice("sha256=".length), "hex");
return timingSafeEqual(given, expected);
}
verify.go
package main
import (
"crypto/hmac"
"crypto/sha256"
"encoding/hex"
"strings"
)
func verifyGitHub(body []byte, header, secret string) bool {
// An empty key would let anyone forge a matching signature.
if secret == "" {
return false
}
digest, ok := strings.CutPrefix(header, "sha256=")
if !ok || len(digest) != 64 || strings.Trim(digest, "0123456789abcdef") != "" {
return false
}
given, err := hex.DecodeString(digest)
if err != nil {
return false
}
mac := hmac.New(sha256.New, []byte(secret))
_, _ = mac.Write(body)
return hmac.Equal(given, mac.Sum(nil))
}

Read the request body as bytes and obtain the header with r.Header.Get("X-Hub-Signature-256") before calling this function.

verify_github.py
import hashlib
import hmac
import re
def verify_github(raw_body, header, secret):
# An empty key would let anyone forge a matching signature.
if not isinstance(secret, str) or not secret:
return False
if not isinstance(raw_body, bytes):
return False
if not isinstance(header, str) or not re.fullmatch(r"sha256=[0-9a-f]{64}", header):
return False
expected = hmac.new(secret.encode("utf-8"), raw_body, hashlib.sha256).digest()
given = bytes.fromhex(header[len("sha256="):])
return hmac.compare_digest(given, expected)

Pass the original request body as bytes, not a decoded JSON object.

Save this beside verify-github.mjs. Supply the webhook secret from your server’s secure configuration when calling createGitHubApp.

express-github.mjs
import express from "express";
import { verifyGitHub } from "./verify-github.mjs";
export function createGitHubApp(secret) {
const app = express();
// Verify before any JSON or form parser can consume the body.
app.post("/webhooks/github", express.raw({
type: ["application/json", "application/x-www-form-urlencoded"],
inflate: false,
limit: 25_000_000,
}), (req, res) => {
if (!verifyGitHub(req.body, req.get("X-Hub-Signature-256"), secret)) {
return res.status(401).send("Invalid signature");
}
return res.status(200).send("Signature verified");
});
app.use(express.json());
return app;
}

This is a verification probe: it only verifies and answers, without accepting or processing an event. It chooses 401 for authentication failure, as GitHub’s TypeScript example does; GitHub’s Python example uses 403, so 401 is a sample choice, not a provider mandate. This sample sets an application body limit of 25,000,000 bytes. Larger requests return 413 before verification. GitHub documents a 25 MB payload cap; it does not deliver events whose payload exceeds that cap. A valid signature on a supported, uncompressed request within the application limit returns 200 with Signature verified. inflate: false rejects compressed requests with 415 before verification. The gzip regression test proves compressed-request rejection.

For an event receiver, durably store or enqueue the verified event before answering 2xx, then process it asynchronously. See the existing GitHub delivery failures guide for timeout and delivery recovery troubleshooting. Keep deduplication in any redelivery flow.

@octokit/webhooks-methods 6.0.0 exports verify(secret, eventPayload, signature) and takes the payload as a string. It throws for missing inputs or a non-string payload and compares signatures with timingSafeEqual. This wrapper converts a UTF-8 Buffer without losing bytes and turns rejected inputs into false:

octokit-verify.mjs
import { verify } from "@octokit/webhooks-methods";
export async function verifyGitHubWithOctokit(rawBody, header, secret) {
if (typeof secret !== "string" || secret.length === 0) return false;
if (!Buffer.isBuffer(rawBody)) return false;
if (typeof header !== "string" || header.length !== 71 || !/^sha256=[0-9a-f]{64}$/.test(header)) return false;
const payload = rawBody.toString("utf8");
// The library takes a string; reject a lossy UTF-8 conversion.
if (!Buffer.from(payload, "utf8").equals(rawBody)) return false;
try {
return await verify(secret, payload, header);
} catch {
return false;
}
}

Call this wrapper with the same raw Buffer, header and secret as the manual Node verifier. It verifies the same signature vectors, including the documented example and non-ASCII body.

  1. GitHub’s documented Hello, World! example passes your verifier.
  2. Verification uses the original body bytes before any parser or JSON encoder.
  3. Any text conversion uses UTF-8 and preserves the bytes.
  4. The header is the complete X-Hub-Signature-256 value, including sha256=.
  5. The secret belongs to this webhook and matches the value loaded by the receiving process.
  6. Form payloads are verified before decoding the payload field.
  7. A proxy preserves the body and header for the same request.
  8. Empty secrets and malformed headers fail, and the digest comparison uses constant time.

Check a test body, header and secret with the webhook signature checker. Select GitHub.

Checked on 2026-11-08.