alikesk222/gitclean
GitHub: alikesk222/gitclean
一款轻量级 Git 仓库敏感信息检测工具,可扫描代码及完整提交历史中意外泄露的 API 密钥与凭证。
Stars: 0 | Forks: 0
# gitclean
一款轻量、依赖极简的 CLI 工具,可在 **60 多种类型的机密信息** 导致数据泄露之前,检测出代码库和 git 历史记录中的这些内容。支持离线运行,可与 CI/CD 集成,并明确告知您下一步该怎么做。
## 为什么选择 gitclean?
意外提交 API 密钥的情况[比您想象的更常见](https://www.bleepingcomputer.com/news/security/dev-leaked-aws-root-account-credentials-on-github-deleted-repo-too-late/)。问题在于,即使您删除了文件,**机密信息仍然永远存在于 git 历史记录中** —— 任何克隆您仓库的人都能获取它。
gitclean 会同时扫描您当前的文件**和**完整的提交历史记录。
## 功能特性
- **60+ 种机密模式** — AWS, GitHub, Google, Stripe, Slack, OpenAI, Anthropic, Discord, Twilio, SendGrid, 私钥, 数据库 URL 等
- **完整 git 历史记录扫描** — 查找过去提交中的机密信息,而不仅仅是当前文件
- **去重处理** — 多次提交中出现的相同机密信息仅报告一次
- **严重性等级** — CRITICAL / HIGH / MEDIUM / LOW
- **修复建议** — 针对不同严重性的逐步修复说明
- **适配 CI/CD** — 发现机密信息时以代码 1 退出,支持 JSON 输出模式
- **精美终端 UI** — 彩色表格和摘要面板
- **快速** — 仅扫描 diff 中的新增行,跳过二进制/锁定文件
## 安装
```
git clone https://github.com/yourusername/gitclean.git
cd gitclean
pip install -e .
```
## 使用方法
### 扫描当前文件
```
# 扫描当前目录
gitclean scan
# 扫描特定 repo
gitclean scan /path/to/repo
# 仅报告 critical 和 high severity
gitclean scan --severity high
```
### 扫描当前文件 + 完整 git 历史
```
gitclean scan --history
# 限制为最后 100 次提交
gitclean scan --history --max-commits 100
```
### 仅扫描 git 历史
```
gitclean history
gitclean history --max-commits 50
```
### CI/CD 集成
```
# 如果发现 secrets 则以 Exit code 1 退出 — 用于 pre-commit hooks 或 pipelines
gitclean scan --history --json -o findings.json
```
### Pre-commit hook
添加到 `.git/hooks/pre-commit`:
```
#!/bin/sh
gitclean scan --severity high
if [ $? -ne 0 ]; then
echo "Secrets detected! Commit blocked."
exit 1
fi
```
## 输出示例
```
============================================================
CRITICAL (2 finding(s))
============================================================
+-----------------------------+--------------------+------+---------------------------+
| Rule | File | Line | Match |
+-----------------------------+--------------------+------+---------------------------+
| AWS Access Key ID | config/settings.py | 14 | AKIAIOSFODNN7EXAMPLE |
| GitHub Personal Access Token| .env.backup | 3 | ghp_xxxxxxxxxxxxxxxxxxxx |
+-----------------------------+--------------------+------+---------------------------+
+- Recommended Actions -------------------------------------------------------+
| 1. Revoke the secret immediately - assume it is compromised. |
| 2. Use 'git filter-repo' to remove it from history. |
| 3. Rotate all credentials and audit access logs. |
+-----------------------------------------------------------------------------+
+- Summary -------------------------------------------------------------------+
| Status: CRITICAL ISSUES FOUND |
| Total Findings: 2 (critical: 2 high: 0) |
| History Scanned: Yes |
+-----------------------------------------------------------------------------+
```
## 检测到的机密类型
| 类别 | 示例 |
|----------|---------|
| AWS | Access Key ID, Secret Access Key |
| GitHub | PAT (classic + fine-grained), OAuth, App tokens |
| Google | API Key, OAuth Client Secret, Service Account JSON |
| Stripe | Live/Test secret keys, Restricted keys |
| Slack | Bot/User tokens, Webhook URLs |
| OpenAI / Anthropic | API Keys |
| Twilio | Account SID, Auth Token |
| SendGrid / Mailgun | API Keys |
| Discord | Bot Token, Webhook URL |
| Telegram | Bot Token |
| Private Keys | RSA, EC, OpenSSH, PGP |
| Database URLs | PostgreSQL, MySQL, MongoDB, Redis (with credentials) |
| Azure | Storage Account Key, SAS Token |
| GCP | Service Account Key JSON |
| Generic | API key/secret/token/password assignments in code |
| Web3 | Ethereum private keys |
| CI/CD | NPM tokens, PyPI tokens |
| Monitoring | Datadog, HuggingFace |
## 如何修复泄露的机密信息
1. **立即撤销** — 不要等待,假设它已经被发现
2. **从历史记录中移除**:`git filter-repo --replace-text secrets.txt`
3. **强制推送**:`git push --force`
4. **使用环境变量** — 切勿硬编码凭证
5. **在创建 `.env` 之前** 将其添加到 `.gitignore`
## 环境要求
- Python 3.11+
- 参见 [requirements.txt](requirements.txt)
## 许可证
[MIT](LICENSE)
标签:API密钥, AWS密钥, CI/CD安全, DevSecOps, Llama, LLM应用, meg, Pre-commit, Python, Secrets Scanning, TLS, 上游代理, 云安全监控, 信息安全, 凭证扫描, 历史扫描, 安全助手, 密钥轮换, 数据泄露, 文档结构分析, 无后门, 逆向工具, 防御工具, 静态分析