最后更新:2026-02-03 严重事件:API Key 误提交 GitHub 已修复
时间:2026-02-03
发现者:莲桑
修复者:Gre
问题:
.keys/目录包含 API Key 被提交到 GitHub- 提交哈希:
fb44b5f及之前
修复措施:
- ✅
git rm -r --cached .keys- 从 Git 移除 - ✅ 添加
.keys/到.gitignore - ✅ 提交修复:
34f4b65
教训:
- 提交前必须检查
git status内容 - 敏感文件必须第一时间加入
.gitignore - 使用
git add .前确认不包含敏感文件
# 1. 检查即将提交的内容
git status
# 2. 确认没有敏感文件
# - .keys/
# - .env
# - *.pem
# - *.key
# - config/secrets.json
# 3. 查看具体修改
git diff --cached
# 4. 确认无误后再提交
git commit -m "..."| 类型 | 示例 | 处理方式 |
|---|---|---|
| API Keys | .keys/keys.json |
加入 .gitignore,使用环境变量 |
| 环境变量 | .env |
加入 .gitignore,提供 .env.example |
| 证书 | *.pem, *.key |
加入 .gitignore,使用密钥管理服务 |
| 配置文件 | config/secrets.json |
加入 .gitignore,使用模板文件 |
| 日志 | *.log |
加入 .gitignore |
| 依赖 | node_modules/ |
加入 .gitignore |
# Security - Never commit
.keys/
.env
.env.local
.env.production
*.pem
*.key
*.p12
*.pfx
secrets.json
config/local*
# Dependencies
node_modules/
__pycache__/
*.pyc
# Logs
*.log
logs/
# OS
.DS_Store
Thumbs.db
# IDE
.vscode/
.idea/
*.swp
*.swo# 第一步:创建 .gitignore
echo ".keys/" >> .gitignore
echo ".env" >> .gitignore
git add .gitignore
git commit -m "chore: add .gitignore with security rules"
# 第二步:创建敏感目录
mkdir .keys
# 不要 git add .keys/
# 第三步:提供示例文件
cp .env .env.example
# 编辑 .env.example,移除真实值
git add .env.example
git commit -m "chore: add env example"- 绝不使用
git add .不加检查 - 绝不在代码中硬编码 API Key
- 总是使用环境变量或密钥管理服务
- 总是在提交前审查
git status
- 检查 PR 是否包含敏感文件
- 检查是否有硬编码的密钥
- 检查
.gitignore是否完整
如果发现敏感信息已提交:
-
立即撤销
git rm -r --cached <敏感目录> echo "<敏感目录>/" >> .gitignore git add .gitignore git commit -m "security: remove sensitive data"
-
轮换密钥
- 立即撤销已暴露的 API Key
- 生成新的 API Key
- 更新环境变量
-
审查历史
# 检查是否还有其他提交包含敏感信息 git log --all --full-history -- .keys/ -
记录事件
- 更新 SECURITY.md
- 记录教训
- 改进流程
- 运行
git status检查文件 - 确认没有
.keys/、.env等敏感文件 - 检查代码中没有硬编码密钥
- 审查
git diff确认修改内容 - 提交信息遵循规范
严格遵守安全规范,保护项目安全! 🔒🦎