Get set up
Verify signatures
Check the X-Gray-Signature header with HMAC-SHA256 to prove a message came from Gray3.
Overview
Every message, including test events, carries an X-Gray-Signature header:
http
X-Gray-Signature: t=1792143000,v1=5f2b0c…e81atis the time Gray3 signed the message, in Unix seconds.v1is a hex HMAC-SHA256 of<t>.<raw body>, using your full webhook key (including thegray_whk_prefix) as the secret.
Steps
- Read
tandv1from the header. - Reject the message if
tis more than 5 minutes from your clock. This blocks replayed messages. - Compute the HMAC of the timestamp, a dot, and the raw body bytes.
- Compare it to
v1with a constant-time comparison.
Code
verify
import crypto from "node:crypto";
const TOLERANCE_SECONDS = 5 * 60;
export function verifyGraySignature(rawBody, header, webhookKey) {
if (!header) return false;
const parts = Object.fromEntries(
header.split(",").map((part) => {
const i = part.indexOf("=");
return [part.slice(0, i), part.slice(i + 1)];
}),
);
const timestamp = Number(parts.t);
if (!Number.isInteger(timestamp) || !parts.v1) return false;
if (Math.abs(Date.now() / 1000 - timestamp) > TOLERANCE_SECONDS) return false;
const expected = crypto
.createHmac("sha256", webhookKey)
.update(`${timestamp}.${rawBody}`)
.digest();
const received = Buffer.from(parts.v1, "hex");
return received.length === expected.length && crypto.timingSafeEqual(received, expected);
}Retries are signed again
Each attempt gets a fresh t and signature, so a retry hours later still passes the 5-minute check. If you rotate the key, retries are signed with the new key.