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

231

u/Deto 13d ago

It's always worked like that.  You don't have to publish directly from GitHub.  You can just publish from your source directory on your machine. 

Yes if the publishing key is compromised this means you're in trouble. 

They probably don't want to require a GH commit because that would force all devs to use GitHub.  You could imagine supporting a few alternatives but all that puts more burden on the pypi devs and the package repo is maintained on a shoestring budget. 

One things devs can do is require a version to be live for a certain amount of time before it is used in code. Basically relies on the fact that these things are usually caught within a day or two.  It's not perfect because still some people will have to get affected.

114

u/Atmosck 13d ago

Your last point is important, and should be standard practice for anything using third-party packages from pypi. It's easy to configure with uv in pyproject.toml:

[tool.uv]
exclude-newer = "7 days"

21

u/morsmordr 13d ago

wouldn't that also prevent you from getting security patches asap too though?

60

u/Atmosck 13d ago

You can configure exceptions for security patches, like this:

[tool.uv]
exclude-newer = "7 days"
# certifi 2026.10.19 drops the root CA leaked in the eMudhra incident
constraint-dependencies = ["certifi>=2026.10.19"]

[tool.uv.exclude-newer-package]
# certifi 2026.10.19 is inside the 7-day window until 2026-10-27, so exempt it for the constraint above to resolve
certifi = false

If you explicitly list certifi as a dependency you can constrain it's version there, but constraing-dependencies covers transitive dependencies, like if you have basically any http library it probably depends on certifi

3

u/omg_drd4_bbq 11d ago

That's a great trick! i know what i'm setting up monday

4

u/nemec 13d ago

As mentioned in the other comment, you can manually override that for specific packages/versions. There's no perfect solution, but automatic patching within a few days is probably better for most people than being pwned within minutes/hours of a malicious upload. And devs working on stuff that isn't released/deployed can benefit from the delay but benefit much less from having immediate security fixes for code that doesn't take untrusted input.

If you're a big company you should probably set up a package proxy with a default delay of a week, auto malware scanning before making packages available, and an override to ingest security critical updates so that you can override once and everybody in your company picks it up without further delay. But that's overkill for the hobby devs.

→ More replies (1)

5

u/Black_Magic100 13d ago

azure devops artifacts does not support this. Fuck microsoft

→ More replies (2)

20

u/RoadsideCookie 13d ago

Microsoft is absolutely not following best practices here. It hasn't always worked like that because there's a better way now. 

You can link the pypi lib to a GitHub project and there is no key to steal, the project itself is trusted, so only a push can trigger a new release. 

If Microsoft weren't incompetent, that breach would never have happened.

9

u/Individual-Flow9158 13d ago

Enabling Trusted Publishers, doesn't revoke all your API keys, certainly not a user's master one (used to create new projects).

Can Trusted Publishers revoke project specific API keys?

1

u/RoadsideCookie 13d ago

I don't know if it can, but you can absolutely revoke them.

Best practice involves using trusted publishers and revoking keys.

In fact, automatically revoking keys would add needless weight to the flow, so I doubt that just enabling trusted publishers revokes anything; I'd check manually if I were them.

2

u/berndverst 12d ago

You are not totally correct here. We have seen GitHub token credential stealers use maintainer tokens (sometimes freshly minted via GitHub CLI) to temporarily alter branches, trigger CI, exfiltrate credentials or trigger release pipelines.

You also have to ensure that no single person can perform a release by themselves!

2

u/RoadsideCookie 12d ago

Fair. I personally block main, and require approval from my specific GitHub account for any job that can publish, including the trusted publisher setup.

3

u/berndverst 12d ago

You are dealing with repos that have 20-30 or so people with write access involved here (of course with branch protection rules etc). It gets just a bit more complex to secure things while not making it harder to get work done.

1

u/RoadsideCookie 12d ago

I agree. I should've been clearer that my last message was not a proposed solution, just how I dealt with the caveat that you raised.

4

u/berndverst 12d ago

I think I understood that :)

By the way, I work at Microsoft and use this ESRP system to publish to PyPI 😅. I have no choice but to use that approach - all repos are required to publish this way, which leads to the official Microsoft PyPI account being the final publisher. Even though my projects aren't involved - I know the project being discussed here.

At the moment the entire centralized publishing approach for Python is under review. I have no insight - but publishing of my own packages is blocked for the time being.

I'm confused how this incident happened myself. Likely a repo-specific misconfiguration.

2

u/RoadsideCookie 12d ago

Thanks for the insight, it's great to know!

By the way, I feel like I should also clarify: when I said Microsoft are incompetent, I don't mean individuals working at Microsoft; I mean Microsoft as an organization is incompetent.

I have absolutely no doubt that individual contributors are competent and could fix all the issues if they were given the opportunity.

3

u/berndverst 12d ago

No offense taken. There are a lot of idiotic bureaucratic systems at our company. Most individuals are great, but fighting the bureaucracy can burn you out :)

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.