Secrets Don't Leak in One Moment: The 6 Ways They Slip Into Your Repo (and How to Catch Each One)
说明与用途
秘密可能通过 .env、.env.example、源代码、日志、git 历史和截图无意间进入公共仓库。通过使用 dotguard、纠正 .gitignore、添加 pre‑push 钩子和 CI 检查,以及清理历史记录,可以检测并防止泄漏。
问题
Secrets can be unintentionally committed to public repositories through various subtle practices, leading to prolonged exposure of credentials.
结果
Adopting automated scanning, proper git ignore handling, and process discipline significantly reduces the risk of secret leaks and enables rapid response when a leak is detected.
工作流
- Identify common leak vectors (env files, examples, source, logs, history, screenshots).
- Use git ls-files to check if .env is tracked; if so, run git rm --cached .env and clean history.
- Install dotguard globally (npm i -g @wuchunjie/dotguard dotguard).
- Run dotguard in the project root to scan for hardcoded secrets.
- Add dotguard to CI pipeline (e.g., GitHub Actions) with a step: npx @wuchunjie/dotguard.
- Set up a pre-push hook that runs dotguard and aborts the push if secrets are found.
- When a secret is found, rotate it immediately, remove it from the working tree, clean history with BFG or git filter-repo, add patterns to scanner and .gitignore, and document exposure duration.
证据
Last year I found a production database password in one of my own public repos. Not a test password. Not a placeholder. The real one, in a config file, visible to anyone who knew how to read git history. It had been there for 8 months.
dotguard scans config files, not just files named .env, so a .env.example with real values gets flagged the same way as the real thing.
git log --all -p -S "password"
- 记录日期: 2026-11-01T02:59:11.171Z