Signature Verification
Verify HMAC-SHA256 webhook signatures to authenticate Cresora deliveries.
Every webhook delivery is signed with HMAC-SHA256 using your endpoint's signing secret. Verify this signature before processing any event.
Headers
| Header | Value |
|---|---|
X-Cresora-Signature | v1=<hex-encoded-hmac> |
X-Cresora-Timestamp | Unix timestamp of delivery (seconds) |
The v1= prefix is a scheme selector, not part of the digest. It is not
included in the HMAC input, and it exists so a future scheme version can be
introduced without breaking existing receivers.
Verification algorithm
- Build the signing payload:
timestamp.raw_body - Compute
HMAC-SHA256(payload, signing_secret) - Compare to the value in
X-Cresora-Signature(constant-time comparison) - Reject if timestamp is older than 5 minutes
import crypto from "node:crypto";
function verifyWebhook(req, secret) {
const timestamp = req.headers["x-cresora-timestamp"];
const signature = req.headers["x-cresora-signature"];
const body = req.rawBody; // raw bytes, not parsed JSON
// Reject stale deliveries (replay protection)
const ageSeconds = Math.floor(Date.now() / 1000) - Number(timestamp);
if (ageSeconds > 300) throw new Error("Webhook timestamp too old");
// HMAC over the raw body bytes — never over a re-serialized string
const payload = Buffer.concat([Buffer.from(`${timestamp}.`), body]);
const expected = Buffer.from(
"v1=" + crypto.createHmac("sha256", secret).update(payload).digest("hex"),
);
const received = Buffer.from(String(signature ?? ""));
// timingSafeEqual throws on inputs of different length, so a forged or
// missing header must be rejected before it is compared — never crash.
if (received.length !== expected.length || !crypto.timingSafeEqual(received, expected)) {
throw new Error("Webhook signature mismatch");
}
}import hmac, hashlib, time
def verify_webhook(body: bytes, headers: dict, secret: str):
timestamp = headers["X-Cresora-Timestamp"]
signature = headers["X-Cresora-Signature"]
age = int(time.time()) - int(timestamp)
if age > 300:
raise ValueError("Webhook timestamp too old")
# HMAC over the raw body bytes — never decode and re-encode the body
payload = timestamp.encode() + b"." + body
expected = "v1=" + hmac.new(secret.encode(), payload, hashlib.sha256).hexdigest()
if not hmac.compare_digest(signature, expected):
raise ValueError("Webhook signature mismatch")Always use constant-time comparison (timingSafeEqual / hmac.compare_digest). String equality (===) is vulnerable to timing attacks. In Node, check the lengths first: timingSafeEqual throws on inputs of different length, and a forged signature must be a clean rejection, not a crash.
Storing your signing secret
Store the signing secret in your secrets manager (not in code or environment files committed to source control).
Rotating your signing secret
Rotate the secret in the Partner Portal or through the API, on your own schedule or, if it may be exposed, at once and as the two paragraphs after these steps describe. If your only concern is that someone who left your team could see it, Rotation policy says when steps 1 to 5 are enough; accept only the new secret in step 3. You are then choosing to trust the deliveries your handler accepted before it accepted only the new secret. Rotation generates the new secret and shows it once, so it cannot be deployed ahead of time. Cresora signs new deliveries, retries and redeliveries with it from then on, although for up to about 5 minutes after the rotation, deliveries can still arrive signed with the old one.
- Before you rotate, make sure your handler answers a signature it cannot verify with
500, not2xx,400or401, and can check a delivery against two secrets (run the verification above once per secret) - Rotate the secret and copy the new one
- Deploy it at once, next to the old one: have your handler try the new secret first, then the old
- Remove the old secret as soon as at least 5 minutes have passed since the rotation and a delivery received after those 5 minutes has verified with the new one. If none arrives, use Send test event (up to 10 per endpoint per hour); a suspended endpoint cannot send one, so remove the old secret once the 5 minutes have passed. If a test event does not arrive and verify with the new secret, check the endpoint's URL, and have a Partner admin check Settings → Audit Log for a rotation nobody on your team made; if you find either, follow Responding to a suspected compromise. If you find neither, check the secret you deployed; if it may be wrong, rotate again and restart at step 2
- Redeliver what failed. If Cresora suspended the endpoint, first resume it as When Cresora suspends an endpoint describes, then use Send test event; if it does not verify, do what step 4 says for that case. Once a delivery or Send test event has verified with the new secret, redeliver from the delivery log, within the replay limits, any delivery that failed or was skipped since the rotation or, if the endpoint was already failing or suspended, since that began. While suspended, an endpoint that is not limited to specific merchants records only the retries that were already pending, so reconcile the rest of that time, and anything past the replay limits, against the API
Between steps 2 and 3, your handler cannot verify new deliveries. Its 500 gets them retried, each retry signed with the current secret; a 400 or 401 is never retried automatically (see Retry handling). Retries stop after about 35 minutes, and each failed attempt counts toward suspending the endpoint, so deploy within minutes. If the old secret may be exposed and you have not already done so following Responding to a suspected compromise, first note the Scopes of every API key that may be compromised or was kept where the secret leaked from, have a Partner admin revoke all of those keys, whatever their Scopes (calls with a revoked key fail until you deploy a replacement), and deactivate any team member you no longer trust or whose Portal sign-in may have leaked with it (or set an Invited one to Read-only), because a rotation hands the new secret to whoever makes it. Then rotate at once, even if step 1 is not in place, and accept only the new secret in step 3; if your handler may have answered 2xx to a delivery it could not verify, redeliver in step 5 every delivery since the rotation, whatever its status. If no Partner admin you trust can do that at once, rotate without waiting and again once they have; if you have none and are not already following it, follow the first bullet of Responding to a suspected compromise. If you revoked a key, deactivated anyone or changed anyone's role, or a key or Portal sign-in may be compromised, or a departure may be hostile, also do the rest of Responding to a suspected compromise after you rotate. Do step 5 once 5 minutes have passed since the rotation and, if you are following those steps, once their step 3 has restored the endpoint's URL, events and merchants. If it leaked from a store, log or host you have not fixed yet, the new secret is exposed too: once that is fixed, rotate again and repeat these steps for that rotation, including step 5 for what failed in between. Treat every signing secret kept in the same place as exposed, and repeat these steps for each of those endpoints.
Anyone who holds an exposed secret can sign a delivery that verifies. Do not trust any delivery that arrives between when the secret may first have been exposed and when your handler accepts only a secret created after you closed the path it leaked through, including deliveries you already acted on. Check what each one reports against the API (for example GET /transactions/{transactionId}), or in the Partner Portal or with support when the API cannot show it (for example hpp_session.payment_unresolved, whose session still reads PENDING). Act only on a change that check shows happened, even if a later change has superseded it, using what the check returns rather than the delivery's own fields; when it shows only the current state, act on that state. Never act twice on the same change, and correct what you already did so it matches what the check returns, undoing it if the change never happened. If a check errors or times out, retry it; never discard a delivery you could not check. A delivery from Send test event reports no change, so there is nothing to check: never act on one. Then also check in the same way, for that period, every object your endpoint subscribes to that may have changed, including ones created earlier (ask support for any the API cannot list), because a forged delivery may have made your handler skip a genuine one, for example by reusing its event_id.