mcp安全扫描仪
一个小型的、无依赖性的CLI,扫描JS/TS/Python/Go代码库,查找MCP服务器和其他“LLM+工具”后端中出现的常见安全漏洞。
这是一个启发式扫描器。它旨在快速发现明显的错误(特别是在早期原型中),并提供一个简单的“高严重性问题上的失败CI”门。
安装/运行
# via npx from GitHub (no npm publish required)
# (pin to a tag or a full commit SHA)
npx --yes --package=github:TheodorNEngoy/mcp-safety-scanner#v0.4.9 mcp-safety-scan . --fail-on=high
# install globally from GitHub (optional)
npm i -g github:TheodorNEngoy/mcp-safety-scanner#v0.4.9
mcp-safety-scan . --fail-on=high
# via Docker (no Node install)
docker run --rm -v "$PWD:/repo" ghcr.io/theodornengoy/mcp-safety-scanner:v0.4.9 /repo --format=github --fail-on=high
# from a local checkout of this repo
npm test
# scan current dir
npm run scan
# scan a target path
node ./src/cli.js /path/to/repo
# json output
node ./src/cli.js /path/to/repo --format=json
# SARIF output (for GitHub code scanning)
node ./src/cli.js /path/to/repo --format=sarif > results.sarif
# fail if >= medium findings exist
node ./src/cli.js /path/to/repo --fail-on=medium
# generate a baseline file (ignore existing findings)
node ./src/cli.js /path/to/repo --write-baseline .mcp-safety-baseline.json
# use a baseline file (only new findings remain)
node ./src/cli.js /path/to/repo --baseline .mcp-safety-baseline.json --fail-on=high
# note: baseline fingerprints include line/column, so if you refactor/move code
# you may need to regenerate the baseline.
# ignore additional directories (by basename)
node ./src/cli.js /path/to/repo --ignore-dir=test --ignore-dir=__tests__
# scan only files listed in a text file (one per line, relative to the scan path)
git diff --name-only origin/main...HEAD > changed-files.txt
node ./src/cli.js . --files-from changed-files.txt
# include test files too (default is to skip common test filename patterns)
node ./src/cli.js . --include-tests抑制
谨慎使用注释指令(更倾向于解决根本问题):
// mcp-safety-scan ignore child-process-exec
execSync("echo hello");
// mcp-safety-scan ignore-next-line dangerous-eval
eval(userInput);# mcp-safety-scan ignore-next-line python-shell-exec
subprocess.run(user_input, shell=True)测试文件
默认情况下,扫描仪跳过常见的测试文件名模式以减少噪音:
*.test.*,*.spec.**_test.gotest_*.py,*_test.py- 文件下
test/,tests/,__tests__/,__mocks__/,fixtures/,__fixtures__/
使用 --include-tests (CLI)或 include-tests: "true" (GitHub Action输入)以包含它们。
GitHub行动
将此添加到工作流中。
笔记:
- 为了供应链安全,请使用完整的提交SHA。
- 为方便起见,使用释放标签(例如。
v0.4.9)或移动的主标签v0.
name: safety-scan
on: [pull_request]
jobs:
scan:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
- uses: TheodorNEngoy/mcp-safety-scanner@5e09227cf63d559ec211ad3d99dfd3272c5a31c3 # v0.4.9
with:
path: .
# files-from: changed-files.txt
# include-tests: "true"
# baseline: .mcp-safety-baseline.json
# ignore-dirs: test,__tests__
fail-on: high
format: github仅扫描PR中已更改的文件(可选,可减少噪音):
- uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Compute changed files
run: |
git diff --name-only "${{ github.event.pull_request.base.sha }}" "${{ github.sha }}" > changed-files.txt
- uses: TheodorNEngoy/mcp-safety-scanner@5e09227cf63d559ec211ad3d99dfd3272c5a31c3 # v0.4.9
with:
path: .
files-from: changed-files.txt
fail-on: high
format: github如果您不想依赖CI中的第三方操作,可以通过以下方式运行扫描程序 npx (固定)改为:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
- name: MCP safety scan (npx)
run: |
npx --yes --package=github:TheodorNEngoy/mcp-safety-scanner#v0.4.9 \
mcp-safety-scan . --format=github --fail-on=high或者通过Docker:
- uses: actions/checkout@v4
- name: MCP safety scan (docker)
run: |
docker run --rm -v "$GITHUB_WORKSPACE:/repo" \
ghcr.io/theodornengoy/mcp-safety-scanner:v0.4.9 \
/repo --format=github --fail-on=highSARIF上传(可选,在某些组织中需要权限):
name: safety-scan
on: [pull_request]
permissions:
contents: read
security-events: write
jobs:
scan:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
- uses: TheodorNEngoy/mcp-safety-scanner@5e09227cf63d559ec211ad3d99dfd3272c5a31c3 # v0.4.9
id: scan
with:
path: .
# files-from: changed-files.txt
# baseline: .mcp-safety-baseline.json
# ignore-dirs: test,__tests__
fail-on: none
format: sarif
sarif-output: mcp-safety.sarif
- uses: github/codeql-action/upload-sarif@v3
with:
sarif_file: ${{ steps.scan.outputs.sarif_file }}预承诺
如果你使用 pre-commit,您可以在提交时对更改的文件运行扫描程序:
repos:
- repo: https://github.com/TheodorNEngoy/mcp-safety-scanner
rev: v0.4.9
hooks:
- id: mcp-safety-scan
args: ["--fail-on=high"]
# args: ["--fail-on=medium", "--include-tests"]它标志着什么(初始规则)
- 通配符CORS(
Access-Control-Allow-Origin: *,cors({ origin: "*" })) - 反映CORS来源(
... = req.headers.origin) - CORS中间件默认值(例如。
cors()没有原产地限制,CORS(app)Python) - 绑定到所有接口(
0.0.0.0/::)(公共网络曝光) - 危险代码执行(
eval(,new Function(pythonexec() - Shell执行(节点
child_process.exec*,spawn("sh", ["-c", ...]),或spawn/execFile(..., { shell: true })pythonsubprocess(..., shell=True)去吧exec.Command("sh", "-c", ...)) - 可疑文件删除(
rmSync(/unlinkSync() - 记录请求标头(
console.log(req.headers...)) - 可信CORS与通配符/反射源相结合(关键)
- Web标准
Request.json()读取时没有明确的大小限制 - python
await request.body()/await request.json()读取时没有明确的大小限制 - 去
io.ReadAll(r.Body)读取时没有明确的大小限制
扫描的文件类型: .js, .mjs, .cjs, .ts, .tsx, .jsx, .mts, .cts, .gs (谷歌应用脚本), .py, .go.
退出代码
0:未发现高于或等于您的--fail-on阈值1:发现的结果达到或超过阈值2:CLI使用错误
