Skip to content

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

FieldValue
IDCODE-0200
CategoryCrypto
SeverityMEDIUM
CWECWE-208
ConfidenceHIGH
ImpactHIGH
LikelihoodMEDIUM
ExploitabilityMODERATE
Tagstiming attack, constant time comparison
OWASPA3:2017-Sensitive Data Exposure, A02:2021-Cryptographic Failures