Session tokens are cryptographically random and at least 128 bits
Why it matters
Session tokens, password reset links, and email verification codes generated with Math.random() or Date.now() are predictable. CWE-338 (Use of Cryptographically Weak PRNG) and CWE-330 (Use of Insufficiently Random Values) describe how an attacker who knows the approximate generation time can enumerate the token space in seconds, enabling account takeover without ever knowing the victim's password. OWASP A02 (Cryptographic Failures) and NIST 800-53 SC-8 both require cryptographically secure randomness for security tokens. Six-digit numeric email codes have only one million possibilities — brute-forceable in under a minute against a rate-unlimited endpoint.
Severity rationale
Critical because a predictable password-reset or session token lets an attacker take over any account without needing the user's credentials, bypassing authentication entirely.
Remediation
Use crypto.randomBytes or crypto.randomUUID() for every security-sensitive token. Store reset tokens hashed (SHA-256), send the plaintext to the user:
import crypto from 'crypto'
const rawToken = crypto.randomBytes(32).toString('hex') // 256 bits
const storedToken = crypto.createHash('sha256').update(rawToken).digest('hex')
await db.passwordReset.create({
data: { token: storedToken, userId, expiresAt: new Date(Date.now() + 3_600_000) },
})
// Email rawToken to the user, never storedToken
UUID v4 (crypto.randomUUID()) is acceptable for session identifiers — 122 bits of randomness.
DetectionPro
The detection method for this pattern is part of AuditBuffet Pro.
The full method (what to look for, pass and fail conditions, and how results get reported) ships inside the Security Hardening Audit. The pattern definition, severity rationale, and remediation remain openly licensed under CC-BY-4.0.
External references
- cwe · CWE-338 — Use of Cryptographically Weak Pseudo-Random Number Generator
- cwe · CWE-330 — Use of Insufficiently Random Values
- owasp:2021 · A02 — Cryptographic Failures
- nist:rev5 · SC-8 — Transmission Confidentiality and Integrity
Taxons
History
- 2026-04-18·v1.0.0·Initial import from security-hardening·automated