Math.random for a secret
This is how the comment reads inline, under the flagged line.
Why it matters
Math.random and random are seeded PRNGs; an attacker who sees a few outputs can predict the next.
What to do
crypto.randomUUID() / crypto.getRandomValues(), or Python's secrets module.
Examples
const token = Math.random().toString(36).slice(2);
const token = crypto.randomUUID();
What it catches
It reads a pattern on each line, aware of strings and comments. Languages: TypeScript and JavaScript, Python. Scope: All files. These cases are the rule's own tests: the first set must be flagged and the second must not.
Flagged
const token = Math.random().toString(36);
Left alone
const jitter = Math.random() * 100;
Research
MD5 or SHA-1 for security, and `Math.random` or `random` for tokens. Hashes collide, and tokens become predictable.
Turn it off or change it
In a config file for the CLI, or in the API's rules field:
{
"rules": {
"disable": [
"insecure-random"
],
"severity": {
"insecure-random": "low"
}
}
}
Math.random()is predictable. It must not generate tokens or secrets.