How to fix: Non-constant-time signature comparison
if (providedSig === storedSig) compares HMACs or tokens with JavaScript ===, which short-circuits on the first differing byte. Attackers measure timing to recover the signature byte-by-byte.
Why it matters
Timing oracle leaks signature bytes, allowing an attacker to forge valid signatures or replay tokens.
The `code-timing-no-constant-time-compare` check verifies that the server does not expose the `timing-no-constant-time-compare` 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
- 1Use crypto.timingSafeEqual(Buffer.from(a), Buffer.from(b)) for any secret comparison.
- 2Hash both sides with SHA-256 first if lengths differ, then compare.
- 3Add a unit test that fails if comparison timing varies by input.
Code examples
Use crypto.timingSafeEqual for token comparison
import { timingSafeEqual, createHmac } from "crypto";
function verifyToken(provided: string, expected: string): boolean {
const a = Buffer.from(provided);
const b = Buffer.from(expected);
// Lengths must match before timing-safe comparison
if (a.length !== b.length) return false;
return timingSafeEqual(a, b);
}References
Detected automatically by VulnRadar. The check id is code-timing-no-constant-time-compare, 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 Non-constant-time signature comparison?
- 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-no-constant-time-compare, which stays the same between runs so you can filter, diff, or gate CI on it.
- Which standards does Non-constant-time signature comparison 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.