Home / patterns

Secrets compared with === (timing-unsafe token and signature checks)

Close-up of a developer's hands typing on a laptop showing blurred code at a kitchen table at night, a small analog stopwatch beside the laptop and a cup of tea under warm lamp light, no brand logos
Quick answer: AI-generated route handlers often check a secret with a plain string comparison: if (req.headers['x-api-key'] === process.env.API_KEY), or signature === expected in a hand-rolled webhook check. A normal comparison stops at the first wrong character, so the response time can leak how much of a guess was right. MITRE files this as CWE-208 (Observable Timing Discrepancy) and lists a real CVE where HMAC signatures were compared with String.equals(). The fix is a constant-time comparison such as Node’s crypto.timingSafeEqual or Python’s hmac.compare_digest, plus rate limiting.

Last updated November 11, 2026

Steps

  1. Search the code for secret checks that use ===, ==, !==, .equals() or startsWith(): API keys, webhook signatures, cron secrets, invite or magic-link tokens, password-reset tokens, admin passcodes and HMAC digests.
  2. In Node.js, compare with crypto.timingSafeEqual(Buffer.from(a), Buffer.from(b)). It throws if the two buffers have different byte lengths, so first hash both values to a fixed length (for example SHA-256 of each) or check lengths and return the same generic failure.
  3. In Python, use hmac.compare_digest(a, b) instead of ==. In other languages, use the constant-time compare from your crypto library.
  4. Prefer the provider’s official verification helper where one exists (for example a payment or messaging SDK’s webhook-signature function) instead of building and comparing the signature yourself.
  5. For passwords, never compare raw strings at all: store a slow hash (bcrypt, scrypt or Argon2) and use the library’s verify function.
  6. Return the same error and status code for every failed check, and add rate limiting to the endpoint so an attacker cannot make thousands of timed guesses.
  7. Retest: confirm valid secrets still pass, wrong ones fail with the same generic error, and wrong-length inputs do not crash the route.

Why it matters

MITRE’s CWE-208 describes operations that take different amounts of time in a way an attacker can observe, revealing security-relevant information such as whether part of a check succeeded. Its observed examples include CVE-2019-10071, a Java framework that compared HMAC signatures with String.equals() instead of a constant-time algorithm. Node.js documents crypto.timingSafeEqual as a constant-time comparison suitable for HMAC digests and secret values like authentication cookies, and Python’s hmac docs recommend compare_digest() instead of == to reduce timing-attack risk. Network noise makes these attacks harder over the internet, but the fix is one line, and rate limiting closes the remaining gap. This page is for checking and fixing your own app.

Sources

MITRE CWE-208: Observable Timing Discrepancy — https://cwe.mitre.org/data/definitions/208.html

Node.js docs: crypto.timingSafeEqual(a, b) — https://nodejs.org/api/crypto.html

Python docs: hmac.compare_digest — https://docs.python.org/3/library/hmac.html

Checked at the source on November 11, 2026.

Related patterns

Found a problem you'd rather not fix yourself?

Tell us what's wrong. A StorenTech engineer reviews your app and replies with a fixed quote, so you know the cost before any work starts.

Get it fixed by StorenTech

Vibe Security Weekly

One email a week with new failure patterns and how to fix them.

By joining you agree we may email you this newsletter. See our privacy policy. Free. Unsubscribe anytime. We'll never sell your email.