Security and Safety
Deterministic Guardrails and Least Privilege
Exam objective: Applying guardrail layering, least privilege, and hooks as deterministic enforcement so that destructive actions cannot execute even under adversarial instruction.
What the exam tests
Whether you can make a destructive action impossible, not merely unlikely. Given an agent, its tools and a threat (a mistake or hostile instructions in content it reads), you choose layers that do not depend on the model agreeing: tool availability, permission rules and modes, hooks, OS or network isolation, and narrowly scoped credentials.
Key rules
Enforcement lives outside the model. System prompts and CLAUDE.md shape what Claude tries to do. Permission rules, modes and hooks are applied by Claude Code.
Rule order. Deny, then ask, then allow. Specificity does not matter, so an allow rule cannot carve an exception out of a deny. A bare deny such as Bash removes the tool from context; a scoped one such as Bash(rm *) blocks matching calls.
Agent SDK flow. Hooks, deny rules, ask rules, permission mode, allow rules, then canUseTool. A call approved early never reaches the callback. allowed_tools only pre-approves and does not constrain bypassPermissions. For a locked-down headless agent, pair it with dontAsk.
Hooks. A PreToolUse hook sees calls before they run and can inspect arguments. Its deny holds in every permission mode; its allow does not override deny or ask rules. When hooks disagree, the most restrictive decision wins.
- Command hooks block with exit code 2, or exit 0 with a JSON
deny. Exit 1 alone does not block. - A missing, crashing or timed-out command hook lets the call continue unless
onFailureis"block". - An SDK callback that times out on
PreToolUseblocks. One that throws lets the call proceed to the permission flow unlessonFailureis set. One that catches its error and returns{}has not failed. PostToolUseruns too late to prevent anything.
{
"permissions": { "deny": ["Bash(git push *)"] },
"hooks": {
"PreToolUse": [
{ "matcher": "Bash",
"hooks": [ { "type": "command",
"command": "node",
"args": ["${CLAUDE_PROJECT_DIR}/.claude/hooks/check.js"],
"onFailure": "block" } ] }
]
}
}
Isolation. Bash rules match command text, so another path, wrapper or script can evade them. The sandbox restricts shell commands and their child processes at OS level, but not built-in file or web tools, hooks or local MCP servers. By default it still reads most of the machine and inherits environment variables. In bypassPermissions mode, hosts outside the allowed domains still connect unless strictAllowlist or the managed allowManagedDomainsOnly is on. Close the unsandboxed retry with allowUnsandboxedCommands: false. For one boundary around everything, use a container or VM.
Least privilege. Mount only what is needed, read-only where possible; restrict egress; keep credentials outside the boundary behind a proxy or custom tool that exposes only the needed operation.
Organization control. Managed settings carry rules users cannot override. Add allowManagedPermissionRulesOnly to ignore other rule sources and permissions.disableBypassPermissionsMode: "disable" to remove bypass mode.
Managed Agents (beta). Policies are always_allow, always_ask and auto. The agent toolset defaults to always_allow, MCP toolsets to always_ask. Policies do not cover custom tools: your application must gate them.
Traps
- Treating a stronger prompt, or a model-based check, as a deterministic guarantee.
- Expecting
allowedToolsto restrict an agent. - Putting the safety check in
canUseToolwhile an allow rule or mode skips it. - Listing more command spellings instead of adding a boundary.
- Assuming a broken policy hook fails closed.
- Relying on the built-in critical-path
rmcheck: it can miss commands and covers onlyrmandrmdir.
Decide quickly
| Situation | Choice | Why |
|---|---|---|
| Tool is never needed | Bare deny rule | Claude never sees it |
| Headless agent with a fixed tool set | Allow list plus dontAsk | Anything else that would prompt is denied |
| Block depends on argument values | PreToolUse hook | Custom logic, runs in every mode |
| Must hold for any script or spelling | Sandbox, container or VM | OS-level boundary |
| Agent needs an authenticated service | Proxy or custom tool outside the boundary | Secret never exposed |
| Human must approve a risky call | Ask rule or always_ask | Pauses even where other calls run freely |
| Policy for every developer | Managed settings with locks | Local settings cannot loosen it |