fee-191/security-template

GitHub: fee-191/security-template

面向加密货币交易所等高安全需求场景的可复用 DevSecOps 框架,集成自定义 Semgrep 规则、CI/CD 安全流水线和 AI 代码审查。

Stars: 1 | Forks: 0

# 安全模板 — DevSecOps + AI [![Security Scan](https://static.pigsec.cn/wp-content/uploads/repos/2026/06/aee35bacbd201741.svg)](https://github.com/fee-191/security-template/actions/workflows/security.yml) [![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](LICENSE) ## 背景 — 本项目存在的原因 加密货币交易所是软件中风险最高的环境:余额、提现或身份验证中的任何逻辑错误都可能导致用户资产遭受直接且不可逆的损失。 | Incident | 年份 | 损失 | 根本原因 | |---|---|---|---| | **Bybit** | 2025 | $1.5B | Supply chain — JS 库通过钓鱼攻击被入侵 | | **Mixin Network** | 2023 | $200M | 云端数据库被入侵 + S3 上的 JS 代码被篡改 | | **Upbit** | 2019 | $50M | 热钱包耗尽 | 本模板可以检测出导致上述事件的模式 — 硬编码的 secrets、eval injection、静态 nonce、用于货币的 float、未加行锁的余额读取 — 这些都是在开发者提交代码时即刻发现,而不是在生产环境上线后。 **合规性:** 越南的加密货币交易所(决议 05/2025/NQ-CP)必须遵守第 4 级信息安全(ATTT)要求以及关于个人数据保护的 NĐ 356/2025。本模板将这些要求直接集成到编码工作流程中。 ## 核心亮点 | | | |---|---| | Semgrep rules | 自定义规则 · 5 种语言 (Python, JS/TS, Kotlin, Swift, Java) · 积极维护 | | 测试套件 | 3 种类型:block / warn-only / false-positive · CI 已验证 | | CI/CD | **GitLab CI** (5 个作业) + **GitHub Actions** | | AI 集成 | **Claude Code** 技能、子代理、斜杠命令 | | 合规性 | NĐ 356/2025 (PII)、第 4 级 ATTT、2022 年 PCRT 法 | ## 4 层防御架构 | 层级 | 工具 | 触发时间 | 级别 | |---|---|---|---| | **1 — Pre-commit** | Gitleaks · Bandit · Semgrep (自定义 CEX 规则) | 每次 `git commit` (~5 秒) | 强制 | | **2 — CI/CD** | Gitleaks · Semgrep · Bandit · pip-audit | 每次 MR/PR | 强制 | | **3 — AI 扫描** | Claude Code `/cex-security` | MR 之前,手动执行 | 推荐 | | **4 — AI 威胁建模** | Claude Code `/threat-model` | 编写功能代码之前 | 推荐 | 第 1 层和第 2 层在**两个独立节点**强制执行:`--no-verify` 可以绕过 pre-commit,但 CI/CD 仍会运行,并在发现 CRITICAL 漏洞时阻止 MR。 ## 规则覆盖范围 — 5 种语言 | 语言 | 规则数 | 覆盖范围 | |---|---|---| | Python | 23 | SQLi、eval/exec injection、弱加密、静态 nonce、余额竞态条件、硬编码 secrets、用于密码的 SHA-256 | | JavaScript / TypeScript | 15 | 命令注入、eval、弱哈希、AES-ECB、静态 IV、用于货币的 float | | Kotlin / Android | 8 | 弱哈希、SQL 注入、敏感日志、静态 IV、弱随机数、不安全的 SharedPreferences | | Swift / iOS | 7 | 弱哈希、不安全的 Keychain、敏感日志、静态 IV、弱随机数 | | Java | 5 | 与 Kotlin 规则共享 | ### 严重性策略 | 严重性 | 行动 | |---|---| | `ERROR` (CRITICAL) | 阻止 commit + 阻止 MR/PR | | `WARNING` (HIGH) | 仅警告 — 开发者可见,但 MR 仍可合并 | ## 安装说明 ### 独立克隆 ``` git clone https://github.com/fee-191/security-template.git cd security-template bash scripts/test-ci-local.sh # phải 80/80 PASS ``` ### 作为 git submodule 添加到项目中 ``` cd /path/to/your-project # 1. 添加 submodule git submodule add https://github.com/fee-191/security-template.git security # 2. Commit git add .gitmodules security git commit -m "chore: add security template submodule" # 3. 运行 setup(复制 config 文件,安装 pre-commit hooks) bash security/scripts/setup-hooks.sh # 4. Commit config git add . git commit -m "chore: add security template config files" ``` 结果:`✅ 设置完成 — 版本 1.2.1`。 ## CI/CD 集成 ### GitLab CI ``` # 添加到 project 的 .gitlab-ci.yml 中 include: - project: 'your-gitlab-group/security-template' ref: 'v1.2.1' # thay bằng tag mới nhất file: '.gitlab-ci.yml' ``` 5 个自动作业:`security:gitleaks` · `security:semgrep-critical` · `security:semgrep-high` · `security:bandit` · `security:pip-audit` 可选:在 CI 变量中设置 `SLACK_WEBHOOK_URL`,以便在 CRITICAL 漏洞阻止 MR 时接收警报。 ### GitHub Actions 查看 [`.github/workflows/security.yml`](.github/workflows/security.yml) — 等效工作流,在每次 Pull Request 时自动运行。 ## Claude Code AI 集成 模板为 Claude Code 预配置了技能和代理: ``` /cex-security # scan uncommitted changes /cex-security all # scan toàn bộ codebase /cex-security diff main # scan diff với branch main /threat-model # phân tích STRIDE threat model trước khi code ``` 技能 `cex-security-scan`(位于 `.claude/skills/`)可以独立使用,以在对话中审查代码。 ## 系统要求 | 组件 | 版本 | |---|---| | Python | 3.10+ | | Git | 2.30+ | | pre-commit | 4.0+ | | Node.js | 18+ *(仅 Claude Code 需要)* | ## 误报处理 | 工具 | 抑制方法 | |---|---| | Semgrep | 在行尾添加 `# nosemgrep: rule-id` | | Bandit | 在行尾添加 `# nosec BXXX` | | Gitleaks | 将指纹添加到 `.gitleaksignore` | | detect-secrets | 在行尾添加 `# pragma: allowlist secret` | ## 更新模板 ``` cd security && git fetch --tags && git checkout cd .. && bash security/scripts/setup-hooks.sh git add security && git commit -m "chore: update security template to " git push ``` ## 文档 | 文件 | 内容 | |---|---| | [`docs/guide.md`](docs/guide.md) | 面向开发者的完整指南 | | [`docs/secure-checklist.md`](docs/secure-checklist.md) | 每次 MR 前的 15 项检查清单 | | [`docs/deck.html`](docs/deck.html) | 演示文稿 — 直接在浏览器中打开 | | [`CLAUDE.md`](CLAUDE.md) | 针对 AI 编程助手的规则绑定 | | [`CHANGELOG.md`](CHANGELOG.md) | 版本历史 | **相关:** [cex-incident-research](https://github.com/fee-191/cex-incident-research) — 对 CEX 黑客事件的分析,是设计这些规则的来源 · [lynxguard](https://github.com/fee-191/lynxguard) — 毕业设计项目的 SOC 平台 [GitHub Issues](https://github.com/fee-191/security-template/issues) · **fee-191 · 2026**
标签:AI代码审查, DevSecOps, IPv6支持, JS文件枚举, Semgrep, WordPress安全扫描, 上游代理, 数据可视化, 逆向工具, 金融安全, 静态代码扫描