Verify a webhook signature
Make sure a message really comes from Mizanpage, with the Standard Webhooks libraries or by hand.
Each message is signed with the secret of its address. Verify the signature before processing the message.
With a library
The signature follows the Standard Webhooks format. Their libraries (PHP, Node.js, Python, Go…) verify it as is with your whsec_… secret.
By hand
- Remove
whsec_from the secret and decode the rest from base64: that is the key. - Compute the HMAC-SHA256 of
{webhook-id}.{webhook-timestamp}.{raw body}with this key, then encode it in base64. - Compare it, in constant time, with the value after
v1,inwebhook-signature. - Refuse a
webhook-timestampmore than 5 minutes away from your clock.
Use the raw body, as received, before any JSON parsing.
PHP
$key = base64_decode(substr($secret, 6));
$expected = base64_encode(hash_hmac('sha256', "$id.$timestamp.$body", $key, true));
$valid = hash_equals('v1,' . $expected, $signatureHeader)
&& abs(time() - (int) $timestamp) <= 300;
Node.js
import crypto from 'node:crypto'
function verify(secret, id, timestamp, body, signatureHeader) {
const key = Buffer.from(secret.slice(6), 'base64')
const expected = crypto.createHmac('sha256', key).update(`${id}.${timestamp}.${body}`).digest('base64')
const a = Buffer.from(`v1,${expected}`)
const b = Buffer.from(signatureHeader)
const recent = Math.abs(Date.now() / 1000 - Number(timestamp)) <= 300
return recent && a.length === b.length && crypto.timingSafeEqual(a, b)
}
Change the secret
POST /v1/webhooks/{id}/secret creates a new secret, in service immediately. Update it on your server right away.