Notes
Why it is so bad
Why is a committed secret so dangerous? It is readable by everyone with repository access, and by the whole internet if the repository is public. Git history keeps it even after you delete the line. Automated bots constantly scan public code and exploit leaked keys within minutes. And a single leaked cloud credential can lead to a full account takeover, data theft or a large cryptomining bill. This is one of the highest-frequency, highest-impact mistakes there is.
- a key in a commit is readable by anyone with repo access, and by anyone if public
- git history keeps it even after you delete the line
- bots scan public registries and exploit keys in minutes
- one leaked cloud key can mean a full account takeover
- deleting the file does NOT remove it from history
How scanners find them
Scanners find secrets three ways. Patterns match known formats: an AWS key, a GitHub token and a Stripe key each have a recognisable shape a regular expression catches. Entropy detection flags random-looking, high-entropy strings, a hallmark of a key even when the format is unknown. And verification, the most powerful, tests a found credential against the provider to see if it is live, cutting false positives by separating real, active secrets from example or expired ones. The best tools combine all three.
- patterns: known formats (AWS keys, GitHub tokens, Stripe keys) via regex
- entropy: random-looking high- entropy strings that may be secrets
- verification: actually test the credential against the provider to confirm it is live (cuts false positives)
Where to scan
Scan at every layer, because each catches what the others miss. A pre-commit hook blocks a secret before it is committed, the best place, because then it never enters history. CI scans every pull request to catch what slipped past the hook, since hooks can be bypassed. A full history scan finds old secrets already in the repository from before you turned scanning on. And provider-side push protection, such as GitHub's, blocks known formats at push. Use all of them together.
- pre-commit hook: block the secret before it is even committed (best)
- CI on every PR: catch what slips past the hook
- whole git history: find old secrets already in there
- provider push protection: GitHub blocks known secret formats at push
- defence in depth: all of them
The golden rule
Here is the rule that matters most, and that people get wrong. The moment a secret is committed, treat it as compromised, full stop. Deleting the line, or even rewriting git history, does not un-leak it, because a bot or a person may already have copied it. The correct response is the rotation runbook from Part four: issue a new secret, switch everything to it, then revoke the old one. Cleaning history is good hygiene afterwards, but it is not the fix. Rotation is the fix, and it is urgent.
- the moment a secret is committed, treat it as compromised
- deleting the line or rewriting history does NOT un-leak it
- someone may already have copied it
- correct response: issue a new secret, switch everything, revoke the old one then clean history
The tools
The tools are free and easy to adopt. Gitleaks is fast and popular, running in pre-commit hooks and CI, and scanning full history. TruffleHog is strong on verification, testing whether found keys are live. GitHub has built-in secret scanning and push protection, and cloud providers offer their own. Beyond the built-in rules, add custom patterns for your organisation's own internal token formats, so your home-grown credentials are caught too, not just the well-known third-party ones.
- Gitleaks: fast, popular, runs in pre-commit and CI, scans history
- TruffleHog: strong on verification (tests if keys are live)
- GitHub secret scanning + push protection: built in cloud/vendor scanners too
- add custom rules for your own internal token formats
Prevent, not just detect
Scanning is a safety net, but the real goal is that secrets never reach the code. Inject them at runtime from a vault or secret manager, never hard-coded in source or config. Use short-lived credentials so even a leaked one expires quickly, which is the whole next episode. And make correct local secret handling easy, because people hard-code secrets most when the right way is painful. Scanning catches the mistakes; good secret management prevents them.
- secrets from a vault or secret manager at runtime, never in code or config
- short-lived credentials so a leak expires fast (next episode)
- developer education and easy local secret handling
- scanning is the safety net, not the strategy
Common mistakes
Mistakes. Deleting a leaked secret without rotating it, so the live credential is still out there. Scanning new commits but never the existing history, where old secrets hide. Relying only on a pre-commit hook, which developers can skip, instead of also enforcing in CI. Ignoring custom-format secrets the default rules miss. And the biggest: treating secrets in code as normal and scanning as the only control, instead of moving secrets into a manager.
- deleting a leaked secret without rotating it
- scanning new commits but not git history
- pre-commit hook only, skippable, not in CI too
- ignoring low-entropy or custom-format secrets
- secrets in code as the norm, scanning as the only control
Say it like this
In the interview: I scan at every layer, pre-commit to block before history, CI on every pull request, a full history scan for old secrets, and provider push protection, using Gitleaks and TruffleHog that combine patterns, entropy and live verification. The rule I stress is that a committed secret is compromised the moment it lands: I rotate it immediately, new one issued and old one revoked, and only then clean history. And scanning is a safety net; the real fix is a vault with short-lived credentials.
- "Scan at every layer: pre- commit, CI, full history, and provider push protection, with Gitleaks or TruffleHog using
- patterns, entropy and verification. A found secret
- is compromised: rotate it, then clean history. But the real fix is a vault and short- lived credentials, not scanning."