r/Python • u/syllogism_ • Mar 25 '26
Discussion Don't make your package repos trusted publishers
A lot of Python projects have a GitHub Action that's configured as a trusted publisher. Some action such as a tag push, a release or a PR merge to main triggers the release process, and ultimately leads to publication to Pypi. This is what I'd been doing until recently, but it's not good.
If your project repo is a trusted publisher, it's a single point of failure with a huge attack surface. There are a lot of ways to compromise Github Actions, and a lot of small problems can add up. Are all your actions referencing exact commits? Are you ever referencing PR titles in template text? etc.
It's much safer to just have your package repo publish a release and have your workflow upload the release artifacts to it. Then you can have a wholly separate private repo that you register as the trusted publisher. A workflow on your second repo downloads the artifacts and uploads them to Pypi. Importantly though don't trigger the release automatically. You can have one script on your machine that does both, but don't let the Github repo push some tag or something that will automatically be picked up by the release machinery. The package repo shouldn't be allowed to initiate publication.
This would have prevented the original Trivy attack, and also prevented the LiteLLM attack that followed from it. Someone will have to actually attack your machine, and even then they have to get into Github 2fa, before they can release an infected package as you.
Edit: This has been more controversial than I expected. Three things.
- Pypi trusted publisher is undoubtedly better than using tokens. Definitely don't add a Pypi token to your repo.
- The main point is to make the boundary easy to reason about. "What can cause a tag to be pushed to my public repo" is a very diffuse permission. If you isolate the publication you have "What can trigger this workflow on this private repo nothing touches". That's much more restricted, so it's much easier to ensure no unauthorised releases are pushed to Pypi.
- If something compromises the actual code in the repo and you don't notice, then yeah it doesn't really matter what your release process looks like. But life is much easier for an attacker if they can commit the exploit and immediately release it, instead of having to rely on it lying dormant in your repo until you cut the next release.
54
u/ManyInterests Python Discord Staff Mar 26 '26
It's definitely hazard-prone, but if you follow PyPI's guidance on how to configure this, you should be fine.
You don't need a separate repository.
Just configure a dedicated PyPI release environment in the GitHub settings, add yourself as a required approver for that environment, and configure PyPI to only accept uploads from that environment.
47
u/latkde Tuple unpacking gone wrong Mar 25 '26
There are different aspects of security. A hyper secure airgapped workflow is pointless if it's so cumbersome that I don't use it.
The "trusted publisher" approach is a big improvement over the previous best practices: there are no credentials to manage, thus no credentials that could be compromised. It also helps link the PyPI page back to the repository ina verifiable manner, which offers important transparency for downstream users.
You raise some points about automation and how releases should be triggered, but these are largely orthogonal to the issue of trusted publishing.
14
u/PurepointDog Mar 26 '26
The security issues you're pointing out here are not at all related to Trusted Publishing, as far as I can tell. They are strictly supply chain attacks, which can happen any number of ways
8
u/divad1196 Mar 26 '26
It's a bit annoying to see people invent security theories.
The point of this post is: if the repos didn't had the push pipeline in the public repos, it would be safe.
First, this is wrong. What they impacted was the build of the artifact, not the push. This would not have solved anything.
Secondly, an account was comprised. With the right account, you are not safe anymore.
It's a supply-chain attack. If you can add a dependency on the artifact it's done anyway. It's just a bit more obvious. Or you caan make your library and hope it gets used by people. Or compromise an existing popular sub-library as thes are many.
The attack could have been avoided if changes on CI files were more thourougly checked. Pretty sure there are or will be bots to detect such scenarios.
7
u/toxic_acro Mar 26 '26
It doesn't affect your points about this particular threat model, but I do want to point out one minor correction
The LiteLLM attacker directly uploaded the two malicious versions to PyPI themselves using stolen credentials, they did not get added to the GitHub repo.
The attackers also had access to the GitHub repo, but fixing this particular security hole would not have prevented the LiteLLM attack.
11
u/denehoffman Mar 26 '26
None of this matters if your GitHub gets hacked. Just don’t be an idiot with Actions. If you have things setup in a stupid way that allows for releases to be made without your permission, that’s entirely your fault.
7
u/kenflingnor Ignoring PEP 8 Mar 26 '26
safer doesn't mean better
-3
u/ComfortableNice8482 Mar 25 '26
good call on this. i'd add that you can mitigate a lot of the risk by using a separate trusted publisher setup where a dedicated CI/CD repo or even a manual release script handles the actual pypi upload. the main repo just builds and tests, then you manually trigger or gate the actual publish step. i've seen projects do this where they keep their release credentials completely separate from their main workflow, which cuts down the attack surface significantly. it's a bit more manual but way safer if your project has any real users depending on it.
6
u/[deleted] Mar 26 '26
[removed] — view removed comment