Security Playbook
18 · Crypto for developersOWASP A04:20256 min read

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.

A cloud key pushed to a public repo: the first hour
  1. 0:00Developer commits .env with an AWS access key and pushes to a public repo
  2. 0:01Bots scraping the public GitHub event feed find the key
  3. 0:04Attacker tests the key: it works
  4. 0:09Spins up GPU instances for crypto-mining in 6 regions
  5. 0:30Lists and downloads every S3 bucket the key can read
  6. day 30You notice: a five-figure cloud bill and a data breach
Removing the commit doesn't help: the key is already copied. Keep secrets out of code, scan before push, scope every key, and revoke immediately when one leaks.
Turn on controls and see where the timeline stops.

Doing it right

PracticeWhy
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 permissionsA leaked key opens one door, not all of them
Rotation runbookWhen 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