AI

Claude Code settings.local.json: Audit and Remove Leaked Secrets

Approving a command in Claude Code does more than run it. The “Yes, and don’t ask again” option saves the command string itself into .claude/settings.local.json, and when that command carries a token, the token lands in the file exactly as it was typed.

Original content from computingforgeeks.com - post 185790

The file stays out of git on the machine where Claude Code wrote it, because the first write adds an entry to your global git excludes file. That protection is a property of your home directory, not of the repository. It does not travel to a second machine, it does not cover a file you created by hand, and once the file is committed, ignore rules stop applying to it entirely.

This article walks the leak end to end on Ubuntu 24.04 with Claude Code 2.1.319, tested in November 2026, in four steps: reproduce, audit, scrub, prevent. Every token in the captures is a fake 36-character string pointed at a local mock API, so nothing real was ever at risk.

Prerequisites

You need Claude Code 2.1.319 or newer, git, and the two audit tools this article uses. The exact versions the lab ran, in the order you will meet them:

  • Claude Code 2.1.319, the agent whose approval writes the file
  • gitleaks 8.16.0, the scanner for the audit and the hook
  • git-filter-repo 2.38.0, the history scrubber
  • git 2.43.0, which supplies the ignore rules everyone relies on

The versions print like this on a fresh Ubuntu 24.04 install:

Terminal showing claude --version 2.1.319, gitleaks 8.16.0, git-filter-repo 2.38.0 and git 2.43.0 on Ubuntu 24.04

Inside the lab sit a throwaway repo, a mock Anthropic API on localhost, and a fake GitHub token, so the agent has something realistic to leak. If you have never looked inside the .claude folder, the dot-claude directory guide covers what each file in it does. And when you want to share rules across a team instead of keeping them local, the plugins guide is the mechanism for that.

Approval Writes the Secret

When the agent proposes a command that needs approval, Claude Code offers four choices. The command here exports a demo token and calls the GitHub API with it:

Claude Code permission prompt offering Yes, Yes and don't ask again, auto mode and No for a command carrying a fake demo token

Option 2 is the one to read carefully. Anthropic’s permissions documentation says that choice “saves the rule to .claude/settings.local.json at the root of the git repository”. The rule is not a sanitized pattern. For a compound command, Claude Code splits it and stores the pieces, and one of those pieces is the literal command line you just approved:

cat .claude/settings.local.json

The file the approval wrote holds three allow rules, token and all. Nothing was sanitized on the way in:

{
  "permissions": {
    "allow": [
      "Bash(curl *)",
      "Bash(export GH_TOKEN=ghp_xxxx…)",
      "Bash(gh api *)"
    ]
  }
}

Three things in that file are worth your attention:

  • the TUI redacts the token in its preview, the file does not
  • Bash(gh api *) is the useful wildcard you meant to save
  • the export rule stores the token, because the rule is the command string itself

Rule three is the trap, so watch what it does next session. It auto-approves every future command that starts by exporting that exact token, which is exactly the command shape you’ll reach for again. The redacted prompt made the approval look safe, and the bytes on disk never got the memo.

The Ignore Rule Is Global

Why doesn’t git status show this file? The settings documentation answers it: “The first time Claude Code writes the file in a git repository that doesn’t already ignore it, it adds **/.claude/settings.local.json to your global git excludes file.” Git looks for that file in a fixed order:

  • core.excludesFile, if you have set it
  • $XDG_CONFIG_HOME/git/ignore, when the variable is defined
  • ~/.config/git/ignore, the default on most machines

On the lab machine it was the last one, the default, because nothing else had ever been configured:

cat ~/.config/git/ignore

The file has one line, and the capture also confirms no core.excludesFile is set:

**/.claude/settings.local.json

Notice where the entry points. It names your home directory, not any repo, and that’s the whole problem:

The global git excludes file at ~/.config/git/ignore holding the settings.local.json pattern, with core.excludesFile unset

