mcp补丁
Python MCP服务器代码的静态安全扫描程序。
43%的流行MCP服务器存在shell注入漏洞。现有的工具都没有使用MCP上下文感知进行AST级扫描。这个确实如此。
这将捕获的真正CVE
- CVE-2025-53967 (Framelink Figma MCP)——通过未经消毒的工具参数进行壳体注射
- CVE-2025-6514 (mcp-remote,437K下载)--通过未经消毒的工具参数执行任意命令
安装
pip install mcp-patch
mcp-patch scan my_server.py用法
# Scan a single file
mcp-patch scan server.py
# Scan a directory
mcp-patch scan ./servers/输出示例
Scanning server.py...
CRITICAL shell_injection line 14
subprocess.run(f"ls {path}", shell=True)
subprocess.run(shell=True) — tool param 'path' flows to shell
Fix: Use subprocess.run([cmd, shlex.quote(arg)]) without shell=True
HIGH path_traversal line 28
open(filename)
open(filename) — tool param 'filename' used as file path without validation
Fix: Use (base_dir / Path(filename).name).resolve() and verify result starts with base_dir
Found 2 issues (1 CRITICAL, 1 HIGH) in 1 file.检查
| 检查 | 严重性 | 检测到什么 |
|---|---|---|
shell_injection | 关键 | subprocess.run/Popen/call(f"...{param}", shell=True), os.system(), os.popen() 使用工具参数 |
path_traversal | 高 | open(param), Path(param) 工具参数直接作为路径传递 |
ssrf | 高 | requests.get/post(url), httpx.get(url), urllib.request.urlopen(url) 哪里 url 是工具参数 |
仅装饰有 @tool 或 @mcp.tool() 被扫描。忽略普通辅助函数。
运作原理
纯stdlib。没有网络呼叫。没有法学硕士。使用以下命令解析Python源代码 ast 模块,查找 @tool 修饰函数,收集它们的参数名称,然后遍历每个函数体,寻找用户控制的参数流入危险水槽的危险调用模式。
假阳性
这是一个MVP扫描器——它更喜欢过度报告而不是错过真正的漏洞。A. path_traversal 查找on open(filename) 即使您在其他地方进行了运行时验证,它也是真实的;修复方法是将验证移动到同一功能中,以便扫描程序(和审阅者)可以看到它。
发展
python -m pytest tests/没有外部依赖关系。Python 3.9+。
