Insecure Random
Math.random() used for tokens, secrets, or session IDs
warningSecurity
insecure-randomWhy this matters
Math.random() is not cryptographically secure. Its output is predictable — attackers can guess session tokens, reset codes, and invite links generated this way.
Example
lib/tokens.tsfunction generateToken() {
return Math.random().toString(36).slice(2);
}lib/tokens.tsimport { randomBytes } from "crypto";
function generateToken() {
return randomBytes(32).toString("hex");
}How to fix
Use crypto.randomBytes() (Node.js) or crypto.getRandomValues() (browser) for any security-sensitive random values.
Reference
- In the output
- Findings carry
"ruleId": "insecure-random"in--jsonand the same id in SARIF and in the terminal. - Silence one line
// prodlint-disable-next-line insecure-random- Silence a whole file
// prodlint-disable insecure-randomat the top of the file- How it works
- Matches text patterns. Source on GitHub