Nothing ever goes into the repository’s own .gitignore, so the protection can’t travel with the project. That’s why a repo can look safe on your machine and still leak from a colleague’s clone, because the safety net never enters the history anyone shares. Clone the repo on a machine without the global file and git sees the settings file as an ordinary untracked path, so a plain git add stages it with no -f required:

A second machine without the global ignore shows the settings file as untracked and git add succeeds without force

There is one more gap the documentation calls out directly: “If you created the file by hand and Claude Code hasn’t written to it yet, add it to .gitignore yourself.” The automatic entry only happens on Claude Code’s first write, so a hand-made file in a new repo is invisible to git until you cover it. Neither gap closes on its own, which is why the repo-side entry belongs in your project template from day one.

Tracked Files Stay Tracked

Ignore rules only ever apply to untracked files, which is the part of git people forget under pressure. Once .claude/settings.local.json is in the index, the global exclude does nothing: a fresh clone on a normal machine lists the file as tracked, git check-ignore reports nothing, and the file re-adds cleanly after any edit:

Fresh clone where settings.local.json is tracked: ignore rules do not apply and the file re-adds cleanly

So the real exposure is not some “fresh clone commits it” magic, and Apiiro’s author conceded exactly that on the Hacker News thread: “Fair, ‘fresh clone’ was wrong. It happens on a new machine or container without the global ignore, in a copied project folder, or once the file has been committed, since ignore rules don’t apply to tracked files.” The leak has three shapes, and only your history decides which one you’re in:

  • a machine or container without the global ignore
  • a project folder copied without its git metadata
  • a commit that already carries the file, because tracked files ignore ignore rules

What survives the myth-cut is the audit question, so ask it of your own machine. Which of your repos already track the file? One loop answers it:

