FasterApiWeb/leash-secrets
GitHub: FasterApiWeb/leash-secrets
一款面向 AI 编码 Agent 的机密信息实时防护插件,在 AI 写入代码时即拦截 API 密钥、密码等敏感凭证,防止其进入代码库。
Stars: 1 | Forks: 2
leash-secrets
你的 AI 编码飞快。leash-secrets 确保它不会带走你的机密信息。
安装 · 查看效果 · 命令 · 模式 · 基准测试 · 贡献
更多示例 —— AWS、OpenAI、数据库、SSH key
### AWS 凭证 ``` ⛔ LEASH-SECRETS — SECRET DETECTED ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ Type: AWS Access Key ID File: config.py:12 Value: AKIAI....3Q7A Risk: Full access to AWS account. Attacker can spin up instances, access S3 buckets, read databases, and run up your bill. ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ FIX: Use AWS_ACCESS_KEY_ID env var or IAM roles ROTATE: https://console.aws.amazon.com/iam/home#/security_credentials ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ ``` ### 数据库连接字符串 ``` ⛔ LEASH-SECRETS — SECRET DETECTED ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ Type: PostgreSQL Connection String with Password File: database.ts:5 Value: postgr....m/db Risk: Direct access to the database. Attacker can read, modify, or delete ALL data. ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ FIX: Use DATABASE_URL environment variable ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ ``` ### 提交到 Git 的 .env 中的 OpenAI API Key ``` ⛔ LEASH-SECRETS — SECRET DETECTED ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ Type: OpenAI Project API Key File: .env:3 Value: sk-pro....Q7xR Risk: Access to OpenAI API. Attacker can run models and incur significant charges on your account. ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ FIX: .env should NEVER be committed. 1. Add .env to .gitignore 2. Remove from git: git rm --cached .env 3. Rotate the key: https://platform.openai.com/api-keys ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ ``` ### SSH 私钥 ``` ⛔ LEASH-SECRETS — SECRET DETECTED ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ Type: OpenSSH Private Key File: deploy/id_rsa:1 Value: -----BEGIN OPENSSH PRIVATE KEY----- Risk: Grants SSH access to any server that has the corresponding public key in authorized_keys. ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ FIX: Never commit SSH keys. 1. Remove the file from the repo 2. Add *.pem, id_rsa, id_ed25519 to .gitignore 3. Use SSH agent forwarding or deploy keys 4. If committed, the key is compromised — generate a new one ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ ```为单个 Agent 或其他 20 多个 Agent 中的任何一个安装
### Cursor 将规则文件复制到你的项目或全局规则中: ``` # 项目级(推荐) cp .cursor/rules/leash-secrets.mdc your-project/.cursor/rules/ # 全局 cp .cursor/rules/leash-secrets.mdc ~/.cursor/rules/ ``` ### Claude Code ``` # 通过 skill cp skills/leash-secrets.md ~/.claude/skills/ # 或添加到 CLAUDE.md cat AGENTS.md >> your-project/CLAUDE.md ``` ### GitHub Copilot ``` # 项目级 cp .github/copilot-instructions.md your-project/.github/ # 全局 cp .github/copilot-instructions.md ~/.copilot/copilot-instructions.md ``` ### Codex ``` cp AGENTS.md ~/.codex/AGENTS.md ``` ### Gemini CLI ``` gemini extensions install https://github.com/FasterApiWeb/leash-secrets ``` ### Windsurf ``` cp skills/leash-secrets.md your-project/.windsurf/rules/leash-secrets.md ``` ### Cline ``` cp skills/leash-secrets.md your-project/.clinerules/leash-secrets.md ``` ### Kiro ``` cp skills/leash-secrets.md your-project/.kiro/steering/leash-secrets.md ``` ### Aider Leash 通过 `AGENTS.md` 约定与 Aider 协同工作: ``` cp AGENTS.md your-project/AGENTS.md ``` ## 命令 | 命令 | 功能 | |---------|-------------| | `/leash-secrets [patrol\|sweep\|lockdown\|off]` | 设置模式或显示当前模式 | | `/leash-secrets-scan` | 扫描当前文件或暂存的 diff 中的机密信息 | | `/leash-secrets-audit` | 完整的 repo 审计 —— 每个文件,每种模式,评分 A–F | | `/leash-secrets-fix` | 自动修复检测到的机密信息(替换为环境变量) | | `/leash-secrets-report` | 生成可共享的安全报告 | | `/leash-secrets-help` | 快速参考 | ### 模式 | 模式 | 行为 | |------|----------| | `patrol` *(默认)* | 扫描 Agent 编写的所有内容。阻止关键风险,对警告发出提醒。 | | `sweep` | 仅按需扫描。与 `/leash-secrets-scan` 搭配使用。 | | `lockdown` | 阻止所有发现,包括警告。用于发布前审计。 | | `off` | 禁用 leash-secrets。你的机密信息现在是你自己的问题了。 | ## 模式 Leash Secrets 跨 11 个提供商类别检测 **70 多种机密类型**: | 类别 | 检测到的机密 | |----------|-----------------| | **AWS** | Access Key ID、Secret Access Key、Session Token、MWS Key | | **GCP** | API Key、OAuth Client Secret、Service Account Key、Firebase Config | | **Azure** | Storage Account Key、Client Secret、Connection String、SAS Token | | **GitHub & Git** | PAT(classic + fine-grained)、OAuth、App token、GitLab PAT、Bitbucket | | **AI 提供商** | OpenAI、Anthropic、Cohere、Hugging Face、Replicate | | **支付** | Stripe(live/test/webhook)、PayPal、Square | | **数据库** | PostgreSQL、MySQL、MongoDB、Redis、Supabase、PlanetScale | | **消息传递** | Slack(bot/user/webhook)、Discord、Twilio、SendGrid、Mailgun、Telegram | | **CI/CD** | npm、PyPI、Docker Hub、Vercel、Netlify、Heroku、Terraform、CircleCI | | **加密** | RSA、OpenSSH、EC、DSA、PGP 私钥、PKCS12 密码 | | **通用** | 密码、机密信息、API key、JWT secret、Bearer token、加密密钥 | 每种模式包括: - 用于检测的 **正则表达式** - 解释攻击者可以做什么的 **风险评估** - 包含特定环境变量名称和方法的 **修复** - 适用情况下的 **轮换 URL** ### 添加自定义模式 Leash Secrets 的模式是可扩展的 JSON 文件。添加你自己的模式: ``` { "provider": "my-company", "display_name": "Internal Services", "patterns": [ { "id": "internal-api-key", "name": "Internal API Key", "severity": "critical", "regex": "myco_[a-zA-Z0-9]{32}", "description": "Internal service API key", "risk": "Access to internal APIs", "fix": "Use INTERNAL_API_KEY environment variable" } ] } ``` 保存到 `patterns/my-company.json` 并将其添加到 `patterns/index.json` 中。有关完整 schema,请参阅 [docs/patterns/custom-patterns.md](docs/patterns/custom-patterns.md)。 ## 基准测试 Leash Secrets 通过此 repo 中可重现的 fixture 套件 (`npm test`) 进行了验证。有关针对已撤销 secret 语料库的更广泛的历史基准测试,请参阅方法论页面。 | 指标 | 无扫描 | “检查机密信息”提示 | Leash Secrets | |--------|----------:|---------------------------:|--------------:| | **捕获的机密** | 0% | 41% | **94%** | | **误报** | 0 | 12% | **3%** | | **自动修复准确性** | N/A | 22% | **89%** | | **Agent 速度影响** | 基准 | +2% | **+5%** | 在受控的基准测试运行中,Leash Secrets 捕获了 94% 的机密信息,因为它使用 **特定的 regex 模式**,而不是依赖 LLM 的一般判断。6% 的遗漏率主要是由于新颖/不常见的机密格式 —— 贡献模式以消除差距。 ## 工作原理 1. **安装** 将一个 skill/rule 文件放入你的 AI Agent 中 2. **Skill** 指示 Agent 在每次代码更改时运行 Leash 协议: - 针对超过 70 种机密模式 **扫描** 每一行 - 将发现 **分类** 为关键/警告/安全 - 使用脱敏的输出和特定的修复方案 **阻止** 关键发现 - 对可能的机密发出 **警告** 并要求确认 3. **Pre-commit hook** 作为后备,捕获 Agent 遗漏的任何内容 4. **模式库** 是可扩展的 JSON —— 添加你自己的模式,向上游贡献 没有服务器,没有 API,没有遥测。Leash 是一个提示词和一个模式库。一切都在本地运行。 ``` ┌──────────────┐ ┌──────────┐ ┌───────────────┐ │ AI Agent │───▶│ leash- │───▶│ Your Code │ │ writes code │ │ secrets │ │ (no secrets) │ └──────────────┘ └──────────┘ └───────────────┘ │ ┌────┴────┐ │ 🔴 STOP │ ← blocks before │ 🟡 WARN │ the code exists │ 🟢 PASS │ └─────────┘ ``` ## 为什么使用 Leash Secrets 而不仅仅是 truffleHog/gitleaks/git-secrets? 那些工具非常出色。也可以使用它们。Leash 与众不同之处在于: | 功能 | truffleHog / gitleaks | leash-secrets | |---------|:--------------------:|:-----:| | 扫描已提交的代码 | ✅ | ✅(通过审计) | | 扫描 git 历史 | ✅ | ✅(通过审计) | | Pre-commit hook | ✅ | ✅ | | **在 AI 代码生成期间捕获机密信息** | ❌ | ✅ | | **理解代码上下文**(测试 vs 生产) | ❌ | ✅ | | **提供上下文修复** | ❌ | ✅ | | **在 20 多种 AI Agent 内部运行** | ❌ | ✅ | | **除了文本文件外无需安装** | ❌ | ✅ | | **任何人都可以贡献的可扩展模式 JSON** | 视情况而定 | ✅ | 结合使用 leash-secrets + truffleHog/gitleaks 以实现深度防御。Leash Secrets 在创建时捕获机密信息。传统工具捕获任何漏网之鱼。 ## 隐私 Leash Secrets 不会回传数据。没有遥测,没有分析,没有账户,没有后端。该 skill 是一个 Markdown 文件。模式是 JSON 文件。该 hook 是一个 bash 脚本。一切都在本地运行,由你的 AI Agent 自己的上下文窗口进行评估。 网络调用仅在安装期间发生(从 GitHub 获取文件),并记录在 [SECURITY.md](SECURITY.md) 中。 ## 在任何地方使用 ### CLI ``` npm install -g leash-secrets leash-secrets scan . # Scan current directory leash-secrets scan src/ --verbose # With risk details leash-secrets scan config.yml --json # JSON output for CI leash-secrets report . # Markdown security report leash-secrets patterns # List all 71 patterns leash-secrets validate # Validate pattern files ``` ### GitHub Action 添加到 `.github/workflows/secret-scan.yml`: ``` name: Secret Scan on: [push, pull_request] jobs: leash-secrets: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 with: fetch-depth: 0 - uses: FasterApiWeb/leash-secrets/action@leash-secrets-v1.1.0 ``` 有关配置选项(`scan-mode`、`fail-on`、`exclude` 等),请参阅 [action/README.md](action/README.md)。 ### GitLab CI ``` include: - remote: 'https://raw.githubusercontent.com/FasterApiWeb/leash-secrets/main/action/gitlab-ci-template.yml' ``` ### VS Code 扩展 已打包就绪,marketplace 发布待定: ``` npm run package-extension code --install-extension vscode-extension/leash-secrets-vscode-*.vsix ``` 键入时提供实时的内联诊断、workspace 扫描以及状态栏指示器。请参阅 [vscode-extension/README.md](vscode-extension/README.md)。 ### 编程 API ``` const { loadPatterns, scanFile, scanString } = require('leash-secrets'); const findings = scanFile('config.py'); // or const findings = scanString('api_key = "sk_live_abc123..."', { filename: 'app.py' }); findings.forEach(f => { console.log(`${f.severity}: ${f.pattern.name} at line ${f.line}`); }); ``` ### 本地开发设置 ``` git clone https://github.com/FasterApiWeb/leash-secrets.git cd leash-secrets # 运行测试(零依赖,无需安装) make test # 扫描仓库本身的 secrets make scan # 在本地提供 http://127.0.0.1:8000 的文档服务 make docs-serve # 查看所有可用命令 make help ``` ### 在提交 PR 之前进行本地测试 ``` make test # Validate patterns + run regex tests make lint # Check shell scripts make scan # Dogfood: scan own repo make docs # Build docs site ``` ### CI/CD 流水线工作原理 | 工作流 | 触发器 | 功能 | |----------|---------|-------------| | **CI** | 每次 push 和 PR | 在 Node 18/20/22 上测试、shell linting、dogfood 扫描 | | **验证模式** | 触及 `patterns/` 的 PR | 验证模式 schema 和正则表达式测试 | | **发布** | push 到 main | release-please 创建发布 PR,发布到 npm,上传 assets | | **部署文档** | push 到 main(文档更改时) | 构建 MkDocs,部署到 GitHub Pages | ### 约定式提交 我们使用[约定式提交](https://conventionalcommits.org)进行自动化发布: ``` feat: add Datadog API key pattern → minor version bump fix: reduce false positives for JWT → patch version bump pattern: add Twilio phone SID detection → patch version bump docs: update installation guide → no version bump ``` ## 发布与交付 ### npm 发布通过 release-please 自动化。当你合并带有约定式提交的 PR 时: 1. release-please 会开启一个提升版本的“发布 PR” 2. 合并发布 PR 会触发: - 带有 provenance 的 `npm publish` - 包含 `.tar.gz` 和 `.zip` assets 的 GitHub Release - 更新 CHANGELOG.md 手动发布(仅限维护者): ``` npm login npm publish --access public ``` ### VS Code Marketplace ``` cd vscode-extension npx @vscode/vsce package # Creates .vsix npx @vscode/vsce publish # Publish (requires PAT) ``` ### GitHub Pages(文档) push 到 main 时自动触发。可通过 Actions > Deploy Docs > Run workflow 手动触发。 文档 URL:**https://fasterapiweb.github.io/leash-secrets** ## 使用方 使用 leash-secrets 进行 AI Agent 机密信息检测的项目: | 项目 | 描述 | |---------|-------------| | [**LibreRing**](https://github.com/FasterApiWeb/laughing-chainsaw) | 开源 Oura Ring 客户端 —— BLE、HealthKit、Supabase 同步 | ## 可信度与规范 这个 repo 进行了 dogfood —— 该工具在每次 PR 和 push 时都会扫描自身。 | 检查项 | 位置 | |-------|-------| | 模式和 fixture测试 | [CI — Test](https://github.com/FasterApiWeb/leash-secrets/actions/workflows/ci.yml)(Node 18/20/22) | | Shell 脚本 lint | [CI — Shell Scripts](https://github.com/FasterApiWeb/leash-secrets/actions/workflows/ci.yml) | | Dogfood 扫描(源中无关键漏洞) | [CI — Dogfood](https://github.com/FasterApiWeb/leash-secrets/actions/workflows/ci.yml) | | 可重现的基准测试 | `npm test` + `node scripts/benchmark-summary.js` | | 安装冒烟测试 | `npm run verify` | ``` npm run verify # all local hygiene checks npm test node scripts/benchmark-summary.js node bin/leash-secrets.js scan src/ scripts/ hooks/ bin/ ``` 全局安装:`npm install -g leash-secrets` · curl 安装程序:请参阅[安装](docs/getting-started/installation.md) ## 路线图 - [x] **leash-secrets-ci** — GitHub Action / GitLab CI 集成 - [ ] **leash-secrets-vscode** — VS Code 扩展(已打包;marketplace 发布待定) - [ ] **leash-secrets-dashboard** — 团队范围的机密暴露指标 - [ ] **模式市场** — 社区贡献的模式包 - [ ] **熵检测** — 捕获不符合已知模式的机密信息 - [ ] **多语言修复模板** — 针对 15+ 种语言的自动修复标签:AI合规, Cutter, MITM代理, 暗色界面, 自定义脚本