Skip to content

no-math-random-for-secrets ​

Math.random() used for something named like a secret: a token, key, salt, nonce, password, session or CSRF value.

Why: Math.random() is xorshift128+: fast and well distributed, but predictable from a few outputs (v8.dev). Anything an attacker must not guess needs the cryptographic generator.

ts
export function newSessionToken(): string {
  return Math.random().toString(36).slice(2);
}
✓ Accepted0 diagnostics
ts
export function newSessionToken(): string {
  return crypto.randomUUID();
}

randomToken() and randomInt() from @evobug_com/safe-ts-std/random use the cryptographic generator too.

What the rule can't see: secrets whose names don't say so. It goes by the name of the variable, property or function the value goes into.

MIT OR Apache-2.0 · Docs by Gemini 4 Neon Extra