for d in ~/work/*/; do
  git -C "$d" ls-files --error-unmatch .claude/settings.local.json 2>/dev/null \
    && echo "TRACKED $d"
done

Two of the five lab repos came back tracked, and a third holds the file on disk without tracking it:

TRACKED  ~/work/billing-api/
TRACKED  ~/work/frontend-app/

Rendered from the lab, the same audit comes with a find that lists the copy which isn’t tracked:

Loop over repos in ~/work reporting which ones track settings.local.json, plus a find listing every copy on disk

Tracking also changes how the rules load, because it changes whose file git thinks it’s reading. The permissions docs state that allow rules in an untracked settings.local.json apply without the workspace trust step because the file is “normally your own file”, while a tracked file is subject to trust. Deny and ask rules apply immediately either way, so only the allow side rewards whoever smuggles the file into your repo.

Scanners Miss Typed Passwords

Apiiro reported finding real secrets in public settings.local.json files on GitHub. Two of its figures held up in the Hacker News discussion:

  • 1,122 public files hold a real secret
  • in 539 of the 1,105 repos it traced, a commit Claude co-wrote changed the file

Both halves of the scanner gap reproduce in the lab, because the two secrets fail differently. gitleaks catches the provider token, since its github-pat rule matches the token format. The lab pins 8.16.0, where detect and protect are the documented commands; releases from 8.19 on hide them behind gitleaks dir and gitleaks git, so match the syntax to your version:

gitleaks detect --source . --no-git -v

The finding names the file and the rule, and Secret: is the raw token:

gitleaks detect finding the fake GitHub token in settings.local.json under the github-pat rule, then finding nothing in a repo where the secret is a password inside a command

Second half of the capture, the miss, is the one that should worry you. A settings file whose secret is -pHunter2S3cret typed inside a command comes back clean, because no provider rule matches a random password. A secret typed inside a command looks like any other argument to a scanner, which is why a custom rule earns its place in your repos:

vim .gitleaks.toml

Keyed on the settings file path, the rule matches token shapes and passwords passed to -p or --password:

title = "agent settings files"

[[rules]]
id = "agent-settings-secret-in-command"
path = '''(^|/)\.claude/settings(\.local)?\.json$'''
regex = '''(?i)(ghp_[0-9a-z]{20,}|sk-[a-z0-9]{20,}|(-p|--password)\S{8,})'''
keywords = ["permissions", "settings"]

Point gitleaks at it with the -c flag and both shapes in the lab repos get flagged. One caveat before you reuse this file elsewhere: a config of your own replaces the default rule set, so the ~170 provider rules for AWS keys and GitHub tokens stop applying unless the config starts with [extend] and useDefault = true. If you’ve never run a full audit, the gitleaks walkthrough covers the flags and the history scan in detail, because the flags differ once you scan history instead of files.

Untracking Keeps the History

Found a tracked copy? The instinctive fix is git rm --cached, and it is not enough. Untracking changes only the next commit; every old commit still carries the file, and git log still walks all of them:

git rm --cached .claude/settings.local.json
git commit -m "stop tracking agent settings file"
git log --oneline --all -- .claude/settings.local.json

History survives the untracking commit, so anyone who can still clone the repo has the token. The list below has three entries, and the oldest one is the leak:

822a5e5 stop tracking agent settings file
811cb00 add project settings
6794157 tweak settings

Removing the history takes git filter-repo, which rewrites every commit that touched the path and then drops the old objects. The tool refuses to run unless the repo is a fresh clone, and --force turns that safety check off, so double-check the path argument before you run it:

git filter-repo --invert-paths --path .claude/settings.local.json --force

Watch the rewrite run, and the proof that follows it:

git filter-repo removing settings.local.json from history so the old commits no longer resolve, after git rm --cached proved untracking keeps the old commits

After the rewrite, the old commit IDs no longer resolve to anything, which is the proof the blobs are gone from this clone. Two follow-ups are mandatory, so do them in this order. Force-push the rewritten history first, because every other clone, fork and CI checkout still holds the old objects until you do. Then rotate every credential the file ever held, because the file may have sat in public history for months. Assume copies exist that no rewrite reaches.

Deny Rules Beat Prompts

Prevention is cheaper than scrubbing, and it starts with rules that never produce a stored allow rule. Deny rules apply from any scope the moment the session starts, and a denied call leaves nothing behind in any settings file:

vim .claude/settings.json

Deny the reads that matter and a prompt never gets the chance to become a stored rule:

{
  "permissions": {
    "deny": [
      "Read(./.env)",
      "Read(./**/.env)",
      "Read(~/.ssh/**)"
    ]
  }
}

Asking for the denied file in the same session produces the error, never a prompt:

Deny rules in .claude/settings.json blocking a Read of .env with the permission settings error

Version 2.1.319 added permissions.denyInsteadOfPrompting, which the changelog describes as denying “a tool call that would wait on a permission prompt”, in terminal sessions, subagents and teammates, so the turn continues instead of stalling. In the lab’s manual-mode sessions it wasn’t honored from project settings, only from user scope, so put it in ~/.claude/settings.json until the docs page appears. The capture shows what that buys you, a denial with no prompt to approve:

denyInsteadOfPrompting set in user settings denying an unapproved command outright with no prompt

The deny list has a blind spot that the approvals list creates. Never accept an interpreter-class allow rule such as Bash(python:*), Bash(node:*) or Bash(bash:*), because the stored rule allows arbitrary code and the interpreter reads files directly: python -c 'print(open(".env").read())' walks straight past Read(./.env) with no prompt to approve. Approve narrow subcommand forms instead, such as Bash(python -m pytest:*), and when you want a prompt every time for something risky, an ask rule beats a stored allow:

{
  "permissions": {
    "ask": [
      "Bash(curl *)"
    ]
  }
}

With that rule in place, every curl call asks, no matter what an allow list or a well-meaning “don’t ask again” click stored earlier. Ask and deny apply right away from any settings file, which is what makes them the two scopes you can accept from a repo you trust. The docs are blunt about the interpreter gap itself: Read and Edit deny rules do not reach arbitrary subprocesses that open files on their own, and they point to Claude Code’s sandbox as the control that does, so treat the deny list as one layer and the narrow allow rule as the other.

