Summary
MetaGPT versions up to 0.8.1 contain an OS command injection flaw in the Terminal.run_command helper within metagpt/tools/libs/terminal.py, where attacker-controlled input can be executed by the underlying shell.
If an attacker can influence the command string passed to this function over a remote interface, they may execute arbitrary system commands with the application's privileges.
Affected context
The vulnerability appears in MetaGPT deployments that invoke Terminal.run_command with data derived from external or semi-trusted sources.
This commonly occurs when:
- The application exposes APIs, plugins, or agent workflows that accept free-form commands or parameters.
- User input is passed into shell commands without proper escaping or strict whitelisting.
- The runtime environment allows the process to spawn shell subprocesses (e.g., via
subprocessor similar).
The issue is more impactful in environments where the MetaGPT process runs with elevated privileges or has access to sensitive files, secrets, or internal services.
Exploit preconditions
- Attack surface: Any feature path that routes remotely supplied input into
Terminal.run_command(e.g., web API, agent interaction, plugin, or automation hook). - Required access: Ability to submit or control data that influences the command string executed by
Terminal.run_command, which may or may not require prior authentication depending on the deployment. - Trigger point: Execution of
Terminal.run_commandwith attacker-tainted input, typically when the application constructs command lines by concatenating or interpolating user-provided values. - Why exploitation is possible: The function allows shell command execution using unvalidated and improperly sanitized input, enabling attackers to inject additional shell metacharacters or arguments that the shell interprets as new commands.
Why this happens
This issue stems from building shell commands from externally influenced strings and passing them directly to a shell interpreter.
At an abstract level, this matches CWE-78: Improper Neutralization of Special Elements used in an OS Command and CWE-77: Improper Neutralization of Special Elements used in a Command ('Command Injection').
Instead of treating user data as opaque values, the implementation appears to interpolate them into a command line where shell metacharacters (such as separators, redirection operators, or subshell syntax) are not neutralized.
This pattern allows untrusted input to alter the structure and behavior of the command, rather than being processed as passive data.
Potential impact
- Execution of arbitrary OS commands with the privileges of the MetaGPT process.
- Reading, modifying, or deleting application and system files accessible to the service account.
- Exfiltration of credentials, API keys, or other sensitive configuration data.
- Lateral movement within the hosting environment, including pivoting to internal services or containers.
- Full compromise of the host system in weaker isolation or high-privilege deployments.
General mitigation guidance
Effective mitigation focuses on eliminating string-based shell command construction and treating all external input as untrusted:
- Prefer dedicated library calls or APIs over shell commands (e.g., using Python libraries to perform filesystem or network operations instead of invoking shell utilities).
- When external programs must be run, avoid shell interpretation by invoking them with argument arrays (e.g., subprocess APIs with
shell=False) so user-controlled data becomes a single argument and not part of a parsed command line. - Apply strict input validation and normalization before using any value in system-level operations, favoring allowlists of permitted commands, flags, or patterns instead of attempting to escape arbitrary input.
- Constrain what the
Terminal.run_commandabstraction is allowed to do, for example by exposing only a small, high-level set of safe operations instead of arbitrary command execution. - Run MetaGPT under least-privilege accounts and, where possible, confine it using sandboxing, containers, or other isolation mechanisms to limit damage if a command injection occurs.
How teams detect issues like this
Organizations can approach detection using both code-focused and operational techniques:
- Static analysis: Configure rules to flag any use of high-risk subprocess or shell-execution APIs within MetaGPT and associated tooling, especially where arguments are built via string concatenation, f-strings, or format operations containing user-controlled variables.
- Code review: During manual reviews, pay special attention to helper functions like
Terminal.run_commandand any call sites that accept external input; look for direct interpolation of request parameters, agent messages, or configuration fields into command strings. - Security testing: Include command injection test cases in automated and manual testing (e.g., fuzzing parameters with shell metacharacters and observing behavior) to identify unsafe execution paths.
- Runtime monitoring: Monitor process creation, unusual child processes, and anomalous command patterns originating from MetaGPT services using endpoint detection and response (EDR) tools or audit logging to catch suspicious activity indicative of exploitation.

