Skip to content

Check signatures

Anyone can send a POST to your endpoint. The signature proves a delivery came from Sabiwave, unchanged, and recently. Check it on every delivery before you trust a word of the body.

X-Sabiwave-Signature: t=1795921193,v1=ecd8e50937742631f946de3a289cd5d3c8d3944a402dfc90d6dbbdb22991e75a
  • t is when Sabiwave signed the delivery, in Unix seconds.
  • v1 is an HMAC-SHA256, in lower-case hex, of {t}.{raw body}: the timestamp, a full stop, then the request body exactly as it arrived. The key is your endpoint’s signing secret, whsec_…, whole.
  1. Read the raw body before anything parses it. Parsed and re-serialised JSON won’t have the same bytes, and the signature won’t match.
  2. Split the header on commas, then each part on the first =, to get t and v1.
  3. Compute the HMAC-SHA256 of {t}.{raw body} with your signing secret.
  4. Compare it with v1 in constant time.
  5. Refuse it if t is more than 5 minutes from your server’s clock. This is what makes a captured delivery useless to replay later: the timestamp is inside what’s signed, so it can’t be changed.

If any step fails, answer 400 and do nothing else.

Each sample is tested against real Sabiwave deliveries, and against tampered, stale and badly formed ones.

verify-signature.js
import { createHmac, timingSafeEqual } from 'node:crypto';
const TOLERANCE_SECONDS = 300; // refuse anything signed more than 5 minutes away from now
/**
* True when `rawBody` (the request body exactly as it arrived, a Buffer or string, before any
* JSON parsing) was signed by Sabiwave with your endpoint's signing secret in the last 5 minutes.
*/
export function verifySabiwaveSignature(rawBody, signatureHeader, secret) {
const parts = {};
for (const item of String(signatureHeader ?? '').split(',')) {
const [key, value] = item.trim().split('=');
if (key && value) parts[key] = value;
}
const timestamp = Number(parts.t);
if (!Number.isInteger(timestamp) || !parts.v1) return false;
if (Math.abs(Date.now() / 1000 - timestamp) > TOLERANCE_SECONDS) return false;
const expected = createHmac('sha256', secret)
.update(`${timestamp}.`)
.update(rawBody)
.digest('hex');
const given = Buffer.from(parts.v1);
const wanted = Buffer.from(expected);
return given.length === wanted.length && timingSafeEqual(given, wanted);
}

With Express, read the webhook route’s body raw:

server.js
import express from 'express';
import { verifySabiwaveSignature } from './verify-signature.js';
const app = express();
// The signature covers the exact bytes Sabiwave sent, so this route reads its body raw.
// Register it before any app-wide express.json().
app.post('/webhooks/sabiwave', express.raw({ type: 'application/json' }), async (req, res) => {
const signed = verifySabiwaveSignature(
req.body, // a Buffer, because of express.raw()
req.get('X-Sabiwave-Signature'),
process.env.SABIWAVE_WEBHOOK_SECRET, // whsec_…, shown when you added the endpoint
);
if (!signed) return res.status(400).send('Signature check failed');
const event = JSON.parse(req.body.toString('utf8'));
await saveEvent(event); // your code: store or queue it, keyed by event.id
res.sendStatus(200); // within 10 seconds, or Sabiwave counts the delivery as failed
});
app.listen(process.env.PORT ?? 3000);

Open the endpoint in Settings › Developer › Webhooks and press New secret. The new secret is shown once and signs every delivery from that moment, with no overlap, so put it on your server straight away. Deliveries your server refused in between are in the Delivery log: press Send again on each once the new secret is in place.

Sabiwave only sends webhooks to public https:// addresses, so it can’t reach localhost. While you build, expose your machine through a tunnelling service and add the tunnel’s https:// address as an endpoint: a test endpoint receives the test pings you send, signed like any other delivery, and a live one your business’s events too.