Skip to main content
HighCodeOWASP A02:2021CWE-208

How to fix: HMAC verification with === operator

if (hmac(secret, msg) === providedTag) uses == / === to compare authentication tags. JavaScript string comparison is not constant time and leaks the prefix of a correct tag.

Why it matters

Network-local attackers recover the HMAC byte-by-byte and forge authenticated requests.

The `code-timing-hmac-equality` check verifies that the server does not expose the `timing-hmac-equality` weakness in the code category. The detection runs against the response headers, body, or auxiliary probes (DNS, TLS, async fetch) as appropriate.

How to fix it

  1. 1Compare HMAC outputs with crypto.timingSafeEqual.
  2. 2Convert both sides to Buffer of equal length; reject length mismatches.
  3. 3Prefer library helpers (e.g. noble-ed25519 verify) that already do this.

Code examples

Use timingSafeEqual for HMAC verification

TypeScript
import { createHmac, timingSafeEqual } from "crypto";

function verifyHmac(payload: string, receivedSig: string, secret: string): boolean {
  const expected = createHmac("sha256", secret).update(payload).digest("hex");
  const a = Buffer.from(receivedSig);
  const b = Buffer.from(expected);
  if (a.length !== b.length) return false;
  return timingSafeEqual(a, b); // not ===
}

References

Detected automatically by VulnRadar. The check id is code-timing-hmac-equality, stable across every scan so you can filter or diff on it.

See every code check

Questions

How severe is HMAC verification with === operator?
VulnRadar rates this high severity in the code category. The severity is fixed per check, not scored per site, so the same finding is the same severity on every scan.
Does VulnRadar check for this automatically?
Yes. It runs as part of every scan under the check id code-timing-hmac-equality, which stays the same between runs so you can filter, diff, or gate CI on it.
Which standards does HMAC verification with === operator map to?
It maps to CWE-208. In the OWASP Top 10 it falls under A02:2021.

Missing a check?

The detection set is open. If you know a misconfiguration this does not catch, propose it: the issue template asks for the threat model, how to detect it, the false-positive risk, and a test fixture, which is the same thing a maintainer would have to work out anyway.

Propose a checkHow a check is built

Scan your own site for hmac verification with === operator

VulnRadar runs this check and hundreds more against a URL in seconds. No agent to install, just paste a URL.

Or scan this site as a demoRead the docs

How to fix: HMAC verification with === operator | VulnRadar