# Verify signatures Every webhook delivery is signed. Verify the signature before you trust the payload, or an attacker who guesses your endpoint URL can send you anything they like. ## The signature - **Algorithm:** HMAC-SHA256. - **Key:** the subscription's signing secret (from the portal, or from the create-subscription response). - **Message:** the **exact raw request body**, byte-for-byte. Do not decode-and-re-encode JSON before hashing — even a whitespace change breaks the check. - **Encoding:** the signature is delivered in the `X-Gallantree-Signature` header as **lower-case hexadecimal**. ## What you have to verify Signature verification is not enough on its own. A complete implementation checks three things: 1. **The signature matches** — proves the payload came from Gallantree and hasn't been tampered with. 2. **The event is new to you** — dedupe on a SHA-256 hash of the raw body (see [Delivery and retries](delivery-and-retries.md#duplicate-deliveries)). This also stops an attacker who captured a genuine delivery from replaying it to you, for as long as you keep the hash. 3. **The event is one you handle** — for an event type you don't handle, return `2xx` and ignore it. Don't return an error: a non-`2xx` response is retried and counts towards [pausing your subscription](delivery-and-retries.md#subscription-auto-pause). ### Don't reject retries as stale The `timestamp` in the body (mirrored in `X-Gallantree-Timestamp`) is when the event was first prepared, and **retries keep it**. Retries arrive up to about 14 hours 36 minutes later (see the [retry schedule](delivery-and-retries.md#retry-schedule)). A tight freshness window, such as five minutes, rejects every retry after the second attempt, which in turn counts as a failure and can pause your subscription. If you want an age check as well as deduplication, read the `timestamp` from the signed body (the header itself is not signed) and allow at least **15 hours**. ## Use a constant-time comparison Compare the signatures with a constant-time function (`crypto.timingSafeEqual` in Node, `hmac.compare_digest` in Python, `subtle.ConstantTimeCompare` in Go). Naive `==` on strings leaks timing information about how many bytes matched — a classic side-channel attack against HMAC. ## Node.js example ```typescript import crypto from "node:crypto"; import express from "express"; const app = express(); // IMPORTANT: keep the raw body for signing. If you use express.json() it consumes the stream // and the raw bytes are gone. Use express.raw({ type: "application/json" }) and JSON.parse yourself. app.post( "/webhooks/gallantree", express.raw({ type: "application/json" }), (req, res) => { const signature = req.header("X-Gallantree-Signature") ?? ""; const rawBody = req.body as Buffer; const secret = process.env.GALLANTREE_WEBHOOK_SECRET ?? ""; // 1. Signature check (constant-time) const expected = crypto.createHmac("sha256", secret).update(rawBody).digest("hex"); const signatureBuf = Buffer.from(signature, "hex"); const expectedBuf = Buffer.from(expected, "hex"); if (signatureBuf.length !== expectedBuf.length || !crypto.timingSafeEqual(signatureBuf, expectedBuf)) { return res.status(401).send("invalid signature"); } // 2. Dedupe — retries re-send the same body, so its hash identifies the event. // `seenBefore` records the hash (keep it for at least 15 hours) and // returns true if it was already there. const bodyHash = crypto.createHash("sha256").update(rawBody).digest("hex"); if (seenBefore(bodyHash)) { return res.status(200).send(); } // 3. Payload is safe to trust — process it asynchronously and 2xx quickly const payload = JSON.parse(rawBody.toString("utf8")); enqueue(payload); return res.status(200).send(); }, ); ``` ## Python example ```python import hashlib import hmac import os from flask import Flask, request, abort app = Flask(__name__) SECRET = os.environ["GALLANTREE_WEBHOOK_SECRET"].encode() @app.post("/webhooks/gallantree") def receive(): signature = request.headers.get("X-Gallantree-Signature", "") raw = request.get_data() expected = hmac.new(SECRET, raw, hashlib.sha256).hexdigest() if not hmac.compare_digest(signature, expected): abort(401, "invalid signature") # Retries re-send the same body, so its hash identifies the event. # seen_before records the hash (keep it for at least 15 hours) and # returns True if it was already there. if seen_before(hashlib.sha256(raw).hexdigest()): return "", 200 enqueue(request.get_json(force=True)) return "", 200 ``` ## Rotating the signing secret Rotate with the portal's **Rotate secret** button (or `PATCH /api/v1/webhooks/{webhookId}`). The response gives you the new secret; the old one is discarded. **The platform signs with the new secret from the moment you rotate.** There is no period where deliveries are signed with both. Deliveries that arrive before your receiver has the new secret fail the signature check. They are retried (the first retry is one minute later), and retries are signed with the current secret, so nothing is lost if you deploy the new secret promptly. To keep the gap short: 1. Make your receiver accept a list of secrets: try each, accept if any matches. 2. Rotate, then add the new secret to that list straight away, keeping the old one. 3. Once the new secret is live, remove the old one. Five failed attempts in a row [pause the subscription](delivery-and-retries.md#subscription-auto-pause), which takes about 2 hours 36 minutes for a single event. Don't leave the receiver without the new secret for that long. ## What to log when a signature fails - **`X-Gallantree-Delivery`** and **`X-Gallantree-Event`** — the delivery id and event type. The delivery id appears in the portal's delivery log, and is the same on every retry of that delivery. - **The `X-Gallantree-Timestamp` value and your server's time** — useful for matching log entries. Remember a retry carries the original timestamp. The most common cause of a false negative is a framework parsing and re-serialising the JSON before you hash it. - **Whether the request was over HTTPS** — a failed signature on an HTTP endpoint is almost certainly not from us. Do **not** log the raw body or the signing secret.