Sign up

Verifying the signature

Check that every notice comes from Closeford.

HeaderContains
X-Webhook-EventThe event, such as record.created.
X-Webhook-DeliveryA unique id per delivery, to ignore repeats.
X-Webhook-TimestampWhen it was sent, in Unix seconds.
X-Webhook-Signaturesha256= and the HMAC-SHA256 of timestamp.body with your webhook's secret.
import crypto from "node:crypto";

// raw = the request body as received (string), before JSON.parse
export function verify(raw, headers, secret) {
  const ts = headers["x-webhook-timestamp"];
  const expected = "sha256=" + crypto
    .createHmac("sha256", secret)
    .update(`${ts}.${raw}`)
    .digest("hex");
  const got = headers["x-webhook-signature"] ?? "";
  return got.length === expected.length &&
    crypto.timingSafeEqual(Buffer.from(got), Buffer.from(expected));
}

Compute the signature over the raw body, before parsing it as JSON.