Verify a webhook signature
developers-webhook-signaturesOn this page
Check the X-Skeduler-Signature-V2 header on every delivery with your signing secret, with examples for Node and PHP.
Anyone can post to your URL. Only Skeduler can sign with your secret, so check the signature on every delivery before you trust it.
Who can do this: Owners and Admins with the Manage Developer Tools permission.
The steps
Read the raw request body, exactly as received. Don't parse it and turn it back into JSON first: the signature covers the exact bytes.
Split the
X-Skeduler-Signature-V2header (t=<unix seconds>,v1=<hex>) intotandv1.Reject the delivery if
tis more than 5 minutes from your clock. This stops an old delivery being replayed.Work out
HMAC-SHA256(secret, "<t>.<raw body>")as lowercase hex, and compare it tov1with a constant-time comparison.
If they match, the delivery is genuine: handle it and answer 2xx. If not, reject it. The examples below answer 400.
Node (Express)
import crypto from "node:crypto";
import express from "express";
const SECRET = process.env.SKEDULER_SIGNING_SECRET;
const app = express();
// express.raw keeps the body as the exact bytes; don't use express.json() here.
app.post("/skeduler/webhook", express.raw({ type: "application/json" }), (req, res) => {
const header = req.get("X-Skeduler-Signature-V2") ?? "";
const parts = Object.fromEntries(header.split(",").map((p) => p.split("=", 2)));
const t = Number(parts.t);
if (!parts.v1 || !Number.isInteger(t) || Math.abs(Date.now() / 1000 - t) > 300) {
return res.sendStatus(400);
}
const expected = crypto.createHmac("sha256", SECRET).update(`${t}.`).update(req.body).digest("hex");
const given = Buffer.from(parts.v1, "hex");
const wanted = Buffer.from(expected, "hex");
if (given.length !== wanted.length || !crypto.timingSafeEqual(given, wanted)) {
return res.sendStatus(400);
}
const delivery = JSON.parse(req.body.toString("utf8"));
// Handle delivery.event; use delivery.delivery_id to ignore a repeat.
res.sendStatus(200);
});
PHP
<?php
$secret = getenv('SKEDULER_SIGNING_SECRET');
$raw = file_get_contents('php://input'); // the exact bytes
$header = $_SERVER['HTTP_X_SKEDULER_SIGNATURE_V2'] ?? '';
parse_str(str_replace(',', '&', $header), $parts); // t=…,v1=… -> ['t' => …, 'v1' => …]
$t = $parts['t'] ?? '';
$given = $parts['v1'] ?? '';
if (! ctype_digit((string) $t) || $given === '' || abs(time() - (int) $t) > 300) {
http_response_code(400);
exit;
}
$expected = hash_hmac('sha256', $t.'.'.$raw, $secret);
if (! hash_equals($expected, $given)) {
http_response_code(400);
exit;
}
$delivery = json_decode($raw, true);
// Handle $delivery['event']; use $delivery['delivery_id'] to ignore a repeat.
http_response_code(200);
An older header, X-Skeduler-Signature (sha256=<hex HMAC of the raw body>), is still sent but has no timestamp. Use X-Skeduler-Signature-V2.
Related
In this section
- How webhooks work: subscriptions, events and deliveries
- Subscribe to webhook events
- Webhook events and their payloads
- Webhook recipes: subscribe, verify and handle deliveries
- Webhook retries, duplicates and switch-off
- I'm not receiving webhook deliveries
- My webhook signature doesn't match
- My webhook subscription was disabled
- Does an integration get webhooks for its own API writes?