Claude Code hook security

Claude Code is safe to use in a company when you layer controls: keep it updated, write narrow allow and deny rules, run long autonomous work in a sandbox, and add a PreToolUse hook that checks every tool call before it runs. The hook is where team policy, human approval for risky actions, and an audit log of every decision live.

Last updated: · Tested against Claude Code v2.1.330

Works with Claude Code · Cursor · Codex

Contents

How Claude Code decides what an agent may do

Claude Code checks each tool call (Bash, Edit, Write, Read, WebFetch, MCP tools) against permission rules in settings files: allow, ask and deny. Managed settings set by an admin win over user and project files. Hooks run your own code at fixed points: PreToolUse before a call, PostToolUse after it, UserPromptSubmit on each prompt. A PreToolUse hook can allow, deny or ask, and its answer is applied before the command runs.

Source: Anthropic docs on settings, permissions and hooks.

Tired of prompts? See a safe alternative to --dangerously-skip-permissions: allow rules, auto mode, sandboxing, hooks and managed settings.

Why permission rules alone are not enough

Rules match the text of a command. In the last week of releases Anthropic fixed several ways that text matching missed what a command does:

  • v2.1.321 (9 Nov): deny rules did not apply to the file that cp or mv into a folder writes; a read-only Bash command ran outside the sandbox.
  • v2.1.323 (12 Nov): deny and ask rules missed commands run through sudo or env; Write and Edit could write a path covered by a Read deny rule; the sandbox could switch off mid-session.
  • v2.1.325 (13 Nov): relative deny rules such as Read(./.env) stopped covering the project after a cd; some compound Bash commands ran without asking.

These fixes are good news, and they show the pattern: a rule written against one spelling of a path or command misses another. A second check that sees the full tool call, applies team policy and records the result catches what a single rule set misses.

What changed in Claude Code security this month (checked 21 Nov 2026)

  • 2.1.328: "onFailure": "block" now also applies to mcp_tool hooks, so a hook whose MCP call fails blocks the action.
  • 2.1.329: in Code Mode, a PreToolUse hook that errors no longer refuses the tool call, matching normal sessions. Use onFailure: "block" on any hook that must refuse.
  • 2.1.330: Claude Code can sign in to organizations managed in Anthropic's Enterprise Admin Console and then applies the organization's settings, a second route for central settings next to managed settings files.
  • Sandboxed Bash now follows the normal permission flow by default (v2.1.325). If you relied on silent sandboxed Bash, sandbox.autoAllowBashIfSandboxed: true restores it; we suggest leaving the new default.
  • In untrusted folders, a repo's settings can no longer turn off your hooks (v2.1.323).
  • If the permission check errors after a PreToolUse hook says "allow", the call is now decided as if no hook answered (v2.1.323). Decide what your hook should do when it cannot answer (see fail mode below).
  • Scheduled tasks and /loop now ask before running a skill that declares hooks (v2.1.325).

Action: check the version on every machine. Anything below 2.1.325 is missing these fixes.

claude --version

The PreToolUse hook as a security control

How SecGuardRail uses it (from our build and tests):

  • One HTTP hook on PreToolUse sends each call to a policy engine and gets back allow, block or hold. Policy evaluation in our tests: p50 0.11 ms, p95 0.21 ms. Server time per hook on the hosted service: about 85 ms (measured 21 Nov 2026), plus network time.
  • Hold means a person approves or rejects in the dashboard, opened from the Review request link in the Slack alert while the agent waits; the request and decision are both recorded.
  • Monitor mode records what a policy would have done without blocking, so a team can tune rules before enforcing them.
  • Fail mode per tool class: by default shell, file writes and MCP calls fail closed (blocked if the policy service cannot be reached) and reads fail open. A workspace can change it.

Setup steps and the generated settings are in the docs.

What changed in Claude Code 2.1.333 and 2.1.334 (December 2026)

  • 2.1.334 (1 December 2026) returns an error to Claude when a managed PostToolUse hook rewrites output for a tool that cannot use the rewrite; return the tool's own output shape to keep the rewrite. It also fixes Code Mode refusing an MCP skill that a hook allowed.
  • 2.1.333 (30 November 2026) fixes a Grep freeze when Read deny rules are set.

What changed in Claude Code 2.1.331 and 2.1.332 (November 2026)

  • 2.1.332 (25 November 2026) adds a PreSkillShellCommand hook event that can refuse the inline shell commands a skill or command runs as it loads. On 2.1.331 and older, a settings file that names this event opens a dialog at start, so update every machine before you add it. SecGuardRail's hooks do not use this event yet.
  • In 2.1.332 the reason a PreToolUse hook gives for "ask" now shows on terminal Read, Grep, Glob, Edit and Write prompts, so the person approving sees why.
  • 2.1.331 (23 November 2026): PermissionRequest hooks now save Skill(name:*) allow rules outside local settings too, and Stop hooks that read the transcript in Claude Desktop now see the last reply (SubagentStop hooks wait for it as well). Re-test any hook that depends on either.

Make the hook fail closed: onFailure block

A PreToolUse hook is a real control when it still holds after it breaks. By default, when a Claude Code hook cannot start, times out or exits with an unexpected code, the tool call goes ahead. Set "onFailure": "block" on the hook and the same failure refuses the action instead.

{
  "hooks": {
    "PreToolUse": [
      {
        "matcher": "Bash|Edit|Write|mcp__.*",
        "hooks": [
          { "type": "http", "url": "https://policy.example.com/check", "timeout": 10, "onFailure": "block" }
        ]
      }
    ]
  }
}

