Secrets in repos: minutes from push to abuse
API keys, database passwords and tokens end up in code, chat and logs all the time. Public leaks get exploited within minutes. Where secrets should live, how to catch leaks before they ship, and what to do when one escapes.
In one sentence
A secret in a repo is leaked, even after you delete it. Keep secrets in a vault or the platform's secret store, scan before push, scope every key, and revoke first, investigate second.
The first hour of a leaked key
Bots watch public code-hosting activity continuously and try every credential they find. Deleting the file or rewriting history doesn't help: forks, clones and caches already have it.
- 0:00Developer commits .env with an AWS access key and pushes to a public repo
- 0:01Bots scraping the public GitHub event feed find the key
- 0:04Attacker tests the key: it works
- 0:09Spins up GPU instances for crypto-mining in 6 regions
- 0:30Lists and downloads every S3 bucket the key can read
- day 30You notice: a five-figure cloud bill and a data breach
Doing it right
| Practice | Why |
|---|---|
| Secret store (Vault, AWS Secrets Manager, GCP Secret Manager, platform env vars) | Secrets never touch the repo; access is logged and revocable |
| Short-lived credentials (OIDC, IAM roles, workload identity) | Nothing long-lived to leak in the first place |
| Pre-commit and server-side secret scanning (gitleaks, GitHub push protection) | Blocks the push before the leak happens |
| Scoped keys: one key per service, minimal permissions | A leaked key opens one door, not all of them |
| Rotation runbook | When a leak happens, revoke and replace in minutes |
Key takeaways
- 01A pushed secret is compromised, even if you delete it right away.
- 02Store secrets in a vault or platform secret store, never in code.
- 03Scan before push and prefer short-lived, scoped credentials.
- 04On a leak: revoke first, then investigate usage.
Check your understanding
1 of 3
You pushed an API key to a repo and deleted it a minute later. Is it safe?
Related concepts
Least privilege
Assume something will get hacked. Least privilege makes sure that when it does, the attacker gets the keys to one room, not the whole building.
6 min read →CI/CD security
Treat PRs from forks as untrusted code, pin actions to a commit SHA, use OIDC instead of stored cloud keys, and gate deploys behind protected branches and reviewers.
6 min read →Hash vs encrypt vs encode
Encoding changes format and hides nothing. Hashing is one-way and proves integrity. Encryption is reversible with a key and provides confidentiality. Pick by the guarantee you need.
6 min read →