NewThe Mac app is out for Apple SiliconGet it →
Fenzi
blog, safety

What are Claude Code security best practices?

Keep your secrets out of Claude Code's reach twice, in its permission rules and in its Bash sandbox, and give the sandbox a short list of hosts. Treat every repository you did not write as untrusted input, because its settings, hooks and MCP servers can run before you approve anything. Skip permission prompts only inside a container or a virtual machine, and on a team, deliver the rules as managed settings and check that they actually applied.

By Boris Kwemo, , seven-minute read

Search for this phrase and you also find Claude Code Security, an Anthropic product announced on 20 February 2026 that scans a codebase for flaws and proposes fixes. This post is about the other question: how to configure the agent itself so that a bad command, a bad file or a bad instruction costs you little. Every practice below comes from Anthropic's own documentation or advisories, and most of them apply to any AI agent you run.

Know what the defaults do

Anthropic's Security page says auto mode is the starting permission mode for interactive terminal and VS Code sessions. In auto mode a second model, a classifier, reviews each action instead of you. In manual mode Claude Code starts read-only and asks before it edits a file or runs a command it does not know to be read-only.

Two defaults surprise people. The working directory boundary is a permission prompt for Claude's file tools, so a shell command you approve can write anywhere your account can. And the Bash sandbox is off until you turn it on.

Rules you write in CLAUDE.md are not settings at all. Anthropic's permissions page says instructions shape what Claude tries to do, while settings decide what Claude Code allows.

"Our Claude Code security policy is a paragraph in CLAUDE.md."

Revision 1: that paragraph is a request to the model. Permission rules, the sandbox and managed settings are what Claude Code enforces.

Deny your secrets in two places

Start with deny rules for the files an agent never needs: .env files, cloud credentials, SSH keys. The permissions page is precise about their reach, and a .claudeignore file is no substitute.

Read and Edit deny rules cover Claude's file tools, file commands it recognizes in Bash such as cat and head, and redirections. They do not cover a command that reads files without naming them, such as grep -r, or a Python or Node script that opens the file itself.

That gap is why the same paths belong in the sandbox too, where the operating system enforces them for every shell command and its children. A settings file that does both looks like this, using keys from Anthropic's settings and sandboxing pages:

{
  "permissions": {
    "deny": ["Read(./.env)", "Read(./.env.*)", "Read(~/.ssh/**)", "Read(~/.aws/**)"]
  },
  "sandbox": {
    "enabled": true,
    "failIfUnavailable": true,
    "allowUnsandboxedCommands": false,
    "filesystem": { "denyRead": ["~/"], "allowRead": ["."] },
    "network": { "allowedDomains": ["registry.npmjs.org"] }
  }
}

failIfUnavailable makes Claude Code exit instead of quietly running unsandboxed when the sandbox cannot start. allowUnsandboxedCommands: false stops Claude from retrying a blocked command outside the sandbox. The denyRead line matters most: by default the sandbox can read almost the whole machine, credential files included.

Secrets in the environment are the other half. The sandbox passes Claude Code's environment variables to every command unless you configure its credentials setting, and anything a command can see can leave through an allowed host.

Sandbox the shell, and know what the sandbox leaves out

The sandboxed Bash tool limits what shell commands can write and which hosts they can reach, with the host list starting empty. It covers shell commands only. Claude's built-in file and web tools, hooks, local MCP servers and your apiKeyHelper run with your full access, and a denyRead entry does not stop the Read tool.

Keep the host list short and specific. Anthropic warns that a broad domain such as github.com can become a path for data exfiltration, because the proxy decides from the hostname the client supplies and does not inspect TLS. Keep writes away from shell startup files and folders on your PATH, which the same page lists as a route to code execution.

Sandboxing reduces risk but is not a complete isolation boundary.Claude Code docs, Sandboxing

The sandbox is software and has had bugs. GHSA-7835-87q9-rgvv, published 25 June 2026, let a malicious repository with prompt injection content use git worktrees to write files such as .zshenv outside the macOS sandbox; 2.1.163 fixed it.

GHSA-5xjx-9qx6-jcrv, published 26 October 2026, let code running inside the sandbox race the Edit tool into overwriting a file outside the workspace; 2.1.251 fixed it. Keep auto-update on.

Treat a cloned repository as untrusted input

