Sub topic 14
Preventing the next leak
This chapter assumes you have already rotated the leaked credential, rewritten history, and force-pushed. Rewriting history fixes one incident. The measures below lower the chance of a second one. No single control catches everything, so you stack several.
Layers of defense
Each layer catches what the previous one missed. The earlier a layer catches a secret, the cheaper the fix, because a secret that never reaches a commit needs no history rewrite.
Keep secrets out of the repository
Load secrets from environment variables or a secrets manager instead of from files in the repository. If a local file is unavoidable, ignore it and commit a template with fake values.
# .gitignore
.env
config/secrets.env# .env.example (committed; contains no real values)
DATABASE_URL=postgres://user:password@localhost:5432/app
API_KEY=replace-meScan staged changes before each commit
A pre-commit hook runs on your machine and blocks the commit if it finds something that looks like a credential. The pre-commit framework with the gitleaks scanner is one common setup.
pip install pre-commit# .pre-commit-config.yaml
repos:
- repo: https://github.com/gitleaks/gitleaks
rev: v8.18.4
hooks:
- id: gitleakspre-commit installAfter pre-commit install, every git commit scans the staged changes. Hooks live in each developer's clone and can be skipped with --no-verify, so treat them as a convenience and not as a guarantee.
Enforce on the server
Server-side checks apply to everyone, including people who skipped the hook. Many hosts offer push protection, which rejects a push that contains a recognized secret pattern. On GitHub this is called secret scanning with push protection. Turn it on for every repository, and also enable scanning of existing content so older leaks surface.
Add the same scanner to continuous integration (CI), so pull requests fail when a secret appears. Run a full-history scan on a schedule too. Hooks and push checks look at new changes, while a scheduled scan can find a secret that was added before the controls existed.
Limit the damage when one slips through
- Give each credential the narrowest permissions it needs.
- Prefer short-lived credentials, such as tokens that expire in hours, over long-lived keys.
- Keep a written rotation procedure so revoking a key takes minutes.
- Alert on unusual use of the credential, since you may learn of a leak from the provider before you find it in the repository.
Common mistakes
| Mistake | Why it fails |
|---|---|
| Relying only on the local hook | Developers can skip it, and new clones do not have it installed until someone runs `pre-commit install`. |
| Adding the secret file to `.gitignore` after committing it | Tracked files stay tracked. |
| Committing a real `.env` "just for now" | Anything pushed can be copied by others, even if you delete it later. |
| Scanning only the latest commit | Secrets in older commits and other branches stay unnoticed. |
Exercise
A teammate says, "We added the pre-commit hook, so push protection on the host is redundant." Give two reasons this is wrong.
Show answer
First, a hook runs only in clones where it was installed, and git commit --no-verify bypasses it, so some commits are never scanned locally. Second, the host's push protection runs on the server for every push from every contributor, and it can be paired with scans of existing history. The two layers cover different gaps.
Exercise
You committed config/secrets.env last month and now add it to .gitignore. Will Git stop tracking the file? What must you do?
Show answer
No. .gitignore does not affect files that are already tracked. Run git rm --cached config/secrets.env and commit to stop tracking it. The file still exists in old commits, so rotate the credentials it held and remove it from history with git filter-repo --invert-paths --path config/secrets.env.