Verify signatures
Check that an event really comes from Meishi and was not replayed.
Each event carries a Meishi-Signature header:
Meishi-Signature: t=1762773241,v1=5d2f8c0e…tis when it was sent, in Unix seconds.v1is the hex HMAC-SHA256 oft, a full stop and the raw request body, made with your signing secret.
To check it:
- Read the raw body, before any JSON parsing (which changes the bytes).
- Compute
HMAC-SHA256(secret, t + "." + rawBody)as hex. - Compare it with
v1in constant time. - Turn away events whose
tis more than 5 minutes from now, so an old event cannot be replayed.
// Express. The signature covers the exact bytes Meishi sent, so read the raw body.
import crypto from 'node:crypto';
import express from 'express';
const SECRET = process.env.MEISHI_WEBHOOK_SECRET; // the signing secret
const app = express();
app.post('/your-meishi-webhook', express.raw({ type: 'application/json' }), (req, res) => {
const header = req.get('Meishi-Signature') ?? '';
const { t, v1 } = Object.fromEntries(header.split(',').map(part => part.split('=')));
const expected = crypto.createHmac('sha256', SECRET).update(`${t}.${req.body}`).digest('hex');
const fresh = Math.abs(Date.now() / 1000 - Number(t)) < 300;
const matches =
typeof v1 === 'string' &&
v1.length === expected.length &&
crypto.timingSafeEqual(Buffer.from(v1), Buffer.from(expected));
if (!fresh || !matches) return res.status(400).send('bad signature');
const event = JSON.parse(req.body);
// A repeated event keeps its id: handle each id once.
if (event.type === 'experience.ready') {
// event.data is the experience: scanUrl, qrSvgUrl, printPdfUrl, orderReference…
}
res.sendStatus(200);
});Replacing the signing secret in the portal stops the old one at once. Deploy the new secret to your server first, then replace it, to lose no events.