How to keep secrets out of AI coding tools
API keys and credentials in repositories end up in AI prompts, logs and indexes. How to find secrets in a codebase, keep them out, and what tools should filter.
If a secret is in your repository, it will eventually be read by something it shouldn't be: a contractor's laptop, a CI log, a public fork, or an AI tool's prompt. AI tools add one more place for secrets to travel, which makes an old problem more urgent.
Where secrets hide in codebases
.envfiles committed by accident- Hard-coded API keys in config files or scripts
- Credentials in test fixtures and seed data
- Private keys and certificates (
.pem,.key,.p12) - Tokens in Jupyter notebooks, logs or sample responses
- Old commits: a secret removed from the latest version is still in git history
Find what's already there
- Run a secret scanner over the repository and its history. Gitleaks and TruffleHog are widely used open-source options; GitHub secret scanning and push protection cover repositories on GitHub.
- Treat every secret found as exposed: rotate it first, then remove it from the code.
- Removing a secret from history requires rewriting history (for example with git-filter-repo), but rotation is what actually protects you.
Keep new secrets out
- Add
.envand key files to.gitignore, and commit a.env.examplewith placeholder values. - Turn on push protection so commits containing recognisable secrets are blocked.
- Load secrets from your platform's secret store or environment, never from files in the repository.
- Add a pre-commit hook with a scanner for extra protection.
What AI tools should do
A careful AI tool filters likely secrets before sending anything to a model:
- Skip
.envfiles and common key and certificate formats - Skip files whose names suggest secrets or credentials
- Ideally, scan contents for secret patterns and mask them
RepoGrasp skips .env files, .pem, .key and .p12 files, and any file whose path contains "secret" or "credential" before anything is sent to a model. Name-based filtering is a safety net, not a guarantee: a key hard-coded in config.ts would still be read. The real fix is keeping secrets out of the code. Repomix, a context-packing tool, runs Secretlint over files for the same reason.
If a secret was sent to an AI tool
Treat it like any other exposure: rotate the secret, check the provider's logs for misuse, and review the tool's retention policy to understand how long the prompt may be stored.
A checklist
- Secret scanner run over code and history
- Every found secret rotated
.envignored,.env.examplecommitted- Push protection on
- Secrets loaded from a secret store at runtime
- AI tools in use filter secret files