GraphQL Cop接入CI/CD指南:JSON输出格式解析与自动化安全门禁
GraphQL Cop接入CI/CD指南:JSON输出格式解析与自动化安全门禁
GraphQL Cop 是一款轻量级 Python 编写的 GraphQL API 安全审计工具,内置 13 项常见 GraphQL 漏洞检测测试,官方定位就是适合在 CI/CD 中做安全检查。本文将带你把 GraphQL Cop 接入 CI/CD 流水线:完整解析它的 JSON 输出格式,再搭建一套按严重级别拦截的自动化安全门禁,让高风险 GraphQL 漏洞在代码合并前就被挡下。
🎯 GraphQL Cop 能检测哪些 GraphQL 风险?
GraphQL Cop 一次扫描覆盖三类风险,共 13 个测试用例,测试源码按风险类型组织在 lib/tests/ 目录下:
| 风险类别 | 覆盖的 GraphQL 测试 |
|---|---|
| 拒绝服务(DoS) | Alias Overloading、Batch Queries、Field Duplication、Directive Overloading、Circular Introspection |
| 跨站请求伪造(CSRF) | GET 请求查询、GET 执行 Mutation、urlencoded POST 查询 |
| 信息泄露(Info Leak) | Introspection、Field Suggestions、GraphiQL、Trace Mode |
每个测试都是独立模块,例如 lib/tests/info_introspect.py 负责 Introspection 检测,lib/tests/dos_alias_overloading.py 负责 Alias 过载检测,便于你按需裁剪。
📦 一键安装步骤
依赖极少(仅 requests、simplejson、termcolor、PySocks,见 requirements.txt),几分钟即可完成安装:
git clone https://gitcode.com/gh_mirrors/gr/graphql-cop
cd graphql-cop
python3 -m venv venv
source venv/bin/activate
python3 -m pip install -r requirements.txt
🔍 最快配置方法:看懂 JSON 输出格式
不带参数时扫描结果直接打印在终端,接入 CI/CD 的关键是加上 -o json 输出可解析的结构化结果:
python graphql-cop.py -t https://staging.example.com/graphql -o json
其中 -t 指定目标 URL;如果只给域名不指定路径,GraphQL Cop 会自动遍历 /graphql、/graphiql、/playground、/console 这些常见 GraphQL 路径(逻辑见 graphql-cop.py)。
输出是一个 JSON 数组,每个元素对应一项测试结果,并按 title 字母序排序(见 graphql-cop.py)。以 Trace Mode 检测为例,单条记录长这样:
[
{
"title": "Trace Mode",
"description": "Tracing is Enabled",
"impact": "Information Leakage",
"severity": "INFO",
"color": "green",
"result": false,
"curl_verify": "curl -X POST -H \"Content-Type: application/json\" -d '{\"query\": \"query { __typename }\"}' 'http://localhost:5013/graphql'"
}
]
逐字段解析如下:
| 字段 | 类型 | 作用 |
|---|---|---|
title |
string | 测试项名称,如 Introspection、Alias Overloading |
description |
string | 具体发现了什么问题 |
impact |
string | 风险类型:Denial of Service、Information Leakage、CSRF 等 |
severity |
string | 严重级别:HIGH / MEDIUM / LOW / INFO |
color |
string | 终端配色(red / green),仅用于终端展示 |
result |
boolean | 是否真的命中风险,安全门禁的核心判断字段 |
curl_verify |
string | 可复现的 cURL 命令,方便人工验证 |
curl_verify 由 lib/utils.py 中的 curlify 函数自动生成,每条发现都自带"复现命令",审计时可以直接复制执行,无需自己构造请求。
🚀 在 CI/CD 中搭建自动化安全门禁
GraphQL Cop 的退出码不会随检测结果变化,因此门禁判断需要流水线自己解析 JSON。下面是一个最小可用的 GitLab CI 示例:python:3.12-slim 镜像与官方 Dockerfile 保持一致,HIGH 级别风险直接让流水线失败:
stages:
- security
graphql-audit:
stage: security
image: python:3.12-slim
script:
- pip install -r requirements.txt
- python graphql-cop.py -t $STAGING_GRAPHQL_URL -o json > cop_result.json
- python - <<'EOF'
import json
findings = json.load(open('cop_result.json'))
high = [f['title'] for f in findings if f['result'] and f['severity'] == 'HIGH']
if high:
raise SystemExit('发现 %d 个高危 GraphQL 风险: %s' % (len(high), high))
print('GraphQL 安全门禁通过')
EOF
推荐的分级门禁策略:
| 严重级别 | 典型测试 | 门禁动作 |
|---|---|---|
| HIGH | Introspection、Alias Overloading、Field Duplication | 阻断流水线,禁止合并 |
| MEDIUM | GET 查询支持、GET 执行 Mutation | 记录告警,人工确认后放行 |
| LOW / INFO | GraphiQL、Trace Mode、Field Suggestions | 仅归档结果,跟踪趋势 |
把 cop_result.json 作为 CI artifact 保存,后续还能对比两次构建的结果,观察安全水位变化。
💡 进阶技巧清单
- Docker 镜像:项目自带 Dockerfile,
docker build -t graphql-cop:latest .构建后,CI 中直接docker run --rm graphql-cop:latest -t <目标URL> -o json,免去依赖安装。 - 带鉴权的 GraphQL 端点:用
-H '{"Authorization": "Bearer xxx"}'附加请求头,可以传多个-H;默认请求头在 config.py 中定义。 - 自定义路径与强制扫描:
-w endpoints.txt指定自定义 GraphQL 路径列表;端点无法被自动识别时加-f强制扫描。 - 调试模式:
-d会为每个请求附加X-GraphQL-Cop-Test头,在网关日志里定位是哪项测试发起了请求。 - 代理扫描:
-x http://127.0.0.1:8080可把流量转发给 Burp Suite 之类的代理,便于人工复核。
❓ 常见问题排查
| 现象 | 原因与解法 |
|---|---|
| 提示 "does not seem to be running GraphQL" | 端点未通过 GraphQL 探测,确认路径正确,必要时加 -f |
| 提示 "URL missing scheme" | -t 必须包含 http:// 或 https:// |
| 提示 "Cannot cast ... into header dictionary" | -H 的值必须是合法 JSON 字符串,如 {"key": "value"} |
| 网关误拦扫描流量 | 用 -d 调试模式识别测试请求,将 User-Agent: graphql-cop/* 加入白名单 |
✅ 小结
GraphQL Cop 把"GraphQL 安全审计"压缩成了一条命令:-o json 输出结构清晰、每条发现自带 curl_verify 复现命令,天然适合流水线消费。只要把 result 与 severity 两个字段接入门禁逻辑,你就拥有了一条自动化的 GraphQL 安全防线。更多参数与示例可参考 README.md 和 graphql-cop.py 中的完整参数定义。
