SKY-L013 · Logic and resilience
Insecure randomness
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
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
"""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
"""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
- Runs with
--qualityon Python files. Skips test files (test_*.py,*_test.pyandconftest.py). - 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()orrandom.getrandbits(). - 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.
- 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.
"""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") orpostcodeare treated as security values. - It misses the common token pattern
token = "".join(random.choices(alphabet, k=32)), because the assigned call isjoin, not arandomfunction, and it misses functions imported withfrom 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.