Webhook Security · X-Hub-Signature-256

GitHub (X-Hub-Signature-256) & Slack (v0=) HMAC-SHA256 Verification Guide

Verify GitHub X-Hub-Signature-256 and Slack v0= webhook signatures using constant-time crypto.timingSafeEqual, raw request bytes, and timestamp replay protection.

Zero Telemetry · Strict CSP (connect-src 'none')

Webhook HMAC-SHA256 Signer & Verifier

Pure Browser crypto.subtle
1-Click Test Vectors & Edge Cases
Paste a received signature header to run constant-time verification.
Computed HTTP Signature Header
X-Hub-Signature-256: sha256=27f0899fbb5bd92a2c859f64210d514cb73c5c468036e5b10ca02e14974bbad7

Signed input: ${rawBody} · 86 UTF-8 bytes · HMAC-SHA256 hex digest of raw UTF-8 body prefixed with sha256=.

HMAC-SHA256 Hex Digest
27f0899fbb5bd92a2c859f64210d514cb73c5c468036e5b10ca02e14974bbad7
HMAC-SHA256 Base64 Digest (Shopify format)
J/CJn7tb2SoshZ9kIQ1RTLc8XEaANuWxDKAuFJdLutc=
Ready-to-Run curl Test Vector
curl -X POST https://api.example.com/webhooks \
  -H "Content-Type: application/json" \
  -H "X-Hub-Signature-256: sha256=27f0899fbb5bd92a2c859f64210d514cb73c5c468036e5b10ca02e14974bbad7" \
  -d '{"ref":"refs/heads/main","repository":{"full_name":"acme/api"},"after":"a1b2c3d4e5f6"}'
Constant-Time Node.js Verification Snippet
import crypto from "node:crypto";

export function verifyWebhook(rawBodyBytes, headers, secret) {
  const sig = headers["x-hub-signature-256"] || "";
  const expected = "sha256=" + crypto.createHmac("sha256", secret).update(rawBodyBytes).digest("hex");
  return sig.length === expected.length && crypto.timingSafeEqual(Buffer.from(sig), Buffer.from(expected));
}
Live API & Edge Audit Zero-Upload SpecDiff

Harden your live domain headers, OpenAPI schemas & AEO discovery

Test your deployed domain's security headers, HSTS, CSP & DMARC live on ReadyCheck, diff OpenAPI contracts & OWASP API Top 10 risks in SpecDiff, or deploy all 9 production configs in minutes with the $49 ReadyCheck Fix Pack.

Protocol Rules & Header Reference

Rule / Header Production Value Behavior & Gotchas
GitHub Header X-Hub-Signature-256: sha256=<hex> Sign raw UTF-8 payload bytes directly; ignore deprecated SHA-1 X-Hub-Signature
Slack Header X-Slack-Signature: v0=<hex> Sign `v0:${X-Slack-Request-Timestamp}:${rawBody}` and reject >300s clock skew
Shopify Header X-Shopify-Hmac-Sha256: <base64> Uses Base64 digest encoding (not hex!) over raw HTTP body
Comparison Function crypto.timingSafeEqual / hmac.compare_digest Never use === / == on signatures (prevents timing side-channel leaks)

Express Raw-Body GitHub X-Hub-Signature-256 Verifier

verify-github-webhook.mjs
import crypto from "node:crypto";
import express from "express";

const app = express();

// Mount express.raw() BEFORE express.json() on webhook paths!
app.post("/webhooks/github", express.raw({ type: "application/json" }), (req, res) => {
  const sig = req.get("X-Hub-Signature-256") || "";
  const secret = process.env.GITHUB_WEBHOOK_SECRET || "";
  const expected = "sha256=" + crypto.createHmac("sha256", secret).update(req.body).digest("hex");

  const sigBuf = Buffer.from(sig, "utf8");
  const expBuf = Buffer.from(expected, "utf8");
  if (sigBuf.length !== expBuf.length || !crypto.timingSafeEqual(sigBuf, expBuf)) {
    return res.status(401).send("Invalid X-Hub-Signature-256");
  }
  const payload = JSON.parse(req.body.toString("utf8"));
  return res.status(200).json({ ok: true, ref: payload.ref });
});

Slack v0= Signature + 5-Minute Replay Guard (Python)

verify_slack_webhook.py
import hmac, hashlib, time

def verify_slack_request(raw_body: bytes, timestamp: str, signature: str, signing_secret: str) -> bool:
    if not timestamp.isdigit() or abs(time.time() - int(timestamp)) > 300:
        return False  # Replay attack protection (>5 min old)
    basestring = f"v0:{timestamp}:".encode("utf-8") + raw_body
    expected = "v0=" + hmac.new(signing_secret.encode("utf-8"), basestring, hashlib.sha256).hexdigest()
    return hmac.compare_digest(expected, signature)

Key Engineering Takeaways

  • The #1 cause of broken webhook verification is parsing JSON first (app.use(express.json())) and calling JSON.stringify(req.body), which alters whitespace, Unicode escaping, and numeric formatting.
  • Always verify bufA.length === bufB.length before calling crypto.timingSafeEqual(bufA, bufB) in Node.js — mismatched buffer lengths throw a RangeError exception that crashes unguarded handlers.
  • For Slack (and Stripe), validate that the signed timestamp is within +-300 seconds of current UTC time to block captured replay requests.

Common Production Pitfalls to Avoid

  • Verifying GitHub's legacy SHA-1 X-Hub-Signature instead of SHA-256 X-Hub-Signature-256.
  • Using hex encoding for Shopify's X-Shopify-Hmac-Sha256 (Shopify sends Base64!).
  • Running crypto.timingSafeEqual on raw strings without matching length guards or using non-constant-time === comparisons.

More CORS & Webhook Signature Guides

← Back to CORS & Webhook HMAC Debugger Hub · OpenAPI OWASP API Top 10 & MCP Scorer → · SBOM & Sigstore Validator → · HTTP Security Headers & CSP →

Frequently Asked Questions

Does this debugger handle empty payloads and multi-byte UTF-8 characters?

Yes — switch to the Webhook HMAC-SHA256 tab above and test the 1-click Empty Body or UTF-8 Unicode presets. Web Crypto crypto.subtle encodes exact UTF-8 bytes identically to Node.js and Python.

Why does crypto.timingSafeEqual throw ERR_CRYPTO_TIMING_SAFE_EQUAL_LENGTH?

Node's timingSafeEqual requires both Buffers to share identical byte lengths. Always check a.length === b.length first (or hash/pad safely) before calling timingSafeEqual.

How can I audit my OpenAPI webhook definitions too?

Paste your OpenAPI spec into SpecDiff (https://spec.runglab.dev) to audit webhook schemas, OWASP API Top 10 issues, and token costs.