Skip to content

Verify webhook signatures

Before you begin

Plan
Included in Pro and Team. How plans and limits work
Role
Admin or higher to read the signing secret.
You need
  • A webhook and a receiver you can change

Anyone who learns your receiver’s address can post to it. Every MockHub delivery is signed with the webhook’s own secret, so your receiver can accept what MockHub sent and refuse everything else. The scheme itself is described in Webhook signatures and retries.

  1. Open the project’s Webhooks tab.

  2. Select More actions on the webhook.

  3. Select Show secret.

  4. Select Copy.

  5. Select Done.

  6. Give the secret to your receiver through its configuration, such as an environment variable named MOCKHUB_WEBHOOK_SECRET, never in its source code.

  7. Add the check below to the receiver, and answer 401 to a request that fails it.

  8. Select Deliveries on the webhook.

  9. Select Send a test.

Each sample receives deliveries at /hooks/orders, checks the signature against the body exactly as it arrived, and allows five minutes between the delivery’s timestamp and the receiver’s clock.

With Express, express.raw() keeps the body as the bytes that were signed. A JSON parser in front of the route would lose them.

import { createHmac, timingSafeEqual } from 'node:crypto';
import express from 'express';
const TOLERANCE_SECONDS = 300;
function verifyMockHubSignature(
secret,
rawBody,
timestamp,
signature,
now = Math.floor(Date.now() / 1000),
) {
if (!secret || !/^\d+$/.test(timestamp ?? '')) {
return false;
}
if (Math.abs(now - Number(timestamp)) > TOLERANCE_SECONDS) {
return false;
}
if (!signature?.startsWith('v1=')) {
return false;
}
const expected = createHmac('sha256', secret).update(`${timestamp}.`).update(rawBody).digest();
const received = Buffer.from(signature.slice(3), 'hex');
return received.length === expected.length && timingSafeEqual(received, expected);
}
const app = express();
// express.raw() keeps req.body as the exact bytes MockHub signed.
app.post('/hooks/orders', express.raw({ type: '*/*' }), (req, res) => {
const valid =
Buffer.isBuffer(req.body) &&
verifyMockHubSignature(
process.env.MOCKHUB_WEBHOOK_SECRET,
req.body,
req.get('X-MockHub-Timestamp'),
req.get('X-MockHub-Signature'),
);
if (!valid) {
return res.sendStatus(401);
}
const event = JSON.parse(req.body.toString('utf8'));
// Handle the event, then answer quickly with a 2xx.
res.sendStatus(204);
});
app.listen(3000);

What the check does, in every language:

  • It signs the timestamp, a full stop and the raw body with the secret, using HMAC-SHA256, and compares the result with the hexadecimal digits after v1= in X-MockHub-Signature.
  • The comparison takes the same time whether the first or the last character differs, so an attacker cannot guess the signature one character at a time.
  • It refuses a timestamp more than five minutes from the receiver’s clock, so a delivery recorded by someone else cannot be sent again later. Each retry is signed afresh, so retries pass. Keep the receiver’s clock in sync.
  • It refuses everything while the secret is empty, so a missing environment variable cannot let a forged delivery through.

Check your code against this fixed example. Its timestamp is from 2023, so pass 1700000000 as the current time:

Input Value
Secret whsec_0123456789abcdef
X-MockHub-Timestamp 1700000000
Body {"a":1}
X-MockHub-Signature v1=9f5e37cdc7ea587c7c02062d93283cad000d66a831b2a1676418696db47ed6c2
console.log(
verifyMockHubSignature(
'whsec_0123456789abcdef',
'{"a":1}',
'1700000000',
'v1=9f5e37cdc7ea587c7c02062d93283cad000d66a831b2a1676418696db47ed6c2',
1700000000,
),
); // true

Change one character of the body or the signature, or move the current time more than 300 seconds away, and the result must be false.

Rotate secret under the webhook’s More actions gives it a new secret once you confirm. From that moment every delivery, including retries of earlier ones, is signed with the new secret only, and your receiver refuses them until it has it.

  1. Select More actions on the webhook.

  2. Select Rotate secret, then Rotate secret again to confirm.

  3. Select Copy, and give the new secret to your receiver.

  4. Select Deliveries on the webhook.

  5. Open Any status and choose Failed.

  6. Select Replay on each delivery your receiver refused while it still had the old secret, and confirm with Replay. A replay is signed with the new secret.

A 401 from your receiver is not retried, so replaying is the only way those deliveries arrive.

Your receiver accepts deliveries from the webhook, and answers 401 to a request with a missing, changed or stale signature.

The test delivery turns Succeeded with your receiver’s status, such as 204.

A delivery your receiver refused reads Failed with 401 in Answer.

  • The signature covers the timestamp and the body, nothing else: not the method, the address or any header, including your static headers.
  • Deliveries can occasionally arrive twice. The timestamp does not prevent that, so if handling an event twice matters, remember the X-MockHub-Delivery of each delivery you handled and ignore a repeat. A replay is a new delivery with its own X-MockHub-Delivery.
  • Each webhook has one secret at a time. MockHub does not sign with the old and the new secret during a rotation.
  • MockHub keeps the secret so that it can sign, and any Admin of the workspace can read it again. Treat it as a password.
  • An alert rule that calls a webhook address signs with a secret of its own, not a webhook’s: select More actions on the rule in the project’s Alerts tab, then Show secret. Its alerts carry X-MockHub-Alert-Rule-Id where a webhook’s carry X-MockHub-Webhook-Id, and are checked the same way.

Report a problem with this page