/

CVE-2026-31040

Severity:

CRITICAL RISK

(9.8)

Code Injection in stata-mcp

CVE-2026-31040

Analyzed

Vulnerability type

Category:

Code Injection

CWE:

CWE-94

Impact:

Remote Code Execution

Severity:

CRITICAL RISK

(9.8)

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

stata-mcp contains a code injection vulnerability where user-supplied Stata do-file content is accepted without sufficient validation and then executed as commands.

An attacker who can influence or supply these do-files may be able to execute arbitrary commands with the privileges of the process running stata-mcp.

Affected context

This vulnerability affects environments where stata-mcp is used to process or run Stata do-files, especially when those files or their contents can be influenced by external users, automated pipelines, or uploaded artifacts.

The risk is higher in multi-tenant or server-side scenarios where do-file content from less-trusted sources is executed, or where processing is automated without human review of the scripts being run.

Exploit preconditions

  • Attack surface: Interfaces or workflows in stata-mcp that accept or process Stata do-files (e.g., upload endpoints, batch processing jobs, integrations).
  • Required access: Ability to create, modify, or otherwise control the Stata do-file content that stata-mcp will process.
  • Trigger point: Execution of a crafted do-file through stata-mcp, such as starting a job, invoking an API, or running an automated task that feeds the file into the tool.
  • Why exploitation is possible: The application does not sufficiently validate or constrain the content of the do-file before execution, allowing attacker-controlled commands to be interpreted and run.

Why this happens

The underlying flaw corresponds to CWE-94: Improper Control of Generation of Code ('Code Injection').

stata-mcp appears to treat user-supplied do-file content as trusted logic, passing it directly to the Stata interpreter (or underlying command execution layer) without enforcing strict constraints, sanitization, or a restricted command subset.

This design allows attackers to embed arbitrary commands or statements into the do-file so that, when processed, the application effectively executes attacker-defined code within its execution context.

Potential impact

  • Execution of arbitrary Stata commands and potentially underlying OS-level commands with the application's privileges
  • Unauthorized access, modification, or deletion of analysis data and output files
  • Theft or exposure of sensitive datasets processed through stata-mcp
  • Lateral movement within the host environment or adjacent services accessed by the application
  • Complete compromise of the host or container running stata-mcp, depending on deployment and permissions

General mitigation guidance

Mitigation generally focuses on separating untrusted input from executable logic and constraining what can be run by the application.

Potential defensive patterns include:

  • Introducing strict validation and allow-listing of permissible Stata commands or constructs in do-files before execution.
  • Executing user-supplied scripts in a heavily sandboxed environment with tightly limited file system, network, and process permissions.
  • Applying the principle of least privilege to the account under which stata-mcp and Stata run, reducing the damage potential of any injected commands.
  • Designing workflows so that only vetted, trusted do-files are run in production contexts, with human review or signing processes for scripts.
  • Separating data input from control logic, ensuring user input cannot directly form executable commands or script control structures.

How teams detect issues like this

Detection efforts can focus on both code-level patterns and runtime behavior.

Possible strategies include:

  • Static analysis rules or manual code review to flag locations where user-controlled do-file content is passed directly to execution APIs without validation or filtering.
  • Establishing coding guidelines that prohibit direct execution of user-supplied script content and require centralized validation utilities.
  • Runtime monitoring and logging of executed commands or do-file content, with anomaly detection for unexpected or high-risk command patterns.
  • Use of security-focused code scanning tools configured to detect CWE-94 style issues, especially where script text or command strings are constructed from external input.

Sources

SourceLink
github.comgithub.com/SepineTam/stata-mcp/commit/52413ce
github.comgithub.com/SepineTam/stata-mcp/issues/20
github.comgithub.com/SepineTam/stata-mcp/pull/21
github.comgithub.com/SepineTam/stata-mcp/releases/tag/v1.13.0

Frequently asked

questions

It can be, if stata-mcp is exposed through a service, API, or workflow where remote users can submit or influence do-file content. In such deployments, remote attackers may trigger code execution by supplying crafted scripts.

The attacker needs the ability to control or modify the Stata do-file content that stata-mcp will process. This may be via an upload interface, shared storage, CI pipeline integration, or any mechanism that lets an untrusted party influence the scripts executed.

The safest approach is to ensure that untrusted do-file content is never executed as-is. Implement strict validation and allow-listing of permitted commands, execute scripts in a constrained sandbox, and ensure the runtime is heavily restricted by least privilege principles.

Treat any external script content as untrusted, avoid direct execution of user-controlled code, enforce centralized validation and policy checks for executable content, and incorporate security reviews and automated scanning for code injection patterns into the development lifecycle.

Find out what's already
exploitable in your codebase.