CyberFvde/claude-security-reviewer
GitHub: CyberFvde/claude-security-reviewer
一款利用分层 Claude 模型在 GitHub Actions 中自动审查 PR 代码安全漏洞的 CI 工具,通过分级模型路由兼顾了审查深度与成本控制。
Stars: 0 | Forks: 0
# Claude 安全审查工具
[](https://github.com/CyberFvde/claude-security-reviewer/actions/workflows/security-review.yml)
[](LICENSE)
一个自动化的 Pull Request 安全审查工具,作为**必需的 CI 检查**运行,并通过**分层的 Claude 模型**路由审查结果,以平衡成本与分析深度。内置了针对 prompt injection 和数据泄露的防护机制,并映射到 [OWASP Top 10 for LLM Applications](https://owasp.org/www-project-top-10-for-large-language-model-applications/) 和 [NIST AI Risk Management Framework](https://www.nist.gov/itl/ai-risk-management-framework),从而确保该工具本身受到治理,可安全地在企业中引入。
## 工作原理
```
PR opened against main
│
▼
┌─────────────────────────────┐
│ Pre-LLM (deterministic) │ diff-only fetch · noise filters · size caps
│ │ secret REDACTION before any API call
│ │ prompt-injection pattern detection
└──────────────┬──────────────┘
▼
┌─────────────────────────────┐
│ Tier 1 · Haiku (triage) │ classifies each file: skip / standard / deep
└──────┬───────────────┬──────┘ path heuristics force auth/crypto/IaC/CI → deep
▼ ▼
┌─────────────┐ ┌─────────────┐
│ Tier 2 │ │ Tier 3 │
│ Sonnet │ │ Opus │ standard app code vs. security-critical
│ (standard) │ │ (deep) │ surfaces (authn/z, secrets, payments, CI/IaC)
└──────┬──────┘ └──────┬──────┘
└───────┬───────┘
▼
┌─────────────────────────────┐
│ Output guardrails │ strict JSON schema validation (fail-closed)
│ │ markdown neutralization + URL defanging
└──────────────┬──────────────┘
▼
One upserted PR comment · optional SARIF · pass/fail gate (fail-on: high)
```
## 快速安装(3 步)
**1.** 添加密钥 `ANTHROPIC_API_KEY`(在仓库或组织级别):*Settings → Secrets and variables → Actions*。
**2.** 添加 `.github/workflows/security-review.yml`:
```
name: Security Review
on:
pull_request:
branches: [main] # PRs to main only — cost control
types: [opened, synchronize, reopened, ready_for_review]
permissions:
contents: read
pull-requests: write
concurrency:
group: claude-security-review-${{ github.event.pull_request.number }}
cancel-in-progress: true # new pushes cancel in-flight reviews
jobs:
claude-security-review:
name: Claude Security Review
if: ${{ !github.event.pull_request.draft && !github.event.pull_request.head.repo.fork }}
runs-on: ubuntu-latest
timeout-minutes: 15
steps:
- uses: actions/checkout@v5
- uses: CyberFvde/claude-security-reviewer@v1
with:
anthropic-api-key: ${{ secrets.ANTHROPIC_API_KEY }}
```
**3.** 将其设为必需检查:*Settings → Branches → Add branch protection rule for `main` → Require status checks to pass → select `Claude Security Review`。* 从那时起,如果 PR 中存在达到或超过 `fail-on` 阈值的未解决发现,则无法合并。
完整的推广、调优和开发者接入指南:**[docs/ONBOARDING.md](docs/ONBOARDING.md)**。
## 成本控制
成本在设计上就是受限的,而不是靠运气:
1. **触发范围** — 仅在针对 `main` 分支的 PR 上运行;从不在直接推送、其他分支或计划任务上运行。
2. **并发取消** — PR 的新 commit 会取消正在进行的审查,因此快速连续的推送不会累积开支。
3. **仅 Diff 分析** — 仅发送已更改的补丁,绝不发送整个仓库。
4. **噪声过滤器** — lockfiles、压缩/生成/vendored/二进制文件会在消耗任何 token 之前被丢弃。
5. **硬性上限** — `max-files`(默认为 40)和 `max-patch-bytes`(默认为 300 KB)限制了每次运行的最坏情况。
6. **分层路由** — 最便宜的模型 (Haiku) 阅读所有内容;中端模型 (Sonnet) 审查普通代码;最昂贵的模型 (Opus) **仅**对被分流标记为安全关键的文件运行。一个典型的 PR 只需几分钱;当前的各模型定价见:[Anthropic 文档](https://docs.claude.com/en/docs/about-claude/models/overview)。
## 配置
| 输入项 | 默认值 | 用途 |
|---|---|---|
| `anthropic-api-key` | — (必需) | Anthropic API key |
| `github-token` | `github.token` | 读取 diff,插入或更新一条 PR 评论 |
| `triage-model` | `claude-haiku-4-5-20251001` | Tier 1 分类器 |
| `review-model` | `claude-sonnet-4-6` | Tier 2 标准审查 |
| `deep-model` | `claude-opus-4-8` | Tier 3 深度审查 |
| `fail-on` | `high` | 导致检查失败的最低严重级别 (`critical`/`high`/`medium`/`low`/`none`) |
| `fail-open` | `true` | AI pipeline 本身报错时的行为 |
| `max-files` | `40` | 成本上限:每次运行的文件数 |
| `max-patch-bytes` | `300000` | 成本上限:每次运行的总 diff 字节数 |
| `sarif-file` | `""` | 可选的 SARIF 2.1.0 输出,用于代码扫描上传 |
## 工具自身的安全性
该审查器假设每一个 diff 都是恶意的。重点内容(完整映射见 **[docs/GOVERNANCE.md](docs/GOVERNANCE.md)**):
- **Prompt injection (LLM01):** diff 被包裹在每次请求随机生成的 nonce 边界中,并被声明为不受信任的数据;注入模式也*会*被确定性检测并作为发现报告 —— 在评论中写下“忽略之前的指令”的攻击者会被标记,而不是被听从。
- **敏感信息泄露 (LLM02):** 已知的密钥格式(AWS keys、GitHub/Slack tokens、私钥块、JWTs、凭据赋值)会在 diff 离开 runner **之前**在本地进行脱敏;脱敏命中的内容会作为确定性的 `critical` 发现呈现。
- **不当输出处理 / 数据泄露 (LLM05):** 模型输出必须能解析为严格的 JSON schema,否则将被丢弃;所有渲染到 PR 评论中的内容都经过了 Markdown 转义、HTML 实体编码、URL 失效处理,并去除了 @mention 功能 —— 没有链接/图像信标,没有 HTML,没有 ping。
- **过度权限 (LLM06):** 模型没有被赋予任何工具,job token 仅具有 `contents: read` + `pull-requests: write` 权限,并且该 action 仅有的副作用是一条评论和一个退出代码。
- **无限制消耗 (LLM10):** 上述成本上限同时起到了拒绝服务钱包攻击的保护作用。
## 本地开发
```
pip install -r requirements.txt pytest
pytest tests/ -q # guardrail suite — no API key or network needed
```
## 许可证
MIT © Tony Fadayomi ([@CyberFvde](https://github.com/CyberFvde))
标签:AI代码审查, CISA项目, Claude, CVE检测, LLM, NIST AI RMF, StruQ, Unmanaged PE, 逆向工具