关于 secret-scanner
用于在代码库中发现意外暴露的密钥和凭据的安全技能,适合开发者在编辑文件、修改配置或提交代码时使用。它识别多类密钥格式,并检查敏感文件是否列入 .gitignore。
主要功能
- 01检测 API 密钥与令牌
- 02识别数据库凭据和连接串
- 03检查私钥与认证秘密
- 04提交前扫描并阻止泄露
- 05验证敏感文件的忽略规则
适用场景
- 01提交代码前检查暂存文件
- 02修改 .env 或配置文件后扫描
- 03排查代码中的硬编码凭据
- 04检查敏感文件是否被 Git 忽略
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ ovachiever/ droid-tings --skill secret-scanner安装后在 Claude Code 中输入/secret-scanner确认已加载。
附带文件README.md
详细介绍
密钥扫描器技能
防止代码库中的密钥意外泄露。
激活时机
- ✅ 执行 git 提交前
- ✅ 文件被修改或保存时
- ✅ 用户提到密钥、密钥字符串或凭据时
- ✅ .env 文件发生更改时
- ✅ 配置文件被修改时
检测内容
API 密钥和令牌
- AWS 访问密钥(AKIA...)
- Stripe API 密钥(sk_live_...、pk_live_...)
- GitHub 令牌(ghp_...)
- Google API 密钥
- OAuth 令牌
- JWT 密钥
数据库凭据
- 数据库连接字符串
- MySQL/PostgreSQL 密码
- MongoDB 连接 URI
- Redis 密码
私钥
- SSH 私钥
- RSA/DSA 密钥
- PGP/GPG 密钥
- SSL 证书
身份验证密钥
- 密码变量
- 身份验证令牌
- 会话密钥
- 加密密钥
告警示例
API 密钥检测
// You type:
const apiKey = 'sk_live_1234567890abcdef';
// I immediately alert:
🚨 CRITICAL: Exposed Stripe API key detected!
📍 File: config.js, Line 3
🔧 Fix: Use environment variables
const apiKey = process.env.STRIPE_API_KEY;
📖 Add to .gitignore: .env
AWS 凭据
# You type:
aws_access_key = "AKIAIOSFODNN7EXAMPLE"
# I alert:
🚨 CRITICAL: AWS access key exposed!
📍 File: aws_config.py, Line 1
🔧 Fix: Use AWS credentials file or environment variables
aws_access_key = os.getenv("AWS_ACCESS_KEY_ID")
📖 Never commit AWS credentials
数据库密码
# You type in docker-compose.yml:
environment:
DB_PASSWORD: "mySecretPassword123"
# I alert:
🚨 CRITICAL: Database password in configuration file!
📍 File: docker-compose.yml, Line 5
🔧 Fix: Use .env file
DB_PASSWORD: ${DB_PASSWORD}
📖 Add .env to .gitignore
检测模式
模式类型
高置信度:
- 已知的 API 密钥格式(Stripe、AWS 等)
- 私钥头部
- JWT 令牌
- 包含凭据的连接字符串
中等置信度:
- 名为 "password"、"secret"、"key" 的变量
- 敏感上下文中的 Base64 编码字符串
- 赋值中的长随机字符串
低置信度(标记以供审查):
- 通用密钥模式
- 注释中的潜在凭据
Git 集成
提交前保护
# Before commit, I scan:
git add .
git commit
# I block if secrets found:
🚨 CRITICAL: Cannot commit - secrets detected!
📍 3 secrets found:
- config.js:12 - API key
- .env:5 - Database password (in gitignore - OK)
- auth.js:45 - JWT secret
❌ Commit blocked - remove secrets first
.gitignore 验证
我会检查敏感文件是否已加入 .gitignore:
✅ .env - In .gitignore (good)
⚠️ config/secrets.json - NOT in .gitignore (add it!)
✅ .aws/credentials - In .gitignore (good)
误报处理
示例文件
// I understand these are examples:
// Example: const apiKey = 'your_api_key_here';
// TODO: Add your API key from environment
测试文件
// Test fixtures are OK (but flagged for review):
const mockApiKey = 'sk_test_1234567890abcdef'; // ✅ Test key
文档
Set your API key: `export API_KEY=your_key_here`
与 security-auditor 的关系
secret-scanner(我): 已暴露的密钥和凭据 security-auditor: 代码漏洞模式
配合使用
secret-scanner: Finds hardcoded API key
security-auditor: Finds how the key is used insecurely
Combined: Complete security picture
快速修复
移至环境变量
// Before:
const apiKey = 'sk_live_abc123';
// After:
const apiKey = process.env.API_KEY;
// .env file (add to .gitignore):
API_KEY=sk_live_abc123
使用密钥管理
// AWS Secrets Manager
const AWS = require('aws-sdk');
const secrets = new AWS.SecretsManager();
const secret = await secrets.getSecretValue({ SecretId: 'myApiKey' }).promise();
配置文件
# docker-compose.yml
services:
app:
environment:
- API_KEY=${API_KEY} # From .env file
# .env (gitignored)
API_KEY=sk_live_abc123
沙箱兼容性
无需沙箱即可运行: ✅ 是(推荐) 在沙箱中运行: ✅ 是
- 文件系统:只读访问
- 网络:无需网络
- 配置:无需配置
自定义
添加公司特定的密钥模式:
cp -r ~/.claude/skills/security/secret-scanner \
~/.claude/skills/security/company-secret-scanner
# Edit SKILL.md to add:
# - Internal API key formats
# - Company-specific secret patterns
# - Custom detection rules
最佳实践
- 切勿提交密钥 - 使用环境变量
- 使用 .gitignore - 添加 .env、secrets.json 等
- 轮换已暴露的密钥 - 如果已提交,立即轮换
- 使用密钥管理 - AWS Secrets Manager、HashiCorp Vault
- 定期审计 - 检查代码中是否有已暴露的密钥
紧急响应
如果密钥已提交
- 立即轮换密钥
- 从 git 历史记录中移除
git filter-branch --force --index-filter \ "git rm --cached --ignore-unmatch config/secrets.json" \ --prune-empty --tag-name-filter cat -- --all - 强制推送(与团队协调)
- 使用新密钥更新所有部署
相关工具
- security-auditor 技能:漏洞检测
- @code-reviewer 子 Agent:安全审查
- /review 命令:全面安全检查
评论
支持 Markdown