Scan Your Python Packages for Known Vulnerabilities (Before Attackers Do)
Here’s a threat most developers underestimate: your dependencies. Your code can be flawless, but if one of the 200 packages in your requirements.txt has a known remote code execution flaw, attackers don’t need to touch your code at all. Supply-chain attacks — malicious or vulnerable packages pulled straight from PyPI — are rising fast. This week alone saw a popular AI library ship a compromised release. The defense starts with one habit: audit what you install.
Meet pip-audit
pip-audit is a free tool (from the Python Packaging Authority ecosystem) that checks your installed packages against vulnerability databases and tells you exactly which ones are exposed — and which version fixes each flaw.
pip install pip-auditpip-audit
That’s it. You’ll get output like:
Found 2 known vulnerabilities in 1 packageName Version ID Fix Versionsflask 2.0.1 PYSEC-2023-0001 2.2.5,3.0.0
Each finding includes the package, your installed version, the vulnerability ID, and the fix versions — the minimum upgrade that closes the hole. No guesswork.
Audit a Requirements File (Don’t Even Install It)
pip-audit -r requirements.txt
This checks the pinned versions in your requirements file without installing anything. Run it before you pip install a new project’s dependencies — especially for code you cloned from the internet.
Fix What You Find
pip-audit --fix
The –fix flag automatically upgrades vulnerable packages to safe versions. Review the changes afterward (upgrades can break things), then re-run the audit to confirm a clean bill:
pip-audit# No known vulnerabilities found
Make It Automatic
One-off scans get forgotten. Build the habit into your workflow:
- Pin your versions. “pip freeze > requirements.txt” — unpinned dependencies silently drift into vulnerable versions.
- Scan in CI. Add “pip-audit -r requirements.txt” to your pipeline so a vulnerable dependency fails the build instead of shipping.
- Scan on a schedule. New CVEs drop daily; a package that was clean last month might not be today.
- Prefer well-maintained packages. Check a package’s release history and maintainer activity before depending on it — abandoned packages don’t get security fixes.
The Bigger Picture
Vulnerability scanning handles known flaws. For malicious packages (typosquats like “reqeusts”, or compromised releases), add these habits: install from requirements.txt with –require-hashes for critical projects, be suspicious of brand-new packages with few downloads, and never pip install something from a random tutorial without checking what it is first.
You can’t secure what you don’t inventory. pip-audit takes ten seconds — and it’s the difference between shipping known vulnerabilities and catching them first.



Comments
Post a Comment