Summary
PraisonAI's run_python() helper constructs a shell command by interpolating user-controlled code into a python3 -c "<code>" string and executing it with shell=True.
Because shell metacharacters like $() and backticks are not safely escaped, an attacker who can influence the code string may execute arbitrary operating system commands before Python is even invoked.
Affected context
This vulnerability arises in PraisonAI deployments that use the run_python() function to execute dynamically provided Python code through a shell wrapper.
The risk is highest when:
- User input, agent-generated content, or other untrusted data is passed into run_python()
- The application runs with elevated OS privileges or access to sensitive resources
- There is no strict validation or sanitization of the code argument before it reaches the shell
In such contexts, what appears to be "Python-only" execution actually traverses a general-purpose shell, exposing a broader attack surface.
Exploit preconditions
- Attack surface: Any interface (API, UI, agent, plugin, or workflow) that passes untrusted or partially trusted strings into run_python().
- Required access: Ability to influence the code parameter supplied to run_python(), either directly or indirectly (e.g., via prompt injection into an agent).
- Trigger point: Invocation of run_python() which builds a command like
python3 -c "<code>"and callssubprocess.run(..., shell=True). - Why exploitation is possible:
- The shell interprets $() and backtick (
`) syntax as command substitution. - The escaping logic only handles backslashes and double quotes, leaving shell metacharacters intact.
- With shell=True, the constructed string is processed by /bin/sh (or equivalent), allowing embedded OS commands to run before Python executes.
Why this happens
The underlying bug is a classic OS command injection pattern aligned with CWE-78: Improper Neutralization of Special Elements used in an OS Command ('OS Command Injection').
Instead of invoking Python directly with a structured API (e.g., passing arguments as a list), run_python():
- Builds a single shell command string that includes untrusted data.
- Uses
shell=True, handing the string to a command interpreter. - Implements incomplete escaping that focuses on
\and"but ignores other shell metacharacters, especially command substitution constructs.
This leads the shell to treat parts of the supplied "code" as first-class commands, blurring the line between Python execution and OS control and enabling attackers to break out of the intended Python-only context.
Potential impact
- Execution of arbitrary OS commands with the privileges of the PraisonAI process.
- Reading, modifying, or deleting local files accessible to the application.
- Exfiltration of environment variables, API tokens, credentials, or configuration secrets.
- Pivoting into internal networks or lateral movement to other services from the compromised host.
- Denial of service by exhausting system resources, terminating critical processes, or corrupting local state.
General mitigation guidance
Effective mitigation centers on removing shell interpretation from the execution path and tightening the trust boundary around dynamic code:
- Prefer direct process invocation over shell usage:
- Call Python (or any interpreter) using argument lists (e.g., subprocess.run([...], shell=False)).
- Avoid constructing shell command strings from untrusted data altogether.
- Eliminate or strictly limit dynamic code execution:
- Reduce or remove features that execute arbitrary code derived from prompts or user input.
- Where dynamic execution is required, constrain it to well-defined, pre-approved operations or scripts.
- Apply rigorous input handling:
- Treat all inputs to run_python()-style helpers as untrusted by default.
- Enforce allowlists or structured formats instead of free-form strings.
- Run with least privilege:
- Ensure the PraisonAI process runs as a non-privileged user with minimal filesystem and network permissions.
- Isolate high-risk execution in sandboxed environments or containers with restricted capabilities.
- Design secure-by-default abstractions:
- Provide high-level APIs that perform work without exposing direct shell or interpreter controls to user-provided strings.
- Make unsafe patterns (dynamic shell commands, raw code strings) harder to use than safe alternatives.
How teams detect issues like this
Organizations can pursue several high-level strategies to uncover and monitor similar issues:
- Static analysis and linting:
- Flag uses of subprocess (or equivalents) where
shell=Trueis combined with variables influenced by user input. - Detect string interpolation or concatenation patterns that build command lines from external data.
- Code review practices:
- Treat any helper that wraps command execution or dynamic code evaluation as security-sensitive.
- Require explicit review for functions that accept "code" or "command" arguments, checking for shell involvement.
- Secure coding guidelines and policy:
- Establish rules that discourage shell=True in production code without a documented, reviewed justification.
- Mandate the use of argument lists for process invocation and enforce them in CI via linters or custom checks.
- Runtime monitoring:
- Log invocations of command execution wrappers along with callsites or context.
- Use host-level monitoring (e.g., EDR, syscall auditing) to detect unexpected commands originating from application processes.

