Key idea
Most secrets don't leak through an attack. They leak through ordinary work: a commit, a log line, a container image, a screenshot. Each path has one habit that closes it, and all four share one rule: once a secret has leaked, rotate it.
1. Git
A key written into a file gets committed, pushed and cloned. Deleting it in the next commit doesn't help, because the old commit still holds it and every clone has a copy. Public repositories are scanned for keys by automated tools, often within minutes of a push.
The habit: read secrets from the environment (lesson 2.4.1), put .env in .gitignore before your first commit (lesson 2.5.5), and run a secret scanner before you push. The next lesson finds a key in history and removes it.
2. Logs
Logs are copied to more places, and kept longer, than anyone expects. The usual culprits are code that prints its whole configuration at start-up, errors that include a full connection string, and request logging that records the Authorization header.
The habit: log the names of the settings you loaded, never their values. If you must show part of one, show the last four characters.
3. Images
A build packages your app into a container image, and anyone who can download the image can read everything in it. A build can copy your .env into the finished app along with the code, and a key that gets in stays in.
The habit: keep .env out of the build, and give secrets to the app when it runs, not when it's built (lesson 2.4.3). Path 3 shows how, in lesson 3.4.4.
4. Screenshots and pastes
A terminal screenshot in a bug report, a screen share during a demo, a config pasted into a chat or an AI assistant to ask for help. Each copies the secret to a place you don't control.
The habit: before you share, look for values, not just names. Paste .env.example rather than .env, and stop screen sharing before you open a settings page with keys on it.
When one gets out anyway
Cleaning up comes second. First rotate: create a new key, give it to your app, then revoke the old one at the provider so it stops working. Only then remove the copies. Removing copies without rotating leaves the key working for anyone who already saw it.
Tools that catch leaks early
- Secret scanners such as gitleaks or trufflehog check files and history for strings that look like keys. Run one as a pre-commit hook (a script Git runs before each commit, which can stop it) and in CI (continuous integration: jobs that run on every push, such as the GitHub Actions in Module 7).
- GitHub secret scanning and push protection flag known key formats in repositories.
- Image scanners check a built image for keys and other problems.
None of these catch everything, so the habits above still matter.
Check yourself