Observable Timing Discrepancy
Description
The application uses string comparisons that are not constant time, allowing an adversary to calculate or observe small timing differences and potentially brute force a string that will match the expected value.
Examples
Insecure Code
javascript
if (password == userInput) { ... }Secure Code
javascript
const crypto = require('crypto'); function constantTimeIsPasswordEqual(userInput) { const password = getPasswordFromSecureDataStore(); return crypto.timingSafeEqual(Buffer.from(userInput, 'utf-8'), Buffer.from(password, 'utf-8')); }Remediation
Use the `crypto.timingSafeEqual` method when comparing strings, as shown in the example: `function constantTimeIsPasswordEqual(userInput) { const password = getPasswordFromSecureDataStore(); return crypto.timingSafeEqual(Buffer.from(userInput, 'utf-8'), Buffer.from(password, 'utf-8')); }`
Rule Details
| Field | Value |
|---|---|
| ID | CODE-0200 |
| Category | Crypto |
| Severity | MEDIUM |
| CWE | CWE-208 |
| Confidence | HIGH |
| Impact | HIGH |
| Likelihood | MEDIUM |
| Exploitability | MODERATE |
| Tags | timing attack, constant time comparison |
| OWASP | A3:2017-Sensitive Data Exposure, A02:2021-Cryptographic Failures |