Summary
A command injection vulnerability exists in the git-mcp-server MCP Server. The vulnerability is caused by the unsanitized use of input parameters within a call to child_process.exec, enabling an attacker to inject arbitrary system commands. Successful exploitation can lead to remote code execution under the server process's privileges.
The server constructs and executes shell commands using unvalidated user input directly within command-line strings. This introduces the possibility of shell metacharacter injection (|, >, &&, etc.).
Details
The MCP Server exposes tools (git_add, git_init, git_logs, etcc) to perform several git operations. An MCP Client can be instructed to execute additional actions for example via indirect prompt injection when asked to read git logs. Below some example of vulnerable code and different ways to test this vulnerability including a real example of indirect prompt injection that can lead to arbitrary command injection.
Vulnerable code
The following snippet illustrates the vulnerable code pattern used in the MCP Server’s tooling. Note: These are only some instances, but similar patterns may exist elsewhere in the codebase.
import { exec } from "child_process";
...
const execAsync = promisify(exec);
// https://github.com/cyanheads/git-mcp-server/blob/v2.1.4/src/mcp-server/tools/gitInit/logic.ts#L122-L138
let command = `git init`;
if (input.quiet) {
command += " --quiet";
}
if (input.bare) {
command += " --bare";
}
// Determine the initial branch name, defaulting to 'main' if not provided
const branchNameToUse = input.initialBranch || "main";
command += ` -b "${branchNameToUse.replace(/"/g, '\\"')}"`;
// Add the target directory path at the end
command += ` "${targetPath}"`; //<---
logger.debug(`Executing command: ${command}`, { ...context, operation });
const { stdout, stderr } = await execAsync(command);