Claude Code wrote an API key into my code: find it, rotate it
Last updated: 7 October 2026
If Claude Code put a real API key into a file, deal with the key before the file. GitHub's guide to removing sensitive data from a repository says that for a password, token or credential, "as a first step you need to revoke and/or rotate that secret. Once the secret is revoked or rotated, it can no longer be used for access, and that may be sufficient to solve your problem." Then find every copy, take it out of the code, and rewrite git history only if you committed it.
1. Rotate the key
Revoke the key where it was issued and create a new one. GitHub's secret scanning page gives the same order: "When you receive an alert, rotate the affected credential immediately to prevent unauthorized access. While you can also remove secrets from your Git history, this is time-intensive and often unnecessary if you've already revoked the credential."
If the key reached a public repository on GitHub, it may already have been found: "Public repositories: Secret scanning runs automatically for free." For the services in its partner program, "When a partner secret is detected, we notify the provider so they can take action, such as revoking the credential."
2. Find every copy
In the files on disk, and in every commit, with gitleaks: gitleaks dir -v scans "directories and files", and gitleaks git -v scans "local git repos", using git log -p under the hood.
With git alone, search the history for the start of the key:
git log --all -S'sk-proj-'
-S looks "for differences that change the number of occurrences of the specified <string> (i.e. addition/deletion) in a file", and --all searches every branch: it acts "as if all the refs in refs/, along with HEAD, are listed on the command line" (git log). Put the first characters of your own key in place of sk-proj-.
3. Take it out of the code
Put the line back the way it read the key before, and keep the new key out of tracked files. From GitHub's guide: "Avoid hardcoding secrets in code. Use environment variables, or secret management services like Azure Key Vault, AWS Secrets Manager, or HashiCorp Vault to manage and inject secrets at runtime." And for a file such as .env: "If the sensitive data is likely to be found in a file that should not be tracked by git, add that filename to .gitignore (and make sure to commit and push that change to .gitignore so other developers are protected)."
That is how it went in a test on 7 October 2026 with Claude Code 2.1.292: the app read its key from process.env, the key sat in a gitignored .env, and the prompt asked Claude to put the key from .env into src/app.js. Claude read .env with cat, wrote the key into line 1 with sed -i, and warned in its reply that the key was now in a file git tracks.
4. Remove it from git history, if you committed it
Rewriting history is a large job, with side effects GitHub lists one by one, and the old commits "may still be accessible elsewhere": "In any clones or forks of your repository", "Directly via their SHA-1 hashes in cached views on GitHub" and "Through any pull requests that reference them". If you still decide to, GitHub's guide uses git-filter-repo, "at least version 2.47". To replace the key's text everywhere in history, list it in a file outside the repository and run, in a fresh clone:
git-filter-repo --sensitive-data-removal --replace-text ../passwords.txt
Then, after checking the result, publish the rewritten history:
git push --force --mirror origin
The guide then has you contact GitHub Support to remove cached views and pull request references, and collaborators must clean their clones. Read the whole guide before you start: "Once you move on to the next step, the rewrite becomes irreversible."
5. Keep Claude away from your .env
Claude Code's settings documentation has an example that, among other things, stops Claude reading .env files. In ~/.claude/settings.json:
{
"$schema": "https://json.schemastore.org/claude-code-settings.json",
"permissions": {
"allow": [
"Bash(npm run lint)",
"Bash(npm run test *)"
],
"deny": [
"Read(./.env)",
"Read(./.env.*)"
]
}
}
In the same test with the two deny rules in the project's .claude/settings.json, and permission prompts switched off, the cat .env call was refused and Claude stopped without changing anything:
The rule has limits, which the permissions documentation states: Read and Edit deny rules cover Claude's file tools and file commands it recognizes in Bash, such as cat, but "They don't apply to a command that reads files without naming them, such as grep -r pattern . run from the directory that holds the file, or to arbitrary subprocesses that read or write files indirectly, like a Python or Node script that opens files itself." For more, see how to stop Claude Code from running destructive commands.
Where turnslip helps
turnslip checks the lines each turn added, at the end of the turn, before you commit. In the test above, without the deny rules, its slip under Claude's reply named the file:
The key in the test is a fake. The 🔑 flag looks for a fixed list of key formats (rules.mjs): keys starting sk-, sk_live_, gh tokens and github_pat_, AKIA, xox, and private key blocks, plus a new .env file. A key in another format is not flagged. From its README: "Paths your .gitignore excludes are never flagged, so a key in a gitignored .env is not reported." It works only if it was installed before the turn, and it flags the key; rotating it is still up to you.
Related: What did Claude Code just change? · All guides · Install turnslip, free