CWE-330: Use of Insufficiently Random Values
A security-sensitive value is generated from a predictable source, so an attacker can guess or reproduce it.
Also known as: Insufficient randomness, Weak random values, Predictable tokens
Risk
Often high. Predictable tokens and codes lead directly to account takeover and authentication bypass.
Definition
What it is
CWE-330 occurs when a value that must be unpredictable is produced by a generator that is not cryptographically secure, such as Math.random or a time seed. The output looks random but follows a pattern an attacker can reconstruct. When that value is a token, session id, reset code, or key, predictability means account takeover or forged access. The fix is to draw every security-sensitive value from a cryptographically secure source with enough entropy.
Use-cases
How it shows up in real code
Guessable password-reset code
Reset codes come from Math.random. An attacker predicts the sequence and takes over accounts without the email.
Predictable session identifiers
Session ids are generated from a time seed. An attacker who knows the approximate time guesses a valid session.
Weak API key generation
API keys use a non-cryptographic generator. Keys are brute-forced or predicted, granting unauthorized access.
Low-entropy one-time password
An OTP is drawn from a small or predictable range, so it can be guessed within the allowed attempts.
AI risk
Why AI-generated code has increased the exposure
- Asked to make a token, id, OTP, or password-reset code, assistants often use Math.random, rand, or a time seed, because it is simple and produces a plausible value in the demo.
- Models learned from examples that use non-cryptographic generators for ids, so the insecure source is reproduced where a secure one is required.
- Generated code does not distinguish a value that only needs to be unique from one that must be unpredictable, because the prompt described intent, not a threat model.
- When scaffolding sessions, reset links, API keys, or nonces, the easiest generated solution reaches for the standard pseudo-random function.
- Each generated secret produced from a weak source is an independent exposure, so more generated auth features means more guessable values.
Vibe coding
How vibe coding amplifies the risk
- Vibe coding ships the generated token code unread, so a predictable generator reaches production guarding real accounts.
- Prompt-and-ship skips the review where Math.random for a reset code would be flagged.
- Iterating by prompt reuses the existing weak generator for new secrets rather than switching to a secure one.
- Tokens look random in testing, so the weakness stays invisible until an attacker predicts or brute-forces them.
- Rapid growth spreads the same weak generator across security-sensitive features faster than review can catch it.
Detection
How to find it
- Look for Math.random, rand, mt_rand, or time-based seeds used to produce tokens, ids, or keys.
- Trace every security-sensitive value to its source and confirm it uses a cryptographically secure generator.
- Check that generated values have enough entropy, not just enough length.
- Review reset codes, session ids, and API keys specifically for predictable construction.
Remediation
How to fix it
- Use a cryptographically secure random source for every security-sensitive value, such as the platform CSPRNG.
- Replace Math.random and similar generators wherever the output must be unpredictable.
- Give tokens enough entropy, at least 128 bits, from a secure source.
- Use a vetted library for id and token generation rather than hand-rolled schemes.
- Add a static analysis rule that flags non-cryptographic randomness feeding secrets.
Frequently asked questions
- What is CWE-330 (use of insufficiently random values)?
- CWE-330, use of insufficiently random values, is when a security-sensitive value such as a token, session id, or reset code is produced by a non-cryptographic generator like Math.random or a time seed, so an attacker can predict or reproduce it.
- How have AI assistants and vibe coding increased use of insufficiently random values risk?
- Assistants often use Math.random or a time seed for tokens because it is simple and produces a plausible value in the demo, not distinguishing unique from unpredictable. Vibe coding ships that generator unread, so predictable secrets reach production, and because tokens look random in testing the weakness stays hidden until they are predicted.
- How do I fix use of insufficiently random values?
- Use a cryptographically secure random source for every security-sensitive value, replace Math.random and similar generators wherever output must be unpredictable, give tokens at least 128 bits of entropy, use a vetted library for id and token generation, and add a rule flagging non-cryptographic randomness feeding secrets.
Related weaknesses
Catch CWE-330 before you ship
Tethlyn Audit scans your codebase and its git history for CWE-330 and the rest of the injection & code vulnerabilities class, then ranks each finding by real exploitability and data sensitivity.