Two older releases tightened the same screw, and both change what you can trust a repo’s settings file to do. Neither one is optional reading if you clone other people’s projects:

  • 2.1.257: a project or local settings file can no longer set defaultMode to bypassPermissions
  • 2.1.211: “always allow” rules are saved at the repo root, so they survive worktrees

For the broader permission model, including the rule syntax and every scope, the Claude Code cheat sheet has the full reference.

The Hook Stops the Next One

A pre-commit hook is the control that works while you’re not paying attention, because it fires on the commit itself. gitleaks ships one for exactly this case:

vim .git/hooks/pre-commit

Pointed at what is staged, protect --staged fails the commit on any finding:

#!/bin/sh
gitleaks protect --staged -v

A hook git does not consider executable never runs, and the commit then sails through unscanned. Make it executable before you test anything:

chmod +x .git/hooks/pre-commit

Note the -f on the add. The global exclude hides the file from git here too, so committing it deliberately takes a forced add, and the hook is what catches it. That catch-22 is the point, because a leak commit should never be one keystroke away:

git add -f .claude/settings.local.json
git commit -m "add local claude settings"

Git refuses the commit before it exists. In the capture you can see the exit code and the empty history:

A gitleaks pre-commit hook blocking the commit that would have added settings.local.json, leaving the repo with no commits at all

No commit ever happens, and in a fresh repo that means no commit exists at all. For agent-specific policy that a plain gitleaks rule can’t express, a PreToolUse hook inside Claude Code itself can block the command before it runs, and the hooks guide shows the exit-code contract. Put the hook in your repo templates and the next contributor is covered before their first approval.

The Order That Protects You

Each layer in this article covers the gap the previous one leaves, so read the table as an order rather than a menu:

LayerThe gap it closesWhat you do
ApprovalsLiteral commands with secrets stored as allow rulesApprove patterns like Bash(npm run:*), never a command carrying a credential and never an interpreter rule, which can read anything your deny rules protect
Global ignoreProtection that lives only in your home directoryAdd .claude/settings.local.json to each repo’s .gitignore before the first write
TrackingIgnore rules that stop applying once the file is committedRun the tracking audit loop across your repos
ScannersPasswords typed inside commands that provider rules missScan settings files with a custom rule, history included
Historyrm --cached commits that keep every old blobgit filter-repo, force-push, then rotate the credentials
RuntimePrompts that invite inline secrets in the first placeDeny rules on credential files, denyInsteadOfPrompting in user scope

One habit underlies the whole table. Stop typing secrets into commands at all, and reference an environment variable that the shell fills in. A secret manager that injects credentials at runtime, such as the setup in the Infisical guide, removes the string the approval would otherwise store. Anthropic’s own response on the Hacker News thread was that Claude Code “has not done secret scrubbing” historically and that they’re “looking into what we can do to prevent this”, which means the layers above stay your job for now.

Keep reading

Ollama Models Cheat Sheet: Best Models by VRAM, Tested (PDF) AI Ollama Models Cheat Sheet: Best Models by VRAM, Tested (PDF) Open Source LLM Comparison Table (2026) AI Open Source LLM Comparison Table (2026) Claude Code Cheat Sheet: Commands, Shortcuts and Flags (PDF) AI Claude Code Cheat Sheet: Commands, Shortcuts and Flags (PDF) Dual GPU LLM Build: PCIe x8/x8 vs x16/x4, PSU and Cooling AI Dual GPU LLM Build: PCIe x8/x8 vs x16/x4, PSU and Cooling MCP vs RAG vs Agent Skills: When to Use Each AI MCP vs RAG vs Agent Skills: When to Use Each Install Weaviate Vector Database on Ubuntu 26.04 / 24.04 AI Install Weaviate Vector Database on Ubuntu 26.04 / 24.04

Leave a Comment

Press ESC to close