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=ecd8e50937742631f946de3a289cd5d3c8d3944a402dfc90d6dbbdb22991e75atis when Sabiwave signed the delivery, in Unix seconds.v1is 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.
Checking it
Section titled “Checking it”- 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.
- Split the header on commas, then each part on the first
=, to gettandv1. - Compute the HMAC-SHA256 of
{t}.{raw body}with your signing secret. - Compare it with
v1in constant time. - Refuse it if
tis 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.
Samples
Section titled “Samples”Each sample is tested against real Sabiwave deliveries, and against tampered, stale and badly formed ones.
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:
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);<?php
/** * True when $rawBody (the request body exactly as it arrived) was signed by Sabiwave with your * endpoint's signing secret in the last 5 minutes. */function verify_sabiwave_signature(string $rawBody, ?string $header, string $secret, int $toleranceSeconds = 300): bool{ $parts = []; foreach (explode(',', (string) $header) as $item) { $pair = explode('=', trim($item), 2); if (count($pair) === 2) { $parts[$pair[0]] = $pair[1]; } } if (!isset($parts['t'], $parts['v1']) || !preg_match('/^\d+$/', $parts['t'])) { return false; } if (abs(time() - (int) $parts['t']) > $toleranceSeconds) { return false; } $expected = hash_hmac('sha256', $parts['t'] . '.' . $rawBody, $secret); return hash_equals($expected, $parts['v1']);}<?php// Your webhook route, e.g. https://yourshop.com/webhooks/sabiwaverequire __DIR__ . '/verify-signature.php';
$rawBody = file_get_contents('php://input');$header = $_SERVER['HTTP_X_SABIWAVE_SIGNATURE'] ?? null;if (!verify_sabiwave_signature($rawBody, $header, getenv('SABIWAVE_WEBHOOK_SECRET'))) { http_response_code(400); exit('Signature check failed');}
$event = json_decode($rawBody, true);save_event($event); // your code: store or queue it, keyed by $event['id']http_response_code(200); // within 10 seconds, or Sabiwave counts the delivery as failedimport hashlibimport hmacimport time
def verify_sabiwave_signature(raw_body: bytes, header: str | None, secret: str, tolerance_seconds: int = 300) -> bool: """True when raw_body (the request body exactly as it arrived, as bytes) was signed by Sabiwave with your endpoint's signing secret in the last 5 minutes.""" parts = {} for item in (header or "").split(","): key, sep, value = item.strip().partition("=") if sep: parts[key] = value timestamp, given = parts.get("t", ""), parts.get("v1", "") if not (timestamp.isascii() and timestamp.isdigit()) or not given: return False if abs(time.time() - int(timestamp)) > tolerance_seconds: return False signed = timestamp.encode() + b"." + raw_body expected = hmac.new(secret.encode(), signed, hashlib.sha256).hexdigest() return hmac.compare_digest(expected, given)Pass the body as bytes: request.get_data() in Flask, request.body in Django, await request.body() in FastAPI. The header is request.headers.get("X-Sabiwave-Signature").
Replacing the signing secret
Section titled “Replacing the signing secret”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.
Building on your own computer
Section titled “Building on your own computer”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.