r/Python • • 4d 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.

244 Upvotes

54 comments sorted by

215

u/Deto 4d 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.

107

u/Atmosck 4d 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"

19

u/morsmordr 4d ago

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

55

u/Atmosck 4d 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

1

u/omg_drd4_bbq 2d ago

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

4

u/nemec 3d 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.

5

u/Black_Magic100 4d ago

azure devops artifacts does not support this. Fuck microsoft

-5

u/RedEyed__ 3d ago

And attackers will just delay for 7 days..

7

u/Atmosck 3d ago

This is for if they publish a compromised version of a package. Waiting on that does nothing, the 7 days start when it's published.

If they do manage to modify an already-published package in-place to insert something, that's covered by your lockfile because it changes the checksum of the artifact. Any setup that guards the checksum, which uv does by default, won't pull a package that was changed in-place.

3

u/enterthearena44 4d 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?

16

u/latkde Tuple unpacking gone wrong 4d 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 4d 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

4

u/yvrelna 4d 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" 3d 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.

21

u/keypusher 4d 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 3d 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 4d ago

There is a way and Microsoft ignored it.

20

u/RoadsideCookie 4d 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 4d 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 3d 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 3d 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 2d 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 2d 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 2d 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.

3

u/berndverst 2d 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 2d 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 2d 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 :)

85

u/aplarsen 4d ago

PyPI is older than Github. It's older than Git itself.

34

u/Gamecrazy721 4d ago

I expected you to be wrong about this but nope, Torvalds first released git in 2005 and pypi has been around since 2003.

2

u/RedEyed__ 3d ago

Wow, it is true!

43

u/XNo_Notes It works on my machine 4d ago

PyPi is a repository itself. You can upload strait from your terminal if you have the access token associated with the account there. Github is a completely separate entity. 

20

u/hstarnaud 4d ago edited 4d ago

"Developers write code in GitHub". That's not quite it, GitHub is just one tool amongst many to publish code. It just happens to be a popular tool.

  1. The criteria to know what is the legitimate source code is the private key / login credentials of the publisher.
  2. Why isn't a verified GitHub commit mandatory? GitHub is just a different organization, they have nothing to do with pypi's criteria for what is a legitimate commit the source code could be hosted exclusively on pypi or anywhere else for that matter. Git (the open source tool used by GitHub and most repo platforms) is a tool to version code, there is a way to sign commits to authenticate who wrote them, but in the end, if you have a private key to log in to pypi authenticated as an authorized publisher, why should they care if you sign your commits or not, you are signed in as the administrator, it's your repository, you manage the content.
  3. Publishing credentials is essentially what prevents attackers to distribute malicious versions in a trusted package. It's like asking, if someone stole the key to your house, what prevents them from opening it. You have maybe cameras or alarm systems but it doesn't prevent anyone from unlocking the door and going in.
  4. what you are asking is essentially the same thing as saying, would authenticating with a private key prevent this. Yes, that's already the case. Should we require two keys to sign in rather than one? Yeah they probably have two factor authentication. Should we have a third factor in the authentication? I mean... You get it
  5. What protections should developers use is a very interesting question deserving a more lengthy answer:
  • First thing, adopt a policy to never install things published less than one week ago, most vulnerabilities are detected rapidly. Most package manager support applying a configuration to do that.
  • use lock files instead of continuously updating versions
  • audit your dependencies using reputable auditing tools. Do so every few weeks in an automated way and verify when updating versions
  • move off abandoned packages
  • least privileged principle, don't run as admin a script that doesn't need to be admin
  • data segregation, store your data out of access of machines that run applications that don't need this sensitive data.

10

u/FastRunningMike 4d ago

"What protections should developers realistically use beyond simply trusting "pip install"?"

>> To answer this question: Always perform a SAST scan before installing or using ANY Python package. Never ever use pip install on anything before validating. PyPI does not validate weaknesses in the packages.

A package being available on PyPI does not mean that the package has been security-audited or that its content can be trusted. There is an important distinction between package availability, artifact integrity, artifact provenance, and software trustworthiness. [2]

A quick look at version 1.3(may 2026) reveals a very concerning Python package from a security point of view, already full of weaknesses that are very hard to mitigate in a decent way. E.g. lines like:

- result = subprocess.run(command, cwd=WORKSPACE_ROOT, check=False)

When using the subprocess library, Python code can invoke arbitrary external executables on the host system.

- getattr(importlib.import_module(IMPORT_PATH), name)

