Check a webhook delivery's signature
Every webhook delivery is signed with HMAC-SHA256 under the signing secret you gave when you added the endpoint. Your endpoint should check the signature, and the time inside it, before it trusts the event.
What a delivery looks like
Valystrum sends a POST with a JSON body and these headers:
| Header | Value |
|---|---|
Content-Type | application/json |
X-Valystrum-Event | The event type, for example finding.created |
X-Valystrum-Attempt | 1 for the first try, up to 5 |
X-Valystrum-Signature-256 | t=<unix seconds>,v1=<hex signature> |
The signature is the hex HMAC-SHA256, under your signing secret, of the timestamp, a full stop, and the raw body: <t>.<body>. Each attempt is signed afresh, so a retry carries a new timestamp.
Check it
- Read the raw request body as bytes, before any JSON parsing changes it.
- Split
X-Valystrum-Signature-256intotandv1. - Refuse the delivery if
tis more than five minutes away from your clock. - Compute HMAC-SHA256 of
t + "." + bodywith your signing secret and compare it withv1in constant time. - Answer with a
2xxstatus only when both checks pass.
A Node.js example:
import { createHmac, timingSafeEqual } from "node:crypto";
export function verify(rawBody, header, secret, now = Date.now()) {
const parts = Object.fromEntries(header.split(",").map((p) => p.split("=")));
const t = Number(parts.t);
if (!Number.isFinite(t) || Math.abs(now / 1000 - t) > 300) return false;
const expected = createHmac("sha256", secret).update(`${t}.${rawBody}`, "utf8").digest("hex");
const a = Buffer.from(expected, "hex");
const b = Buffer.from(String(parts.v1 || ""), "hex");
return a.length === b.length && timingSafeEqual(a, b);
}