Skip to content

tutorials · tutorial

Verify webhook signatures in Node, correctly

HMAC verification with timingSafeEqual, timestamp replay protection and the three mistakes that make the check useless.

3 min readPublished 2026-11-25Updated 2026-11-25Reviewed by a person, not a template
Jump to the short answer
On this page
  1. The check
  2. The three mistakes
  3. Delivery semantics

The short answer

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/scrapefall", express.raw({ type: "application/json" }), (req, res) => {
  const sig = req.header("x-scrapefall-signature") ?? "";
  const ts  = Number(req.header("x-scrapefall-timestamp") ?? 0);

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

  const expected = crypto
    .createHmac("sha256", process.env.SCRAPEFALL_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 with fill rates, then a written quote in 24 hours.

Get a quote

Questions this raises

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.

Scrapefall 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.

Reviewed by a second engineer before publishing · last updated 2026-11-25

All articles

Send one URL. Get one sample. Decide from data, not a sales call.

100-row sample, masked where needed, with the source URL on every row. Written quote within 24 hours.