Summary
PraisonAI's AST-based Python sandbox can be bypassed using type.__getattribute__, enabling arbitrary code execution when untrusted agent code is evaluated.
Because the sandbox only filters direct attribute access in ast.Attribute nodes and does not account for dynamic attribute resolution via built-ins like type.__getattribute__, attackers can reach sensitive attributes such as __subclasses__ and fully escape the sandbox.
Affected context
This vulnerability affects PraisonAI deployments that execute untrusted or partially trusted agent Python code using the AST-filtering sandbox in praisonaiagents/tools/python_tools.py.
The issue appears when the sandbox attempts to restrict access to dangerous attributes (for example, __subclasses__, __globals__, __bases__) by scanning only ast.Attribute nodes, while still allowing use of built-in functions and methods that can resolve these attributes dynamically from constant strings.
Environments that let external users define or influence agent tools, actions, or Python snippets are especially exposed.
Exploit preconditions
- Attack surface: The Python execution tool used by PraisonAI agents (e.g., an agent-capable Python REPL/tool inside the system).
- Required access: Ability to supply or influence Python code executed by the sandbox (for example, via prompts, tool code, or untrusted workflows).
- Trigger point: Invocation of
_execute_code_director related execution paths that parse code to an AST, run the attribute filter, and then execute the resulting code. - Why exploitation is possible: The filter:
- Only inspects
ast.Attributenodes for blocked attribute names. - Does not treat constant strings (e.g.,
"__subclasses__") or dynamic resolution viatype.__getattribute__as attribute access. - Therefore does not block indirect access to sensitive attributes, allowing attackers to reconstruct powerful primitives from seemingly harmless operations and escalate to arbitrary code execution.
Why this happens
The core issue is an incomplete enforcement of sandbox restrictions at the AST level, which aligns with:
- CWE-94: Improper Control of Generation of Code ('Code Injection') – because the system executes attacker-controlled Python code under the false assumption that the AST filter fully neutralizes dangerous functionality.
- CWE-693: Protection Mechanism Failure – because the sandboxing mechanism does not cover all relevant language constructs and circumvention paths.
The implementation focuses on statically blocking specific attribute names (__subclasses__, __globals__, __bases__) when they appear as ast.Attribute nodes. However, Python's object model allows attributes to be resolved indirectly through methods like type.__getattribute__ using string keys. Those strings appear as ast.Constant nodes and are never compared against the blocked list.
As a result, the sandbox fails to model the full semantics of attribute access in Python and relies on syntactic checks that are too narrow. This design gap allows attackers to chain built-in behaviors and reach privileged attributes that should have been blocked, ultimately enabling arbitrary code execution from code that appears to pass the AST filter.
Potential impact
- Arbitrary code execution within the PraisonAI worker process running the sandbox.
- Read and modification of sensitive data accessible to the PraisonAI process (API keys, model secrets, configuration files).
- Lateral movement to other services or infrastructure reachable from the compromised runtime environment.
- Abuse of the compromised agent to perform further attacks, such as data exfiltration, prompt/response tampering, or supply-chain style manipulation of downstream workflows.
- Denial of service by executing resource-exhausting operations (CPU, memory, network).
General mitigation guidance
Effective mitigation focuses on designing defense-in-depth sandboxing that does not rely exclusively on narrow AST patterns:
- Prefer execution environments that do not share the main process trust boundary, such as:
- Isolated containers, VMs, or micro-VMs with strict resource and syscall controls.
- Separate low-privilege worker processes that handle untrusted code with minimal capabilities.
- Move from attribute name blacklists to more robust policies:
- Use allowlists of safe modules, functions, and attributes.
- Disable or remove powerful reflection primitives (
__getattribute__,__subclasses__,__mro__,__globals__, etc.) from the accessible object graph wherever feasible. - Restrict access to the standard library and built-ins:
- Provide a curated, limited set of safe built-ins.
- Avoid exposing
type,eval,exec,compile,inspect, and similar primitives to untrusted code. - Treat AST-based filtering as a supplemental measure only:
- Combine it with runtime guards (e.g., policy-enforcing proxies for objects, restricted import hooks).
- Recognize that complex languages like Python are difficult to fully sandbox by static AST patterns alone.
- Run any untrusted execution under least privilege:
- Constrain filesystem access, environment variables, network egress, and credentials available to the sandboxed code.
How teams detect issues like this
Detection can target both the vulnerable pattern in code and signs of sandbox abuse at runtime:
- Static analysis and code review:
- Flag security mechanisms that:
- Rely solely on AST blacklists of attribute names or node types.
- Only inspect specific AST node classes (like
ast.Attribute) while leaving other resolution paths unchecked. - Look for custom "sandbox" or "safe eval" implementations that:
- Use
exec/evalon user-influenced code. - Attempt manual filtering of Python syntax instead of leveraging proven sandbox techniques or isolation.
- Policy-driven reviews:
- Enforce coding standards that ban bespoke sandboxing for high-risk languages unless reviewed and tested by security specialists.
- Require secure defaults such as process isolation for untrusted code execution.
- Runtime monitoring:
- Log and monitor usage of high-risk reflection primitives (
__subclasses__,__getattribute__, etc.) in environments that should be sandboxed. - Inspect unusual code execution patterns or errors that indicate reflection-based probing by untrusted agents.
- Automated tooling:
- Use security linters or SAST tools capable of:
- Tracking flows into
exec,eval, or dynamic imports from untrusted sources. - Detecting ad-hoc sandbox implementations and flagging them for manual review.

