Study notes · 10% of the exam

Built-in Tools, Permission Modes and Rules

Claude Code, not the model, enforces what an agent may do. Every tool call passes through a fixed chain (hooks, deny rules, ask rules, permission mode, allow rules, then your canUseTool callback), and a call approved at any step never reaches the later ones. Know the six modes, the rule syntax for Bash, Read/Edit, WebFetch and MCP, what no mode will auto-approve, and where the OS sandbox takes over from text-matching rules.

Key points

  1. 1

    Evaluation order in the SDK: PreToolUse hooks, deny rules, ask rules, permission mode, allow rules, canUseTool. A hook can deny outright, but its allow does not skip deny or ask rules. Deny rules block even in bypassPermissions; ask rules send the call to the callback even in bypassPermissions.

  2. 2

    Modes (PermissionMode): default (unresolved calls go to canUseTool), acceptEdits (file edits plus mkdir/touch/rm/rmdir/mv/cp/sed inside the working directories), plan (edits never auto-approved, even with an allow rule or hook allow), dontAsk (anything that would prompt is denied, callback skipped), bypassPermissions (approves everything that reaches the mode step), auto (a classifier reviews shell commands, network requests and similar actions).

  3. 3

    Set the mode with permissionMode / permission_mode, change it mid-session with setPermissionMode() / set_permission_mode(). TypeScript requires allowDangerouslySkipPermissions: true for bypass. In auto mode, switching to an unsupported model drops the session to default (a system status message), and switching back needs an explicit setPermissionMode('auto').

  4. 4

    allowedTools / allowed_tools adds allow rules; it is not a whitelist. Unlisted tools still exist and fall through to the mode, so allowed_tools=['Read'] plus bypassPermissions still runs Bash. Restrict with disallowedTools or the tools option. The locked-down pattern is allowedTools + permissionMode: 'dontAsk'.

  5. 5

    Deny rules: a bare name such as Bash (or a glob like * or mcp__*) removes the tool from the request; a scoped rule such as Bash(rm *) keeps the tool and blocks matching calls in every mode. Rules are evaluated deny, then ask, then allow; specificity never lets an allow carve an exception out of a deny.

  6. 6

    Bash rule syntax: * matches any text including spaces; a trailing * that is the only wildcard also matches the bare command (Bash(ls *) matches ls but not lsof; Bash(ls*) matches lsof); :* at the end equals *. Allow rules must match every subcommand of &&, ||, ;, | chains, while deny and ask rules apply when any subcommand matches. Wrappers like timeout, nice, nohup are stripped before matching.

  7. 7

    Read/Edit rules use gitignore patterns with four anchors: //path absolute, ~/path home, /path relative to the settings source (project root for project settings, ~/.claude for user settings), path relative to the current directory. Edit(path) governs Write and NotebookEdit too; Write(path) and Glob(path) rules never match. A Read deny also blocks Edit/Write on that path.

  8. 8

    WebFetch rules use domain: (WebFetch(domain:*.example.com) covers subdomains but not the apex). MCP: mcp__server__tool, or mcp__server__*; allow globs need a literal mcp__<server>__ prefix, so mcp__* or * in an allow list is ignored. Agent(Explore) style rules control subagents.

  9. 9

    canUseTool(toolName, input, options) returns { behavior: 'allow', updatedInput?, updatedPermissions? } or { behavior: 'deny', message, interrupt? } (Python PermissionResultAllow / PermissionResultDeny). It fires only for calls nothing earlier resolved, can stay pending indefinitely, and suggestions can be echoed back as updatedPermissions (a localSettings destination persists to .claude/settings.local.json). Python rejects can_use_tool together with permission_prompt_tool_name.

  10. 10

    No mode auto-approves: explicit ask-rule matches, AskUserQuestion and MCP tools marked requiresUserInteraction, organisation ask connector tools, and recognised rm/rmdir of critical paths (root, top-level dirs, home, the working directory and its parents). In SDK sessions in auto mode those removals are denied without calling the callback. Protected paths (.git, .husky, .vscode, most of .claude, .bashrc, .npmrc, .mcp.json...) are prompted in default/acceptEdits, denied in dontAsk, and allowed only in bypass.

  11. 11

    The sandbox option (SandboxSettings: enabled, autoAllowBashIfSandboxed, excludedCommands, allowUnsandboxedCommands, network, filesystem) confines Bash, PowerShell and Monitor commands and their children at the OS level. Read/Edit/WebFetch, hooks and local MCP servers run outside it. In TypeScript the option merges into an inline settings.sandbox: scalars are replaced, restriction lists such as denyRead are combined.

  12. 12

    additionalDirectories / add_dirs are passed as --add-dir: the directory becomes readable and editable under the same rules, and its skills, commands and subagents load through the project setting source; its CLAUDE.md loads only with CLAUDE_CODE_ADDITIONAL_DIRECTORIES_CLAUDE_MD=1, and its hooks and permission rules do not load.

Common traps

  • Putting audit or policy checks in canUseTool: anything approved by an allow rule, acceptEdits or bypassPermissions never reaches it. Use a PreToolUse hook for checks that must see every call; the SDK warns with CLAUDE_SDK_CAN_USE_TOOL_SHADOWED (Python CanUseToolShadowedWarning) for bare allowedTools entries and bypass mode.

  • Treating Bash(curl *) or Read(./.env) deny rules as a security boundary: /usr/bin/curl, sh -c '...' and a Python script that opens .env all slip past. Enforce with the sandbox (allowedDomains, filesystem.denyRead) or a container.

  • Writing Read(/secrets/**) in ~/.claude/settings.json and expecting it to protect every project: in user settings / anchors at ~/.claude.

  • Assuming a more specific allow rule beats a broader deny, or that a hook returning allow skips an ask rule. Neither is true.

  • Expecting an AgentDefinition's permissionMode: 'plan' to restrain a subagent when the parent runs in bypassPermissions: the subagent inherits bypass. A subagent's own mode applies only when the parent is in default, dontAsk or plan.

  • Relying on CLAUDE.md or .claudeignore to keep secrets away: CLAUDE.md is guidance only and .claudeignore has no effect; use Read deny rules.

  • On Windows with Git Bash, any Bash deny rule silently turns off the PowerShell tool unless CLAUDE_CODE_USE_POWERSHELL_TOOL=1 or a scoped PowerShell(...) rule is set.

Further reading

Suggest a resource

No extra reading for Built-in Tools, Permission Modes and Rules yet. Found a guide, video or cheat sheet that made it click? Share it and help the next learner.

Test yourself on Built-in Tools, Permission Modes and Rules

Ten questions, with the answer and explanation after each one.