Severity:

HIGH RISK

(7.3)

OS Command Injection in MetaGPT Terminal.run_command

CVE-2026-5972

Analyzed

Vulnerability type

Category:

OS Command Injection

CWE:

CWE-77, CWE-78

Impact:

Arbitrary Command Execution

Severity:

HIGH RISK

(7.3)

CVSS version:

3.1

Catch this CVE while code is being written

Detect vulnerable code directly in pull requests, so CVEs are fixed before merge.

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 subprocess or 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_command with 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_command abstraction 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_command and 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.

Sources

SourceLink
github.comgithub.com/FoundationAgents/MetaGPT/
github.comgithub.com/FoundationAgents/MetaGPT/issues/1929
github.comgithub.com/paipeline/MetaGPT/commit/d04ffc8dc67903e8b327f78ec121df5e190ffc7b
vuldb.comvuldb.com/submit/791745
vuldb.comvuldb.com/vuln/356526
vuldb.comvuldb.com/vuln/356526/cti

Frequently asked

questions

Yes, remote exploitation is possible when a MetaGPT deployment exposes functionality that routes remote inputs into Terminal.run_command. The exploit does not require local shell access, only a path to influence the command string.

The attacker must be able to control or significantly influence data that is passed into Terminal.run_command. Depending on how MetaGPT is integrated, this might require authenticated access to an API, interaction with an automated agent, or, in weaker setups, unauthenticated access to an exposed endpoint.

The safest pattern is to remove direct shell command execution for untrusted inputs and replace it with high-level, parameterized APIs. Where external commands are unavoidable, they should be invoked via non-shell subprocess calls with strict allowlists for commands and arguments.

Establish secure coding standards that forbid building shell commands from user input, enforce these via code review and static analysis, and encapsulate any necessary system interactions behind well-designed APIs that do not expose arbitrary command execution capabilities to higher-level components or user-controlled data.

Find out what's already
exploitable in your codebase.