Summary
MetaGPT up to version 0.8.1 contains an OS command injection issue in the Bash.run function within metagpt/tools/libs/terminal.py, where attacker-controlled input can be passed into shell commands.
In scenarios where untrusted or semi-trusted data is allowed to reach this function, a remote party could execute arbitrary system commands with the privileges of the MetaGPT process.
Affected context
This vulnerability affects applications and services that embed or integrate FoundationAgents MetaGPT (up to 0.8.1) and use the Bash.run helper for shell execution.
Risk materializes when:
- User-controlled or external data is interpolated into command strings passed to
Bash.run - The environment executes these commands via a shell (e.g.,
/bin/sh,/bin/bash) - There are network-accessible endpoints, agents, or plugins that bridge remote input to this terminal helper
In such architectures, MetaGPT may be used as an orchestrator that issues shell commands, and any unsafe concatenation of parameters can become an injection channel.
Exploit preconditions
- Attack surface: Any API endpoint, agent action, plugin, or workflow that accepts remote input and feeds it—directly or indirectly—into
Bash.run. - Required access: Network-level access to the affected service or control over data sources that influence shell commands (e.g., prompts, task parameters, configuration, or messages).
- Trigger point: Execution of
Bash.runwith a command string containing unvalidated, unsanitized external input. - Why exploitation is possible: The
Bash.runimplementation constructs shell commands using string concatenation or equivalent patterns without enforcing strict argument separation or escaping. When a shell interprets these commands, meta-characters (such as;,&&,|, and redirection operators) and command substitutions are processed, allowing arbitrary command execution.
Why this happens
The underlying bug follows the pattern described by CWE-78: Improper Neutralization of Special Elements used in an OS Command ('OS Command Injection').
Instead of treating external data as opaque arguments, the code likely embeds user-influenced strings directly into shell command lines. This pattern delegates parsing to the shell, which interprets special characters and constructs, giving an attacker a pathway to extend or replace the intended command.
Abstractly, the issue is:
- A helper responsible for executing shell commands accepts a free-form command string.
- External input is merged into this string without strict validation, escaping, or switching to argument-based APIs.
- The shell executes the resulting string, interpreting attacker-supplied control characters as part of the command language.
Potential impact
- Execution of arbitrary system commands with the permissions of the MetaGPT process
- Reading, modifying, or deleting application and system files accessible to the service account
- Pivoting to other internal systems or services reachable from the host
- Exfiltration of sensitive data such as API keys, model configuration, or credentials
- Interference with AI agent workflows, including tampering with prompts, logs, or outputs
- Potential use as a stepping stone for broader infrastructure compromise depending on deployment context
General mitigation guidance
Mitigation centers on removing shell interpretation of untrusted data and constraining command execution:
- Prefer non-shell command execution APIs (e.g., using argument arrays instead of a single shell string) so that external input cannot modify the command structure.
- If shell usage is unavoidable, apply rigid input validation and allow only a constrained, enumerated set of commands or flags; reject or neutralize meta-characters and command substitution constructs.
- Introduce command whitelisting patterns so that higher-level logic maps user intents to predefined safe operations, rather than passing raw text to the shell.
- Run MetaGPT and associated tools under least-privilege service accounts and consider process-level isolation (containers, jails, or sandboxes) to reduce blast radius.
- Limit the capability of agents to invoke arbitrary shell commands; configure guardrails and policy layers that mediate what
Bash.runis allowed to execute.
How teams detect issues like this
Detection can combine code-centric and runtime approaches:
- Static analysis: Search for uses of
Bash.runand inspect any place where its command parameters are derived from external input. Automated rules can flag string concatenation or f-strings that mix user-controlled values into shell commands. - Code review: During security-focused reviews, treat any invocation of shell execution helpers as a high-risk hotspot. Look for missing validation, absence of whitelisting, or overbroad agent capabilities.
- Policy enforcement: Integrate security checks into CI pipelines to prevent introducing new shell execution paths without explicit review. Tools that understand Python ASTs can enforce patterns like "no shell=true" or "no dynamic command composition."
- Runtime monitoring: Use logging, EDR, or command auditing (e.g., shell histories, process tracing) to identify unexpected command patterns triggered by MetaGPT, such as outbound network utilities, file exfiltration commands, or privilege escalation attempts.

