skip to content

In Claude Code, how does a hook handler's `if` field differ from its group's `matcher`, and why is `if` not an enforcement boundary?

level: seniorimportance: nice to knowfreq 6%

answer

  1. group filter versus handler filter
  2. tool name plus arguments
  3. permission-rule syntax, one rule
  4. tool events only
  5. best-effort on shell commands

basics

~20 s

The matcher filters a group by tool name; if filters one handler by tool and arguments in permission-rule syntax, such as Bash(git *). It works only on tool events and is best-effort: a spawn filter, not policy.

solid answer

~40 s

A `matcher` sits on the group and compares the event's field, the tool name for tool events, as an exact list or a regex. `if` sits on an individual handler and holds exactly one permission rule, such as `Bash(git *)` or `Edit(*.ts)`, so it checks the tool and its arguments together; the handler's process is spawned only when it matches. It is evaluated only on `PreToolUse`, `PostToolUse`, `PostToolUseFailure`, `PermissionRequest` and `PermissionDenied`; on any other event a handler with `if` never runs. There is no `&&`, `||` or list syntax. For Bash it checks each subcommand and commands inside `$()`, and when Claude Code cannot tell what a command runs it runs the hook anyway. The docs call it best-effort: hard allow and deny belong to permission rules.

go deeper

for a junior

Recall that the matcher picks the tool and the if field narrows by arguments, using the same syntax as permission rules.

for a middle

Explain the levels: group matcher, handler if, and that if only works on the five tool events while an unsupported matcher is just ignored.

for a senior

Show why if is best-effort on shell text, why the script must still validate input, and why hard denies belong in permission rules.

for a principal

Decide which controls live in hooks versus permission rules for a team, and how to review hook filters so a narrow if cannot quietly disable a guard.

## Two filters at two levels A Claude Code hook configuration has three levels: the **event** (for example `PreToolUse`), the **matcher group** under it, and the **handlers** inside the group. Two filters decide whether a handler runs: | | `matcher` | `if` | |---|---|---| | Lives on | the group | one handler | | Compares | the event's field, such as the tool name | tool name **and** arguments | | Syntax | exact names, `\|` lists, or a regex | one permission rule, such as `Bash(git *)` | | Works on | every event with matcher support | tool events only | | Combining | `\|` or `,` for alternatives | none: one rule per handler | ```json { "hooks": { "PreToolUse": [ { "matcher": "Bash", "hooks": [ { "type": "command", "if": "Bash(git push *)", "command": "/opt/hooks/check-push.sh" }, { "type": "command", "if": "Bash(rm *)", "command": "/opt/hooks/check-rm.sh" } ] } ] } } ``` A typical pair is `"matcher": "Bash"` on the group and `"if": "Bash(rm *)"` on the handler: the group activates for every Bash call, but the script only spawns when a subcommand matches `rm *`. A `git status` call never starts the process, which is the point: `if` saves spawning a script for calls it would ignore. ## Where `if` works and where it silently disables a hook `if` is evaluated only on the five tool events: `PreToolUse`, `PostToolUse`, `PostToolUseFailure`, `PermissionRequest` and `PermissionDenied`. On any other event, such as `Stop`, `SessionStart` or `UserPromptSubmit`, a handler with `if` set **never runs**. That differs from a matcher on an event without matcher support, which is ignored, so the hook runs every time. Both mistakes are silent. Other rules of the field: - **One rule only.** There is no `&&`, `||` or list syntax. To cover `git push` and `npm publish`, define two handlers, each with its own `if`, or move the alternation to the group's matcher where `|` works for tool names. - **File patterns follow permission-rule paths.** `Edit(*.ts)` matches TypeScript files; `Edit(src/**)` matches only the `src` directory in the working directory, while `Edit(**/src/**)` matches a `src` directory at any depth. - **MCP tools use their full name.** For a server bundled in a plugin, that is the scoped `mcp__plugin_<plugin>_<server>__<tool>` form. ## How Bash patterns are matched For `Bash` rules the check works on the parsed command: 1. Leading `VAR=value` assignments are stripped, so `FOO=bar git push` matches `Bash(git *)`. 2. Each subcommand is checked, so `npm test && git push` matches `Bash(git *)`. 3. Commands inside `$()` and backticks are checked, so `echo $(rm -rf /)` matches `Bash(rm *)`. 4. A pattern that specifies more than the command name, such as `Bash(git push *)`, runs the hook anyway when the input contains `$()`, backticks or `$VAR`. 5. When Claude Code cannot determine which commands the input runs, it runs the hook regardless of the pattern. ## Why it is not the enforcement layer The documentation calls the `if` filter **best-effort** and says to use the permission system, not a hook, to enforce a hard allow or deny. In practice: - When the filter cannot tell, it runs the hook, so the script must still validate the full input itself; the documented `block-rm.sh` example re-checks the command for `rm -rf` even though `if` already narrowed to `rm *`. - A guard whose `if` is too narrow simply never sees the calls it should stop. A policy that only lives in a pattern on a handler is easy to bypass with a command shape the author did not anticipate. - `if` decides whether a script **runs**; the decision to block still comes from what the script returns. Use `if` as a performance and noise filter on top of a script that checks for itself, and put the non-negotiable rule in `permissions.deny`, whose grammar belongs to the permission-rules material.

  • A Stop hook has `"if": "Bash(npm test)"` so that it runs only on turns that ran tests. What actually happens?
    It never runs. `if` is evaluated only on the five tool events; on `Stop` and every other event a handler with `if` set is skipped. To gate on whether tests ran, drop `if` and have the Stop script inspect the transcript or its own state.
  • Why might you keep a group `matcher` of `Bash` even when every handler has an `if` starting with `Bash(`?
    The matcher scopes the whole group to Bash calls, so a handler added later without an `if` does not suddenly run on every tool, and it documents the group's intent. The `if` rules then narrow each handler by arguments. The two filters compose; neither replaces the other.

  • The if field accepts several rules joined with ||.
  • An if field on a Stop hook narrows which turns run it.
  • if and matcher are two spellings of the same filter.
  • A matching if pattern guarantees the command is blocked.
  • if is a reliable way to enforce a hard deny on Bash.