settings.json (deny wins at every level, in every mode), the sandbox for operating-system limits on shell commands, PreToolUse hooks for your own checks, and managed settings for a whole organisation. Deny rules match what Claude writes, not what a program does, which is why the sandbox matters.1. Permission modes
The mode sets the baseline for what runs without a prompt. Switch with Shift+Tab in the CLI, start with claude --permission-mode <mode>, or set permissions.defaultMode in a settings file.
| Mode | Runs without asking | Anthropic's "best for" |
|---|---|---|
default (shown as Manual; manual is accepted as an alias) | File reads in your working directories and actions that need no approval | Sensitive work, unfamiliar code |
acceptEdits | Reads, file edits and common filesystem commands (mkdir, touch, mv, cp…) | Iterating on code you're reviewing |
plan | Reads, plus classifier-approved commands when auto mode is available | Exploring a codebase before changing it |
auto | Everything, with a background classifier checking actions | Long tasks, reducing prompt fatigue |
dontAsk | Reads and pre-approved tools; anything that would prompt is denied | Locked-down CI and scripts |
bypassPermissions | Everything | Isolated containers and VMs only |
- Which mode you start in: in a terminal or the VS Code extension, the built-in default is
autofrom Claude Code v2.1.283 (earlier versions depended on plan and feature flags).claude -pand the Agent SDK start indefault. SettingdisableAutoModeto"disable"in any settings file makes sessions start indefault. - A cloned repo can't switch you into the risky modes.
"defaultMode": "auto"or"bypassPermissions"in a project's.claude/settings.jsonor.claude/settings.local.jsondoesn't take effect; for bypass, the session starts in Manual mode. - Turn bypass off for good:
"permissions": {"disableBypassPermissionsMode": "disable"}. Claude Code then rejects--dangerously-skip-permissions. It works from any settings file, so you can lock yourself out of bypass mode in your own user settings. - Protected paths such as
.git,.claude,.vscode,.husky,.mcp.json,.npmrcand shell startup files (.bashrc,.zshrc…) are prompted indefaultandacceptEdits, sent to the classifier inauto, denied indontAskand allowed inbypassPermissions. Allow rules in settings don't pre-approve writes to them.
2. Allow, ask and deny rules
Where rules live
| Scope | File | Applies to |
|---|---|---|
| Managed | managed-settings.json, MDM / registry policy, or server-managed settings | Everyone the organisation deploys it to; can't be overridden |
| Project local | .claude/settings.local.json | You, in this project (kept out of git when Claude Code creates it) |
| Shared project | .claude/settings.json | Everyone who works in the repo, once committed |
| User | ~/.claude/settings.json (%USERPROFILE%\.claude on Windows) | You, in every project on this machine |
Rule lists from every file are merged, not replaced. Rules are evaluated deny, then ask, then allow, and the first match wins regardless of how specific a rule is: a deny for Bash(aws *) blocks aws s3 ls even if another file allows Bash(aws s3 ls). A deny at any level can't be overridden by an allow at another level, or by --allowedTools. Run /permissions to see every rule and the file it came from.
Audit the local file now and then: when you pick "Yes, and don't ask again" for a Bash command or a web domain, Claude Code saves an allow rule to .claude/settings.local.json at the repository root, and it applies to every future session in that repository.
Rule syntax
| Rule | Matches |
|---|---|
Bash (or Bash(*)) | Every Bash command. As a deny rule, removes the tool from Claude's context |
Bash(npm run *) | npm run build, npm run test --watch, plain npm run; not npm install |
Bash(git * main) | Every git subcommand ending in main, including git push origin main. Put the * after the subcommand |
Read(./.env) | Reading .env in the current directory |
WebFetch(domain:example.com) | Fetches to example.com |
mcp__github__* | Every tool from the MCP server named github; "mcp__*" as a deny rule blocks every MCP tool |
Three traps
- Bash rules match text, not programs. The docs' own examples:
Bash(curl *)doesn't stop/usr/bin/curl …orsh -c 'curl …';Bash(git push *)doesn't stopgit -C . push origin main. Use deny rules for the forms Claude normally writes, and the sandbox or a hook for real enforcement. - A single leading slash is not the filesystem root. In file rules,
//pathis absolute,~/pathis your home directory,/pathis relative to the settings file's location, andpathor./pathis relative to the current directory. SoRead(/secrets/**)in your user settings protects~/.claude/secrets, not your project'ssecretsfolder. On Windows, paths are normalised to the form/c/Users/…before matching. - Read deny rules cover Claude's file tools and the file commands it recognises (
cat,head,tail,sed,tee, redirections). They don't covergrep -r pattern .run from the folder above, or a Python or Node script that opens the file itself. A.claudeignorefile has no effect; move its entries into Read deny rules. A Read deny also blocks Edit and Write on that path, but not NotebookEdit.
3. A safe starter settings.json
Every key and rule form below appears in Anthropic's settings and permissions docs. Save it as ~/.claude/settings.json to cover every project on your machine, or as .claude/settings.json in a repo and commit it to cover your team. Merge it with what you already have rather than overwriting.
{
"permissions": {
"deny": [
"Read(**/.env)",
"Read(**/.env.*)",
"Read(secrets/**)",
"Read(~/.ssh/**)",
"Read(~/.aws/**)"
],
"ask": [
"Bash(curl *)",
"Bash(wget *)",
"Bash(git push *)",
"Bash(dangerouslyDisableSandbox:true)"
],
"disableBypassPermissionsMode": "disable"
},
"sandbox": {
"enabled": true,
"credentials": {
"files": [
{ "path": "~/.ssh", "mode": "deny" },
{ "path": "~/.aws/credentials", "mode": "deny" }
]
}
}
}
deny: Claude's file tools can't read (or edit) any.envor.env.*file at or below the folder you start in, any folder namedsecretsat any depth below it, or your SSH and AWS folders..env.*also catches.env.example; drop that line if Claude needs to read your example file.ask: network downloads, pushes and any request to rerun a command outside the sandbox always prompt you. Ask rules are among the actions no mode auto-approves, includingautoandbypassPermissions.disableBypassPermissionsMode: no--dangerously-skip-permissionson this machine or in this repo.sandbox: shell commands run inside the OS sandbox, and sandboxed commands can't read your SSH keys or AWS credentials file. The sandbox needs macOS, Linux or WSL2. On native Windows, Claude Code runs commands unsandboxed, so there the deny rules are your main file protection.
One more step from the docs: set the environment variable CLAUDE_CODE_SUBPROCESS_ENV_SCRUB=1 before starting Claude Code to strip credentials from the environment of the subprocesses it starts (Bash commands, hooks and stdio MCP servers). And keep secrets out of the env block of any settings file you commit, since everyone with the repo can read it. Claude Code stores its own API keys and tokens in the macOS Keychain when available, in a file with mode 0600 on Linux, and in a file that inherits your user profile's access controls on Windows.
4. The sandbox
The sandbox is an operating-system boundary around the shell commands Claude runs (Bash, PowerShell and Monitor commands, and the processes they start). It is off by default; turn it on with /sandbox or "sandbox": {"enabled": true}. macOS uses the built-in Seatbelt framework; Linux and WSL2 need bubblewrap and socat.
| Access | Default inside the sandbox | Change it with |
|---|---|---|
| Writes | The working directory, a per-user temp directory and added directories | filesystem.allowWrite, filesystem.denyWrite |
| Reads | Most of the machine, including ~/.ssh and ~/.aws/credentials | filesystem.denyRead, credentials |
| Network | Only through a local proxy that checks each host against your allowed domains, which start empty | network.allowedDomains, network.deniedDomains |
| Environment variables | Inherited, including secrets | credentials, CLAUDE_CODE_SUBPROCESS_ENV_SCRUB |
Note the read default: without denyRead or credentials entries, a sandboxed command can still read your keys. "There is no built-in credential deny list."
- What runs outside it: Claude's own file and web tools (Read, Edit, Write, WebFetch, WebSearch follow permission rules instead), command hooks, local MCP servers, the status line command and
apiKeyHelper. Those run with your full access. - The escape hatch: when a command fails in the sandbox, Claude can retry it with
dangerouslyDisableSandbox. In Manual mode you get a prompt; inbypassPermissionsit runs without one; a matching allow rule such asBash(curl *)also approves the retry. To remove the hatch, set"allowUnsandboxedCommands": falseundersandbox(strict sandbox mode); to refuse to start when the sandbox can't run, also set"failIfUnavailable": true. - Want everything in one boundary? Run the whole Claude Code process in a dev container, a VM or Anthropic's sandbox runtime.
5. Hooks
A PreToolUse hook runs your own script before a tool call. Exit code 2 blocks the call and sends your stderr message to Claude; exit 0 means "no objection", and the normal permission flow still applies. A hook that returns "permissionDecision": "deny" blocks the tool in every mode, including bypassPermissions, which makes hooks the place for rules people must not switch off. This registration, from Anthropic's hooks guide, runs a script before every Edit or Write:
{
"hooks": {
"PreToolUse": [
{
"matcher": "Edit|Write",
"hooks": [
{
"type": "command",
"command": "\"$CLAUDE_PROJECT_DIR\"/.claude/hooks/protect-files.sh"
}
]
}
]
}
}
Two cautions from the same docs. Claude can still change a protected file with a shell command, so pair the hook with an Edit deny rule and the sandbox. And hooks are programs that run with your access: a hook in a repository's .claude/settings.json is code from that repository.
6. MCP servers
- Nobody audits them for you. Anthropic reviews connectors before listing them in its directory, "but does not security-audit or manage any MCP server". The MCP docs add: "Servers that fetch external content can expose you to prompt injection risk."
- Project servers need approval. In interactive sessions, Claude Code asks before using servers from a project's
.mcp.json, and a cloned repo can't approve its own servers:enableAllProjectMcpServersorenabledMcpjsonServerscommitted in an untrusted folder are ignored. Butclaude -pruns, Agent SDK sessions and some cloud sessions load project servers without asking. - Block a server everywhere with
"disabledMcpjsonServers": ["name"], which applies in every permission mode, or start with--strict-mcp-configto load only the servers you pass with--mcp-config. - Limit what a server's tools can do with rules such as
mcp__github__*in ask, or deny"mcp__*"to block all MCP tools. .mcp.jsonisn't the full list. Servers can also come from user scope, claude.ai connectors and plugins;/mcpandclaude mcp listshow what a session loads.
Check your config with the MCP config checker (hard-coded tokens, unpinned packages, whole-disk filesystem access) and look up servers in the MCP server safety directory.
7. Prompt injection from repository content
Anything Claude reads can carry instructions: a README, an issue, a code comment, a web page, an MCP tool result. The docs list the defences built in: the permission system, approval for network commands such as curl and wget in Manual mode, a separate model that summarises most WebFetch pages instead of passing the raw page, the workspace trust dialog, and, in Manual mode, a prompt before running a Bash command Claude Code can't fully analyse. They also say: "no system is completely immune to all attacks."
The part most people miss is what a repository can do before you trust it. From Anthropic's permissions page:
| What the repo supplies | You trusted only a parent folder | claude -p or SDK, folder never trusted |
|---|---|---|
Hooks in settings files, the env block, helpers such as apiKeyHelper | Used | Used |
permissions.allow rules in .claude/settings.json | Not used until you accept the trust dialog | Not used (warning printed) |
Servers in .mcp.json | Claude Code asks before connecting | Connected without asking |
Before running claude -p in a repository you didn't write, the docs suggest one of: --setting-sources user (ignore the project's settings and .mcp.json), --bare (no project hooks, skills, plugins or MCP servers), or --settings '{"disableAllHooks": true}'. Setting disableAllHooks only in your user settings isn't enough, because project settings take precedence. For hostile content in general, Anthropic recommends reviewing commands before approving them, not piping untrusted content straight to Claude, and using VMs for scripts and tool calls that touch external services.
Instruction files are worth scanning too: the free CLAUDE.md security check looks for curl | sh, exfiltration, hidden instructions, bypassPermissions and auto-approved commands, and the hidden Unicode checker finds invisible characters in rules files.
8. Enterprise: managed settings
Managed settings sit above every other level: "Nothing in your own settings files or --settings overrides a managed key." Four ways to deliver them: server-managed settings from the claude.ai admin console (fetched at startup and polled hourly), MDM or OS policy (macOS profile or Windows HKLM registry), a file, or a Windows HKCU registry value. File locations:
- macOS:
/Library/Application Support/ClaudeCode/managed-settings.json - Linux and WSL:
/etc/claude-code/managed-settings.json - Windows:
C:\Program Files\ClaudeCode\managed-settings.json(the legacyC:\ProgramDatapath is not read)
Anthropic's example blocks two file reads, turns off bypass mode and ignores permission rules from every non-managed source:
{
"permissions": {
"deny": [
"Read(./.env)",
"Read(./secrets/**)"
],
"disableBypassPermissionsMode": "disable"
},
"allowManagedPermissionRulesOnly": true
}
Other keys only a managed source can set include allowManagedHooksOnly, allowManagedMcpServersOnly (only managed allowedMcpServers count; deniedMcpServers still merges), sandbox.network.allowManagedDomainsOnly, strictKnownMarketplaces for plugins and forceRemoteSettingsRefresh to fail closed when the policy can't be fetched. To confirm a file policy applied, run /status: the "Setting sources" line shows Enterprise managed settings (file). A few restrictive keys are honoured from lower levels even against a managed value, such as permissions.blockReadsOutsideWorkingDirectories: true.
A 10-minute audit
- Run
/permissionsand read every allow rule, especially old "don't ask again" entries in.claude/settings.local.json. - Run
/statusto see which settings sources loaded, andclaude doctorfor settings errors. - Search your settings files for
bypassPermissions,enableAllProjectMcpServersand broad allows such asBash(git *). - Read the
hooksandenvblocks of every committed.claude/settings.json. - List MCP servers with
claude mcp listand check each is pinned and needed. - Add the deny and ask rules above, and turn on the sandbox if you're on macOS, Linux or WSL2.
Sources
All from Anthropic's Claude Code docs, read 1 Dec 2026: Security, Configure permissions, Choose a permission mode, Settings files and precedence, All settings, Sandboxed Bash tool, Hooks guide, MCP, Deploy managed settings and Environment variables. Claude Code updates often and several behaviours above depend on version: if something here no longer matches, the official docs win.
Related
- OWASP Top 10 for LLM Applications (2026): LLM03 Excessive Agency and LLM04 Supply Chain apply to coding agents too
- What is prompt injection?
- Install Claude Code on Windows
settings.json files, CLAUDE.md, .mcp.json and the MCP servers they launch, for leaked keys, over-broad permissions, unpinned servers and hooks you did not mean to run. Written findings with the file and line, and a fix for each. Request a Quick ReviewFAQ
How do I stop Claude Code from reading my .env file?
Add Read deny rules to permissions.deny in settings.json, for example "Read(**/.env)" and "Read(**/.env.*)". A Read deny rule also blocks the Edit and Write tools on the same path. It does not stop a script or a command like grep -r that reads files without naming them, so for operating-system enforcement also turn on the sandbox and deny the paths there.
What does --dangerously-skip-permissions do in Claude Code?
It starts the session in bypassPermissions mode, which skips permission prompts, including for writes to protected paths such as .git and .claude. Anthropic's docs say to use it only in isolated environments like containers or VMs. Setting permissions.disableBypassPermissionsMode to "disable" makes Claude Code reject the flag.
Does the Claude Code sandbox work on Windows?
Not on native Windows. The sandbox runs on macOS, Linux and WSL2; on native Windows Claude Code runs commands unsandboxed. To use it on a Windows machine, run Claude Code inside a WSL2 distribution.
Can CLAUDE.md or a prompt change Claude Code's permissions?
No. Anthropic's docs say permission rules are enforced by Claude Code, not by the model; instructions in a prompt or CLAUDE.md shape what Claude tries to do but don't change the rules. Use /permissions, settings files, a permission mode or a PreToolUse hook to change access.
Can a cloned repository run code through Claude Code?
Yes, in some cases. Hooks in a repository's settings files, its env block and helper commands such as apiKeyHelper are used even when you only trusted a parent folder or run claude -p, and claude -p connects .mcp.json servers without asking. Before running claude -p on a repo you didn't write, pass --setting-sources user, start with --bare, or pass --settings '{"disableAllHooks": true}'.
Where are Claude Code managed settings stored?
As managed-settings.json in /Library/Application Support/ClaudeCode/ on macOS, /etc/claude-code/ on Linux and WSL, and C:\Program Files\ClaudeCode\ on Windows. They can also come from MDM or registry policy, or as server-managed settings from the claude.ai admin console. Nothing in user or project settings overrides a managed key.