Summary
A sandbox escape vulnerability in n8n's python-task-executor allows authenticated users to bypass Python sandbox restrictions using crafted string formatting and exception handling patterns.
When triggered through the Code block by a user with basic permissions, this flaw can enable arbitrary Python code execution on the underlying OS, potentially leading to full instance takeover in "Internal" execution mode and confined code execution in a Sidecar container in "External" execution mode.
Affected context
The vulnerability occurs within n8n workflows that use the Python Code block executed by the python-task-executor.
It is relevant when:
- n8n is deployed with the python-task-executor enabled
- Users with basic authenticated access can create or modify workflows using the Code block
- The platform relies on sandboxing to constrain Python execution
- The instance runs in:
- Internal execution mode, where the executor has direct access to the main n8n environment, or
- External execution mode, where the executor runs in a Sidecar container with separated privileges and filesystem
Exploit preconditions
- Attack surface: n8n web interface / API exposing the Python Code block within workflows.
- Required access: Authenticated n8n account with basic permissions sufficient to create or edit workflows that include Python Code.
- Trigger point: Execution of a workflow containing malicious Python Code that leverages string formatting and exception-handling constructs to evade sandbox filters.
- Why exploitation is possible: The sandbox's filtering and restriction logic can be circumvented by composing code dynamically through string interpolation and manipulating control flow via exceptions, allowing forbidden operations to be reconstructed and executed despite intended policy controls.
Why this happens
This issue stems from incomplete enforcement of code execution restrictions in a sandbox that evaluates dynamically generated Python code.
The python-task-executor attempts to constrain available operations but still permits patterns where:
- Code can be constructed at runtime (e.g., via string formatting or concatenation), and
- Exception handling and control-flow features allow reaching sensitive interpreter features or APIs that should be blocked.
This aligns with CWE-95: Improper Neutralization of Directives in Dynamically Evaluated Code, where user-influenced expressions are evaluated in a context that fails to neutralize or block dangerous constructs that can break out of the sandbox and reach the underlying operating system.
Potential impact
- Execution of arbitrary Python code within the python-task-executor runtime.
- Full compromise of the n8n instance, including workflow data and credentials, when running in Internal execution mode.
- Reading, modifying, or exfiltrating data accessible to the executor process.
- Lateral movement or pivoting to other systems reachable from the compromised environment.
- In External execution mode, arbitrary code execution confined to the Sidecar container, potentially affecting:
- Data and secrets mounted into that container
- Local filesystems and services exposed to that container
General mitigation guidance
Effective mitigation focuses on strengthening sandbox boundaries and minimizing trust in user-supplied code:
- Prefer high-level, declarative workflow nodes over generic code execution wherever possible, reducing the need for user-provided Python.
- Harden the python-task-executor sandbox by:
- Restricting or removing dynamic evaluation features entirely (e.g., disallowing
eval-style behavior and reflective imports). - Enforcing strict whitelists of allowed modules, attributes, and builtins rather than attempting to blacklist dangerous ones.
- Treating any user-controllable string interpolation or code construction as untrusted and disallowing it in sandboxed contexts.
- Isolate execution environments using process and container isolation, applying least-privilege OS-level policies (e.g., limited filesystem access, restricted networking, reduced OS capabilities).
- Limit which user roles are allowed to run arbitrary code blocks, and prefer separate, more trusted accounts for administrative automation tasks.
- Centralize configuration so that sandbox settings default to the most restrictive posture and must be explicitly relaxed only for controlled environments.
How teams detect issues like this
Organizations can detect and monitor for this class of issue through:
- Static analysis and code scanning of the python-task-executor:
- Identify any remaining dynamic evaluation primitives (e.g., generic
exec,eval, or similar mechanisms). - Flag patterns that allow user input to influence code strings, especially in combination with try/except or other control flow designed to bypass filters.
- Security-focused code review of sandbox logic:
- Verify that restrictions are enforced at the interpreter boundary, not just via superficial string checks.
- Examine exception handling paths to ensure they cannot be abused to reintroduce dangerous functionality.
- Runtime monitoring and logging:
- Capture detailed logs of Code block executions, including which workflows and users initiated them.
- Alert on anomalous patterns such as:
- Unusual imports or OS-level interactions from Code blocks.
- Repeated failures followed by successful executions that indicate probing for bypasses.
- Policy enforcement in CI/CD:
- Integrate SAST or linters into the development pipeline to flag reintroduction of dynamic evaluation or overly permissive sandbox configurations.

