Claude Code hooks security: hooks as a guard and as a risk
Claude Code hooks security has two sides. A hook is a script that runs by itself at a set moment. As a guard you write, a hook that exits with code 2 blocks a tool call, and any other failure lets the action go ahead. As code you inherit, a hook is a shell command that runs with your full user permissions, and hooks committed to a repository's .claude/settings.json come with the repository. One advisory, CVE-2026-40068, was exactly this: a cloned repo could bypass the trust dialog and run its hooks right away.
This page covers Claude Code hooks security: what a hook can and cannot guarantee, and what one in someone else's repository can do to you. The same question, AI agent hook security, comes up in other tools; Cursor's version is in Cursor security best practices. A Claude Code hook starts at a fixed point in Claude Code's life cycle, for example before a tool runs, and can be a shell command, an HTTP endpoint, an MCP tool call, an LLM prompt or a subagent. For how to set hooks up, with copyable examples, see Claude Code hooks. For the wider picture of known flaws, see Claude Code security.
01Where hooks live, and how you end up with someone else's
Two events matter for security. PreToolUse fires before a tool call executes and can block it. PermissionRequest fires when a tool call needs a permission decision.
They live in settings files. .claude/settings.json in a project applies to that one project and can be committed to the repository, which means it arrives with every clone. ~/.claude/settings.json covers all your projects and stays on your machine. Hooks from every level add up: user, project and local settings add their own hooks without removing managed ones, the ones an administrator sets. So when you clone a repository and open it in Claude Code, you may inherit commands you never read.
02Hooks as a guard: what exit code 2 does
| Outcome | Result |
|---|---|
| Exit code 2 | The action is blocked; the error message goes back as the reason |
| Any other exit code, no start, invalid JSON | Non-blocking error; the action goes ahead |
| Timeout on PreToolUse | The tool call is not blocked, unless the hook's failure is set to block |
| Mistyped path | The script never runs, so nothing is checked |
A guard reads the planned action as JSON text on its standard input. For a Bash call, the command field holds the shell command. It answers with its exit code: exit with code 2 and a PreToolUse hook blocks the tool call, while a UserPromptSubmit hook rejects the prompt. The message that comes with the block is the hook's error output, stderr. A hook can also answer in JSON with permissionDecision set to deny; the JSON form is explained on the hooks page. When several hooks answer, the most restrictive answer wins, and an allow answer never overrides your deny and ask rules.
03Where a guard fails open
Fail-open means that when a guard breaks, the action goes ahead. Five cases to know:
- Other exit codes do not block. The script exits with any other code, fails to start or prints invalid JSON, and the action goes ahead.
- A mistyped path means the script never runs. To block the action when the hook cannot start, set
onFailure: "block". - A stalled check does not stop anything by default. On PreToolUse, a timed-out script does not block the tool call, unless its failure is set to block, so a slow check is a poor gate.
- Files you reference with @ are added without any tool call. PreToolUse never fires for them.
- Shell commands go around file checks. A guard that blocks edits to
.envdoes not stop Claude changing the file with a shell command. Add deny rules or a sandbox, a fenced area where commands run with limited access.
On macOS and Linux, the script must also be executable for Claude Code to run it. When you write the script, validate the input and always quote shell variables, so that a strange file name cannot become a command. Test the guard by triggering the thing it should block: ask Claude to add a comment to .env and confirm the edit is stopped before it runs. Rules and modes are explained on the permissions page.
04Hooks as a risk: they run with your permissions
Command hooks execute shell commands with your full user permissions. They can modify, delete or access any file your user account can reach. The documentation says to review and test every one before you add it.
The risk grows because settings travel with a repository, and so do any hooks in them. A project skill's hooks, the env block and helper commands such as apiKeyHelper sit in the same group as the hooks in .claude/settings.json. They are used even when only a parent folder was trusted, and in claude -p.
Two ways of opening a folder behave differently. In an interactive session, Claude Code holds hooks back from every settings file until you accept the workspace trust dialog, the prompt that asks if you trust the folder. In claude -p or an SDK session there is no dialog: the folder is treated as trusted even though you never approved it, so hooks committed in the repository run.
05Advisories where hooks or the trust dialog were the way in
An advisory is a published security notice from the Claude Code repository. A CVE is the public ID number of a known flaw. Only the ones about hooks and the trust dialog are listed here; the full list is on Claude Code security.
| CVE | Published, severity | What happened |
|---|---|---|
| CVE-2025-59536 | 2025-10-03, high | Code in a project could run before the startup trust dialog was accepted; it needed Claude Code started in an untrusted directory |
| CVE-2026-25725 | 2026-02-06, high | Code inside the sandbox could create .claude/settings.json and inject persistent hooks such as SessionStart that run with host privileges on restart |
| CVE-2026-33068 | 2026-03-18, high | A repository could set permissions.defaultMode to bypassPermissions in a committed .claude/settings.json, so the trust dialog was silently skipped |
| CVE-2026-40068 | 2026-04-24, high | A crafted git worktree setting bypassed the trust dialog and ran hooks from .claude/settings.json; it needed a cloned malicious repository and a path you had already trusted |
The CVE-2026-33068 advisory concerns an older version: current docs say bypassPermissions set in the project .claude/settings.json or settings.local.json does not take effect and the session starts in Manual mode. The modes are explained on the auto mode page. If you update Claude Code automatically, you already have these fixes. If you update by hand, update now.
06Opening a repository you did not write
Treat its .claude/ folder like a script you downloaded. Read .claude/settings.json and .claude/settings.local.json before you start Claude Code in it, and look for the commands they run. In a normal session you get the trust dialog first.
For scripted runs there is no dialog, so the rule is stricter. Before you script claude -p over a repository you did not write, review its .claude/ settings files, start with --bare, or turn hooks off for that run.
--bare makes Claude Code read no hooks, skills, custom commands, subagents, plugins or .mcp.json servers from the project. Without it, servers from .mcp.json are connected without asking in claude -p. To turn hooks off for that one run, pass the setting on the command line:
claude -p --settings '{"disableAllHooks": true}'Putting disableAllHooks in your user settings file is not enough, because the repository's project settings take precedence and can set it back to false. See run Claude Code safely for the wider routine and MCP security for the servers.
07If you manage a team
For a team, Claude Code hooks security comes down to managed settings, the admin-level settings file. With allowManagedHooksOnly, user, project, local and plugin hooks are blocked. Only disableAllHooks set at the managed level can disable the managed ones. For HTTP hooks, allowedHttpHookUrls limits which URLs run, and httpHookAllowedEnvVars limits which environment variables are placed into hook headers.
08Claude Code hook security: five things to do
- Read the project's hooks before you open a repository you did not write.
- Use
--bare, or turn the hooks off, for scripted runs over unknown code. - Set
onFailure: "block"on every guard that must hold. - Back file guards with deny rules or a sandbox.
- Keep Claude Code on auto-update.
Code you did not write has already run by the time you notice it. If you want to check the Mac after opening an unknown repository, the free, open-source core of Artificial Immunity looks for the places where programs set themselves to start and reports what it finds when you ask; it sees program starts only with its optional collector.
FAQ
Are Claude Code hooks safe?
A hook you wrote and reviewed is as safe as its script. Command hooks run shell commands with your full user permissions and can modify, delete or access any file your account can reach. A hook in a repository you cloned is code someone else wrote, so review it before you open the project.
How does a Claude Code hook block an action?
It exits with code 2. A PreToolUse hook then blocks the tool call, and the message that comes with the block is the hook's stderr.
What happens if a hook crashes or times out?
Any other exit code is a non-blocking error and the action goes ahead. A timed-out PreToolUse hook does not block the tool call either, unless its failure is set to block. Set onFailure to block if the hook must hold.
Can a cloned repository run hooks on my Mac?
Yes. Hooks in a repository's .claude/settings.json can be committed to the repo. In interactive sessions Claude Code holds them back until you accept the workspace trust dialog. claude -p runs Claude Code from a script, and SDK sessions run it from a program; in both there is no dialog and the folder is treated as trusted even though you never approved it.
How do I turn hooks off for an unknown repository?
In a scripted claude -p run, start with --bare so no hooks, skills, commands, subagents, plugins or .mcp.json servers are read from the project, or turn hooks off for that one run with --settings. In a normal session, read the repository's .claude/settings.json before you accept the trust dialog.
Can a hook stop Claude changing a protected file?
Not by itself. A hook on the Edit tool does not cover shell commands, and Claude can still change protected files that way. Add deny rules or a sandbox for those paths.