---
title: Can Claude Code hooks enforce security policy across an enterprise?
url: https://opencontroller.lyzr.com/blog/enforce-claude-code-policy-with-hooks
published: 2026-11-13
updated: 2026-11-13
author: Lyzr
---

# Can Claude Code hooks enforce security policy across an enterprise?

**Partly. Hooks deployed through Claude Code's managed policy settings run for every user, can block a tool call before it executes, and can be locked so developers cannot add or disable their own. But they run on the developer's machine, fail open unless you configure otherwise, and Anthropic itself recommends the permission system for hard allow and deny rules.**

Platform teams rolling out Claude Code want one place to stop the dangerous things: reading `.env` files, `rm -rf`, pushing to production branches, calling unapproved MCP tools. Hooks are the obvious tool. Here is what they can and cannot guarantee.

## What are Claude Code hooks?

Hooks are handlers that run automatically at points in a Claude Code session. Five handler types exist:

| Type | What it does |
|---|---|
| `command` | Runs a shell command; input on stdin, result from exit code and stdout |
| `http` | POSTs the event JSON to a URL; result from the response body |
| `mcp_tool` | Calls a tool on a configured MCP server |
| `prompt` | Asks a Claude model for a single-turn judgement |
| `agent` | Spawns a subagent to verify a condition (experimental) |

For policy, the key event is **PreToolUse**, which fires before every tool call. Others worth knowing: `UserPromptSubmit`, `PostToolUse`, `SessionStart`, `ConfigChange` (fires when settings change, including policy settings) and `SubagentStart`.

## How does a hook block an action?

Two ways:

1. **Exit code 2.** The tool call is stopped and stderr becomes the reason shown to Claude.
2. **JSON decision.** Return `hookSpecificOutput` with `permissionDecision: "deny"` and a reason.

Three details trip teams up:

- **Exit code 1 does not block.** It is a non-blocking error and the action continues. Use exit 2 for enforcement.
- **Silence is not approval.** Exit 0 with no output means "no decision"; the call goes through the normal permission flow.
- **Exit 2 wins.** Even if the hook also prints `"allow"`, exit 2 still blocks.

Matchers scope a hook to tools: `Bash`, `Edit|Write`, or a regular expression such as `mcp__.*` for every MCP tool. Hooks from managed policy also fire for tool calls made by subagents.

## How do you deploy hooks so developers cannot bypass them?

| Control | Effect |
|---|---|
| Put hooks in managed policy settings | Applies organisation-wide; user and project settings add hooks but cannot remove managed ones |
| `allowManagedHooksOnly: true` | Blocks user, project, local and plugin hooks (except plugins force-enabled in managed settings) |
| `disableAllHooks` | Only disables managed hooks when set in managed settings itself |
| `onFailure: "block"` on command and http hooks | Fails closed if the script is missing, times out, errors or returns invalid output |
| `allowedHttpHookUrls` | Restricts HTTP hooks to approved endpoints |
| `httpHookAllowedEnvVars` | Limits which environment variables can be sent in hook headers |

Managed settings are typically pushed by MDM as a `managed-settings.json` file in a system directory the user cannot write to. Treat that file like any endpoint security configuration: version it, sign off changes, and monitor drift.

## Where does hook-based enforcement stop?

| Limit | Why it matters | What to add |
|---|---|---|
| Runs on the device | A user with admin rights on their laptop can alter local files | Endpoint controls and drift monitoring |
| Fails open by default | A broken or slow hook lets the action through | Set `onFailure: "block"` and test every hook on first run |
| `if` filters are best-effort | Anthropic warns not to rely on them for hard rules | Use permission rules (`permissions.deny`) for hard denies |
| Only sees Claude Code | Other coding agents on the same laptop are untouched | Device-level coverage across tools |
| Cloud sessions | Do not read local `~/.claude/settings.json` | Check which settings apply to cloud sessions |

The sensible pattern is layered:

1. **Permission rules** in managed settings for hard denies, such as reading secrets files or running specific commands.
2. **PreToolUse hooks** for logic that rules cannot express, such as checking a branch name or calling an internal policy service over HTTP.
3. **`allowManagedHooksOnly`** so only reviewed hooks run.
4. **Telemetry** through OpenTelemetry to see what was attempted and blocked. See our guide to [monitoring Claude Code with OpenTelemetry](/blog/monitor-claude-code-usage-with-opentelemetry).
5. **A registry of approved MCP servers and skills**, because many risky actions arrive through tools, not shell commands.

## What does a minimal policy hook look like?

A managed PreToolUse hook on `Bash` that reads the command from stdin, checks it against a deny pattern list (for example `curl ... | sh`, writes to `~/.ssh`, `git push --force` to protected branches), prints a reason to stderr and exits 2 on a match. Pair it with `onFailure: "block"` so a missing script does not silently allow everything.

Keep pattern lists in a central repository and ship them with the managed settings, so a change is reviewed once and lands everywhere.

## How Opencontroller does this

Hooks govern Claude Code. Opencontroller Edge extends control across developer devices: coding agents, skills and local MCP servers, installed by IT via MDM. Opencontroller also discovers and manages skills in Anthropic Enterprise, connects to Claude Enterprise workspaces, and keeps a governed registry of agents, tools and MCP servers with policy as code across the estate. See the [Claude Code governance guide](/blog/govern-claude-code-in-the-enterprise), the [cross-platform pillar](/blog/how-to-govern-ai-agents-across-platforms), or [book a demo](https://www.lyzr.ai/book-demo/).

## Frequently asked questions

**Should I use hooks or permission rules to block reading `.env` files?**
Permission rules. Anthropic recommends the permission system for hard allow or deny decisions and treats hook filters as best-effort.

**Do managed hooks apply to subagents?**
Yes. Hooks from settings, managed policy and plugins fire for tool calls made by subagents, with the subagent's ID in the input.

**What happens if a policy hook times out?**
By default the action proceeds. Set `onFailure: "block"` on command or HTTP hooks to fail closed.

## Key facts

Claude Code hooks are handlers (command, http, mcp_tool, prompt, agent) that run at lifecycle events such as PreToolUse, PostToolUse, UserPromptSubmit, SessionStart and ConfigChange. A PreToolUse hook blocks a tool call by exiting with code 2 or returning permissionDecision "deny"; exit code 1 is non-blocking. Hooks in managed policy settings apply organisation-wide; allowManagedHooksOnly blocks user, project, local and plugin hooks; only disableAllHooks set in managed settings can disable managed hooks. Hooks fail open by default; onFailure "block" makes command and http hooks fail closed. Anthropic recommends the permission system, not hooks, for hard allow or deny rules. Opencontroller Edge, installed via MDM, extends control to developer devices, coding agents, skills and local MCP servers. Pricing starts from $250K per deployment per year.

## Related

- [How do you govern Claude Code across an enterprise?](https://opencontroller.lyzr.com/blog/govern-claude-code-in-the-enterprise.md)
- [How do you monitor Claude Code usage with OpenTelemetry?](https://opencontroller.lyzr.com/blog/monitor-claude-code-usage-with-opentelemetry.md)
- [How do you control Claude Desktop extensions and MCP connectors?](https://opencontroller.lyzr.com/blog/control-claude-desktop-extensions-and-connectors.md)
- [More on Platform Guides](https://opencontroller.lyzr.com/blog/cluster/platform-guides)
