Skip to content

Lesson 5 of 12 · 20m

Secrets Scanning: Committed Keys Are Forever

Deleting the line doesn't delete the secret — git is append-only, bots watch every public commit, and the only real remediation is rotation.

Years ago I watched an engineer commit an AWS access key "just to test something," delete it four minutes later, and open an incident anyway — the key was already in git history, already in every clone and CI cache, and the bots that watch public commits don't sleep. We spent the weekend rotating keys and paging through CloudTrail. The deletion commit changed nothing. That's the lesson this whole control exists to prevent.

Deleting the line doesn't delete the secret

The wrong mental model is "the file is clean now." Git is append-only. A credential in any commit survives in every clone, every fork, every checkout cache, every mirror, every IDE local-history plugin that ever opened it. On a public repo it's worse: GitHub's event feed is streamable by anyone, and credential harvesters have been scraping it for over a decade — a committed key is measured in minutes of exposure, not days.

Once a real credential touches a repo, the only remediation is rotate it at the provider, then scrub history if you must. git filter-repo and BFG clean the copies you control. They do nothing for the copies you don't — which is why the order is rotate first, investigate second, scrub last.

Two scanners, two jobs

# gitleaks: regex + entropy scanning of history and the working tree
gitleaks detect --source . --redact --exit-code 1   # CI / audit mode
gitleaks protect --staged --verbose                  # pre-commit mode

# trufflehog: similar coverage, plus live verification
trufflehog git file://. --only-verified

--only-verified is the flag that matters. TruffleHog actually tries each candidate credential against its provider and reports only the ones that work. That separates a live AWS key — page someone now — from a thousand test fixtures and documentation examples that belong on a cleanup queue, not an incident bridge.

Coverage in three places

  • Pre-commit — gitleaks protect --staged via .pre-commit-config.yaml catches the secret before history exists.
  • CI — gitleaks detect with fetch-depth: 0 catches what bypassed the hooks, and hooks do get bypassed.
  • Platform — GitHub push protection and secret scanning, GitLab's secret detection. A third net for what both of yours miss; push protection can reject the commit before it lands at all.
# .pre-commit-config.yaml
- repo: https://github.com/gitleaks/gitleaks
  rev: v8.18.4
  hooks:
    - id: gitleaks

For false positives — test fixtures, example configs — # gitleaks:allow inline markers and .gitleaks.toml allowlists exist. Require a reason in the comment and review them like code.

Where secrets hide

Not just .env — that file gets caught because it's obvious. The leaks that reach incidents live in terraform.tfvars committed with the module, a config.yml in CI, test fixtures copied from production, a dotfiles repo carrying ~/.aws/credentials, and Docker image layers (COPY . . strikes again). git log -p is the archaeology tool; the scanners already do it, which is why fetch-depth: 0 matters and why shallow clones lie.

When it fires

Rotate first, investigate after. Write the playbook per secret type before you need it: revoke at the provider, check provider-side audit logs for use (aws iam get-access-key-last-used, CloudTrail LookupEvents filtered by access key ID), commit the removal, then baseline the historical string in the scanner so it stops re-firing.

Field notes

  • Entropy detection catches high-entropy strings regex misses — and flags every minified JS blob in the repo. Tune it or drown.
  • The real fix is upstream: if developers have a secrets manager (Vault, Doppler, SOPS, the cloud-native ones) they actually like, they stop pasting keys into .env in the first place.
  • CI logs are a leak path: a tool that echoes an env var writes the secret somewhere the scanner never looks. Masking exists for a reason.

Was this lesson helpful?

Sign in to rate

Keep reading

Related articles from the SudoSecurity blog.