Skip to content

pre-tool-use-gate Hook

The PreToolUse safety gate: blocks catastrophic commands and secret-file access, warns on risky ones, and blocks when it cannot decide


pre-tool-use-gate Hook

The pre-tool-use-gate hook is the PreToolUse hook massu init installs. Before Claude Code runs a Bash, Write or Edit call, it runs the security checks and the deletion-impact checks in one process and decides: it blocks the call, warns about it, or lets it run.

How Claude Code reads the decision

Claude Code decides a PreToolUse call by the hook's exit code. Exit 0 lets the call run, and exit 2 blocks it. The message on standard error is shown to Claude as the reason. A message alone never blocks anything.

FindingWhat the gate does
A catastrophic command, or a command that names a secret fileBlocks the call (exit 2) and says why
A risky but legitimate command or editLets the call run (exit 0) and adds a warning to Claude's context
Nothing foundLets the call run (exit 0) with no output
The gate could not read, parse or evaluate the callBlocks the call (exit 2)

massu init also sets "onFailure": "block" on this hook (Claude Code 2.1.295 or later). Without it, a gate that crashed, timed out or printed invalid output would let the call run. With it, Claude Code treats any of those failures as a block. On older Claude Code, the massu-hook launcher turns a crash into a block too.

What it blocks

Commands: rm -rf /, curl … | bash and wget … | sh, writing to /etc/passwd, /etc/shadow or /etc/sudoers, dd to a raw disk, mkfs on a device, fork bombs, eval of remote curl output, and base64 -d … | sh.

Secret files a command names: .env and .env. (but not the templates .env.example, .env.template, .env.sample, .env.defaults and .env.dist), .pem, .key, .p12, .pfx, .secret, SSH private keys (id_rsa, id_ed25519, id_ecdsa), ~/.ssh/config, any …/credentials file, credentials.json, .netrc, kubeconfig, secrets.yaml, and secrets data files under any name, such as config.secrets.yaml or app.secret.json.

The check reads the command's path arguments, not its raw text. So jq '.key', Object.keys(x), process.env.HOME and a commit message that mentions .env.example are never refused, and neither is a source file such as secrets.ts.

What it warns about

chmod 777 and other risky-but-legitimate commands, writes to a protected file through Write or Edit, dangerous Python constructs in a .py file you're writing, and deleting code that registered features depend on (the pre-delete-check pipeline).

Upgrading

Releases 2.5.2 through 2.5.5 had a defect: this hook warned about everything it should have blocked and let the call run. 2.5.6 fixes it. After upgrading, run massu init again so your settings carry "onFailure": "block".

Settings written before 1.12.0 may still name the older security-gate and pre-delete-check hooks. From 2.5.6 each of those makes the same decision as this hook, so they block too. Running massu init replaces them with the single gate.

Example: a blocked call

Input on standard input:

json
{
  "session_id": "abc123-def456",
  "tool_name": "Bash",
  "tool_input": { "command": "cat .env" }
}

The hook exits 2 and writes to standard error:

text
MASSU SECURITY GATE — BLOCKED

SECURITY GATE: command names a secret file: .env file
Command: cat .env
BLOCKED. If this file is a template or fixture, name it with a template suffix (.example/.template/.sample) rather than weakening this check.

Example: a warning

For a risky but legitimate call, the hook exits 0 and prints the warning as context:

json
{
  "hookSpecificOutput": {
    "hookEventName": "PreToolUse",
    "additionalContext": "SECURITY GATE: Dangerous command pattern detected: chmod 777 (world-writable permissions)\n…"
  }
}

Performance

Claude Code gives this hook 5 seconds. It usually finishes well under 500 ms, because both check pipelines run in one process, the security checks are pure string matching, and the deletion check is a single SQLite query.