Skip to main content
HighCodeOWASP A02:2021CWE-208

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

  1. 1Use crypto.timingSafeEqual(Buffer.from(a), Buffer.from(b)) for any secret comparison.
  2. 2Hash both sides with SHA-256 first if lengths differ, then compare.
  3. 3Add a unit test that fails if comparison timing varies by input.

Code examples

Use crypto.timingSafeEqual for token comparison

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

See every code check

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.

Propose a checkHow a check is built

Scan your own site for non-constant-time signature comparison

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

Non-constant-time signature comparison fix | VulnRadar