> ## Content Index
> Fetch the complete content index at: https://ettayeb.fr/llms.txt
> Use this file to discover other available public pages before exploring further.

# LiteLLM’s regex sandbox falls to bytecode rewriting and hands over a remote shell
- URL: https://ettayeb.fr/en/ai/litellm-cve-2026-40217-guardrail-rce-en/
- Published: 2026-11-18T00:00:00.000Z
- Updated: 2026-11-23T15:24:40.000Z
- Description: On November 18, 2026, CISA added CVE-2026-40217 to its KEV catalog: LiteLLM’s guardrail-testing endpoint executes arbitrary code by rewriting Python bytecode, and the Docker image runs as root.
- Author: Walid Ettayeb
- Tags: AI, litellm, sandbox-escape, llm-gateway, cisa-kev, bytecode, #en, #cve, #Import 2026-11-23 14:52

**April 10, 2026.** The **NVD** publishes **CVE-2026-40217**: the open-source **LLM** gateway **LiteLLM** executes arbitrary code through **bytecode rewriting** on its `/guardrails/test_custom_code` endpoint. **November 18, 2026.** **CISA** adds the flaw to its **KEV** catalog on evidence of active exploitation, with a federal due date of **December 2, 2026**. **November 18, 2026.** A fix has existed since **v1.83.10-stable**, yet the official **Docker** image still runs the process as **root**. Why it matters: seven months separate disclosure from the alert, and for all of that time the **inference gateway** — the single point holding the keys to dozens of models — was an open door.

## A sandbox that filtered text, not code

**LiteLLM** is an **LLM gateway** that unifies access to dozens of providers — **OpenAI**, **Anthropic**, **Gemini** — behind one API. It ships a **guardrails** feature: custom **Python** code that filters requests and responses before they reach the model. To test that code without deploying it, the admin interface exposes the `/guardrails/test_custom_code` endpoint, which runs the supplied **Python** inside a home-grown **sandbox**.

That sandbox relied on a mechanism the researchers at **X41 D-Sec** dismantled: a **regex** filter that rejected the literal presence of dangerous strings — `__globals__`, `__builtins__`, `import`, `__class__`. The implicit assumption was that code not containing those strings could not use them. It is exactly the assumption **Python** breaks, because the language resolves its attributes **dynamically at runtime**.

The consequence is blunt. The sandbox did not filter the **capabilities** of the code — it filtered **substrings**. An attacker can rebuild any forbidden string by **concatenation**: `"_"+"_gl"+"ob"+"als"+"_"+"_"` produces `__globals__` without ever making it appear literally in the source. The regex filter is bypassed before it has begun to protect anything.

## Bytecode rewriting, not a classic injection

The exploitation goes further than simple string concatenation. An attacker submitting code to the sandbox follows a precise path, described in advisory **x41-2026-001** and echoed by **CISA**. First, they climb Python’s object hierarchy without touching the filtered `__class__` attribute: the expression `str.mro()[1]` returns the root `object` via the method resolution order. Next, they extract a generator function’s code object through `gi_code`, which was not on the blocklist.

Then comes the step that gives the flaw its name. The attacker calls the `code.replace()` method to **rewrite the name table** of the function’s bytecode, injecting the rebuilt `__globals__` string. They apply the modified bytecode through `object.__setattr__`. When the function runs, it resolves `__globals__` against an unrestricted environment the sandbox provides — a trusted utility such as `http_get` — and the attacker obtains the **globals** dictionary of that trusted context.

The final line of the payload leaves no doubt:

```python
def apply_guardrail(inputs, request_data, input_type):
    obj = str.mro()[1]
    def g(fn):
        yield fn.placeholder
    c = g(None).gi_code
    gn = "_"+"_gl"+"ob"+"als"+"_"+"_"
    cn = "_"+"_co"+"de_"+ "_"
    obj.__setattr__(g, cn, c.replace(co_names=(gn,)))

```

From there, access to `__builtins__` opens the `os` module, and `os.popen("id").read()` runs a system command whose output comes back in the endpoint’s **HTTP** response. A single authenticated **POST** request is enough.

## Root by default, with the master key as the only lock

The seven-month gap between April’s disclosure and November’s **KEV** entry deserves to be named. **LiteLLM** shipped the **v1.83.10-stable** fix shortly after disclosure, and the **X41** advisory described the exploit technique in the open, payload included. For all that time, exposed instances kept running as **root** with a bypassable sandbox — which is precisely the gap between "fix available" and "fix applied" that the **KEV** tries to close by imposing a deadline.

