Verificar la firma
Comprueba que cada aviso viene de Closeford.
| Cabecera | Contiene |
|---|---|
X-Webhook-Event | El evento, como record.created. |
X-Webhook-Delivery | Un id único por entrega, para ignorar repeticiones. |
X-Webhook-Timestamp | Cuándo se envió, en segundos Unix. |
X-Webhook-Signature | sha256= y el HMAC-SHA256 de timestamp.body con el secreto de tu webhook. |
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));
}Calcula la firma sobre el cuerpo tal cual llega, antes de convertirlo en JSON.