Summary
A critical code-injection vulnerability in the MCP Context Forge Python sandbox allows untrusted code submitted through the execute_code MCP tool to escape intended restrictions and execute operating-system commands.
The risk is greatest when the sandbox is exposed through an unauthenticated HTTP or SSE transport, although stdio-only deployments generally have a narrower attack surface.
Affected context
The vulnerability occurs in the python_sandbox_server subproject, specifically its FastMCP server implementation. It may affect deployments that expose the execute_code tool through HTTP or SSE without authentication and that rely on runtime validation to prevent access to sensitive Python capabilities. Core Context Forge gateway and proxy components are not directly implicated by this issue.
Exploit preconditions
- Attack surface: The Python sandbox's
execute_codeMCP tool, particularly over HTTP or SSE transport - Required access: The ability to invoke the exposed MCP tool; unauthenticated exposure increases accessibility
- Trigger point: Processing and execution of submitted Python code by the sandbox server
- Why exploitation is possible: Raw attribute access is exposed through
safe_builtins, a required attribute-access guard is absent, and validation focuses on detecting literal dangerous names rather than preventing their runtime construction - Deployment consideration: Stdio-only deployments generally provide reduced network reachability, but local or otherwise authorized callers may still matter
Why this happens
The underlying bug is an incomplete sandbox boundary. The server exposes raw getattr behavior without enforcing the required _getattr_-style guard and relies on checks for literal dangerous dunder strings. Because restricted names can be constructed dynamically, validation does not reliably constrain Python object traversal. This can allow access to powerful runtime objects and operating-system process functionality, mapping primarily to CWE-94: Improper Control of Generation of Code and secondarily to CWE-693: Protection Mechanism Failure.
Potential impact
- An attacker may execute arbitrary operating-system commands with the server process privileges
- The sandbox process and its accessible data could be compromised
- Credentials, environment variables, or service tokens may be exposed
- The host or container could be used for lateral movement
- Connected MCP, A2A, REST, or gRPC resources could be manipulated or abused
- Availability could be affected through unauthorized process execution
General mitigation guidance
Mitigation should focus on treating sandboxing as a defense-in-depth boundary rather than relying on string-based validation alone.
Common defensive patterns include:
- Removing raw reflective operations such as unrestricted attribute access from the permitted execution environment
- Enforcing an explicit allowlist of language features, objects, methods, and modules
- Applying structural and semantic policy checks that cannot be bypassed by dynamically constructed names
- Requiring authentication and authorization for HTTP and SSE tool exposure
- Preferably isolating untrusted execution in a separate, tightly restricted process or workload
- Running the sandbox with minimal operating-system privileges, restricted filesystem access, and limited network connectivity
- Avoiding exposure of secrets and high-value service credentials to the sandbox process
- Applying resource limits, execution timeouts, and process containment controls
How teams detect issues like this
Organizations can look for this class of issue through:
- Static analysis rules identifying raw reflection, unrestricted
getattr, dynamic object traversal, and broad built-in exposure in sandbox code - Code review checks for denylist-only validation, especially checks based solely on literal dangerous strings
- Tests that evaluate dynamically constructed identifiers and indirect access paths
- Policy enforcement requiring authentication and authorization on network-exposed MCP tools
- Runtime monitoring for unexpected child-process creation, shell activity, filesystem access, and outbound connections from sandbox workers
- Automated security review tools that flag incomplete interpreter isolation and missing sandbox guards