The exploitation prerequisite is authentication: the endpoint requires a valid bearer token, typically the `LITELLM_MASTER_KEY`. That is what sets the **CVSS 3.1** score at **8.8** (`AV:N/AC:L/PR:L/UI:N/S:U/C:H/I:H/A:H`) — **low privileges** are required. But two details make the impact far worse.

First, the official **LiteLLM** **Docker** image runs the process as **root** by default. A sandbox escape therefore becomes a full container compromise, not a limited-user one. Second, the **master key** is no marginal secret: it unlocks the gateway’s administration and, indirectly, the **API keys** of every provider the gateway aggregates. An **RCE** on that machine means reading every model key in cleartext.

This is not the first **LiteLLM** alert we have covered. The project’s **release pipeline** was already compromised to distribute an info-stealer, and the [Canto Incognito](https://ettayeb.fr/en/securite/poellm-canto-incognito-cryptojacking-2026-en/) campaign explicitly targeted exposed **LiteLLM** instances to turn them into miners. The pattern is constant: an **LLM** gateway is a **stateful**, secret-rich service, often exposed for developer convenience — and treated with less rigor than a database server.

## The fix abandons the home-grown sandbox

The fix, shipped in **v1.83.10-stable**, does not harden the regex filter — it abandons it. **LiteLLM** integrated the **RestrictedPython** library, which operates at the **abstract syntax tree** (**AST**) level rather than on source text. **RestrictedPython** rewrites every attribute access into a call to a guard function that enforces a strict runtime policy: internal attributes such as `gi_code` or `mro` become unreachable, and the concatenation and bytecode-manipulation vectors disappear.

It is the right call, and a general lesson. A sandbox that filters **text** to secure a **dynamic** language is stillborn. The only reliable boundary is the one applied to the program’s **structure** — the **AST** — not to its textual surface.

The endpoint itself deserves a second look. Guardrails are meant to run arbitrary administrator-supplied code by design — that is their purpose. The mistake was not exposing a code-testing endpoint; it was assuming a regex could keep a dynamic language inside a box. Any feature that executes user-supplied **Python** is a code-execution surface first and a convenience feature second.

## Remediation

For a self-hosted **LiteLLM** instance, the path forward is three actions. **Upgrade to v1.83.10-stable or later** — that is the release that replaces the regex sandbox with **RestrictedPython**. **Run the container as a non-root user**: the official image defaults to `root`, and a dedicated user turns an **RCE** into a bounded compromise. Finally, **treat the master key as a root secret** — rotate it immediately if it may have been exposed, and remove the `/guardrails` endpoint from any unnecessary exposure. For teams running the gateway internally, schedule this with the urgency of a database patch: a single compromised gateway hands an attacker the API keys of every provider your applications call, so the rotation work is larger than the upgrade itself.

```bash
# Check the deployed version
docker exec litellm python -c "import litellm; print(litellm.__version__)"

# Pull the new image and restart as a non-root user
docker pull ghcr.io/berriai/litellm:main-latest
docker run -d --user 1000:1000 -p 4000:4000 \
  -e LITELLM_MASTER_KEY="$NEW_MASTER_KEY" ghcr.io/berriai/litellm:main-latest

```

**CISA** sets the federal deadline at **December 2, 2026**. For a private team the reflex is the same: that date is not a suggestion, it is the time active exploitation leaves you.

## The verdict

**If you run LiteLLM older than v1.83.10-stable, upgrade immediately — the flaw is actively exploited, listed in the KEV, and the Docker image runs the process as root.** The priority goes to gateways that are exposed or shared across teams, which concentrate the keys of dozens of model providers.

**If you are already current, do not relax**: run the container as **non-root**, restrict access to `/guardrails`, and rotate the **master key**. The lesson goes beyond **LiteLLM** — any **inference gateway** you deploy is a vault of secrets. A sandbox that filters text to contain **Python** is a door someone will eventually push open.

## References

- [X41 D-Sec — X41-2026-001: LiteLLM (guardrails/test\_custom\_code) sandbox escape](https://www.x41-dsec.de/lab/advisories/x41-2026-001-litellm/?ref=ettayeb.fr)
- [NVD — CVE-2026-40217](https://nvd.nist.gov/vuln/detail/CVE-2026-40217?ref=ettayeb.fr)
- [CISA Known Exploited Vulnerabilities Catalog — CVE-2026-40217](https://www.cisa.gov/known-exploited-vulnerabilities-catalog?field%5Fcve=CVE-2026-40217&ref=ettayeb.fr)
- [GitHub — LiteLLM security advisory (RestrictedPython fix)](https://github.com/BerriAI/litellm/security/advisories?ref=ettayeb.fr)