Skip to content
TodayTell Data

Verify webhook signatures in Node, correctly

Updated 2026-11-25 · 3 min read · By BYM Data team

Compute the HMAC over the raw request body, compare it with timingSafeEqual, and reject anything older than five minutes. Using the parsed JSON or a plain string comparison turns a security control into decoration.

The check

import crypto from "node:crypto";
import express from "express";

app.post("/webhooks/bym", express.raw({ type: "application/json" }), (req, res) => {
  const sig = req.header("x-bym-signature") ?? "";
  const ts  = Number(req.header("x-bym-timestamp") ?? 0);

  if (Math.abs(Date.now() / 1000 - ts) > 300) return res.status(400).end("stale");

  const expected = crypto
    .createHmac("sha256", process.env.BYM_WEBHOOK_SECRET!)
    .update(req.body)              // raw Buffer, not JSON.parse
    .digest("hex");

  const a = Buffer.from(sig, "hex");
  const b = Buffer.from(expected, "hex");
  if (a.length !== b.length || !crypto.timingSafeEqual(a, b)) return res.status(401).end("bad signature");

  const event = JSON.parse(req.body.toString("utf8"));
  // de-duplicate on event.id, then queue
  res.status(200).end("ok");
});

The three mistakes

  • Verifying JSON.stringify(parsed) — key order changes the bytes
  • Comparing with === — leaks timing and is easy to weaken later
  • Ignoring the timestamp — a captured request replays forever

Delivery semantics

Assume at-least-once and unordered. Store processed event ids and return 200 quickly; do the work in a queue.

Want this data without building it?

Free 100-row sample, written quote in 24 hours.

Get a quote

Questions

Why raw body?

The signature is over bytes. Any parse-and-restringify step can change them.

What if my framework already parsed the body?

Keep the raw buffer (e.g. verify hook in Fastify) — most frameworks allow it.

Should I retry failed deliveries?

The sender does. Your job is to be idempotent.

BYM Data team

Data engineering — We build and run the extraction pipelines behind every dataset we sell. Guides are written by whoever maintains the source, and reviewed by a second engineer before publishing.

Get a free sampleWhatsApp