| Title | https://github.com/mcp-use/ mcp-use <= 1.7.0 Improper Control of Generation of Code |
|---|
| Description | # Sandbox escape in CodeExecutor leads to RCE via the execute_code MCP tool
## Summary
`CodeExecutor._build_namespace()` (`libraries/python/mcp_use/client/code_executor.py`) restricts `__builtins__` to a small allowlist and describes itself as providing "secure code execution" / "a secure execution environment." Restricting `__builtins__` only blocks calling excluded builtin *functions* by name - it does nothing to attribute access, which Python always permits on any object already reachable in the namespace via plain `.` syntax. This is a well-known, unfixable-by-blocklist Python sandboxing anti-pattern.
`execute_code` is registered as an MCP tool (`client/connectors/code_mode.py`'s `CodeModeConnector`) whenever a client is constructed with `code_mode=True`, so the code string reaching `CodeExecutor.execute()` is whatever an LLM agent decides to write - directly steerable via prompt injection from any content the agent reads, not just something a developer types by hand.
## Details
No builtin function call is needed at all. From a literal empty tuple, plain attribute access reaches the interpreter's live class graph:
```python
for cls in ().__class__.__bases__[0].__subclasses__():
if cls.__name__ == 'Popen':
cls(['touch', '/tmp/PWNED']).wait()
break
```
`().__class__` is `<class 'tuple'>`; `.__bases__[0]` is `object`; `.__subclasses__()` enumerates every class currently loaded in the process, including `subprocess.Popen` - none of `open`, `eval`, `exec`, `__import__` (all already excluded from `safe_builtins`) are used.
## Proof of Concept
(available upon request)
## Impact
Full arbitrary code execution in the MCP client's own process (which typically holds API keys, credentials for other configured MCP servers, and filesystem/network access), reachable via prompt injection against any agent running in `code_mode=True`, or via any application that passes less-than-fully-trusted code strings to `execute_code`/`CodeExecutor.execute()` believing the library's own "secure execution environment" description.
## Suggested fix
I have a fix ready (an AST-based pre-execution check rejecting any dunder name/attribute access, closing the whole family of gadgets that route through `__class__`/`__bases__`/`__subclasses__`/`__globals__`/`__builtins__`/`__import__`/etc., not just the one PoC above) plus new regression tests, and corrected the module's docstring to stop describing this as a security sandbox (it substantially raises the bar but is still a blocklist, not a proof of containment; isolation needs OS-level sandboxing, not any refinement of in-process `exec()`). Will open a PR shortly.
## Affected versions
`main` HEAD as of 2026-09-04, mcp-use 1.7.0.
|
|---|
| Source | ⚠️ https://github.com/mcp-use/mcp-use/issues/2816 |
|---|
| User | carfeii (UID 97470) |
|---|
| Submission | 11/10/2026 16:07 (20 days ago) |
|---|
| Moderation | 11/30/2026 22:52 (20 days later) |
|---|
| Status | Accepted |
|---|
| VulDB entry | 446843 [mcp-use up to 1.7.1 CodeExecutor code_executor.py _build_namespace code code injection] |
|---|
| Points | 20 |
|---|