Skip to content
Docs

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…e81a
  • t is the time Gray3 signed the message, in Unix seconds.
  • v1 is a hex HMAC-SHA256 of <t>.<raw body>, using your full webhook key (including the gray_whk_ prefix) as the secret.

Steps

  1. Read t and v1 from the header.
  2. Reject the message if t is more than 5 minutes from your clock. This blocks replayed messages.
  3. Compute the HMAC of the timestamp, a dot, and the raw body bytes.
  4. Compare it to v1 with 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.