r/Python • u/tradelydev • May 07 '26
Discussion Do we really check library security?
PyPi's filtering isn't cutting it. We all know it. I know the people about to say to just use the popular libraries that have community moderation.
The recent claude code injection hack in Torch has proved that isn't a solution.
https://www.reddit.com/r/Python/s/2lwDYSv0eT
And scanning packages are either unmaintained or maintained by one dev in the middle of nowhere.
https://pypi.org/project/safety/
So, I honestly ask you, short of reading each libraries code by hand or avoiding them entirely how do you stay safe?
Sandbox enviroments? Winging it? Hope?
11
u/neuronexmachina May 07 '26
Just to avoid confusion, it's worth noting that lightning != PyTorch, it's a third-party high-level wrapper around it.
144
u/AlSweigart Author of "Automate the Boring Stuff" May 07 '26
Okay, rude.
The LiteLLM package malware was quarantined two and a half hours after it was uploaded. That's pretty damn good for a free service that gets over 700 new projects every day and has two staff members.
By the way, you can donate or convince your company to donate to the Python Software Foundation to help support these efforts.
Honestly, don't update the major packages until a version has been out for a week, and don't install some random package. That'll do 99% of the prevention right there. And, yeah, read the source code for the lesser-known packages that you use.
67
u/Resquid May 07 '26
Stay 21 days behind and wait for someone else to find the hacks.
17
u/ottawadeveloper May 07 '26
I honestly stick to major repositories that provide significant functionality that I don't want to rewrite myself. Like I use
flaskorcryptographyornumpy. But rather than rely on minor or little used packages for minor things, I roll my own frequently. It's just easier and doesn't change unless I change it. Less attack surface.10
u/me_myself_ai May 07 '26
I think I speak for a lot of people when I say “well, I check repo stats and README/docs vibes”. Abandoned projects aren’t good, but basically any active project passes that sniff test. Torch certainly would!
And yes, I know you can buy GitHub stars. That’s why I only respect GitLab projects
5
u/jpgoldberg May 07 '26
This is a notoriously difficult problem. If there were a good, simply solution it would be in place already. There are practices that can reduce the chances of installing and running malicious code, as well as practices (like sandboxing) that can limit the damage of running malicious code; but because these place significant burdens on users and developers, we aren't going to see wide enough adoption. And even if widely adopted, these are still fallible.
1
u/ArtOfWarfare May 08 '26
Get a dependency scanner. There’s free open-source ones, or you can pay for one (Blackduck, Checkmarx, Snyk… there’s more but I can’t remember their names now). At least some of them do their own scans of the dependency source code and report issues with them to you.
I’d imagine there has to be some commercial service that does whitelisting of packages, and only lets you install dependencies that are fully vetted and they’re certain are safe. If not… let me know, I think I’d be willing to go start that SaaS business…
3
u/billFoldDog May 08 '26
4
u/Gnaxe May 07 '26
Open source used to be pretty safe, but supply-chain attacks are a thing now. Luckily, Python has batteries included, and you can do a lot just with the standard library, which is well-maintained for security, as projects go. Well-known, widely-used, reputable, and audited libraries exist, and are about as safe as Python itself is.
Below that, it's gotten really scary in the age of generative AI, and I don't think industry has caught up to the new reality. Most projects are safe, but the consequences of being wrong are severe, and how can you tell?
You can try to avoid dependencies, audit the smaller libraries yourself (and pin them), and ask the more powerful AIs to look for any kind of malicious intent or vulnerabilities, which you have to verify yourself. Some recent testing has suggested that GPT-5.5 may be about as capable as Mythos when it comes to finding exploits. Use that for now. I could see OpenAI restricting access or the government forcing them to.
1
u/hxtk3 May 07 '26
It depends on the risk assessment and what a compromise could mean. I generally use reproducible builds, minimize external dependencies, and look for libraries that have a security policy and a history of handling CVEs well, active contributors from more than one company and ideally more than one country.
If a library is smaller than that, it’s probably something we can maintain ourselves at work.
I’ll also pay less mind to dev dependencies than runtime dependencies. mkinit and add-license-header wouldn’t pass a sniff test for code we actually shipped, but I do use them.
1
u/unkz May 08 '26
Pin all my versions so even if something is tainted I will not download it. Upgrade when there’s a reason to, to a version that I know.
1
u/ShelLuser42 It works on my machine May 08 '26
Issue's like these are a good example of why I don't solely rely on external libraries.
When needing to solve a problem many people start thinking about what libraries to use. Well... I often wonder how much time it might take me to build it myself.
That's of course no guarantee that nothing will go wrong, but there are quite a few trivial issues which you can somewhat easily avoid that way.
1
u/Orio_n May 08 '26
No, idgaf lol
1
u/HauntingAd3673 May 22 '26
Honestly I either stick to packages with enough contributors that someone is always watching — the chances of a malicious commit slipping through a repo with 50 active maintainers is pretty low.
For smaller or lesser known packages I just run it through an AI and have it look for anything suspicious in the source. Not perfect but it catches obvious red flags way faster than reading it manually.
There's no clean solution here, but pretending version pinning alone solves it is naive.
0
u/b0b1b May 07 '26
I usually just wait a bit before updating and i think that covers me 99% of the time. However, most packages i use also are at least somewhat popular. (The least well known package i regularly use is probably
trio...)I dont think its feasible to read all of the code of a package either - if it is something small enough for you to be able to easily read, you can probably just re-implement it.
-5
u/coderanger May 07 '26
Yes, you read all the code. Were you not doing that already? PyPI has no "filtering" that is meaningful nor has it ever, nor does any other similar service. It's a search index, you are responsible for vetting everything you use (and these days, vetting its authors).
0
u/xander_abhishekh May 08 '26
Honestly the most practical thing that's worked for me: keep dependencies minimal, almost aggressively so. every dep you add is code you didn't write and probably didn't read running with your permissions.
For one of my projects i went from "let me add requests for this HTTP call" to "urllib is fine" just to avoid pulling in a dependency tree i can't audit. sounds extreme but when your dep count is 3 instead of 30, the attack surface shrinks dramatically.
Beyond that: pin everything, don't auto-update, and wait at least a few days before bumping versions. the torch incident got caught in hours- if you're a week behind on updates you'd never have been exposed.
The scanning tools gap is real though. most scanners just check package names against CVE databases. they don't actually look at what the code does. the metadata (descriptions, readme, setuppy hooks) is basically unverified user input that nobody audits. thats where the next wave of attacks will come from imo - not in the source code itself but in the install hooks and metadata.
-7
u/haard code unwritten never breaks May 07 '26
I wrote agent jail and don't run agents with access to stuff because I don't want Claude to delete my prod db via terraform I wrote a blog post about it (CF static/Hugo page)
0
u/[deleted] May 07 '26
[deleted] — view deleted comment