10 Common Python Security Vulnerabilities and How to Fix Them
This article outlines ten frequent security flaws in Python—from input injection and unsafe XML parsing to misuse of assert statements and vulnerable third‑party packages—explaining how each can be exploited and providing concrete remediation techniques to write safer code.
Writing secure code is challenging; many Python developers are unaware of common pitfalls in the standard library and third‑party packages.
1. Input Injection
Injection attacks such as SQL injection, command injection via popen, subprocess, or os.system can occur when user‑controlled data is concatenated into queries or shell commands. Use ORM sanitization, framework utilities, or shlex for proper escaping.
2. Unsafe XML Parsing
Parsing untrusted XML can trigger denial‑of‑service attacks like the “billion laughs” payload or external entity expansion, allowing attackers to consume massive memory or access internal resources. Replace vulnerable standard modules with defusedxml to mitigate these risks.
3. Misuse of assert Statements
Using assert for runtime security checks is unsafe because Python can be started with optimizations that skip these statements, potentially exposing privileged code paths. Reserve assert for testing only.
4. Timing Attacks
When password comparison is performed with naive string equality, an attacker can measure execution time to infer correct characters. Use secrets.compare_digest (Python 3.5+) for constant‑time comparisons.
5. Compromised site‑packages or Import Path
Installing malicious packages that mimic popular names or injecting code via the import system can give attackers arbitrary execution. Employ virtual environments, audit dependencies with tools like PyUp.io, and verify package signatures.
6. Insecure Temporary Files
Creating temporary files with mktemp() is vulnerable to race conditions; another process may replace the file between name generation and opening. Use tempfile.mkstemp() or higher‑level tempfile utilities instead.
7. Unsafe yaml.load
Loading untrusted YAML with yaml.load can execute arbitrary objects, similar to pickle.load. Always prefer yaml.safe_load unless a trusted schema is guaranteed.
8. Pickle Deserialization Vulnerabilities
Deserializing untrusted data with pickle allows execution of arbitrary code via the __reduce__ protocol. Never unpickle data from unknown sources; use safer formats like JSON.
9. Using Out‑of‑Date System Python
System‑provided Python interpreters (often Python 2) may contain known C‑level memory safety bugs. Regularly upgrade to the latest Python releases to obtain upstream security patches.
10. Unpatched Dependency Packages
Third‑party libraries can harbor vulnerabilities; relying on fixed versions without monitoring updates is risky. Use dependency‑checking services (e.g., PyUp.io) and automation tools like InSpec to verify that installed packages are free of known CVEs.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
