Skip to content

Use of Cryptographically Weak Pseudo-Random Number Generator (PRNG)

Description

Using cryptographically weak random number generators like `crypto.pseudoRandomBytes()` and `Math.random()` for security-critical tasks can expose systems to significant vulnerabilities. Attackers might predict the generated random numbers, compromising the integrity and confidentiality of cryptographic operations.

Examples

Insecure Code

javascript
const crypto = require('crypto');
const insecureBytes = crypto.pseudoRandomBytes(256);
console.log(`Insecure random bytes: ${insecureBytes.toString('hex')}`);

Secure Code

javascript
const crypto = require('crypto');
const secureBytes = crypto.randomBytes(256);
console.log(`Secure random bytes: ${secureBytes.toString('hex')}`);

Remediation

Replace the use of these cryptographically weak random number generators with `crypto.randomBytes()`, a method provided by Node.js's `crypto` module that generates cryptographically secure random numbers.

Rule Details

FieldValue
IDCODE-0352
CategoryCrypto
SeverityMEDIUM
CWECWE-338
ConfidenceHIGH
ImpactHIGH
LikelihoodMEDIUM
ExploitabilityEASY
Tagsrandom number generator, crypto
OWASPA3:2017-Sensitive Data Exposure, A02:2021-Cryptographic Failures