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:
{
"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,askordeny.default:allow,defer(the agent's own permissions decide),askordeny.tool: a regular expression on the whole tool name.command(shell tools),path(file tools) orcontent(what the call writes): a regular expression, only one of the three.contentis searched incontent,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 rulesdeny-secret-write-toolsanddeny-secret-write-shelluse it to refuse writing an AWS key, a GitHub token, ansk-key or a PEM private key into a file (the audit shows the file path, never the text).reasonis shown to the agent and written to the audit.quoted: "ignore"judges a command without the text of quoted strings, soecho "never terraform apply"is only mentioned, not run.agentlimits 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
node skills/lsh-rules/scripts/rules.js check --rules .leashr/rules.json
node skills/lsh-rules/scripts/rules.js test --rules .leashr/rules.jsonOK: .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):
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
- A shell command is split into segments: each side of
;,&&,||,|, newlines, the code ofbash -c,$(...)and backticks, and again withoutsudo,env,timeprefixes. - In a segment, the most restrictive matching rule wins:
denyoveraskoverallow, whatever the order in the file. - The call takes the most restrictive segment. So
ls && rm -rf buildis 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:
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 --writeWhen 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. askis enforced only by Claude Code, as a prompt in an interactive session. Codex, Cursor and Antigravity never receiveask: 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(ruleanalysis-limit). - Claude Code ignores
OTEL_*variables set in project settings. Put them in your user settings, managed settings or shell.
Status
| Function | Status | Proof |
|---|---|---|
Rules, check, test, merge-defaults | Tested, Claude Code 2.1.310 | skills/lsh-rules/test/rules.test.js |
| Fail closed on bad input, timeout, audit error | Tested | skills/lsh-rules/test/rules.test.js |
| Linear analysis time (every pattern under 50 ms) | Tested | skills/lsh-rules/test/perf.test.js |
| Team policies delivered by Leashr | Planned. Claude Code managed settings can ship the same files today | no code |