Allowing dynamic module imports makes it easy for attackers to execute arbitrary code.

I count 41 places where importlib.import_module occurs. Anyone who checked this package beforehand with a simple good Python SAST [1] that works directly on PyPI packages should think again before using it.

[1] E.g. https://pypi.org/project/codeaudit/

[2] https://nocomplexity.github.io/pythonsecurity/pypi-security/#packages-on-pypi-are-not-secure-by-default

12

u/MrSlaw 4d ago

Essentially just described a list of reasons why people (and especially corporations) should be using trusted publishing for automated releases.

Prevents a rogue API key spoiling your day at least

18

u/Tall-Introduction414 4d ago

Not everyone uses GitHub. I publish to pypi from my terminal with uv.

4

u/DigThatData 3d ago edited 3d ago

How is this even possible?

...

someone with the right publishing credentials can upload a malicious wheel directly

bro you answered your own question.


the unasked question you should be asking here is "what could microsoft have done differently to prevent this?" and the answer to that is they should be regularly cycling their credentials. If they're already doing that, perhaps they could have increased the cycling schedule in light of the recent wave of CVEs.

We (the public) don't and probably will never know how the credentials were exposed, so we can't know if there are other mitigations that might have helped. For example, if this was an insider threat, then the appropriate mitigation might not even be on the engineering side at all but rather HR's responsibility.

2

u/blbd 3d ago
  1. It can't besides M2M and human auth credentials that can be pwned.
  2. Because there are zillions of packages with zillions of repo styles and the repo is sometimes pwned instead of the M2M credentials anyways.
  3. Nothing. 
  4. Code signing can make it easier to revoke but code signing certs and signed code get pwned in Windows all the time. 
  5. Supply chain, dependency, and SBOM security checkers like GitHub Dependabot and their other security tools. 

3

u/reluactivations 4d ago

a `.pth` file that runs before you even import anything. supply chain said "no need to call me, I'll let myself in"

1

u/Physical-Court-7453 3d ago

one thing worth adding for the "what can devs actually do" part: this one ran from a .pth file, so it fired on every python start, no import needed. and it went after CI stuff, pipeline OIDC tokens and managed identity tokens. cooldowns and lockfiles stop you installing it, but if something slips through anyway, it can steal whatever that process can reach

so on the CI side, split your jobs. the step that runs pip install and tests shouldn't have the same identity or token permissions as the step that deploys. on a dev machine, short-lived logins beat credential files sitting in your home dir

also a quick check you can run on any env: list the .pth files in site-packages. most envs only have a couple of known ones (setuptools' distutils-precedence.pth, editable installs), so anything random in there is worth a look

1

u/hornetmadness79 3d ago

I think you're overthinking this. Not everything has to be in GitHub and go through a cicd pipeline. It could be something as simple as a developer typing UV and then uploading the resulting wheels. Your idea of supply chain is more of a contrived idea around that basic process.

0

u/habitsofwaste 4d ago

I wonder if this was something to do with the ghostaction campaign n

-2

u/poppy_92 4d ago

Why isn't there a post on DPO on this? The newly elected PPC along with PSRT teams should be looking at these with high priority.

-7

u/real_carddamom 4d ago

Believe in this time and age Github sucks... It is only a frontend or sink for Copilot and company, which is sad

0

u/[deleted] 3d ago

[deleted] — view deleted comment

1

u/MustaKotka 3d ago

Provenance is the name of being able to trace back the path to current state from another prior state while being able to name the involved entities along the way. It's not an em dash. It's an actual word with a unique meaning. Rarely needed but absolutely necessary when needed.

1

u/cunningjames 3d ago

Provenance is an actual word that, yes, gets used in real life occasionally. This post does not especially ping my AI-dar overall, lacking the usual tells.

3

u/[deleted] 4d ago

[removed] — view removed comment

5

u/damesca 4d ago

Hash pinning doesn't help here.

6

u/OkCardiologist8006 4d ago

for this incident yeah, it was new version numbers, so an exact version pin in a lockfile is what actually protected people.

hash pinning isn't useless against a stolen token though. pypi won't let you reuse a filename, but it does let you add new files to an existing release, so an attacker could upload an extra wheel with a more specific platform tag under an old version and pip might prefer it. `--require-hashes` (or a uv/poetry lockfile, which records hashes) catches that.

1

u/damesca 4d ago

Ah that's a really interesting point and good clarification - don't think I realised that.

1

u/Wurstinator 4d ago

But the version string didnt match