A repository can carry more than code. Its .claude/settings.json can hold hooks, an env block and helper commands, and its .mcp.json can name MCP servers to start. In an interactive session, Claude Code shows a trust dialog the first time you open a folder, and asks before it connects a project's MCP servers.

Headless runs skip all of that. The permissions page says a claude -p run never shows the trust dialog, uses the repository's hooks and env block, and connects the servers in its .mcp.json without asking. That is the normal shape of a CI job, such as Claude Code in GitHub Actions, or a script, and it is where the prompt injection examples in vendor advisories stop being theory.

Before you run claude -p in a repository you did not write, Anthropic lists the switches. --setting-sources user ignores the project's settings and its .mcp.json. --bare skips the project's hooks, skills, plugins and MCP servers, though its env block still applies. --settings '{"disableAllHooks": true}' turns hooks off for that run.

For real untrusted code, Anthropic's page on choosing a sandbox environment recommends a dedicated virtual machine or a cloud session.

Match the permission mode to the isolation

Each mode trades questions for a different kind of check, and our notes on how Claude Code permissions work cover the rules underneath. The short version, from Anthropic's permission modes page:

ModeWho decidesRun it where
ManualYou, for anything not read-onlyYour machine, while you read the prompts
AutoA classifier modelTasks you trust in direction; isolation adds depth for unattended runs
dontAskYour allow rules; everything else is deniedCI and other unattended jobs with a fixed task
bypassPermissionsNobody, apart from deny rulesA container, a VM or the sandbox runtime only

Auto mode is useful and still not a boundary; Anthropic says it "does not guarantee safety." Bypass mode is the one that needs a box around the whole process, which is most of the answer to whether dangerously skip permissions is safe. For CI, the docs show the narrow form:

claude -p "run the test suite" --permission-mode dontAsk --allowedTools "Bash(npm test)" "Read"

Set it once for the team, then check it applied

A committed .claude/settings.json shares rules with everyone who clones the repository, but each developer can widen them locally. Managed settings, delivered by a file, device management or the claude.ai admin console, sit above every other level and cannot be overridden. Three keys do most of the work there: permissions.disableBypassPermissionsMode and permissions.disableAutoMode set to "disable", and allowManagedPermissionRulesOnly, which makes managed settings the only source of permission rules.

Then check. Running /status shows a Setting sources line that names the managed source in force.

That check is not a formality. GHSA-gfvf-j8jh-jxxw, published 29 September 2026, describes sessions that started without the organization's server-managed policy, deny rules included, when a stored API key was used to fetch it. Version 2.1.260 fixed it; file and device management delivery was not affected.

For a record of what agents did, Claude Code can export OpenTelemetry metrics and events, including a tool decision event each time a permission is accepted or rejected. It is opt-in, prompt text is redacted by default, and admins can set the destination in managed settings so a repository cannot change it.

The list, in one place

Secrets. Deny .env files, SSH keys and cloud credentials in permission rules and in the sandbox's denyRead, and keep long-lived keys out of the agent's environment.

Shell. Turn the sandbox on with failIfUnavailable, turn off unsandboxed retries, and allow named hosts rather than broad domains.

Repositories. Read the trust dialog. In headless runs on code you did not write, ignore project settings or use a VM.

Modes. Manual or auto on your machine, dontAsk with explicit allow rules in CI, bypass only inside a container or VM.

Team. Deliver the rules as managed settings, disable the modes you do not want, check /status, and stay on current releases. These are the Claude Code version of the AI agent security best practices we answer for any agent, and they come down to the same four rules as agentic AI security: directories, hosts, keys, and a person who approves more.

A side note on how we do it

We build Fenzi, which starts Claude Code inside an OpenShell sandbox with a copy of the project, a short host list and a placeholder instead of the real key, so the whole process sits inside the boundary, hooks and MCP servers included. The default access sheet lists what it can reach. Anthropic's sandbox runtime, a dev container or a VM can put the same boundary around it.

So, where do you start?

With the two things a bad instruction needs: a secret within reach and a host to send it to. Deny the first in both permission rules and the sandbox, keep the second to a short list, and be most careful in headless runs, where nobody sees a prompt. Then pick the mode, and on a team make it a managed setting you have checked. Anthropic's Security page for Claude Code is the best next read.

One line to install.
One command to run.

curl -fsSL https://fenzi.dev/install.sh | sh

Then fenzi run claude. Ask it to open a pull request with the GitHub API, and watch the deny land.