Every message carries an Unforged-Signature header like t=1795384200,v1=5257a8…. t is when we sent it, in seconds, and v1 is an HMAC-SHA256 of t, a full stop, and the raw body, keyed with your signing secret.
Work it out yourself and compare. Use the body exactly as it arrived, before any JSON parsing, and the whole secret including its whsec_ start. Turn away anything more than five minutes old.
import { createHmac, timingSafeEqual } from 'node:crypto';
/** Whether `header` (the Unforged-Signature header) signs `body`, the raw request body, with your
* endpoint's secret, and was sent within `toleranceSeconds` of now. */
export function verify(
secret: string,
header: string,
body: Buffer,
toleranceSeconds = 300
): boolean {
const parts = header.split(',').map((p) => p.trim().split('='));
const t = parts.find(([k]) => k === 't')?.[1];
if (!t || !/^\d+$/.test(t) || Math.abs(Date.now() / 1000 - Number(t)) > toleranceSeconds)
return false;
const expected = createHmac('sha256', secret)
.update(Buffer.concat([Buffer.from(`${t}.`), body]))
.digest();
return parts
.filter(([k]) => k === 'v1')
.some(([, v]) => {
const got = Buffer.from(v ?? '', 'hex');
// compares in constant time
return got.length === expected.length && timingSafeEqual(got, expected);
});
}Trying it
In Settings, then Integrations, open Webhooks and choose What was sent beside your address. You can send it a test of anything it takes, see what we sent and what your address answered, and send any message again once your code is fixed. A message sent again keeps its id, so your check for ones you’ve already handled still works.
If the secret gets out
Remove the address in Settings, then Integrations, and add it again. It gets a new secret, and the old one stops working at once.