Skip to content
Leashr
Tested

Hooks and rules

A rule matches a tool call and picks one decision: deny, ask or allow. The rules file lives in your project and the hook applies it before each call.

Write a rule

Rules are in .leashr/rules.json, versioned with your code. This is the real default rule that asks before sudo, followed by one you could add to deny a command of your own:

json
{
  "version": 1,
  "default": "defer",
  "rules": [
    {
      "id": "ask-sudo",
      "decision": "ask",
      "tool": "Bash",
      "command": "(?:^|\\s)sudo\\s",
      "reason": "Elevated privileges.",
      "quoted": "ignore"
    },
    {
      "id": "deny-terraform-apply",
      "decision": "deny",
      "tool": "Bash",
      "command": "\\bterraform\\s+apply\\b",
      "reason": "Apply from CI only."
    }
  ]
}
  • decision: allow, ask or deny. default: allow, defer (the agent's own permissions decide), ask or deny.
  • tool: a regular expression on the whole tool name. command (shell tools), path (file tools) or content (what the call writes): a regular expression, only one of the three. content is searched in content, new_string, edits[].new_string, new_source, the + lines of a patch, and the shell command itself; removed text is never judged. The two default rules deny-secret-write-tools and deny-secret-write-shell use it to refuse writing an AWS key, a GitHub token, an sk- key or a PEM private key into a file (the audit shows the file path, never the text). reason is shown to the agent and written to the audit.
  • quoted: "ignore" judges a command without the text of quoted strings, so echo "never terraform apply" is only mentioned, not run. agent limits a rule to a sub-agent name. flags: "i" ignores case.
  • Any other key is an error, so a typo cannot silently widen a rule.

Check and test it

shell
node skills/lsh-rules/scripts/rules.js check --rules .leashr/rules.json
node skills/lsh-rules/scripts/rules.js test --rules .leashr/rules.json
output
OK: .leashr/rules.json (36 rules, default defer)

320/320 cases passed.
Audit: 320 event(s) for 320 decision(s), one per decision, decisions match.

test replays a table of dangerous, benign and malformed calls through the real hook script and checks that each decision writes exactly one audit event. Exit 1 on any failure. Run it after every rule change.

What a decision looks like

Real outputs of .leashr/bin/evaluate.js for three calls (input: the PreToolUse JSON on stdin):

output
deny   exit 2, stderr:
  Leashr blocked this Bash call (rule deny-git-force-push): Force push rewrites shared history (--force-with-lease is allowed).

ask    exit 0, stdout:
  {"hookSpecificOutput":{"hookEventName":"PreToolUse","permissionDecision":"ask","permissionDecisionReason":"Leashr rule ask-sudo: Elevated privileges."}}

allow  exit 0, stdout:
  {"hookSpecificOutput":{"hookEventName":"PreToolUse","permissionDecision":"allow","permissionDecisionReason":"Leashr rule allow-read-only-shell: Read-only or test command."}}

defer (no rule matched) exits 0 with no output.

How a call is judged

  1. A shell command is split into segments: each side of ;, &&, ||, |, newlines, the code of bash -c, $(...) and backticks, and again without sudo, env, time prefixes.
  2. In a segment, the most restrictive matching rule wins: deny over ask over allow, whatever the order in the file.
  3. The call takes the most restrictive segment. So ls && rm -rf build is denied.

Default rules

lsh-init writes 36 rules. Denied: recursive forced delete, force push, hard reset, --no-verify, secret files, downloaded scripts piped into a shell, disk overwrite, fork bomb, git clean -f, docker system prune -a, DROP DATABASE, sending env to another host. Asked: sudo, production targets, eval, rm -r, publishing a package, DROP TABLE, kubectl delete, terraform destroy, and any edit of .leashr/ or .mission/. The list may still change before launch; an existing rules.json is never rewritten.

To bring an older project up to the defaults without losing your rules:

shell
node skills/lsh-rules/scripts/rules.js merge-defaults --rules .leashr/rules.json --dry-run
node skills/lsh-rules/scripts/rules.js merge-defaults --rules .leashr/rules.json --write

When a hook fails

The hook refuses (fail closed, always as a deny with the cause) when the rules file is missing or invalid, a regular expression is bad, the input is not a JSON object, nothing arrives within 3 s, the audit cannot be written, or the script crashes. lsh-init declares it with timeout: 5 and onFailure: "block"; observed on 2.1.310, a hook that hangs past its timeout blocks the call. In Claude Code itself a hook error or timeout does not block by default.

Limits

  • A guard rail, not a sandbox: variables ($CMD), aliases, shell functions and scripts that run a dangerous command inside are not resolved.
  • ask is enforced only by Claude Code, as a prompt in an interactive session. Codex, Cursor and Antigravity never receive ask: a risky action is refused instead and a read is allowed. See Limits by agent.
  • A command with more than 1000 distinct pieces is not fully read: the answer is at least ask (rule analysis-limit).
  • Claude Code ignores OTEL_* variables set in project settings. Put them in your user settings, managed settings or shell.

Status

FunctionStatusProof
Rules, check, test, merge-defaultsTested, Claude Code 2.1.310skills/lsh-rules/test/rules.test.js
Fail closed on bad input, timeout, audit errorTestedskills/lsh-rules/test/rules.test.js
Linear analysis time (every pattern under 50 ms)Testedskills/lsh-rules/test/perf.test.js
Team policies delivered by LeashrPlanned. Claude Code managed settings can ship the same files todayno code