Claude Code works with your access. It runs commands in your shell, reads and edits your files, and reaches the network as you. That is what makes it useful, and it is also why Claude Code security is worth setting up on purpose rather than trusting the model to behave. This post explains how an agent run goes wrong and the layers Claude Code gives you to stop it: permission rules, auto mode, the sandbox, and managed settings, plus what changes when nobody is watching the run.
What Claude Code can reach
The project folder you start Claude Code in is not a wall. Claude's commands run as your user account, so they can read anything that account can read: other projects, your shell history, a file with saved logins in your home folder. In Manual mode, Claude Code asks before its file tools read outside the working directories and before most commands run. In auto mode, many of those decisions go to a classifier instead of you, so a read outside the project can happen without a prompt.
To keep Claude's file tools inside the working directories in every mode, set permissions.blockReadsOutsideWorkingDirectories to true. It covers the file tools only, not shell commands. For those, you need the sandbox.
How an agent run goes wrong
There are three common ways a run goes wrong, and none of them needs a malicious model:
- A mistake. Claude misreads the request or takes a shortcut, such as changing the wrong files to make a check pass
- A project that brings its own configuration. A repository you clone can carry settings that allow commands, hooks that run scripts, and MCP servers. Claude Code lists the project's allow rules in the trust dialog before it applies them, but a dialog is easy to accept without reading
- Prompt injection. Text in something Claude reads, such as a log line, an issue, or a web page, tells it to do something you never asked for, like uploading a secrets file to an outside server
Instructions in your prompt or CLAUDE.md only shape what Claude tries. The layers below are enforced by Claude Code or by the operating system, so they hold even when the model is fooled.
Permission rules: what may run
Permission rules are allow, ask, and deny lists in your settings files. Claude Code checks them in a fixed order: deny first, then ask, then allow. Rules from all your settings files are combined, so a deny in any file wins over an allow in any other.
{
"permissions": {
"deny": ["Read(./.env)"],
"ask": ["Bash(git push *)"],
"allow": ["Bash(npm test)"]
}
}
Rules have one limit that matters for security: a Bash rule matches the text of a command. The same program run another way, through a script, or by a program that opens a file itself, gets past it. Claude Code permissions covers how to write rules and exactly where they stop. For checks a rule can't express, a hook can inspect each command before it runs, as in block commands with Claude Code hooks.
Auto mode: a classifier instead of prompts
Auto mode lets Claude work without routine permission prompts. Each action still goes through your permission rules first. Reads and edits in the project are then approved, and everything else goes to a classifier: a separate model that judges each action before it runs. It blocks actions that go beyond what you asked for, reach infrastructure it doesn't recognize, or look driven by content Claude read. A clear request from you lifts most of its blocks, but sending data out of your environment stays blocked even when you ask.
By default, the classifier trusts only your working directory and its git remotes. You tell it about the rest of your infrastructure, such as source control, internal domains, and storage buckets, with autoMode.environment in your user settings. Project settings can't add to that list, so a cloned repository can't widen what the classifier trusts. claude auto-mode defaults prints the built-in rules, and claude auto-mode config shows the result with your entries.
Treat auto mode as a safety net, not a wall. In our own testing, a runbook step that the classifier blocked one day went through a few days later, with the same project and the same settings. It is a judgment, and judgments vary.
The sandbox: what commands can reach
The sandbox is enforced by the operating system, not by Claude Code's reading of a command. It wraps shell commands and every program they start. Commands can write only inside the project and a temporary folder, and reach the network only through a proxy that lets through the hosts you allow. Reads still reach most of the machine unless you deny them.
The sandbox also borrows your file and web permission rules. A Read deny on .env, which on its own stops Claude's file tools and commands like cat, then stops every program from opening the file. Claude's own file and web tools, hooks, and MCP servers run outside the sandbox. Claude Code sandbox shows how to turn it on, test it, and close its escape hatch.
Managed settings: a policy nobody can override
Everything so far lives in files that a user or a project can change. Managed settings sit above all of them: no user, project, or command-line setting can override them. The simplest form is one file in a system folder: /etc/claude-code/managed-settings.json on Linux and WSL, /Library/Application Support/ClaudeCode/managed-settings.json on macOS, and C:\Program Files\ClaudeCode\managed-settings.json on Windows. Writing it needs administrator rights, which Claude doesn't have. /status shows whether it is in force.
The settings worth locking there:
- Deny rules for files no one should read, such as
.envor credential files disableBypassPermissionsMode, so bypass mode can't be turned on- The sandbox, with
enabledandfailIfUnavailableset totrueandallowUnsandboxedCommandsset tofalse, so it can't be skipped or left allowManagedPermissionRulesOnlyandallowManagedHooksOnly, when only the policy should supply rules and hooks
Unattended runs
A headless run with claude -p has nobody to answer prompts, and it shows no trust dialog: it uses the project's hooks and .mcp.json servers without asking. For a repository you didn't write, start from your own settings only, give the run an exact list of what it may do, and deny everything else:
claude -p "Summarize the last ten commits" \
--setting-sources user \
--strict-mcp-config \
--permission-mode dontAsk \
--allowedTools "Bash(git log *)"
One trap: with the sandbox on, sandboxed commands are approved automatically, even ones that aren't on your list. Add --settings '{"sandbox":{"autoAllowBashIfSandboxed":false}}' so the list decides. Bypass mode belongs only in a disposable container or virtual machine, never on your own machine, and Claude Code refuses to start it as root.
Conclusion
Claude Code security comes in layers, and each one covers what the one before it can't. Permission rules decide what may run, auto mode judges actions you didn't spell out, the sandbox limits what commands can reach whatever they look like, and managed settings make sure nobody can switch any of it off. None of them is enough alone. Together, they let an agent do real work without the power to do real damage.
Relevant Training
Every layer in this post is taught and demonstrated in Claude Code Agent Security, part of Cybrec's Claude AI training program, with hands-on tasks in an AI Lab.


0 Comments