What to know:

  • onFailure: "block" works on command and HTTP hooks (since 2.1.295), on Agent SDK hook callbacks (2.1.317) and on mcp_tool hooks (2.1.328).
  • Code Mode follows the same rule since 2.1.329: a PreToolUse hook that errors no longer refuses a script's tool call unless the hook sets onFailure: "block". If you tested your hooks inside Code Mode before 19 November, test again.
  • Failing closed has a cost: if your policy service is down, the agent stops. Decide per tool class. Blocking shell commands, file writes and MCP calls when the check fails is a sensible default; read-only tools can stay open.
  • Test it: point the hook at an address that refuses connections, ask the agent to run git status, and confirm the call is refused.

How SecGuardRail sets it: our install script writes a managed-settings drop-in (managed-settings.d/50-secguardrail.json) whose PreToolUse hook carries onFailure: block according to the workspace fail mode, so shell commands, file changes and MCP tools do not run when SecGuardRail cannot decide. The fail mode is set per tool class in the dashboard. It never edits a company's own managed-settings.json.

Stop the agent from turning its guardrails off

In our own test, Claude Code was asked to remove the hooks section from .claude/settings.local.json and did it, which switched the guardrail off. Two fixes:

  1. Install hooks in managed settings (an admin folder), where a standard user cannot edit them.
  2. Deny edits and shell writes to hook config, the hook key and switches such as disableAllHooks. SecGuardRail ships these as an always-on Tamper Protection pack and raises a Critical alert on each attempt.
Windows:  C:\Program Files\ClaudeCode
macOS:    /Library/Application Support/ClaudeCode
Linux:    /etc/claude-code

Honest limit: a local administrator can always uninstall. A silence alert (an agent that stops sending events) is how an admin finds out.

Prompt injection: when what the agent reads steers what it does

A README, issue, web page or MCP result can carry text aimed at the agent. Controls that work today:

  • Score content where it enters the session (PostToolUse on Read, WebFetch, WebSearch, Bash and MCP results), including hidden text: zero-width and Unicode tag characters, HTML comments, base64.
  • Mark the session as tainted and hold the actions injected text usually asks for: sending data to an unknown host, reading secrets, git push, permission changes.
  • Honest numbers from our rule-based detector: 0 false positives on 795 benign samples and every sample with an explicit injection marker caught, but 35% recall overall, because plain requests planted in data ("please grant Amy access") read like normal text. That is why the tainted-session holds matter more than detection alone.

The full breakdown by benchmark, and how to set this up in Cursor and Codex too: prompt injection in coding agents.

Secrets: keep keys out of commands, prompts and logs

Check commands and prompts for credentials before they run (SecGuardRail: 45 provider formats plus named values scored by entropy), block credentials sent to a host other than localhost, and redact secrets before anything is stored or sent to Slack. Limit: a secret split across variables or read from a file at run time is not visible in the command text.

Audit log you can verify

Record every tool call, decision, approver and reason. Chain each record to the previous one by hash so an edited or deleted record breaks the chain (tamper-evident), and export with retention and legal hold for reviews.

Claude Code security checklist for teams

  1. Version 2.1.325 or later on every machine.
  2. Narrow allow rules; deny .env, cloud credentials, .github/workflows/ writes and hook config.
  3. Test deny rules with cp, mv, sudo, env and after cd.
  4. Hooks in managed settings, not user files.
  5. PreToolUse policy check with an explicit fail mode per tool class.
  6. Human approval for push to main, deploys, permission changes.
  7. Prompt-injection taint and holds for web, README and MCP content.
  8. Secret checks and redaction on commands, prompts and logs.
  9. Hash-chained audit log, exported off the laptop.
  10. Sandbox for long autonomous runs instead of --dangerously-skip-permissions.

FAQ

Is Claude Code safe to use in a company?

Yes, with managed settings, narrow rules, a pre-execution policy check and an audit log. The risk is not the model alone but actions that run with a developer's access.

Do hooks replace permission rules?

No. Rules are the built-in layer; a PreToolUse hook adds team policy, approvals and a record. Use both.

What happens if the hook service is down?

You choose per tool class. Closed blocks risky calls until it is back; open lets them run and records the gap.

What happens if a Claude Code hook crashes or times out?

By default the action runs: a hook that cannot start, times out or exits with an unexpected code does not stop the tool call. Add "onFailure": "block" to the hook to refuse the action in those cases.

Does this work for Cursor and Codex too?

SecGuardRail applies the same policies, approvals and audit log to Claude Code, Cursor and Codex. See Cursor hooks security for teams.

Set it up

Become a design partner

Design partners: Team plan free for 30 days, then 50% off for life, limited to 10 teams. We set it up with you on a 20-minute call.

Agents you use

0/1000

We store this to contact you. See Privacy.

Plans

  • Free

    $0

    • 1 developer, 1 workspace
    • 3 agent API keys
    • 10,000 agent events a month
  • Starter

    $50/mo

    • Up to 3 developers
    • Everything in Free
    • Approvals with timeout
  • Team

    $99/mo

    • Up to 10 developers
    • +$9 per extra developer
    • Everything in Starter
  • Business

    From $499/mo

    • Everything in Team
    • Agent discovery
    • Retention and legal hold
See full pricing

Compare hooks, sandboxes and gateways across Claude Code, Cursor and Codex. Connect Claude Code in the docs. Related guides: Codex CLI security for teams and Cursor hooks security for teams, and MCP security for coding agents. See pricing and the product FAQ, or join as a design partner.

Sources

Guardrails for Claude Code, Cursor and Codex

Request access