Anyone who learns your URL can send requests to it. Verify every request before you act on it.
Headers#
| Header | Value |
|---|---|
X-JEMERO-Webhook-Timestamp | When this attempt was sent: Unix time in milliseconds. |
X-JEMERO-Webhook-Signature | HMAC-SHA256, as 64 hexadecimal characters. |
Content-Type | application/json |
User-Agent | Jemero-Webhooks/1.0 (anyone can send this; it proves nothing) |
Header names are not case-sensitive. Many frameworks give them to you in lowercase.
The signature#
signature = hex( HMAC_SHA256( key = secret, message = timestamp + ":" + raw_body ) )- The key is the webhook's secret, used as text. Don't decode it from hexadecimal.
timestampis the header's text, exactly as received.raw_bodyis the request body's bytes, exactly as received.
Steps#
- Read both headers and the raw body.
- Reject the request if the timestamp is more than a few minutes from your clock. 5 minutes is a reasonable limit.
- Compute the expected signature.
- Compare it with the header using a constant-time comparison. Check the lengths first if your function needs equal lengths.
- Only then parse the JSON.
Jemero signs each attempt again with a new timestamp, so retries pass the time check.
Examples#
Each example reads the secret from JEMERO_WEBHOOK_SECRET.
// receiver.mjs — Node.js 18 or later, no packages.
import { createHmac, timingSafeEqual } from 'node:crypto';
import { createServer } from 'node:http';
const TOLERANCE_MS = 5 * 60 * 1000;
const secret = process.env.JEMERO_WEBHOOK_SECRET;
if (!secret) throw new Error('Set JEMERO_WEBHOOK_SECRET');
function isFromJemero({ secret, timestamp, signature, rawBody, now = Date.now() }) {
if (!/^\d+$/.test(timestamp ?? '') || !signature) return false;
if (Math.abs(now - Number(timestamp)) > TOLERANCE_MS) return false;
const expected = createHmac('sha256', secret)
.update(`${timestamp}:`)
.update(rawBody)
.digest();
const received = Buffer.from(signature, 'hex');
return received.length === expected.length && timingSafeEqual(received, expected);
}
const server = createServer((request, response) => {
const chunks = [];
let size = 0;
request.on('data', (chunk) => {
size += chunk.length;
if (size > 5_000_000) request.destroy();
else chunks.push(chunk);
});
request.on('end', () => {
const rawBody = Buffer.concat(chunks);
const valid = isFromJemero({
secret,
timestamp: request.headers['x-jemero-webhook-timestamp'],
signature: request.headers['x-jemero-webhook-signature'],
rawBody,
});
if (!valid) {
response.writeHead(401).end();
return;
}
const payload = JSON.parse(rawBody.toString('utf8'));
// Save the payload to a queue here, then do slow work elsewhere.
console.log('received', payload.event);
response.writeHead(204).end();
});
});
server.listen(Number(process.env.PORT ?? 3000));# receiver.py — Python 3.9 or later, no packages.
import hashlib
import hmac
import json
import os
import re
import time
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
TOLERANCE_MS = 5 * 60 * 1000
def is_from_jemero(secret, timestamp, signature, raw_body, now_ms=None):
if not timestamp or not re.fullmatch(r"[0-9]+", timestamp):
return False
if not signature or not re.fullmatch(r"[0-9a-f]{64}", signature):
return False
now_ms = int(time.time() * 1000) if now_ms is None else now_ms
if abs(now_ms - int(timestamp)) > TOLERANCE_MS:
return False
expected = hmac.new(
secret.encode(), timestamp.encode() + b":" + raw_body, hashlib.sha256
).hexdigest()
return hmac.compare_digest(expected, signature)
class Handler(BaseHTTPRequestHandler):
def do_POST(self):
length = int(self.headers.get("Content-Length", 0))
if length > 5_000_000:
self.send_response(413)
self.end_headers()
return
raw_body = self.rfile.read(length)
if not is_from_jemero(
os.environ["JEMERO_WEBHOOK_SECRET"],
self.headers.get("X-JEMERO-Webhook-Timestamp"),
self.headers.get("X-JEMERO-Webhook-Signature"),
raw_body,
):
self.send_response(401)
self.end_headers()
return
payload = json.loads(raw_body)
# Save the payload to a queue here, then do slow work elsewhere.
print("received", payload["event"], flush=True)
self.send_response(204)
self.end_headers()
if __name__ == "__main__":
os.environ["JEMERO_WEBHOOK_SECRET"] # Fail at once if it isn't set.
ThreadingHTTPServer(("", int(os.environ.get("PORT", 3000))), Handler).serve_forever()<?php
// webhook.php — PHP 8.0 or later.
const TOLERANCE_MS = 300000;
function isFromJemero(
string $secret,
string $timestamp,
string $signature,
string $rawBody,
?int $nowMs = null
): bool {
if (!ctype_digit($timestamp) || $signature === '') {
return false;
}
$nowMs ??= (int) floor(microtime(true) * 1000);
if (abs($nowMs - (int) $timestamp) > TOLERANCE_MS) {
return false;
}
$expected = hash_hmac('sha256', $timestamp . ':' . $rawBody, $secret);
return hash_equals($expected, $signature);
}
$secret = getenv('JEMERO_WEBHOOK_SECRET');
if ($secret === false || $secret === '') {
http_response_code(500);
exit;
}
$rawBody = file_get_contents('php://input');
$valid = isFromJemero(
$secret,
$_SERVER['HTTP_X_JEMERO_WEBHOOK_TIMESTAMP'] ?? '',
$_SERVER['HTTP_X_JEMERO_WEBHOOK_SIGNATURE'] ?? '',
$rawBody
);
if (!$valid) {
http_response_code(401);
exit;
}
$payload = json_decode($rawBody, true, 512, JSON_THROW_ON_ERROR);
// Save the payload to a queue here, then do slow work elsewhere.
error_log('received ' . $payload['event']);
http_response_code(204);When verification fails#
| Check | |
|---|---|
| The secret | Each webhook has its own. Copy it again, without spaces. |
| The body | Make sure no middleware parsed or changed it first. |
| The time | The header is in milliseconds, not seconds. |
| The message | It is the timestamp, a colon, then the body. |
Answer 401 to requests that fail. Jemero records the failure and retries, so you can see it in the delivery log.