Verify webhook signatures
Each request includes:
X-ErgOps-Webhook-Id: stable event UUID; deduplicate it.X-ErgOps-Webhook-Timestamp: Unix seconds.X-ErgOps-Webhook-Signature:v1=plus lowercase HMAC-SHA256 hex.
The signed bytes are ${timestamp}.${rawBody}. Verify against the raw body before JSON parsing using a constant-time comparison. Reject stale timestamps; five minutes is the recommended default tolerance unless your risk policy requires less.
const expected = crypto.createHmac("sha256", process.env.ERGOPS_WEBHOOK_SECRET)
.update(`${timestamp}.${rawBody}`).digest("hex");
const valid = crypto.timingSafeEqual(Buffer.from(signature.slice(3), "hex"), Buffer.from(expected, "hex"));
Webhook signing secrets must be recoverable by the delivery runtime, so ErgOps encrypts them at rest with a deployment-held key. API authentication secrets are different: only their irreversible hash is retained.
Secret rotation overlap#
When you rotate a signing secret with an overlap window, X-ErgOps-Webhook-Signature carries one v1= value per currently trusted secret, separated by commas — for example v1=abc123...,v1=def456.... Accept the delivery when any trusted signature matches; do not require all of them to match, and do not assume there is only one. Once the overlap window you configured expires, only the current secret's signature is present.
// trustedSecrets: the current secret, plus the previous one only while you
// are mid-rotation. Drop the previous secret once its overlap window ends.
const signed = `${timestamp}.${rawBody}`;
const candidates = signatureHeader.split(",").map((value) => value.trim().replace(/^v1=/, ""));
const isValid = trustedSecrets.some((secret) => {
const expected = crypto.createHmac("sha256", secret).update(signed).digest("hex");
return candidates.some((candidate) =>
candidate.length === expected.length &&
crypto.timingSafeEqual(Buffer.from(candidate, "hex"), Buffer.from(expected, "hex")));
});
Only ErgOps' delivery process has access to your secret during the overlap window; the previous secret is never re-exposed to you through the console after rotation. Rotate promptly and remove the previous secret from your own systems once the overlap window has passed.