Security rules

NGB070: Timing-unsafe secret comparison

Reports server-side code that compares a value from the incoming request (header, body, query, cookies) with a secret or an expected MAC using ===, !==, == or !=: webhook signatures checked against createHmac(…).digest(), API keys, bearer / access tokens and shared secrets.

Property Value
Rule ID NGB070
Category Angular SSR security
Default severity medium
CWE CWE-208
Built-in presets recommended: error, strict: error

Confidence is reported per finding (high, medium, low) and is independent of severity. See Reading findings.

What it checks

Reports server-side code that compares a value from the incoming request (header, body, query, cookies) with a secret or an expected MAC using ===, !==, == or !=: webhook signatures checked against createHmac(…).digest(), API keys, bearer / access tokens and shared secrets. Only server files (SSR entry points, files importing Node / Express modules) are analyzed; comparisons with crypto.timingSafeEqual() are the safe form and are not reported.

Why it matters

String equality stops at the first differing character, so the response time leaks how many leading characters of the guess were right. Over many requests — especially from the same data center — an attacker recovers a valid HMAC signature or API key byte by byte without ever knowing the secret. Webhook endpoints and internal API-key checks in SSR / BFF servers are the typical place for this bug.

Detection behavior and supported patterns

  • req.headers["x-signature"] === createHmac("sha256", secret).update(body).digest("hex") (directly or through const variables)
  • if (req.get("x-api-key") !== process.env.API_KEY), req.body.token == expectedToken, comparisons with values named signature, hmac, digest, apiKey, accessToken, secret
  • Requests: Express / Node request objects, including untyped (req, res) handlers

Example

TypeScript
if (req.get('x-hub-signature-256') !== 'sha256=' + createHmac('sha256', secret).update(raw).digest('hex')) return res.sendStatus(401);

Safer alternative

TypeScript
const expected = Buffer.from('sha256=' + createHmac('sha256', secret).update(raw).digest('hex'));const given = Buffer.from(req.get('x-hub-signature-256') ?? '');if (given.length !== expected.length || !timingSafeEqual(given, expected)) return res.sendStatus(401);

Remediation

Compare secrets in constant time: convert both sides to equal-length buffers and use crypto.timingSafeEqual(a, b) after checking a.length === b.length, or compare HMACs of both values. Prefer verifying signatures with the provider SDK, which already does this.

False-positive considerations

  • Comparisons with values that are not secret despite their names (public key IDs, signature algorithm names) may be reported; rename or except them.

Known limitations

  • Password checks (user.password === req.body.password) are not reported here: plaintext password storage is the larger problem.
  • Secrets that reach the comparison through function calls or class fields with neutral names are not recognized.

References

Run it

Shell
ngbastion explain NGB070