modelcontextprotocol / modelcontextprotocol/python-sdk

Don’t use shell=True in mcp dev subprocess on Windows (command injection risk)

未关闭 适合新手
#1,257 3 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

bug fix proposed P1 ready for work
主要语言
Python
星标
24.3k
派生
4k
平均合并
1 天 1 小时
30 天内合并 PR
31

描述

Initial Checks
Description

What’s happening:

When you run the mcp dev command on Windows, it starts another program using a method (subprocess.run with shell=True) that lets the Windows command prompt (cmd.exe) handle the command. This is risky because if any part of the command includes special characters (like &, |, %, etc.), Windows might run something you didn’t expect — even another program, if the file path or arguments are weirdly named or crafted.

Why this is a real problem:

  • This isn’t just a theory — it’s a well-known risk with shell=True in Python. If anyone (or any script) can control part of the file path or arguments, they might be able to run extra commands on your computer.
  • The Python documentation says to avoid shell=True when possible for exactly this reason.
  • The fix is easy: use shell=False and make sure the right Windows executable is picked (like npx.cmd).
  • This keeps things safe and works the same on all systems.

What should happen instead:

  • The command should be run without shell=True on Windows, just like it is on Linux/Mac.
  • File paths and arguments should always be passed as a list, not a single string.

How this could be abused:

  • If someone manages to sneak a file or argument with a shell special character into your project, running mcp dev could run extra commands (for example, opening Calculator if the file had &calc in its name).

Please fix:

  • Remove shell=True from the subprocess.run call in src/mcp/cli/cli.py (Windows part).
  • Make sure the command and its arguments are always passed as a list.
  • Make sure it works on Windows by using the right executable (like npx.cmd).

Thanks!

Example Code
# Example of risky situation on Windows:
# If a file is named "server&calc.py" and you run:
#   mcp dev path\to\server&calc.py
# Windows might run Calculator because of the &

# Please see src/mcp/cli/cli.py (mcp dev command) for the subprocess.run([npx_cmd, ...], shell=True, ...)
Python & MCP Python SDK
Python 3.11, Windows 11, latest MCP Python SDK (main branch, August 2025)

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

从 src/mcp/cli/cli.py 中针对 Windows 的 mcp dev subprocess.run 调用开始。检查命令和参数是如何组装的,然后验证是否为 Windows 适当地选择了可执行文件,以及参数是否以列表形式传递且未使用 shell=True。完成的标准是 mcp dev 能在 Windows 上运行,并且路径或参数中的 shell 元字符不会被解释为额外的命令。

由索引模型根据 Issue 内容生成。

评估

技术栈
python
领域
cli, security
Issue 类型
缺陷
难度
2/5
预计耗时
1-3 小时
活跃度
冷清
描述清晰度
描述清楚
新手友好度
72/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。