How to fix: String Comparison Vulnerable to Timing Attacks
A direct string equality check was found on what appears to be a token, API key, or password comparison. Standard string comparison (===) is not constant-time and leaks information through response timing.
Why it matters
Timing attacks allow an attacker to determine how many characters of a secret they guessed correctly by measuring response time differences. Over many requests, they can reconstruct the full secret character by character.
JavaScript string comparison (===) short-circuits on the first mismatched byte. An attacker can send thousands of requests with slightly different tokens, measure average response times, and determine the correct prefix of the secret.
How to fix it
- 1Use crypto.timingSafeEqual() for all secret comparison operations.
- 2Ensure both buffers are the same length before comparing (pad or use fixed-length tokens).
- 3Prefer opaque session tokens validated against a database lookup over direct comparison.
- 4This matters most for API keys, CSRF tokens, and webhook signatures.
Code examples
Constant-time comparison
import { timingSafeEqual } from 'node:crypto';
function verifyToken(provided: string, expected: string): boolean {
// Ensure equal length to prevent length oracle
if (provided.length !== expected.length) return false;
return timingSafeEqual(
Buffer.from(provided, 'utf8'),
Buffer.from(expected, 'utf8')
);
}References
Detected automatically by VulnRadar. The check id is vibe-timing-attack-risk, stable across every scan so you can filter or diff on it.
More ai code checks
- LowGeneric Error Messages Leak No Context
- MediumTODO/FIXME Security Note in Response
- Higheval() Usage in Page Scripts
- HighSSL Certificate Verification Disabled
- HighMath.random() Used for Security Tokens
- CriticalHardcoded Credential in Authentication Logic
- CriticalJWT 'none' Algorithm Accepted
- CriticalSQL String Concatenation Pattern Detected
Questions
- How severe is String Comparison Vulnerable to Timing Attacks?
- VulnRadar rates this medium severity in the ai 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 vibe-timing-attack-risk, which stays the same between runs so you can filter, diff, or gate CI on it.
- Which standards does String Comparison Vulnerable to Timing Attacks 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.