Scan Python code for ten AST security issues: eval/exec, shell=True, pickle, unsafe yaml, weak hashes, disabled TLS, hardcoded secrets, SQL built from strings, mktemp, debug=True. Use when the user asks for a security scan or SARIF.
92%
Does it follow best practices?
Run evals on this skill
Adds up to 20 points to the overall score
Medium
Suggest reviewing before use
Every rule reports a rule id, severity, 1-based line, 1-based column in Unicode code points, and a message. Findings are sorted by path, line, column and rule id.
Syntax error, undecodable (non UTF-8) file, unreadable file, or a file above the size limit (1 MiB by default). The file was not analysed. Fix the syntax or encoding and re-run.
Detects calls to the built-in eval or exec by bare name. A local name that shadows the built-in
through an import is not reported.
# vulnerable
result = eval(user_expression)
# safer: parse literals only
import ast
result = ast.literal_eval(user_expression)Detects subprocess.run, call, check_call, check_output and Popen with a literal
shell=True, and any call to os.system or os.popen.
# vulnerable
subprocess.run(f"tar xf {archive}", shell=True)
# safer: argument list, no shell
subprocess.run(["tar", "xf", archive], check=True)Detects pickle.load, pickle.loads, _pickle.load(s), marshal.load(s) and shelve.open.
These can execute code while loading untrusted data.
# vulnerable
obj = pickle.loads(payload)
# safer: a data-only format
import json
obj = json.loads(payload)Detects yaml.load(...) with no Loader= keyword, or with a loader other than SafeLoader /
CSafeLoader. The loader must be passed as a keyword argument.
# vulnerable
config = yaml.load(text)
# safer
config = yaml.safe_load(text)
# or
config = yaml.load(text, Loader=yaml.SafeLoader)Detects hashlib.md5, hashlib.sha1, and hashlib.new("md5") / hashlib.new("sha1"), unless the
call passes usedforsecurity=False.
# flagged
digest = hashlib.md5(data).hexdigest()
# safer for security uses
digest = hashlib.sha256(data).hexdigest()
# acceptable for non-security checksums (cache keys, dedup)
digest = hashlib.md5(data, usedforsecurity=False).hexdigest()Detects requests.get/post/put/patch/delete/head/request with a literal verify=False, and
ssl._create_unverified_context.
# vulnerable
requests.get(url, verify=False)
# safer: keep verification; point to a CA bundle if needed
requests.get(url, timeout=10)Detects a non-empty string literal assigned (plain or annotated assignment) to a variable or
attribute whose name matches password, passwd, secret, token or api_key, optionally
prefixed with _-separated words (for example db_password, API_TOKEN). Empty strings are
allowed. The secret value is never printed.
# flagged
db_password = "change-me"
# safer: load it from your secret store at run time
db_password = load_secret("db_password")Detects .execute(...) or .executemany(...) where the first argument is an f-string with
placeholders, a .format(...) call, or a % / + expression that is not made of two constants.
# vulnerable
cur.execute(f"SELECT * FROM users WHERE id = {user_id}")
# safer: bound parameters
cur.execute("SELECT * FROM users WHERE id = ?", (user_id,))Detects tempfile.mktemp, which returns a name without creating the file (race condition).
# vulnerable
path = tempfile.mktemp()
# safer
with tempfile.NamedTemporaryFile() as handle:
...Detects any .run(...) call with a literal debug=True (for example app.run(debug=True)).
# flagged
app.run(debug=True)
# safer: no debug flag in code that ships
app.run()