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 --stagedvia.pre-commit-config.yamlcatches the secret before history exists. - CI —
gitleaks detectwithfetch-depth: 0catches 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: gitleaksFor 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
.envin 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?
Keep reading
Related articles from the SudoSecurity blog.
Buying Time: Package Managers Are Adding Delay as a Security Control
Composer 2.11 shipped a cooldown that refuses to install a version until it has aged. vlt now holds npm publishes until their dependencies actually exist, because 22% of new npm versions land broken. And Ruby Central's reviewers threw out 90% of what an AI scanner flagged.
Your npm Install Just Ran Someone Else's Code: Why Dependency Security Has to Start Before CI
A pull request checkout, a poisoned GitHub Actions cache, and an OIDC token pulled straight out of runner memory. That's how 84 malicious versions of TanStack's packages hit npm in six minutes.
CVE-2025-62718: Critical Axios SSRF Vulnerability Explained
A critical SSRF flaw in Axios was used in a North Korean supply chain attack. Here's what it does and how to patch it today.
What is Supply Chain Security? Dependencies, SBOMs, and the Attack That Hit 2.6 Billion npm Downloads
Your application is only as secure as every package it depends on. In September 2025, attackers compromised 27 npm packages with 2.6 billion weekly downloads by phishing one maintainer.