Shipping an AI app or agent? Security review in 1 working day: $149 / ₹13,900. Request a review →
AI Review Ready
Home / Claude Code / Security settings
Guide · Claude Code · Security

Claude Code security: permissions, sandbox and a safe settings.json

Claude Code can read your files, run shell commands and connect to MCP servers. What it may do without asking is decided by its permission mode, the allow, ask and deny rules in settings.json and, for shell commands, an operating-system sandbox. Here is each control as Anthropic documents it, the traps in the rule syntax, and a starter configuration that keeps secrets out.

Short answer: Anthropic's docs put it in one line: "Permission rules are enforced by Claude Code, not by the model." So the controls that hold are configuration, not instructions: a permission mode, deny / ask / allow rules in 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.

ModeRuns without askingAnthropic's "best for"
default (shown as Manual; manual is accepted as an alias)File reads in your working directories and actions that need no approvalSensitive work, unfamiliar code
acceptEditsReads, file edits and common filesystem commands (mkdir, touch, mv, cp…)Iterating on code you're reviewing
planReads, plus classifier-approved commands when auto mode is availableExploring a codebase before changing it
autoEverything, with a background classifier checking actionsLong tasks, reducing prompt fatigue
dontAskReads and pre-approved tools; anything that would prompt is deniedLocked-down CI and scripts
bypassPermissionsEverythingIsolated containers and VMs only
  • Which mode you start in: in a terminal or the VS Code extension, the built-in default is auto from Claude Code v2.1.283 (earlier versions depended on plan and feature flags). claude -p and the Agent SDK start in default. Setting disableAutoMode to "disable" in any settings file makes sessions start in default.
  • A cloned repo can't switch you into the risky modes. "defaultMode": "auto" or "bypassPermissions" in a project's .claude/settings.json or .claude/settings.local.json doesn'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, .npmrc and shell startup files (.bashrc, .zshrc…) are prompted in default and acceptEdits, sent to the classifier in auto, denied in dontAsk and allowed in bypassPermissions. Allow rules in settings don't pre-approve writes to them.

2. Allow, ask and deny rules

Where rules live

ScopeFileApplies to
Managedmanaged-settings.json, MDM / registry policy, or server-managed settingsEveryone the organisation deploys it to; can't be overridden
Project local.claude/settings.local.jsonYou, in this project (kept out of git when Claude Code creates it)
Shared project.claude/settings.jsonEveryone 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

RuleMatches
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 … or sh -c 'curl …'; Bash(git push *) doesn't stop git -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, //path is absolute, ~/path is your home directory, /path is relative to the settings file's location, and path or ./path is relative to the current directory. So Read(/secrets/**) in your user settings protects ~/.claude/secrets, not your project's secrets folder. 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 cover grep -r pattern . run from the folder above, or a Python or Node script that opens the file itself. A .claudeignore file 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 .env or .env.* file at or below the folder you start in, any folder named secrets at 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, including auto and bypassPermissions.
  • disableBypassPermissionsMode: no --dangerously-skip-permissions on 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.

AccessDefault inside the sandboxChange it with
WritesThe working directory, a per-user temp directory and added directoriesfilesystem.allowWrite, filesystem.denyWrite
ReadsMost of the machine, including ~/.ssh and ~/.aws/credentialsfilesystem.denyRead, credentials
NetworkOnly through a local proxy that checks each host against your allowed domains, which start emptynetwork.allowedDomains, network.deniedDomains
Environment variablesInherited, including secretscredentials, 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; in bypassPermissions it runs without one; a matching allow rule such as Bash(curl *) also approves the retry. To remove the hatch, set "allowUnsandboxedCommands": false under sandbox (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: enableAllProjectMcpServers or enabledMcpjsonServers committed in an untrusted folder are ignored. But claude -p runs, 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-config to 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.json isn't the full list. Servers can also come from user scope, claude.ai connectors and plugins; /mcp and claude mcp list show 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 suppliesYou trusted only a parent folderclaude -p or SDK, folder never trusted
Hooks in settings files, the env block, helpers such as apiKeyHelperUsedUsed
permissions.allow rules in .claude/settings.jsonNot used until you accept the trust dialogNot used (warning printed)
Servers in .mcp.jsonClaude Code asks before connectingConnected 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 legacy C:\ProgramData path 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

  1. Run /permissions and read every allow rule, especially old "don't ask again" entries in .claude/settings.local.json.
  2. Run /status to see which settings sources loaded, and claude doctor for settings errors.
  3. Search your settings files for bypassPermissions, enableAllProjectMcpServers and broad allows such as Bash(git *).
  4. Read the hooks and env blocks of every committed .claude/settings.json.
  5. List MCP servers with claude mcp list and check each is pinned and needed.
  6. 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

Want a second pair of eyes on it? A Quick Review ($149 / ₹13,900, 1 working day) checks one repo, config or setup: your Claude Code 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 Review

FAQ

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.