SKY-D250 · Security in code
Weak random numbers for security values
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
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
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
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
export function createSessionToken(): string { return Math.random().toString(36).slice(2) + Date.now().toString(36);}Not flagged
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
- Runs with
--danger. In Python it finds calls to therandommodule's generators (random(),randint(),randrange(),choice(),choices(),sample(),randbytes(),getrandbits(),uniform()and the distribution functions such asgauss()), on the module, through an import alias, or on arandom.Random()object. Calls onrandom.SystemRandom()orsecrets.SystemRandom()are not reported, andrandom.shuffle()is not on the list. - 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_keyand similar), or the value flows into one. - 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 asotp=…, stored insession[...], set as a cookie value, returned from a security-named function, or compared with a request cookie. - 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 astoken,nonce,csrf,session,secret,password,apikey,jwt,cookie,salt,otp…,resetcodeormagiclink. Outer functions are not read, and seed, fixture and mock data generators (seed/,fixtures/,mocks/folders) are skipped. - In Go every call into
math/randormath/rand/v2is reported (by the Go engine as SKY-G209, shown as SKY-D250), whatever the value is used for. - In Java it reports
Math.random(),new Random()andnext…()calls on aRandom(notSecureRandom) inside a method that mentions a token, session or remember-me value or callsgetSession().
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.
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)insiderender_session_banner(). - Go reports every
math/randcall, 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)}"inreset_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 onlyAccept the findings you already have with
skylos baseline ., then scan with--baselineso only new ones are listed.[tool.skylos] ignore = ["SKY-D250"]turns the rule off for the whole project.