Skip to main content

SKY-D250 · Security in code

Weak random numbers for security values

MediumCWE-330

Python, TypeScript/JavaScript and Go findings are MEDIUM. Java findings are reported as HIGH.

General-purpose random generators, such as Python's random module, Math.random(), Go's math/rand and java.util.Random, are built for simulations and games, not secrets. SKY-D250 reports them where they make a token, password, session ID or similar value.

Languages
Python, TypeScript/JavaScript, Go, Java
Quality gate
counted toward the gate limits (full and diff scans)
Finding type
review
Catalog name
Weak random source
Go engine ID
SKY-G209, reported as SKY-D250
Wrong result? Report itA false positive, a missed case or a confusing message: it goes privately to the founder.

Why it matters

Their output can be predicted: after seeing a few values, or knowing roughly when the process started, an attacker can compute the next password-reset token or session ID and take over the account.

What Skylos flags

app/tokens.py · PythonReported on line 8
import randomimport string ALPHABET = string.ascii_letters + string.digits  def make_reset_token() -> str:    return "".join(random.choice(ALPHABET) for _ in range(32))

What Skylos leaves alone

app/sampling.py · Python
import random  def sample_orders_for_audit(orders: list, size: int = 20) -> list:    return random.sample(orders, min(size, len(orders)))  def retry_delay(attempt: int) -> float:    return random.uniform(0, 2**attempt)

Why Skylos leaves it alone

Picking orders to spot-check and adding jitter to a retry delay need no secrecy. The function names hold no security word and the values do not flow into a token, password, session or cookie, so Skylos stays quiet.

More languages

TypeScript

Flagged

src/auth/session.ts · TypeScriptReported on line 2
export function createSessionToken(): string {  return Math.random().toString(36).slice(2) + Date.now().toString(36);}

Not flagged

src/net/backoff.ts · TypeScript
export function retryDelayMs(attempt: number): number {  const jitter = Math.random() * 250;  return Math.min(30_000, 2 ** attempt * 100 + jitter);}

Why Skylos leaves it alone

Neither the statement nor the enclosing function or variable names mention a token, session, password or other security term.

How Skylos decides

  1. Runs with --danger. In Python it finds calls to the random module's generators (random(), randint(), randrange(), choice(), choices(), sample(), randbytes(), getrandbits(), uniform() and the distribution functions such as gauss()), on the module, through an import alias, or on a random.Random() object. Calls on random.SystemRandom() or secrets.SystemRandom() are not reported, and random.shuffle() is not on the list.
  2. A Python call is reported only with security context: the enclosing function's name contains a security word (token, password, secret, session, nonce, otp, salt, csrf, jwt, cookie, auth, credential, signature, signing, api_key and similar), or the value flows into one.
  3. The flow is followed through str(), slicing, formatting, joins, comprehensions and up to four steps of local variables and module-level helpers. It counts when the value is stored under a security-named variable, attribute or dictionary key, passed as a security-named keyword such as otp=…, stored in session[...], set as a cookie value, returned from a security-named function, or compared with a request cookie.
  4. In TypeScript and JavaScript it reports Math.random() when the statement around it, the variable it initializes, or the name of its nearest enclosing function, method or variable contains a security term such as token, nonce, csrf, session, secret, password, apikey, jwt, cookie, salt, otp…, resetcode or magiclink. Outer functions are not read, and seed, fixture and mock data generators (seed/, fixtures/, mocks/ folders) are skipped.
  5. In Go every call into math/rand or math/rand/v2 is reported (by the Go engine as SKY-G209, shown as SKY-D250), whatever the value is used for.
  6. In Java it reports Math.random(), new Random() and next…() calls on a Random (not SecureRandom) inside a method that mentions a token, session or remember-me value or calls getSession().

How to fix it

Make security values with a cryptographic generator: secrets in Python (token_urlsafe(), token_hex(), choice()), crypto.randomBytes() or crypto.randomUUID() in Node, crypto.getRandomValues() in browsers, crypto/rand in Go and SecureRandom in Java.

app/tokens.py · Python · the fix, against the flagged file
Removed: import randomRemoved: import stringRemoved:  Removed: ALPHABET = string.ascii_letters + string.digitsAdded: import secrets  def make_reset_token() -> str:Removed:     return "".join(random.choice(ALPHABET) for _ in range(32))Added:     return secrets.token_urlsafe(24)

When it can be wrong

  • Context is read from names, so a harmless value in a security-named function is reported, such as random.choice(COLORS) inside render_session_banner().
  • Go reports every math/rand call, including retry jitter and test data.
  • Missed: a weak random reset code built into a link in a function whose name and variables carry no security word, such as f"/reset/{user.id}/{random.randint(0, 10**8)}" in reset_link().

Seeing a case that isn't listed here? Report the wrong result so the rule can be fixed. Don't paste company code: describe the shape of it.

How to silence it

  • Mark a reviewed line when the value really is not a secret.

    Python
    hue = random.choice(PALETTE)  # skylos: ignore[SKY-D250] banner colour only
  • Accept the findings you already have with skylos baseline ., then scan with --baseline so only new ones are listed.

  • [tool.skylos] ignore = ["SKY-D250"] turns the rule off for the whole project.

From the Skylos rule handbook, Skylos 5.8.0 (commit 5b880b0). All rules.