SYCO7/codemoat

GitHub: SYCO7/codemoat

一款封装 Semgrep 与 Gitleaks 并附带 AI 专属规则集的安全扫描器,旨在检测 AI 编码助手在代码中引入的典型漏洞。

Stars: 0 | Forks: 0

# CodeMoat [![CI](https://static.pigsec.cn/wp-content/uploads/repos/cas/ad/ad5834178f7599af9fdda11629d49cae07f2997beec49821b2920eff5bfd50e7.svg)](https://github.com/SYCO7/codemoat/actions/workflows/ci.yml) [![License: MIT](https://img.shields.io/badge/license-MIT-blue.svg)](./LICENSE) AI 生成代码的安全扫描器。CodeMoat 会扫描你的 pull request diff, 专门检测 AI 编码助手(如 Claude Code、Cursor、 Copilot 等)实际引入的漏洞类别——包括硬编码的 secret、注入 缺陷、不安全的反序列化,以及一系列 AI agent 特有的隐患,例如 通配符 CORS、被禁用的 auth middleware,以及被遗忘的 debug 路由——并 发布一条持续更新的 PR 评论,其中包含严重程度、CWE 引用以及 建议的修复方案。 CodeMoat 并非重新发明静态分析。它封装了两个成熟的 开源引擎,并增加了一项它们所不具备的功能: - **[Semgrep](https://semgrep.dev)** — 基于模式的 SAST,使用真实的 registry 规则集(`p/security-audit`、`p/owasp-top-ten`、`p/secrets`)。 - **[Gitleaks](https://github.com/gitleaks/gitleaks)** — secret 检测。 - **CodeMoat AI 规则集** — 包含 12 条 Semgrep 规则,专门针对 AI agent 编写的代码中特有的失败模式(见[下文](#the-ai-ruleset))。 每个发现都会附带真实的 CWE ID,并与 [cwe.mitre.org](https://cwe.mitre.org) 交叉链接。 ## 为什么 已发表的独立研究一致发现,AI 生成的代码 引入漏洞的比率很高: - Veracode 的 [2025 GenAI 代码安全报告](https://www.veracode.com/resources/analyst-reports/2025-genai-code-security-report/) 发现,在针对 100 多个 LLM 的测试中,**45%** 的测试里 AI 生成的代码引入了 OWASP Top 10 安全缺陷。 - NYU Tandon 的 [“在键盘前睡着了?”](https://cyber.nyu.edu/2021/10/15/ccs-researchers-find-github-copilot-generates-vulnerable-code-40-of-the-time/) 研究发现,在涵盖 89 种场景的 1,692 个 GitHub Copilot 生成的程序中,**约 40%** 包含可利用的 bug 或设计缺陷。 - 一项针对 GitHub 项目中真实 Copilot 生成代码的 2025 年实证 研究 ([arXiv:2310.02059](https://arxiv.org/abs/2310.02059))发现,**29.5%** 的 Python 代码片段和 **24.2%** 的 JavaScript 代码片段 存在安全弱点。 通用的 SAST 工具能检测出其中一部分,但却会漏掉那些专门针对 编码 agent 失败模式的模式——例如为了疏通前端而添加的 CORS 通配符, 在调试时“暂时”注释掉的 auth 检查,或者是 搭建后从未移除的 `/debug` 路由。CodeMoat 的 AI 规则集正是指向 这一空白领域。 ## 安装(GitHub Action) 添加一个在 pull request 上运行的工作流: ``` # .github/workflows/codemoat.yml name: CodeMoat on: pull_request: permissions: contents: read pull-requests: write jobs: scan: runs-on: ubuntu-latest steps: - uses: actions/checkout@v7 with: fetch-depth: 0 - uses: SYCO7/codemoat@v1 with: fail-on-severity: high # critical | high | medium | low | none ``` 这就是全部设置。无需账户、无需 API key,也不会向任何 CodeMoat 运营的服务发起网络调用——CodeMoat 只会与 PyPI、GitHub Releases 和 Semgrep registry 通信,以获取其自带的扫描器。 ### Action 输入 | 输入 | 默认值 | 描述 | |---|---|---| | `github-token` | `${{ github.token }}` | 用于读取 PR 文件列表以及发布/更新结果评论的 token。 | | `fail-on-severity` | `none` | 导致检查失败的最低严重程度:`critical`、`high`、`medium`、`low` 或 `none`。 | | `semgrep-configs` | *(空)* | 除默认配置外,还要运行的额外 Semgrep registry 配置(以逗号分隔)。 | | `api-key` | *(空)* | CodeMoat 仪表盘的 API key。设置后,结果**也会**被 POST 到仪表盘。保持不设置则以完全独立自包含的方式运行。 | | `api-url` | *(空)* | 覆盖仪表盘的 API 基础 URL(用于自托管/测试)。 | | `semgrep-version` | `1.168.0` | 锁定的 Semgrep 版本。默认锁定,这样上游发布就不会静默改变你的结果。 | | `gitleaks-version` | `8.30.1` | 锁定的 Gitleaks 版本,理由同上。 | ### Action 输出 | 输出 | 描述 | |---|---| | `findings-count` | 报告的发现总数。 | | `critical-count` | critical(严重)级别的发现数量。 | ## 安装(CLI) 在推送之前在本地运行 CodeMoat——无需 GitHub Action。CLI 尚未 发布到 npm registry,因此请从源码安装: ``` git clone https://github.com/SYCO7/codemoat.git cd codemoat npm ci && npm run build npm link # puts `codemoat` on your PATH codemoat scan ``` 默认情况下,它会扫描你未提交和未跟踪的更改。实用标志: ``` codemoat scan [options] --base Diff against this git ref (e.g. origin/main). --path Repo directory to scan. Default: current directory. --fail-on Exit non-zero at or above this severity: critical|high|medium|low|none. Default: high. --all Scan every tracked file instead of just the diff. ``` 需要在你的 `PATH` 中包含 [`semgrep`](https://semgrep.dev/docs/getting-started/) 和 [`gitleaks`](https://github.com/gitleaks/gitleaks#installing)。 ### 示例输出 ``` Scanning 2 file(s) against base 4c1a9f2e... CRITICAL src/routes/upload.py:41 [codemoat-path-traversal-unsanitized] (CWE-22) A filesystem path is built by joining a directory with a raw request-supplied value. Without sanitizing the segment and verifying the resolved path stays inside the intended directory, an attacker can supply "../" sequences to read or write files outside the intended folder. HIGH src/middleware/cors.ts:12 [codemoat-cors-wildcard-credentials] (CWE-942) CORS is configured with a wildcard origin ("*") combined with credentials: true. 2 issue(s) found. ``` ## AI 规则集 包含十二项 Semgrep 规则,每项都有对应的真实 CWE,每项都经过 手动编写的易受攻击样本(真阳性)和修复后的对照样本 (无误报)的验证——参见 `samples/vulnerable/` 和 `samples/safe/`。 | 规则 | CWE | |---|---| | `codemoat-cors-wildcard-credentials` | [CWE-942](https://cwe.mitre.org/data/definitions/942.html) | | `codemoat-debug-route-no-guard` | [CWE-489](https://cwe.mitre.org/data/definitions/489.html) | | `codemoat-commented-auth-middleware` | [CWE-306](https://cwe.mitre.org/data/definitions/306.html) | | `codemoat-flask-debug-mode-enabled` | [CWE-489](https://cwe.mitre.org/data/definitions/489.html) | | `codemoat-django-debug-true` | [CWE-489](https://cwe.mitre.org/data/definitions/489.html) | | `codemoat-weak-hardcoded-credential` | [CWE-259](https://cwe.mitre.org/data/definitions/259.html) | | `codemoat-jwt-algorithms-none` | [CWE-347](https://cwe.mitre.org/data/definitions/347.html) | | `codemoat-eval-request-input` | [CWE-95](https://cwe.mitre.org/data/definitions/95.html) | | `codemoat-path-traversal-unsanitized` | [CWE-22](https://cwe.mitre.org/data/definitions/22.html) | | `codemoat-insecure-pickle-deserialize` | [CWE-502](https://cwe.mitre.org/data/definitions/502.html) | | `codemoat-insecure-yaml-load` | [CWE-502](https://cwe.mitre.org/data/definitions/502.html) | | `codemoat-ssrf-unvalidated-url` | [CWE-918](https://cwe.mitre.org/data/definitions/918.html) | ## 局限性 值得直说,这样你可以判断它是否合适: - **AI 规则集是基于模式的,不进行污点分析。** 这些是 Semgrep 语法模式和正则表达式。它们仅在列出的规则所描述的形状上 触发,仅此而已——`fetch(req.body.url)` 会被捕获,而一个先通过三个 辅助函数传递的 URL 则不会。高精度,刻意 限制了召回率。内置的 registry 规则包(`p/security-audit`、 `p/owasp-top-ten`)执行更广泛的扫描。 - **除样例外,不做无误报的声明。** 每条规则都在 CI 中通过一个 手动编写的漏洞文件和一个修复后的对照样本进行了验证。这证明了 该规则会触发,并且对于明显的安全重写不会触发。这 并不是在真实语料库上的基准测试结果,也没有被当作 基准测试结果来呈现。 - **CodeMoat 无法区分 AI 编写的代码和人类编写的代码。** 它针对的是 在 agent 编写的代码中出现比例异常高的漏洞*类别*。无论 是谁写的,它都会扫描你的整个 diff。 - **设计上限定在 diff 范围内。** 仅扫描 PR 中更改的文件; 未触及的文件中原有的漏洞将不会被报告。请使用 `codemoat scan --all` 进行全仓库扫描。 - **语言覆盖范围取决于规则。** AI 规则集涵盖 JavaScript、 TypeScript 和 Python。Semgrep 的 registry 规则包覆盖范围要广得多,因此 其他语言仍然会被扫描——只是没有针对 AI 的特定 层面。 ## 免费与付费 | | 免费 | 付费 | |---|---|---| | 公开仓库 | 无限次扫描 | — | | 私有仓库 | — | ✅ | 账单由 [Dodo Payments](https://dodopayments.com) 处理。从 仪表盘升级会创建一个托管的结账会话;在支付成功后, webhook 会将组织切换到付费计划(并在取消、过期或支付失败时 切回免费计划)。 ## 开发 ``` npm install npm run build # tsc -> dist/ npm test # unit + integration tests (requires semgrep + gitleaks on PATH) npm run typecheck ``` 需要 Node 20+,且本地需安装 [`semgrep`](https://semgrep.dev) 和 [`gitleaks`](https://github.com/gitleaks/gitleaks) 才能运行 测试套件(集成测试会直接调用真实的二进制文件——没有 使用 mock)。 ## 安全 发现了 CodeMoat 本身的漏洞?请参见 [SECURITY.md](./SECURITY.md) — 请私下报告,不要作为公开的 issue 提出。 ## 许可证 MIT — 参见 [LICENSE](./LICENSE)。
标签:AI代码审计, DevSecOps, GitHub Action, MITM代理, SAST, StruQ, 上游代理, 代码安全, 文档安全, 机密检测, 漏洞枚举, 盲注攻击, 自动化攻击, 静态代码扫描