Verifying the signature
Check that every notice comes from Closeford.
| Header | Contains |
|---|---|
X-Webhook-Event | The event, such as record.created. |
X-Webhook-Delivery | A unique id per delivery, to ignore repeats. |
X-Webhook-Timestamp | When it was sent, in Unix seconds. |
X-Webhook-Signature | sha256= 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.