ESLint InterlaceESLint Interlace
Plugin: node-securityRules

no-cryptojs-weak-random

ESLint rule documentation for no-cryptojs-weak-random

📡 Live from GitHub — This documentation is fetched directly from no-cryptojs-weak-random.md and cached for 6 hours.

Keywords: crypto-js, WordArray.random, weak random, Math.random, CVE-2020-36732, CWE-338, ESLint rule CWE: CWE-338
CVE: CVE-2020-36732
OWASP: A02:2021-Cryptographic Failures

Disallow crypto-js WordArray.random() (CVE-2020-36732)

Detects usage of crypto-js WordArray.random() which used insecure Math.random() in versions prior to 3.2.1. This rule is part of eslint-plugin-node-security and provides LLM-optimized error messages.

🚨 Security rule | 💡 Provides suggestions | ⚠️ Set to error in recommended

Quick Summary

AspectDetails
CWE ReferenceCWE-338 (Weak PRNG)
CVE ReferenceCVE-2020-36732
SeverityCritical (security vulnerability)
Auto-Fix💡 Suggests crypto.randomBytes()
CategorySecurity
ESLint MCP✅ Optimized for ESLint MCP integration
Best ForProjects using or migrating from crypto-js

Vulnerability and Risk

Vulnerability: In crypto-js versions prior to 3.2.1, CryptoJS.lib.WordArray.random() used JavaScript's Math.random() for generating random bytes. Math.random() is not cryptographically secure and produces predictable values.

Risk: Keys, IVs, salts, and tokens generated with this function are predictable. An attacker can reproduce the random values and decrypt data, forge signatures, or bypass authentication.

Rule Details

This rule detects:

  • CryptoJS.lib.WordArray.random()
  • WordArray.random()
  • CryptoJS.random()

Why This Matters

RiskImpactSolution
🎲 Predictable RandomGenerated values can be reproducedUse crypto.randomBytes()
🔑 Weak Keys/IVsEncryption keys become guessableMigrate to native CSPRNG
🔓 Token ForgeSession tokens can be predictedNever use Math.random for crypto

Configuration

OptionTypeDefaultDescription
allowInTestsbooleanfalseAllow in test files
{
  rules: {
    'node-security/no-cryptojs-weak-random': ['error', {
      allowInTests: false
    }]
  }
}

Examples

❌ Incorrect

import CryptoJS from 'crypto-js';

// Weak random - uses Math.random() in versions < 3.2.1
const salt = CryptoJS.lib.WordArray.random(16); // ❌ CVE-2020-36732

// Generating IV with weak random
const iv = WordArray.random(16); // ❌ Predictable IV

// Generating key material
const key = CryptoJS.lib.WordArray.random(32); // ❌ Weak key

✅ Correct

import crypto from 'node:crypto';

// Use cryptographically secure random bytes
const salt = crypto.randomBytes(16); // ✅ Uses OS CSPRNG

// Generating IV
const iv = crypto.randomBytes(16); // ✅ Secure

// Generating key material
const keyBytes = crypto.randomBytes(32); // ✅ Secure

// In browsers, use Web Crypto API
const browserRandom = new Uint8Array(16);
globalThis.crypto.getRandomValues(browserRandom); // ✅ Secure

CVE-2020-36732 Details

  • Affected Versions: crypto-js < 3.2.1
  • CVSS Score: 7.5 (High)
  • Attack Vector: Remote
  • Issue: Used Math.random() which has only 2^52 possible states

The Problem with Math.random()

// Math.random() internals (V8 example)
// Uses xorshift128+ with only 128 bits of state
// State can be reconstructed from ~5 outputs
// NOT suitable for cryptographic use

// DO NOT USE for crypto:
const weakKey = Array.from({ length: 32 }, () =>
  Math.floor(Math.random() * 256),
);

Security Impact

VulnerabilityCWEOWASPCVSSImpact
Weak PRNG338A02:20217.5 HighPredictable crypto values
Insufficient Entropy331A02:20215.9 MediumKey material weakness

Migration Guide

Phase 1: Audit

# Check crypto-js version
npm list crypto-js
# If < 3.2.1, vulnerable!

Phase 2: Replace

// Before (vulnerable)
import CryptoJS from 'crypto-js';
const random = CryptoJS.lib.WordArray.random(32);

// After (secure)
import crypto from 'node:crypto';
const random = crypto.randomBytes(32);

Phase 3: Remove crypto-js

npm uninstall crypto-js

Known False Negatives

Aliased Imports

Why: Aliased references not tracked.

// ❌ NOT DETECTED
const rand = CryptoJS.lib.WordArray.random;
rand(16);

Mitigation: Search for all WordArray references.

Further Reading

On this page

No Headings