Skip to main content

SKY-L013 · Logic and resilience

Insecure randomness

High

The catalog gives no severity for this rule; the detector always reports HIGH.

A function from Python's random module produces a value that is stored under a security name, such as a reset code, token, session ID or salt. random is built for simulations and games: its output can be predicted by anyone who sees enough earlier values.

Languages
Python
Quality gate
counted toward the gate limits (full and diff scans)
Finding type
review
Wrong result? Report itA false positive, a missed case or a confusing message: it goes privately to the founder.

Why it matters

An attacker who requests a few reset codes for their own account can work out the generator's state and predict the next code, then reset someone else's password. The secrets module exists for exactly these values.

What Skylos flags

accounts/reset.py · PythonReported on line 10
"""Password reset by SMS code.""" import random from accounts.cache import cachefrom accounts.sms import send_sms  def start_password_reset(user):    reset_code = random.randint(100000, 999999)    cache.set(f"reset:{user.id}", reset_code, timeout=600)    send_sms(user.phone, f"Your reset code is {reset_code}")

What Skylos leaves alone

accounts/reset.py · Python
"""Password reset by SMS code.""" import randomimport time from accounts.sms import send_sms  def resend_reset_sms(user, message):    delay = random.uniform(1.0, 5.0)    time.sleep(delay)    send_sms(user.phone, message)

Why Skylos leaves it alone

random is used here, but for a retry delay (jitter). Nobody gains anything by predicting it, and the name delay contains no security word, so Skylos leaves it alone.

How Skylos decides

  1. Runs with --quality on Python files. Skips test files (test_*.py, *_test.py and conftest.py).
  2. Looks at assignments whose value is a direct call written random.randint(), random.randrange(), random.random(), random.uniform(), random.choice(), random.choices(), random.sample(), random.shuffle(), random.randbytes() or random.getrandbits().
  3. Reports HIGH when the name being assigned (a variable, an attribute or the container of a subscript) contains one of these words: token, secret, key, password, nonce, session, cookie, otp, totp, mfa, sso, salt, csrf, auth, jwt, hmac, signature, code, pin, verification or confirm.
  4. The match is on part of the name, ignoring case, and the finding is on the line of the assignment.

How to fix it

Use the secrets module. secrets.randbelow(n) gives an unpredictable number below n; secrets.token_urlsafe() gives a token string.

accounts/reset.py · Python · the fix, against the flagged file
"""Password reset by SMS code.""" Removed: import randomAdded: import secrets from accounts.cache import cachefrom accounts.sms import send_sms  def start_password_reset(user):Removed:     reset_code = random.randint(100000, 999999)Added:     reset_code = 100000 + secrets.randbelow(900000)    cache.set(f"reset:{user.id}", reset_code, timeout=600)    send_sms(user.phone, f"Your reset code is {reset_code}")

When it can be wrong

  • Because part of the name is enough, names such as monkey, shipping_choice (contains "pin") or postcode are treated as security values.
  • It misses the common token pattern token = "".join(random.choices(alphabet, k=32)), because the assigned call is join, not a random function, and it misses functions imported with from random import randint.

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

  • Put the marker on the line of the assignment.

    Python
    promo_code = random.choice(CODES)  # skylos: ignore[SKY-L013] not secret
  • [tool.skylos] ignore = ["SKY-L013"] turns the rule off for the project.

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