This sheet is past its review date. Deadlines or advice may have changed since 5 Nov 2026.
Python: which everyday calls turn into vulnerabilities?
Before your next release, move off Python 3.10 and older2, keep pickle-based loaders away from outside data, and run Ruff's S rules and pip-audit in CI. Judge everything else by whether input stays data: never code, a shell command, SQL text, a template or a path outside the directory you meant.
If you only do five things
Pass data as arguments, never as code.
Bind SQL values as parameters, call
subprocesswith a list and noshell=True, and never render user text as a template. SQL injection ranks 2nd and OS command injection 9th in the 2025 CWE Top 251.Let tools find the obvious ones.
Run Ruff's
Srules andpip-auditin CI. Ruff flags pickle,yaml.load(),shell=True, SQL built from strings, missing timeouts andverify=False23.
Review a Python pull request
Where does outside data enter?
Find request handlers, CLI arguments, queue consumers, uploads and downloaded models. Each should become a pydantic model or form before other code uses it.
Does it reach a loader?
Search for
pickle,shelve,yaml.load,torch.loadandfrom_pretrained. Data from outside needs a format that can't run code.Does it reach something that runs strings?
Check SQL text,
subprocess,eval, template source and**unpacking into ORM calls.Does it reach the file system?
Paths from input need
resolve()plusis_relative_to(). Archives needfilter="data", an empty directory and size limits.Does it reach the network?
Calls need a timeout and certificate checks. URLs from users need an allowlist against server-side request forgery, 22nd in the CWE Top 251.
Python traps and what to use instead
pickle.loads(),shelveandyaml.load(). Unpickling can run arbitrary code5, andshelveandmultiprocessing'sConnection.recv()use pickle6. Usejson, andyaml.safe_load()for YAML5.torch.load()andtrust_remote_code=True. PyTorch has defaulted toweights_only=Truesince 2.67, but before 2.10.0 that mode could still run code8. Prefer safetensors10.trust_remote_code=Trueruns code from the Hub repository on your machine9.eval()andast.literal_eval(). Ruff's S307 suggestsliteral_eval()in place ofeval()23, but Python's docs advise against it for untrusted data: small inputs can exhaust memory or crash the process11. Usejson.loads().subprocess.run(cmd, shell=True). With a shell, quoting is your job12. Pass a list such as["convert", src, dst]withoutshell=True12, and put--before values that may start with a dash, which programs read as options.- SQL in f-strings,
%or.format(). Pass values as parameters (%sin psycopg) or in a Python 3.14t"..."string, which psycopg 3.3 parameterizes; anfin its place pastes values into the SQL13. In Django, never unpack a user-supplied dict with**into QuerySet methods: that still allowed SQL injection in February 202614. - Templates built from user input.
render_template_string()orfrom_string()on user text can lead to remote code execution15. Pass input as variables to fixed template files, and setautoescape=select_autoescape(), since Jinja doesn't escape by default16. tarfile.extractall(). Passfilter="data"before Python 3.14, which made it the default17. It isn't a sandbox: from June to October 2026 the PSF published seven advisories for archives that got past the filters4. Extract into a new empty directory with OS-level limits17.os.path.join(base, name)andPath(base) / name. An absolutenamediscardsbase18. Resolve the result, then checkis_relative_to(base.resolve()); on unresolved paths that check ignores..18.requests.get(url)withouttimeout=, or withverify=False. Requests never times out unless told to, andverify=Falseaccepts any certificate. It trusts the CA list incertifi, so keep certifi updated19.assertfor checks.python -OandPYTHONOPTIMIZE=1skip asserts; aiohttp's POST parsing could then loop forever (CVE-2025-69227)20. Raise exceptions for permission and input checks.
Tools that catch it
- Ruff's
Srules. Add"S"toextend-selectin[tool.ruff.lint]. Fix S301 (pickle), S506 (YAML), S602 (shell), S608 (SQL), S202 (tar), S113 (timeouts) and S311 (random) first23. - Bandit for ML code. B614 flags
torch.load()10; Ruff has no such rule23. - pip-audit. It checks environments, requirements and, with
--locked,pylock.*.tomlfiles against PyPI or OSV advisories24.
What changed in 2026
- Python 3.10 is end of life. Support ended on 1 October 2026, and 3.15.0 shipped on 9 October2.
- A CA compromise reached Python's trust stores. After October's emSign incident, install certifi 2026.10.19, pip 26.2.2 and uv 0.13.6 or later in every virtual environment, move to Python 3.11.18, 3.12.16, 3.13.18, 3.14.10 or 3.15.2 and rebuild images without cached layers3.
What you can skip
- defusedxml for standard library parsers. Expat doesn't read local files or open connections by default, and Python's XML docs warn of entity expansion only for Expat before 2.7.227. Check
pyexpat.EXPAT_VERSIONin CI and turn off S313 to S31923. - Noisy lint rules. Turn off S603, which fires on every
subprocesscall without a shell, and preview rules S403 and S404, which fire on any import ofpickleorsubprocess23. - A
find_classallowlist for pickle. Change the format. PyTorch's own restricted unpickler needed a code execution fix in 20268.
Mistakes that cost the most
- Shipping with the debugger on. Flask's debugger runs Python sent from the browser28. Django's
DEBUG = Trueleaks source excerpts and settings; runmanage.py check --deployin CI29. - Trusting FastAPI's
OAuth2PasswordBearer. It only extracts the token. Verify the signature,exp,issandaudwith PyJWT and a fixedalgorithmslist22. - Ignoring
.pthfiles. Theirimportlines run at every interpreter start, as do the.startfiles Python 3.15 adds to replace them25. The malicious LiteLLM 1.107.0 shippedcook.pthandsitecustomize.py26. The supply-chain sheet covers the rest.
What this looks like for your code
New project
- Start on Python 3.14 or 3.15, where tar extraction uses the
datafilter by default17. - Turn on Ruff's
Srules and pip-audit in the first CI pipeline. - Give every input type a pydantic model with
extra="forbid".
Existing codebase
- Move every image off Python 3.10 or older first.
- Fix Ruff's S301, S506, S602 and S608 hits in code that handles outside input first.
- Set
TORCH_FORCE_WEIGHTS_ONLY_LOAD=1wherever models load7.