Verify a webhook
Receive a delivery, check that QuackUI sent it, and ignore duplicates.
Each request carries three headers.
| Header | Holds |
|---|---|
X-QuackUI-Delivery |
An id that stays the same on every retry |
X-QuackUI-Timestamp |
Unix seconds |
X-QuackUI-Signature |
sha256= and an HMAC-SHA256 of timestamp.body |
A receiver
This runs on Node 20 or later with no dependencies.
import { createHmac, timingSafeEqual } from "node:crypto";
import { createServer } from "node:http";
const secret = process.env.QUACKUI_SIGNING_SECRET;
const seen = new Set(); // use your database in real life
createServer(async (req, res) => {
const chunks = [];
for await (const c of req) chunks.push(c);
const raw = Buffer.concat(chunks).toString("utf8");
const timestamp = req.headers["x-quackui-timestamp"] ?? "";
const signature = req.headers["x-quackui-signature"] ?? "";
const expected =
"sha256=" + createHmac("sha256", secret).update(`${timestamp}.${raw}`).digest("hex");
const fresh = Math.abs(Date.now() / 1000 - Number(timestamp)) < 300;
const ok =
fresh &&
signature.length === expected.length &&
timingSafeEqual(Buffer.from(signature), Buffer.from(expected));
if (!ok) return res.writeHead(401).end();
const id = req.headers["x-quackui-delivery"];
if (!seen.has(id)) {
seen.add(id);
const body = JSON.parse(raw);
console.log(body.event, body.report?.name, body.blocks?.length ?? 0, "blocks");
}
res.writeHead(204).end();
}).listen(3000);
Check the signature against the bytes you received. Parsing and re-encoding the JSON changes them.
The body
{
"version": 1,
"event": "report.delivery",
"deliveryId": "...",
"report": { "id": "...", "name": "Stock watch" },
"schedule": { "id": "...", "name": "Low stock check" },
"run": { "id": "...", "finishedAt": "..." },
"link": "https://bi.example.com/...",
"status": "succeeded",
"alert": { "condition": "...", "matchCount": 6, "matches": [] },
"blocks": [{ "id": "low", "title": "...", "columns": [], "rows": [], "incomplete": false }]
}
event is report.delivery, run.failed, run.attention or test. The last three fields are there only when the destination may have data. With row security on, or a field on a protected column, a webhook never does.
Answering
Any 2xx counts as delivered. Answer quickly and do slow work afterwards, because the request times out after 30 seconds. Temporary failures are retried for about two and a half hours with the same delivery id.