gloss

Claude Code's PreToolUse Hooks Don't See a Skill's Own Commands

Claude Code runs a skill's injected shell commands while the skill loads, and PreToolUse hooks never see them.

A stainless steel tripod turnstile stands in a tiled building lobby beside a separate metal side gate that has swung open, leaving a clear walkway past it.
  • A skill or slash command can run shell commands while it loads, written as !`command` in its file. Those commands don't go through PreToolUse. In 20 runs on 2.1.331 and 2.1.332, a PreToolUse hook that blocks curl and touch let a skill's curl and touch through every time, whether I typed the skill or Claude picked it. The same curl sent through Claude's Bash tool was blocked.
  • Claude Code 2.1.332 adds a separate hook event, PreSkillShellCommand. Once I added it, it blocked 10 of 10 runs, and it also caught forked skills, plugin skills, a subagent's preloaded skill and the built-in /security-review. Your existing PreToolUse hook still sees nothing new.
  • The new hook fails open. A guard script that crashed, timed out or wasn't there let the command run, and claude -p said nothing about it. Set onFailure: "block", keep your deny rules, and don't count on a hook in a skill's own frontmatter: it skips the first load, and every resumed claude -p run counts as a first load.

Claude Code 2.1.332 came out on 24 November. One of its changelog lines adds a PreSkillShellCommand hook event that "can refuse a skill or command's inline shell commands".

It needs its own event for a reason that's easy to miss. A skill can contain lines like !`git diff HEAD`, and Claude Code runs them before Claude sees the skill, pasting the output in. Those commands go through the Bash tool, but PreToolUse hooks don't fire for them. The hooks page now says so plainly.

If you run a PreToolUse hook as a guard or an audit log, there is a whole class of commands it never sees. I wanted to know how wide the gap was and how well the new event closes it.

What I ran

A scratch git repo with one project skill called probe. Its body has two injected lines: touch markers/probe and curl -s http://127.0.0.1:8765/beacon, which hits a small capture server on my Mac. The skill's allowed-tools line pre-approves Bash(touch *) and Bash(curl *), so the permission check passes without me allowing anything.

The guard was a Python script that logs everything it receives and exits 2 when the command contains curl or touch. Every run was claude -p on Haiku 5.5 with a clean environment, project settings only, the hooks passed in with --settings, and no MCP servers. Both versions were already on disk, so nothing was downloaded.

That came to 78 headless runs for $0.11. A run counts as "ran" when the marker file appeared and the server logged the request.

The guard that wasn't asked

Five runs per row. "Typed" means the prompt was /probe. "Claude" means I asked about the build environment and let Claude pick the skill through the Skill tool.

Version Hooks set Skill started by Commands ran
2.1.331 PreToolUse Typed 5 of 5
2.1.331 PreToolUse Claude 5 of 5
2.1.332 PreToolUse Typed 5 of 5
2.1.332 PreToolUse Claude 5 of 5
2.1.331 PreToolUse and PreSkillShellCommand Typed 5 of 5
2.1.332 PreToolUse and PreSkillShellCommand Typed 0 of 5
2.1.332 PreToolUse and PreSkillShellCommand Claude 0 of 5

The PreToolUse hook was called 43 times across these runs. Every call was for a command Claude ran itself afterwards, such as ls or git status. None was for the skill's touch or curl.

The control worked as expected. When I asked Claude to run the same curl through its Bash tool, with Bash(curl *) allowed on the command line, the hook blocked it on both versions and the server got nothing.

One run showed the two doors side by side. In my first try at the Claude rows I forgot to allow the Skill tool, so the skill never loaded. Claude read SKILL.md instead and ran touch markers/probe; curl ... through Bash. The hook stopped that one.

On 2.1.331 the PreSkillShellCommand entry was simply ignored. The docs say an interactive session on that version opens with a warning dialog about an unknown hook event. In claude -p there was no warning at all: empty stderr, exit 0, commands ran.

What the new hook does catch

On 2.1.332 the hook receives the command as it would run, the skill's name, where it came from and who started it. In my logs invoked_by was user for a typed skill, claude for the Skill tool and other for a subagent that preloads the skill. command_source was projectSettings, plugin or builtin.

A hook defined in settings blocked a forked skill (context: fork), a plugin skill loaded with --plugin-dir and a subagent's preloaded skill. In each case PreToolUse alone let the commands run.

Built-in commands go through it too. A hook that returns continue: false stops the invocation after collecting every command, so nothing runs and nothing is billed. Pointed at /security-review, it listed four git commands: git diff origin/HEAD..., git log --no-decorate origin/HEAD..., git status and git diff --name-only origin/HEAD....

When a command is blocked, Claude reads [not run: blocked by hook: <reason>] where the output would be, and the skill continues. In headless mode the JSON result doesn't mention it. My runs ended with DONE, is_error: false, as if nothing happened.

It fails open, and quietly

The default for a broken hook is to let the command through. I tried three kinds of broken guard on 2.1.332, each with and without onFailure: "block".

Guard problem Default With onFailure: "block"
Script exits 1 Ran Blocked
Script sleeps past a 3-second timeout Ran Blocked
Hook command points at a file that isn't there Ran Blocked

In all six runs claude -p exited 0 with empty stderr. The docs say the terminal prints a Ran anyway in /<name> line. A CI log has no such line.

There's a trap in the third row. My first "missing file" test called python3 guard-renamed.py, and Python exits with code 2 when it can't find the script. Exit 2 means block, so that broken hook blocked by accident. A missing shell script exits 127, which counts as a failure, and the command ran. Whether a broken guard blocks shouldn't depend on which interpreter it happens to break in.

The skill's own hook arrives late

Skills can declare hooks in their frontmatter. The docs warn that a skill's injected commands run without those hooks on its first load in a session. I gave a second skill its own blocking PreSkillShellCommand hook and tested it two ways.

Three claude -p runs on the same session, using --resume: the hook never fired and the curl reached the server 3 of 3 times, although the session ID was the same each time. One process that received /probe2 twice over stream-json input: the first load ran, the second was blocked. So "first load in a session" means first load in a process. A headless job that resumes a session starts over every time.

What already worked

Deny rules covered injected commands all along. With Bash(curl *) in the deny list, both versions stopped the skill before any command ran, including the touch that comes first in the file. The run cost nothing and the result text was the error: Shell command permission check failed for pattern.... It still exited 0.

disableSkillShellExecution: true stopped both commands on both versions, and Claude replied anyway. It doesn't reach built-in commands. With the setting on, /security-review still handed its four git commands to the hook, which means they would have run. The project skill's commands never got that far.

What I'd set

Keep the deny rules. Then point the same guard script at both events in managed settings, with failure set to block:

"PreSkillShellCommand": [
  { "matcher": "Bash|PowerShell",
    "hooks": [{ "type": "command", "command": "/opt/guard/check.sh", "onFailure": "block" }] }
]

The docs suggest requiredMinimumVersion so nobody keeps running 2.1.331, where this entry does nothing. And if your guard is an audit log rather than a blocker, check it against a skill with an injected command before you trust the log.

What I didn't test

Interactive sessions, including the 2.1.331 dialog and the Not run and Ran anyway lines. Auto mode, which hands commands that need approval back to Claude as normal tool calls. Windows and PowerShell, http and mcp_tool hooks, VS Code, the desktop app and the Agent SDK. Skills from MCP servers, whose ! lines don't run at all according to the docs.