r/Python • • 13d ago

Discussion Microsoft's PyPI package got compromised without a GitHub code change. How is this even possible?

I was reading about yesterday's Microsoft Agent Framework security incident, and something caught my attention.

Apparently, attackers published two malicious versions (1.26.1 and 1.26.2) of Microsoft's official Python package on PyPI. These versions could steal cloud credentials just by starting Python.

Both versions were removed quickly.

What surprised me was that the malicious code wasn't even checked into Microsoft's GitHub repository.

From what I understand:

- Developers write code in GitHub.

- CI/CD builds it into a Python wheel (.whl).

- The wheel gets published to PyPI.

- When we run "pip install", we download that package.

But apparently, someone with the right publishing credentials can upload a malicious wheel directly, without going through the original GitHub repository.

That raises some interesting questions:

  1. How does PyPI verify that a package actually came from the legitimate source code and CI/CD pipeline?
  2. Why isn't a verified GitHub commit or build provenance mandatory for publishing updates to established packages?
  3. If publishing credentials are compromised, what prevents attackers from distributing malicious versions under trusted package names?
  4. Would mandatory attestations, release signing, or an approval process have prevented this?
  5. What protections should developers realistically use beyond simply trusting "pip install"?

I'm still learning about software supply-chain security, so I'd love to hear how experienced Python developers and security engineers think this should work.

268 Upvotes

57 comments sorted by

View all comments

Show parent comment

3

u/enterthearena44 13d ago

Thanks, that makes sense. I hadn't realized PyPI allows direct publishing independently of GitHub.

The dependency cooldown is an interesting approach. But I'm curious: shouldn't there be some way to verify that a published wheel actually came from the project's approved source code and build pipeline, regardless of whether they use GitHub or something else?

Would build provenance or trusted publishing help prevent something like this?

17

u/latkde Tuple unpacking gone wrong 13d ago

PyPI does have some provenance features. A Trusted Publishing workflow can optionally provide Attestations, which binds the PyPI release to Provenance information such as a specific GitHub commit. Relevant docs:

However, it's perfectly fine and secure to not use this. The absence of attestations is not a security problem. For many (cultural) reasons, this will never become mandatory. GitHub must never become mandatory, and self-hosted forges (like GitLab/Gitea/Forgejo instances) are generally not secure/tamper-resistent enough to be eligible as a Trusted Publishing provider.

A potential warning sign is when a project used to use Trusted Publishing in the past, but then stopped. Therefore, PyPI will show provenance-related warnings in some scenarios: https://docs.pypi.org/using-pypi/package-security/provenance/#provenanceattestation-warnings

These warnings are only shown on the PyPI website. There's currently no good way for package managers like pip to surface this information. PyPI does offer an API that makes attestations available on a per-file basis, but this is a rather low-level view. All of this is pretty new, so tooling will likely improve in the future.

11

u/Apprehensive-Cod699 13d ago

trusted publishing helps, but only if the project also deletes its old api tokens, otherwise a leaked token still works. files published that way also get a provenance attestation (PEP 740), and the file page on pypi shows the exact repo, workflow and commit it was built from. the catch is pip doesn't check any of that when installing, so for now it's something you can look at, not something that blocks a bad wheel

5

u/yvrelna 13d ago

In theory, they could make a verified build system where PyPI would pull source code from a Git repository, it doesn't need to be GitHub, then run a build script. 

But that would be much more complex and expensive to run than a simple blob distribution system which runs mainly on untrusted CDN and mirrors, while a build system would require actual compute resources. It would make the build system part of the security chain. Someone would need to build the system and fund that, and figure out all the security. 

The main security mechanism of PyPI is for you to verify the cryptographic signature of the package. With cryptographic signature, PyPI doesn't need to be part of the security chain. The PyPI infrastructure itself can and should be considered untrusted. 

3

u/AlSweigart Author of "Automate the Boring Stuff" 13d ago

I mean, pip lets you can bypass PyPI altogether and install directly from GitHub:

pip install git+https://github.com/username/repository.git

Of course, now the problem is that you have to trust GitHub. There's no silver bullet for this problem.

22

u/keypusher 13d ago

separation of concerns. spend some time on CS fundamentals and understanding unix philosophy. pypi is a package manager. it cares about hosting packages well and validating the credentials guard publishing. it does not, and should not, integrate deeply with other systems or for-profit companies such as GitHub. it does not care if your code is hosted in git, svn, hg or uses no version control at all. if YOU want to add specific approval steps and enforcement rules to YOUR project, fine. Great. But no, that is not PyPi’s job.

9

u/Wurstinator 13d ago

Not only is this is an unnecessarily condescending comment, it's also completely wrong. Instead of telling others to study CS fundamentals, you should study some real world software engineering instead.

https://docs.pypi.org/trusted-publishers/adding-a-publisher/

0

u/RoadsideCookie 13d ago

There is a way and Microsoft ignored it.