Severity:

HIGH RISK

(7.3)

OS Command Injection in MetaGPT Bash.run Tooling

CVE-2026-5974

Analyzed

Vulnerability type

Category:

OS Command Injection

CWE:

CWE-77, CWE-78

Impact:

Remote Code Execution via OS Command Injection

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 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.run with a command string containing unvalidated, unsanitized external input.
  • Why exploitation is possible: The Bash.run implementation 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.run is allowed to execute.

How teams detect issues like this

Detection can combine code-centric and runtime approaches:

  • Static analysis: Search for uses of Bash.run and 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.

Sources

SourceLink
github.comgithub.com/FoundationAgents/MetaGPT/
github.comgithub.com/FoundationAgents/MetaGPT/issues/1931
github.comgithub.com/FoundationAgents/MetaGPT/pull/1940
vuldb.comvuldb.com/submit/791758
vuldb.comvuldb.com/vuln/356528
vuldb.comvuldb.com/vuln/356528/cti

Frequently asked

questions

Yes, it can be. If MetaGPT is exposed via network-accessible APIs, agents, or plugins that accept remote input and route it into Bash.run, a remote attacker may leverage that path to perform OS command injection.

An attacker needs the ability to influence data that ultimately forms part of the command executed by Bash.run. This could be through HTTP requests, chat messages, task descriptions, or configuration entries, depending on how MetaGPT is integrated.

The safest pattern is to replace shell-based execution of dynamic strings with APIs that take explicit argument lists, avoid invoking a shell by default, and strictly validate or whitelist the operations that agents are allowed to perform. Where shell use cannot be removed, isolate it in a tightly constrained and sandboxed environment.

Establish coding standards that treat all shell executions as high-risk, mandate secure wrappers for command execution, and require reviews for any code that touches OS-level tooling. Combine these standards with automated static analysis and CI checks so that unsafe command construction patterns are caught before deployment.

Find out what's already
exploitable in your codebase.