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
- 1Compare HMAC outputs with crypto.timingSafeEqual.
- 2Convert both sides to Buffer of equal length; reject length mismatches.
- 3Prefer library helpers (e.g. noble-ed25519 verify) that already do this.
Code examples
Use timingSafeEqual for HMAC verification
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.
More code checks
- CriticalForm Submits Data Over Insecure HTTP
- MediumPotential Prototype Pollution Sinks Detected
- HighPotential Command Injection Vectors
- HighJWT Decoded Without Signature Verification
- HighPotential Path Traversal Vulnerability
- HighInsecure Authentication Mechanisms Detected
- HighPotential Insecure Deserialization
- Mediumeval() Usage in Inline Scripts
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.