r/webdev • • 12d ago

Microsoft's official agent-framework PyPI package (190K downloads/mo) was compromised with .pth startup hooks — why are we still running AI agents in flat site-packages?

Versions 1.26.1 and 1.26.2 of Microsoft's official agent-framework package on PyPI were hijacked yesterday (published 33 minutes apart) with a credential stealer. PyPI quarantined both releases quickly, but the mechanics of this attack should bother anyone building AI agent runners or Python backends.

The payload didn't wait for import agent_framework. Both wheels dropped .pth files (agent_framework_meta.pth and af_runtime_diag.pth) directly into site-packages. Because Python executes import lines inside .pth files on every interpreter startup, running python, pytest, or even pip itself triggered the stealer in 0 ms—even in projects that never imported the framework. Worse, it still pulled agent-framework-core as a dependency so applications kept running without throwing a single error.

On top of reading static files on disk (.env, .npmrc, pip.conf, .netrc, .git-credentials, .docker/config.json, Hugging Face tokens, and ~/.azure/*.json), the stealer actively minted new cloud credentials at runtime: - Querying Azure IMDS (169.254.169.254), IDENTITY_ENDPOINT, and MSI_ENDPOINT for Managed Identity access tokens. - Using SYSTEM_ACCESSTOKEN and SYSTEM_OIDCREQUESTURI in Azure DevOps to request federated api://AzureADTokenExchange tokens. - Harvesting GitHub Actions OIDC tokens in 1.26.2.

Everything was compressed, Base64-encoded, and POSTed out. Rotating static secrets on disk doesn't cover tokens minted dynamically during a CI run.

We spend weeks arguing about LLM prompt injection guardrails, yet most dev laptops and CI runners still install agent dependencies into flat, unpinned environments with unrestricted outbound network access.

Are you enforcing --require-hashes and strict egress allowlists on your Python agent runners yet, or is everyone still trusting pip install by default?

23 Upvotes

23 comments sorted by

7

u/Patient_Celery_2406 12d ago

Where does the 190k come from anyway? PyPI download counts include every CI rebuild and mirror pulling the same wheel, so that number says nothing about how many environments actually got the bad versions.

1

u/Delicious_Character6 11d ago

PyPI download counts are mostly CI rebuilds and mirror syncs, which is actually the worst-case blast radius for a .pth startup hook. Those ephemeral build runners aren't harmlessly inflating a vanity metric—they're holding your OIDC tokens and deploy keys, and site.py executes the payload the millisecond Python initializes before agent_framework is even imported.

Enjoy the compromised CI runners :)

1

u/kockuma 11d ago

Cuts the other way too though. a lot of those downloads arre CI runners rebuilding and runners are where the azure devops and github OIDC token sit,which is what this thing went after.We only had it on one internal repo , the dependancy list made that quick to check.Then rotated that runner's creds anyway even though the lockfile says 1.25.didn't trust that every job uses the lockfile

1

u/Delicious_Character6 11d ago

Rotating those creds was the only real move, because .pth hooks execute on Python interpreter startup—not on import—so any stray linter, build script, or unpinned pip invocation in the pipeline fires the payload straight into your OIDC environment. Giving build-time site-packages ambient access to deployment identity tokens turns every unpinned CI step into a silent exfiltration path. Lockfiles can't fix ambient trust :)

1

u/Sweet-Structure-9298 11d ago

I would surely be a major contributor I have a lot of rebuilding😭

3

u/Ok-Snow-6553 12d ago

This is absolutely terrifying. I'm a solo dev and I've always just trusted pip install blindly. I never even knew .pth files could execute code on startup like that.

It feels like keeping up with AI tools is already hard enough, and now we have to worry about supply chain attacks too. Might be time to finally look into --require-hashes for my projects.

7

u/fiskfisk 12d ago

It's time to drop pip and instead go for something like uv; it has features like --exclude-newer which means that you'll never install packages (unless you explicitly require them) that are more recent than 1 day for example.

pip also supports this directly now through the argument --uploaded-prior-to=P1D, but juggling all the manual parameters and hashes and not really having decent lock file support, etc. gets tiring very quickly.

Switch to uv. It's a new world.

1

u/Ok-Snow-6553 11d ago

I've heard a lot of buzz about uv recently but haven't tried it yet. The --exclude-newer feature sounds like exactly what I need to avoid these zero-day supply chain attacks. Just checked the docs and the lockfile support looks way better than pip. I'll give it a try this weekend. Thanks for the tip!

2

u/fiskfisk 11d ago

It's far better for actually managing projects instead of just trying to wrangle dependencies from pypi. For small one-off scripts you can also use inline dependencies where uv will run the script and make the dependencies available to the script in the background, instead of having to work with a new virtual environments for every utility script you have laying around.

You can also manage the python versions each project should use through uv, and have that defined as part of the pyproject.toml file that lives with your project, so you know whether you'll be running under 3.14, 3.15 or 3.8.

0

u/[deleted] 11d ago

[deleted] — view deleted comment

1

u/webdev-ModTeam 11d ago

Your post/comment has been determined to be a low-effort post or comment. This includes title-only posts, easily searchable questions, vague/open-ended discussion prompts, LLM generated posts or comments, and posts/comments that do not provide enough context for meaningful replies or discussion.

0

u/[deleted] 11d ago

[deleted] — view deleted comment

1

u/fiskfisk 11d ago

Sorry, Mr,/Ms./Mrs. LLM, but if you want to have a longer period like P3D, extend the delay. Most compromised dependencies are caught within minutes or hours, not days.

It's all about reducing the attack surface. Something like 1D or 3D is a good compromise between getting updates quickly and not being exposed to most situations with compromised dependencies.

1

u/Delicious_Character6 11d ago

--require-hashes` only freezes whatever payload was already in the wheel the day you generated your lockfile—it does nothing when an upstream CI token gets popped in an AI framework shipping daily releases. You just get reproducible pwnage.

Running agents with live API keys in a flat virtualenv means a .pth hook exfiltrates os.environ during site.py init before your main() even executes, so your only real defense is an ephemeral container with zero host mounts and strict egress rules. Lockfiles won't save you :)

2

u/leweir 11d ago

The .pth trick is a good reason to stop treating the venv as the boundary and treat the whole run as disposable: a fresh container per job, nothing from your home dir mounted, and no long-lived creds on disk to read. Block 169.254.169.254 and the other metadata endpoints from agent containers unless the job actually needs them, because that's what turns a file stealer into a cloud token minter. Pinning with hashes (--require-hashes or a uv lockfile) plus a short wait before adopting new releases would have skipped both bad versions here. It's also worth grepping site-packages for .pth files with import lines now and then, since legit ones are rare.

2

u/Conscious-Part-9303 10d ago

why are we still letting arbitrary .pth files execute in production containers?

2

u/PLBjt 12d ago

The nasty part is the .pth file, not the import. Python runs lines inside those on every interpreter start, so pytest or even pip in that env executes the payload whether your code ever imports the framework or not. I'd pin the wheel by hash and fail the install if a new release adds a .pth you didn't already expect, then glance at site-packages after the fact instead of trusting that you never call the library. The tradeoff is a hash pin means a real security release doesn't land until you bump it, so review the file list diff and not just the changelog.

1

u/Traditional-Hall-591 12d ago

Are we sure AI didn’t put the malware in itself.

1

u/Federal_Dish4057 12d ago

the real lesson is that installing a wheel is already giving it code execution, and .pth files make that painfully obvious, so agents need the same isolation and dependency pinning as any other

1

u/tanyadev4827 12d ago

relevant

1

u/worldgorger1 12d ago

Honestly fuck AI. If you see any posts praising it or glorifying it, please downvote.