PAFTAK
← All insights

Secrets in the Repo: How They Get In, How to Keep Them Out

Secrets in the repo is one of those problems every engineering team nods along to and then discovers fresh in their own history anyway. A database password hardcoded in a config file, an API key pasted into a test script to get something working, a .env file that was supposed to be gitignored but got added before the gitignore rule existed. None of it is malicious. All of it ends up permanently in git history, readable by anyone with clone access, long after the person who typed it has forgotten it was ever there.

How they actually get there

The common path is not a careless developer ignoring policy. It is a developer under time pressure doing the fastest thing that makes a local build or a CI run pass. Picture a team debugging a flaky integration test at the end of a sprint: someone drops a real API key into a config file to confirm the test passes against the live service, intends to swap it for an environment variable before committing, and then gets pulled into a meeting. The commit goes in with the key still there. Nobody reviewing the pull request notices, because reviewers are reading for logic, not scanning every line for credential-shaped strings. A second common path is dependency and tooling defaults: scaffolding tools and quickstart templates sometimes generate example config files with placeholder secrets, and those placeholders get replaced with real values without anyone updating the gitignore to match. A third is forking or copying code between repos, carrying a secret from an internal tool into a project that ends up public.

The part that makes this worse than a normal bug is that git does not forget. Deleting the file in a later commit does not remove the secret from history, and anyone who has ever cloned the repo has a copy of that history sitting on their disk whether they know it or not. A key fact that's easy to underestimate: once a secret has been pushed to a remote, even briefly, the correct response is to treat it as compromised and rotate it, not to quietly delete it and move on. Scrubbing git history with something like filter-branch or BFG is worth doing for hygiene, but it does not undo exposure that already happened.

Keeping them out

The reliable fix is to make committing a secret require active effort rather than being the path of least resistance. That starts with a repo-local gitignore that covers .env files and common credential file patterns before the first commit, not after the first incident. It continues with pre-commit scanning that greps staged changes for patterns that look like keys, tokens, and connection strings, and blocks the commit locally before it ever reaches a shared branch. Server-side scanning on push or in CI is a second layer for the cases a developer's local hook missed or was bypassed with --no-verify. Neither layer is a substitute for the other; local hooks catch things early and cheaply, but they rely on the developer having them installed and enabled, so a server-side check is what actually guarantees coverage across a team.

The deeper fix is structural rather than detective: secrets that live in a managed secrets store or vault and get injected as environment variables at runtime never have the opportunity to be typed into a file that git tracks in the first place. This is a case where the access-control framing we apply to PAM work generally is also the right lens for secrets hygiene — a credential sitting in a config file is a standing grant with no expiry and no audit trail, available to anyone who can read the repo, indefinitely. A secret pulled from a vault at deploy time, scoped to the service that needs it, is the same information with an actual access boundary around it. Teams that treat secrets management as a credential lifecycle problem, with rotation and scoped access, end up with far fewer of these incidents than teams that treat it as a reminder to be careful.

None of this requires exotic tooling. It requires accepting that a secret committed to source control is not a one-off mistake to be quietly fixed but a question of who had access to what, for how long, without anyone deciding it should be that way. That is the same question we ask about a service account with a standing admin grant or an AI agent with an unscoped API token: access control is access control, whether the thing holding the credential is a person who meant to delete it before lunch or a line in a config file that outlived everyone's memory of why it was there.

Related practice area: Software Development

More on this topic: What Bugweg's Sandbox Egress Lockdown Protects Against · Static Site vs. Framework: What Breaks as You Grow · The Deploy Pipeline Is Part of the Product — bugweg is a PAFTAK product.

Secrets in the Repo: How They Get In, How to Keep Them Out