Verify signatures

Changed

Sign in to see or rotate your signing secret.

Each request carries X-Bullmania-Timestamp and X-Bullmania-Signature. Compare the signature against the raw body, not against a re-serialized object. The bytes that are signed are the timestamp, a dot, and the raw body.

The timestamp is Unix seconds, digits only. Reject a missing timestamp, a timestamp that is not digits, and a timestamp more than 5 minutes from your clock.

X-Bullmania-Signature is v1= and the lowercase hex HMAC-SHA256 of those bytes. The key is the member's signing secret as text. A member key is 64 hex characters, with no prefix. It is shown in full only once, when you create or rotate it.

Read the header names without caring about letter case. Compare the signature in constant time, on the bytes, after checking the lengths.

Copy your key when it is shown, and use that text as the HMAC key. A later visit shows only the last four characters. The example secret below is only for the sample body. It is not your key.

1const crypto = require("crypto");
2
3function header(headers, name) {
4 const want = name.toLowerCase();
5 for (const key of Object.keys(headers)) {
6 if (key.toLowerCase() === want) return headers[key];
7 }
8 return undefined;
9}
10
11function verify(headers, rawBody, secret, nowSeconds) {
12 const timestamp = header(headers, "x-bullmania-timestamp");
13 const signature = header(headers, "x-bullmania-signature");
14 if (typeof timestamp !== "string" || !/^[0-9]+$/.test(timestamp)) return false;
15 if (typeof signature !== "string") return false;
16 if (Math.abs(nowSeconds - Number(timestamp)) > 300) return false;
17 const expected =
18 "v1=" +
19 crypto.createHmac("sha256", secret).update(timestamp + ".").update(rawBody).digest("hex");
20 const a = Buffer.from(expected);
21 const b = Buffer.from(signature);
22 if (a.length !== b.length) return false;
23 return crypto.timingSafeEqual(a, b);
24}

Headers#

HeaderValue
Content-Typeapplication/json
User-AgentBullmania-Alerts/1
X-Bullmania-TimestampUnix time in seconds.
X-Bullmania-Signaturev1= and the hex HMAC-SHA256 of the timestamp, a dot, and the raw body.
X-Bullmania-DeliveryA new id for this attempt, so a repeat can be ignored.

Example#

Timestamp 1732800000. The example secret is 00112233445566778899aabbccddeeff00112233445566778899aabbccddeeff. It is 64 hex characters and it is only for this sample. A member key has that shape and no prefix. It is shown in full only once, when you create or rotate it. Copy your own key when it is shown. A later visit shows only the last four characters. Signature v1=ec3e7d713c0f5acab5dac202f3b85124cf902b45fd6c7d9f4d648bc675b66210. The body is the example on the payload page. Reject a timestamp more than 300 seconds from your clock.