Verify signatures
Anyone can POST to a public address. The signature proves a delivery came from CAT Engage, wasn't changed on the way, and isn't an old one being replayed.
The header#
Header
X-Cat-Signature: t=1795775538,v1=5f2c9b1e0d7a…a3e41ctis when the delivery was signed, in Unix seconds. Every attempt is signed afresh, so a retry has a newt.v1is the HMAC-SHA256, in hex, oft, a dot and the raw body, keyed with your endpoint's secret.
Check it in three steps#
Read the raw body
Use the exact bytes you received. Parsing the JSON and turning it back into text changes spacing and breaks the check.
Recompute the signature
HMAC-SHA256 of
${t}.${rawBody}with your secret, as lowercase hex, then compare it withv1in constant time.Reject old deliveries
Refuse it if
tis more than 5 minutes from your clock. Keep your server's clock in sync.
Code#
import crypto from 'node:crypto';
import express from 'express';
const app = express();
const SECRET = process.env.CAT_WEBHOOK_SECRET;
// Keep the raw body: the signature is over the exact bytes we sent.
app.post('/cat-webhooks', express.raw({ type: 'application/json' }), (req, res) => {
const raw = req.body.toString('utf8');
if (!verify(req.get('X-Cat-Signature') ?? '', raw, SECRET)) return res.sendStatus(401);
const event = JSON.parse(raw);
res.sendStatus(200); // answer first, then do the work
handle(event);
});
function verify(header, raw, secret, toleranceSeconds = 300) {
const parts = Object.fromEntries(
header.split(',').map((kv) => {
const i = kv.indexOf('=');
return [kv.slice(0, i).trim(), kv.slice(i + 1).trim()];
}),
);
const t = Number(parts.t);
if (!Number.isFinite(t) || !parts.v1) return false;
if (Math.abs(Date.now() / 1000 - t) > toleranceSeconds) return false; // too old: replay
const expected = crypto.createHmac('sha256', secret).update(`${t}.${raw}`).digest('hex');
const a = Buffer.from(expected);
const b = Buffer.from(parts.v1.toLowerCase());
return a.length === b.length && crypto.timingSafeEqual(a, b);
}Your secret#
Each endpoint has its own secret. It is shown once: when you add the endpoint in Integrations → Webhooks, or in the secret field when you create it through the API. We keep it encrypted and never show it again.
Lost the secret?
Delete the endpoint and add it again to get a new secret, then update your server.