litellm was compromised on PyPI twice in 2026: how a .pth file steals secrets on every Python start
litellm 1.82.7 and 1.82.8 in March, then 1.107.0 and floating Docker tags in October, shipped code that harvested credentials. The October build used a .pth file that runs on every interpreter start, even without importing litellm. Here is how we found it, cleaned up and locked down our LLM gateway.
Our LLM gateway is a litellm proxy in front of OpenAI, Anthropic and Gemini, plus a few batch jobs that import litellm directly as a library. On 2026-03-24 the litellm repository got an issue titled "CRITICAL: Malicious litellm_init.pth in litellm 1.82.8 — credential stealer". By the afternoon PyPI had quarantined the whole project, and pip install litellm returned "No matching distribution found" for every version.
Seven months later, on 2026-10-21, it happened again with 1.107.0, this time including the floating Docker tags. Both times we had to answer the same questions: which hosts ran a bad version, what could the code read there, and how do we stop the next one.
Reproducing the exposure
We did not run the payload. We reproduced how each install would resolve:
# What a fresh, unpinned install would pick at a given moment
pip index versions litellm
# What is installed in each environment
python -m pip show litellm | grep -i '^version'
docker exec gateway python -m pip show litellm
The proxy containers were built from the official image pinned by digest, so the March release never reached them. The batch jobs were the problem: their requirements.txt said litellm>=1.80, and one job rebuilt its virtual environment nightly.
What each release did
From the two upstream issues:
Release | Date (UTC) | Mechanism | Trigger |
|---|---|---|---|
| 2026-03-24 | payload in |
|
| 2026-03-24 | same, plus | any Python start |
| 2026-10-21 |
| any Python start |
The March payload collected SSH keys, environment variables, cloud and Kubernetes credentials, database passwords and shell history, encrypted them and posted them to a lookalike domain. The October payload did the same and, when DATABASE_URL was set, dumped the proxy's own tables that hold upstream provider keys. Its sitecustomize.py also polled a remote "dead drop" for more code.
Root cause
For us: unpinned ranges plus nightly rebuilds pulled whatever was newest. Upstream, the March post-mortem in the issue attributes the publish to a hijacked maintainer PyPI account linked to a compromised security scanner in CI. The October release was built from attacker commits on a fork and pushed before the normal release workflow ran.
What is happening under the hood
Python's site module runs at interpreter startup. For every .pth file in site-packages it adds listed paths to sys.path, and any line that starts with import is executed. A one-line cook.pth containing import cook therefore runs cook.py every time anything starts Python in that environment: the app, a test run, pip itself, a cron job.
sitecustomize.py is similar: if a module with that name is importable, site imports it at startup. Neither needs your code to touch litellm. That is why "we only import the proxy module in one service" was not a safe answer for 1.82.8 or 1.107.0.
# What site does at startup, simplified
for line in open(pth_file):
if line.startswith(("import ", "import\t")):
exec(line) # runs with full interpreter rights
else:
sys.path.append(line.strip())
Environment
Component | Version / value |
|---|---|
Python | 3.12 and 3.13 |
Gateway | litellm proxy, official image pinned by digest |
Batch jobs |
|
Installer | pip 25, later uv 0.10 |
Secrets in env | provider API keys, |
Who is affected
Any environment that installed 1.82.7, 1.82.8 or 1.107.0, or pulled the floating Docker tags on 2026-10-21 before they were yanked. Clean October pin per the issue: 1.106.2. If a host was online while one of these ran, assume its readable secrets are gone.
The fix
1. Find the files. Search every site-packages, not only the app's virtualenv. Base images, CI caches and developer machines count:
python - <<'PY'
import site, sys, pathlib
dirs = set(site.getsitepackages() + [site.getusersitepackages()])
bad = {"litellm_init.pth", "cook.pth", "cook.py", "sitecustomize.py"}
for d in dirs:
for p in pathlib.Path(d).glob("*"):
if p.name in bad:
print("FOUND", p)
PY
sitecustomize.py can be legitimate (some distributions ship one), so read it before deleting. Then list every .pth file and review any line that starts with import:
find / -name '*.pth' -path '*site-packages*' 2>/dev/null \
| xargs grep -Hn '^import' 2>/dev/null
For containers, run the same search in each image you deployed, by digest:
docker run --rm --entrypoint sh ghcr.io/berriai/litellm@sha256:<digest> \
-c 'find / -name "cook.pth" -o -name "litellm_init.pth" 2>/dev/null'
2. Rebuild, do not clean. We found litellm_init.pth in the nightly batch environment. We deleted that environment and the runner image, rebuilt from scratch with a pinned version, and did the same for the CI cache that had stored the wheel.
3. Rotate what the process could read. For the batch host: every provider API key in its environment, the database password, and the cloud role. For the October release the issue also lists provider keys stored in the proxy database and the proxy master and virtual keys. We checked cloud audit logs for GetCallerIdentity and other calls from the role around the install time.
4. Pin with hashes. Ranges are what let the bad version in. We moved the jobs to a lockfile with hashes:
# uv: lock once, then install exactly what was reviewed
uv lock
uv sync --locked
# pip: compile a hash-pinned requirements file
uv pip compile requirements.in --generate-hashes -o requirements.txt
pip install --require-hashes -r requirements.txt
--require-hashes makes pip refuse any file whose hash is not in the requirements file, so a new upload under the same version number would also fail.
5. Add a cooldown. In pyproject.toml we set uv to ignore anything uploaded in the last week:
[tool.uv]
exclude-newer = "7 days"
Both malicious releases were removed within hours, so a seven day delay would have skipped them.
6. Pin images by digest. latest, main-latest and main-stable all pointed at the bad October build for a while. Our Kubernetes manifests and compose files now reference image@sha256:... only.
Alternatives and trade-offs
Run the gateway with fewer secrets in its environment. Mount provider keys from a secret store per request path where possible, and give the proxy database user only the tables it needs.
Private package mirror. Mirror reviewed versions only. More control, more work to operate.
Cooldown delays fixes too. For a same-day CVE fix use
exclude-newer-packageto exempt one package for one install, and review that version by hand.
How to test the fix
Drop a harmless test
.pthwithimport sys; print("pth ran", file=sys.stderr)into a scratch venv and confirm the scanner above reports it.In CI, edit one hash in
requirements.txtand confirmpip install --require-hashesfails.Publish-time test: run
uv lockin a branch withexclude-newer = "7 days"and confirm it does not pick a release from yesterday.
Confirming in production
After rotation we watched provider dashboards and cloud audit logs for usage from unknown networks for 30 days. One provider key showed calls from an address we did not own two days after the March install, before rotation finished; that key had already been revoked by the time we looked, and the provider confirmed no further use. After October no hits: by then every environment was pinned and nothing had pulled 1.107.0.
Regression guard
CI fails if any
requirements*.txtline lacks--hashor ifuv syncruns without--locked.A scheduled job scans running images for unexpected
.pthfiles withimportlines.Image references without a digest are rejected by our admission policy.
Security implications
An LLM gateway is a high-value target: it holds keys to several paid providers, often a database of customer usage, and runs with cloud credentials. Treat it like a payments service. Give it the smallest set of secrets, restrict its outbound traffic to provider endpoints, and alert on new destinations. Credential stealers that run at interpreter start defeat any "we never import that module" reasoning, so reviews must include what gets installed, not only what gets imported.
Prevention checklist
Pin Python dependencies with hashes and install with
--require-hashesoruv sync --locked.Use a release age delay for new versions.
Pin container images by digest, never floating tags.
Scan site-packages for
.pthfiles that execute code.Keep a list of secrets per service so rotation is fast.
Rebuild compromised environments instead of cleaning them.
References
litellm issue, malicious
litellm_init.pthin 1.82.8: https://github.com/BerriAI/litellm/issues/24512litellm issue, March compromise timeline: https://github.com/BerriAI/litellm/issues/24518
litellm issue, 1.107.0 and Docker tags compromised: https://github.com/BerriAI/litellm/issues/47826
Python docs,
sitemodule and.pthfiles: https://docs.python.org/3/library/site.htmlpip docs, secure installs and hash checking: https://pip.pypa.io/en/stable/topics/secure-installs/
uv docs,
exclude-newersetting: https://docs.astral.sh/uv/reference/settings/#exclude-newer
Add a label
Embed this post
<iframe src="https://hibug.me/litellm-pypi-compromise-pth-credential-stealer" width="700" height="420" frameborder="0" loading="lazy"></iframe>
I am an application and cloud security engineer. Most of my day is spent with development teams: reviewing designs, untangling IAM policies, hardening AWS accounts and making security checks fast enough that nobody wants to skip them. I believe security works best when it is concrete. Instead of long lists of best practices, my posts look at one decision at a time: a policy that granted more than intended, a design reviewed before the first line of code, a deletion that did not delete. Each one ends with checks you can run on your own systems.
Related Stories
Azure managed identity gets 401 from our API: token issued for the wrong audience
Comments
0No comments yet. Be the first to comment!
Sign In to post a comment.