# Python Sandbox Escapes
> Failures of in-interpreter restrictions on untrusted Python, such as removed builtins, AST validators, proxy objects and patched builtins, that let user code reach the host process. The practical lesson is to isolate at the OS or VM boundary, not inside CPython.
## How it works
Products increasingly run user- or LLM-supplied Python: BI query runners, notebook servers, agent "code interpreter" tools and MCP script tools. Many try to make this safe *inside* the interpreter by denylisting imports, builtins and attribute names. That approach is structurally weak:
- Python's introspection means capabilities are reachable through many paths, not just the obvious `import` or `getattr`. Blocking the common forms leaves less obvious ones open.
- Replacement "safe" builtins or proxies can behave differently from the originals, and those differences become the gap.
- pysandbox's author concluded that a sandbox inside CPython is the wrong design because the code to audit is the whole interpreter.
Because of this, the security boundary should be treated as **the process, container or VM**. In-interpreter filtering is at best a usability guard rail.
## Notable cases
- **AWS MCP Server `aws___run_script`** (fixed Oct 2026, HackerOne VDP): the tool combined an AST validator with runtime proxy builtins in a Lambda. The researcher found a language feature the validator didn't model, which led to command execution as the Lambda user. That exposed the Lambda execution-role credentials and the sandbox's own source. Impact stayed contained because the role was deny-all apart from `sts:GetCallerIdentity`, there was no internet egress and each session had its own container.
- **Redash Python data source** (OX Security, unpatched): a mistake in how the sandbox reassigns `getattr` leaves the unrestricted builtin reachable. Any user with query rights gets RCE on the Redash server. The vendor advice is to disable the Python data source.
- **CrewAI Code Interpreter (CVE-2026-2275, CVE-2026-2287)**: when Docker is unreachable, or stops mid-run, the tool **falls back** to an in-process `SandboxPython` that permits C function calls via `ctypes`. Reachable through direct or indirect [[Prompt Injection]]. The vendor plans to fail closed.
- **Langflow < 1.9.0 (CVE-2026-33873)**: the Agentic Assistant executed LLM-generated Python during "validation" with no isolation at all, so manipulating model output gave server-side code execution.
- **Template "sandboxes"**: SGLang CVE-2026-5760 (GGUF `tokenizer.chat_template`) and fsspec CVE-2026-104851 (`ReferenceFileSystem`) rendered attacker templates with an un-sandboxed Jinja2 environment. The fix is `ImmutableSandboxedEnvironment`. See [[Jinja2 SSTI]].
- **BentoML** (GHSA-v959-cwq9-7hr6): not even a template sandbox. An unsandboxed `Environment` with `jinja2.ext.do` rendered an imported archive's template on the host, outside the container the build was meant to run in. [[raw/github.com/bentoml-ssti-via-unsandboxed-jinja2-in-dockerfile-generation-15849|BentoML advisory]]
- **Token blacklists as a "WAF"** (FortiPy lab): a substring filter in front of `render_template_string` blocked `__`, `globals`, `os`, `[` and similar, but Jinja decodes `\xNN` escapes inside string literals after the filter has run, and `|attr()` plus `__getitem__` replace dot and subscript access. The app ran as root, so one request was root RCE. [[raw/ssstrickys.com/fortipy-ssti-to-root-writeup-15855|FortiPy]]
- **Arize Phoenix**: a `DotDict.__getattr__` guard on a `str.format` template was ineffective because field resolution bypasses the wrapper. See [[Unsafe Python Functions]].
## Defenses
- **Isolate outside the interpreter.** Use a VM or microVM (Firecracker, Fargate, Lambda, Fly Machines), or at minimum a separate process. Discard all state after each run.
- **Process-level controls** (healeycodes pattern): fork a child, apply `seccomp` with an EPERM default (allow `write` only to stdout/stderr fds), and set `resource.setrlimit` on `RLIMIT_CPU`, `RLIMIT_AS` and `RLIMIT_FSIZE` before `exec`. Enforce a wall-clock timeout from the parent. These limits can't be revoked by the guest.
- **Fail closed**: if the isolation backend (Docker, VM) is unavailable, refuse to run. Never degrade to an in-process mode.
- **Least privilege in the sandbox**: give it no credentials, or deny-all credentials. Block egress and use per-session containers. The AWS case shows these limit blast radius once an escape occurs.
- **Treat in-interpreter filters as untrusted**. When testing, assume a bypass exists and measure what the process can reach: env vars, the instance role, the filesystem, the network.
- For templates, use the engine's sandboxed environment and never render user templates with live objects in context.
## Related
- [[Python Security]]
- [[Unsafe Python Functions]]
- [[Python AI and ML Framework Vulnerabilities]]
- [[Code Injection]]
- [[Remote Code Execution]]
- [[Jinja2 SSTI]]
- [[MCP Security]]
- [[Agentic AI Risks]]
- [[Cloud Metadata Service Exploitation]]
## Sources
- [[raw/healeycodes.com/running-untrusted-python-code-683|Running Untrusted Python Code]]
- [[raw/xplo1t-sec.github.io/breaking-the-aws-mcp-python-sandbox-12381|Breaking the AWS MCP Python Sandbox]]
- [[raw/ox.security/known-unpatched-exploitable-redashs-python-sandbox-escape-gives-attack-5213|Redash's Python Sandbox Escape Gives Attackers Full Server Access]]
- [[raw/kb.cert.org/crewai-contains-multiple-vulnerabilities-including-ssrf-rce-2963|CrewAI contains multiple vulnerabilities including SSRF, RCE]]
- [[raw/thehackernews.com/sglang-cve-2026-5760-cvss-9-8-enables-rce-via-malicious-gguf-model-fil-4812|SGLang CVE-2026-5760 RCE via malicious GGUF]]
- [[raw/advisories.gitlab.com/fsspec-referencefilesystem-ssti-leads-to-rce-cve-2026-104851-12784|fsspec ReferenceFileSystem SSTI leads to RCE]]
- [[raw/github.com/arize-phoenix-format-string-injection-in-applychattemplate-graphql-que-14497|Arize Phoenix: format string injection in applyChatTemplate]]
- [[raw/sentinelone.com/cve-2026-33873-langflow-agentic-assistant-rce-vulnerability-2981|CVE-2026-33873: Langflow Agentic Assistant RCE]]
- [[raw/github.com/bentoml-ssti-via-unsandboxed-jinja2-in-dockerfile-generation-15849|BentoML GHSA-v959-cwq9-7hr6: SSTI via unsandboxed Jinja2]]
- [[raw/ssstrickys.com/fortipy-ssti-to-root-writeup-15855|FortiPy: SSTI to root writeup]]