|
| 1 | +#!/usr/bin/env bash |
| 2 | +# Self-test for guard-main-checkout.sh — run it after touching that hook: |
| 3 | +# |
| 4 | +# .claude/hooks/guard-main-checkout.selftest.sh |
| 5 | +# |
| 6 | +# Feeds the hook the same JSON payload shape Claude Code delivers on PreToolUse and asserts |
| 7 | +# the block/allow verdict per case. Hermetic: it builds its OWN throwaway git repos, a linked |
| 8 | +# worktree of each and a non-repo directory under $TMPDIR, so the matrix never depends on |
| 9 | +# which machine or which checkout it runs from. Needs jq and git and nothing else — no |
| 10 | +# install, no build, no network. Exit 0 = all cases hold. |
| 11 | +# |
| 12 | +# Companion to guard-main-checkout-bash.selftest.sh, which covers the Bash half of the same |
| 13 | +# worktree-first pair. That matrix is mostly SHELL SPLITTING; this hook parses no shell at |
| 14 | +# all — it reads .tool_input.file_path and makes a PATH-AND-WORKTREE decision — so these |
| 15 | +# cases are derived from what this hook actually decides, not ported from the sibling. |
| 16 | +# |
| 17 | +# PORTED from objectstack's copy of this matrix (objectstack-ai/objectstack, .claude/hooks/ |
| 18 | +# guard-main-checkout.selftest.sh @ d63c8a2) under objectui#6451, and the port is VERBATIM: |
| 19 | +# the two repos' guard-main-checkout.sh differ by 9 diff lines that are all inside one |
| 20 | +# comment block, no executable line differs, and both settings.json route the identical |
| 21 | +# Edit|Write|NotebookEdit matcher — so the sibling file was first run here BYTE-FOR-BYTE |
| 22 | +# unmodified (via the two env vars below) and returned 87 passed, 0 failed. Not one case |
| 23 | +# needed adapting. The only edits below the header are the two KNOWN HOLE sections, whose |
| 24 | +# issue references are re-pointed at this repo's own cards for the same two defects. |
| 25 | +# ⛔ Keep the two copies converged: a case that has to differ is evidence the HOOKS have |
| 26 | +# drifted, and that drift is the finding — not something to paper over here. |
| 27 | +# |
| 28 | +# Fail-open by default, on purpose: the process cwd AND CLAUDE_PROJECT_DIR both default to a |
| 29 | +# directory in no repo at all, which is the input on which this hook allows everything. A |
| 30 | +# case that expects `block` therefore cannot pass by accident — the verdict can only have |
| 31 | +# come from the path in the payload. Sections that need a different default say so. |
| 32 | +# |
| 33 | +# GUARD_MAIN_CHECKOUT_HOOK points the matrix at a scratch copy of the hook, and |
| 34 | +# GUARD_MAIN_CHECKOUT_SETTINGS at a scratch copy of settings.json; that is how the mutation |
| 35 | +# runs that prove these cases can fail are driven (see NON-VACUITY at the foot of this file). |
| 36 | +# Both default to the real files, so a plain invocation checks the real hook and real wiring. |
| 37 | + |
| 38 | +set -uo pipefail |
| 39 | + |
| 40 | +here="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" |
| 41 | +hook="${GUARD_MAIN_CHECKOUT_HOOK:-$here/guard-main-checkout.sh}" |
| 42 | +settings="${GUARD_MAIN_CHECKOUT_SETTINGS:-$here/../settings.json}" |
| 43 | +pass=0 |
| 44 | +fail=0 |
| 45 | +skip=0 |
| 46 | + |
| 47 | +command -v jq >/dev/null 2>&1 || { echo "selftest needs jq to build payloads" >&2; exit 1; } |
| 48 | +command -v git >/dev/null 2>&1 || { echo "selftest needs git to build the fixture" >&2; exit 1; } |
| 49 | +[ -x "$hook" ] || { echo "hook not executable: $hook" >&2; exit 1; } |
| 50 | + |
| 51 | +# --- fixture --------------------------------------------------------------------------- |
| 52 | +# MAIN a shared PRIMARY checkout WT a linked worktree of MAIN |
| 53 | +# SIB a SECOND primary checkout SIBWT a linked worktree of SIB |
| 54 | +# PLAIN a directory inside no repo at all ODD a PRIMARY checkout whose own path |
| 55 | +# carries a literal `worktrees` segment |
| 56 | +tmp="$(mktemp -d)" |
| 57 | +nojq="" |
| 58 | +trap 'rm -rf "$tmp" ${nojq:+"$nojq"}' EXIT INT TERM |
| 59 | +MAIN="$tmp/mainrepo" |
| 60 | +WT="$tmp/mainrepo-task" |
| 61 | +SIB="$tmp/siblingrepo" |
| 62 | +SIBWT="$tmp/siblingrepo-task" |
| 63 | +PLAIN="$tmp/plain" |
| 64 | +ODD="$tmp/worktrees/oddrepo" |
| 65 | +mkdir -p "$MAIN/pkg/deep" "$SIB/pkg" "$PLAIN" "$ODD/pkg" |
| 66 | +( |
| 67 | + for r in "$MAIN" "$SIB" "$ODD"; do |
| 68 | + cd "$r" || exit 1 |
| 69 | + git init -q . |
| 70 | + git config user.email selftest@example.com |
| 71 | + git config user.name selftest |
| 72 | + : > README.md |
| 73 | + mkdir -p pkg |
| 74 | + : > pkg/x.ts |
| 75 | + : > pkg/x.ipynb |
| 76 | + git add -A |
| 77 | + git commit -qm init |
| 78 | + done |
| 79 | + cd "$MAIN" && git worktree add -q "$WT" -b selftest-wt |
| 80 | + cd "$SIB" && git worktree add -q "$SIBWT" -b selftest-sib-wt |
| 81 | +) >/dev/null 2>&1 || { echo "could not build the git fixture" >&2; exit 1; } |
| 82 | + |
| 83 | +CWD="$PLAIN" # the hook's PROCESS cwd for the cases that follow; reassigned per section |
| 84 | +PROJ="$PLAIN" # CLAUDE_PROJECT_DIR for the cases that follow; reassigned per section |
| 85 | + |
| 86 | +short() { # short <text> -> fixture paths rendered as their variable names |
| 87 | + local s="$1" |
| 88 | + # longest paths first: $WT and $SIBWT have $MAIN and $SIB as prefixes |
| 89 | + s="${s//$SIBWT/\$SIBWT}"; s="${s//$WT/\$WT}"; s="${s//$SIB/\$SIB}" |
| 90 | + s="${s//$MAIN/\$MAIN}"; s="${s//$PLAIN/\$PLAIN}"; s="${s//$ODD/\$ODD}" |
| 91 | + s="${s//$tmp/\$tmp}" |
| 92 | + printf '%s' "$s" |
| 93 | +} |
| 94 | + |
| 95 | +verdict() { # verdict <payload> [env…] -> block | allow | exitN |
| 96 | + local payload="$1"; shift |
| 97 | + local rc |
| 98 | + ( cd "$CWD" && printf '%s' "$payload" | env CLAUDE_PROJECT_DIR="$PROJ" "$@" "$hook" >/dev/null 2>&1 ) |
| 99 | + rc=$? |
| 100 | + case "$rc" in |
| 101 | + 0) printf 'allow' ;; |
| 102 | + 2) printf 'block' ;; |
| 103 | + *) printf 'exit%s' "$rc" ;; |
| 104 | + esac |
| 105 | +} |
| 106 | + |
| 107 | +check() { # check <block|allow> <label> <payload> [env…] |
| 108 | + local want="$1" label="$2" payload="$3"; shift 3 |
| 109 | + local got; got="$(verdict "$payload" "$@")" |
| 110 | + if [ "$got" = "$want" ]; then |
| 111 | + pass=$((pass + 1)); printf ' ok %-5s %s\n' "$got" "$(short "$label")" |
| 112 | + else |
| 113 | + fail=$((fail + 1)); printf ' FAIL want=%s got=%s %s\n' "$want" "$got" "$(short "$label")" |
| 114 | + fi |
| 115 | +} |
| 116 | + |
| 117 | +payload() { # payload <file_path> [tool_name] -> the Edit/Write payload shape |
| 118 | + jq -nc --arg f "$1" --arg t "${2:-Edit}" \ |
| 119 | + '{session_id:"selftest",cwd:"/payload-cwd-must-be-ignored",tool_name:$t, |
| 120 | + tool_input:{file_path:$f,old_string:"a",new_string:"b"}}' |
| 121 | +} |
| 122 | + |
| 123 | +expect() { # expect <block|allow> <file_path> [env…] — the common case |
| 124 | + local want="$1" f="$2"; shift 2 |
| 125 | + check "$want" "$f" "$(payload "$f")" "$@" |
| 126 | +} |
| 127 | + |
| 128 | +echo "== the core verdict: shared PRIMARY checkout is blocked ==" |
| 129 | +expect block "$MAIN/pkg/x.ts" |
| 130 | +expect block "$MAIN/pkg/deep/y.ts" |
| 131 | +expect block "$MAIN/README.md" # repo ROOT: git prints a RELATIVE git-dir here |
| 132 | +expect block "$MAIN/.changeset/x.md" |
| 133 | +expect block "$MAIN/pkg/x.ipynb" |
| 134 | + |
| 135 | +echo "== the SAME files inside a linked worktree are allowed ==" |
| 136 | +expect allow "$WT/pkg/x.ts" |
| 137 | +expect allow "$WT/pkg/deep/y.ts" |
| 138 | +expect allow "$WT/README.md" |
| 139 | +expect allow "$WT/pkg/x.ipynb" |
| 140 | + |
| 141 | +echo "== files in no repo at all are allowed (scratchpad, /tmp, \$HOME dotfiles) ==" |
| 142 | +expect allow "$PLAIN/notes.md" |
| 143 | +expect allow "$PLAIN/deep/er/still.md" |
| 144 | +expect allow "/tmp/os-selftest-scratch.log" |
| 145 | + |
| 146 | +echo "== new files in not-yet-created directories: judged by the nearest EXISTING ancestor ==" |
| 147 | +# The hook walks up until it finds a directory that exists. Without that walk `git -C` would |
| 148 | +# fail on the missing directory and the guard would fail open past exactly the case that |
| 149 | +# matters most — writing a brand-new file into the shared checkout. |
| 150 | +expect block "$MAIN/brand/new/tree/file.ts" |
| 151 | +expect block "$MAIN/pkg/deep/brand/new/file.ts" |
| 152 | +expect allow "$WT/brand/new/tree/file.ts" |
| 153 | +expect allow "$PLAIN/brand/new/tree/file.ts" |
| 154 | + |
| 155 | +echo "== the EDITED FILE's own repo decides — sibling repos are guarded from any session ==" |
| 156 | +# CLAUDE_PROJECT_DIR is pointed at the WRONG repo in every case here, so a hook that judged |
| 157 | +# the session instead of the file would get all four backwards. |
| 158 | +PROJ="$WT" |
| 159 | +expect block "$SIB/pkg/x.ts" |
| 160 | +expect block "$MAIN/pkg/x.ts" |
| 161 | +PROJ="$MAIN" |
| 162 | +expect allow "$SIBWT/pkg/x.ts" |
| 163 | +expect allow "$PLAIN/notes.md" |
| 164 | +PROJ="$SIB" |
| 165 | +expect block "$MAIN/pkg/x.ts" |
| 166 | +expect allow "$WT/pkg/x.ts" |
| 167 | +PROJ="$PLAIN" |
| 168 | + |
| 169 | +echo "== tool_name is never consulted — scoping lives in the settings.json matcher ==" |
| 170 | +# The hook decides on the path alone. Which tools reach it is the matcher's job, asserted |
| 171 | +# in the wiring section below; these cases pin that the hook itself does not second-guess it. |
| 172 | +for t in Edit Write NotebookEdit MultiEdit AnythingElse; do |
| 173 | + check block "tool_name=$t into \$MAIN" "$(payload "$MAIN/pkg/x.ts" "$t")" |
| 174 | + check allow "tool_name=$t into \$WT" "$(payload "$WT/pkg/x.ts" "$t")" |
| 175 | +done |
| 176 | + |
| 177 | +echo "== escape hatch: OS_ALLOW_MAIN_EDITS must be exactly 1 ==" |
| 178 | +check allow 'OS_ALLOW_MAIN_EDITS=1 into $MAIN' "$(payload "$MAIN/pkg/x.ts")" OS_ALLOW_MAIN_EDITS=1 |
| 179 | +check allow 'OS_ALLOW_MAIN_EDITS=1 into $SIB' "$(payload "$SIB/pkg/x.ts")" OS_ALLOW_MAIN_EDITS=1 |
| 180 | +check block 'OS_ALLOW_MAIN_EDITS=0' "$(payload "$MAIN/pkg/x.ts")" OS_ALLOW_MAIN_EDITS=0 |
| 181 | +check block 'OS_ALLOW_MAIN_EDITS=(empty)' "$(payload "$MAIN/pkg/x.ts")" OS_ALLOW_MAIN_EDITS= |
| 182 | +check block 'OS_ALLOW_MAIN_EDITS=true' "$(payload "$MAIN/pkg/x.ts")" OS_ALLOW_MAIN_EDITS=true |
| 183 | +check block 'OS_ALLOW_MAIN_EDITS=yes' "$(payload "$MAIN/pkg/x.ts")" OS_ALLOW_MAIN_EDITS=yes |
| 184 | +check block 'OS_ALLOW_MAIN_EDITS=11' "$(payload "$MAIN/pkg/x.ts")" OS_ALLOW_MAIN_EDITS=11 |
| 185 | +check block 'OS_ALLOW_MAIN_EDITS=" 1" (padded)' "$(payload "$MAIN/pkg/x.ts")" OS_ALLOW_MAIN_EDITS=" 1" |
| 186 | + |
| 187 | +echo "== a payload carrying no usable path is judged by CLAUDE_PROJECT_DIR — fails CLOSED ==" |
| 188 | +# This is the branch the hook takes when it learns nothing from the payload. It is the |
| 189 | +# opposite posture from the Bash sibling (which fails open on an unparseable command): here |
| 190 | +# an unreadable payload on the shared checkout still BLOCKS. Safe direction, and deliberate |
| 191 | +# — it is an explicit `else` in the hook, not a fall-through. |
| 192 | +for probe in '{"tool_name":"Edit","tool_input":{}}' '{}' 'not json at all' '' '{"tool_input":{"file_path":""}}'; do |
| 193 | + PROJ="$MAIN"; check block "no usable path, CLAUDE_PROJECT_DIR=\$MAIN [$probe]" "$probe" |
| 194 | + PROJ="$WT"; check allow "no usable path, CLAUDE_PROJECT_DIR=\$WT [$probe]" "$probe" |
| 195 | + PROJ="$PLAIN"; check allow "no usable path, CLAUDE_PROJECT_DIR=\$PLAIN [$probe]" "$probe" |
| 196 | +done |
| 197 | + |
| 198 | +echo "== with CLAUDE_PROJECT_DIR unset the no-path branch falls back to the PROCESS cwd ==" |
| 199 | +nopath='{"tool_name":"Edit","tool_input":{}}' |
| 200 | +for pair in "$MAIN:block" "$WT:allow" "$PLAIN:allow"; do |
| 201 | + CWD="${pair%:*}" |
| 202 | + ( cd "$CWD" && printf '%s' "$nopath" | env -u CLAUDE_PROJECT_DIR "$hook" >/dev/null 2>&1 ) |
| 203 | + rc=$?; got=allow; [ "$rc" = 2 ] && got=block |
| 204 | + if [ "$got" = "${pair#*:}" ]; then |
| 205 | + pass=$((pass + 1)); printf ' ok %-5s no path, no CLAUDE_PROJECT_DIR, cwd=%s\n' "$got" "$(short "$CWD")" |
| 206 | + else |
| 207 | + fail=$((fail + 1)); printf ' FAIL want=%s got=%s no path, no CLAUDE_PROJECT_DIR, cwd=%s\n' "${pair#*:}" "$got" "$(short "$CWD")" |
| 208 | + fi |
| 209 | +done |
| 210 | +CWD="$PLAIN"; PROJ="$PLAIN" |
| 211 | + |
| 212 | +echo "== a RELATIVE file_path is judged by the PROCESS cwd; the payload's cwd field is ignored ==" |
| 213 | +rel='{"cwd":"/payload-cwd-must-be-ignored","tool_name":"Edit","tool_input":{"file_path":"pkg/x.ts"}}' |
| 214 | +CWD="$MAIN"; check block 'relative "pkg/x.ts", process cwd=$MAIN' "$rel" |
| 215 | +CWD="$WT"; check allow 'relative "pkg/x.ts", process cwd=$WT' "$rel" |
| 216 | +CWD="$PLAIN"; check allow 'relative "pkg/x.ts", process cwd=$PLAIN' "$rel" |
| 217 | +# and the payload's own cwd field never overrides an absolute path |
| 218 | +CWD="$PLAIN" |
| 219 | +check allow 'payload cwd=$MAIN, absolute path in $WT' \ |
| 220 | + "$(jq -nc --arg f "$WT/pkg/x.ts" --arg w "$MAIN" '{cwd:$w,tool_name:"Edit",tool_input:{file_path:$f}}')" |
| 221 | +check block 'payload cwd=$WT, absolute path in $MAIN' \ |
| 222 | + "$(jq -nc --arg f "$MAIN/pkg/x.ts" --arg w "$WT" '{cwd:$w,tool_name:"Edit",tool_input:{file_path:$f}}')" |
| 223 | + |
| 224 | +echo "== a submodule of the shared checkout is not a linked worktree ==" |
| 225 | +# git-dir is .git/modules/<name> there. It must not be mistaken for .git/worktrees/<name>. |
| 226 | +if ( cd "$MAIN" && git -c protocol.file.allow=always submodule add -q "$SIB" sub ) >/dev/null 2>&1; then |
| 227 | + expect block "$MAIN/sub/README.md" |
| 228 | + expect block "$MAIN/sub/pkg/x.ts" |
| 229 | +else |
| 230 | + skip=$((skip + 2)); printf ' skip submodule fixture unavailable in this git build\n' |
| 231 | +fi |
| 232 | + |
| 233 | +echo "== the jq-less fallback still reaches the same verdict on real payloads ==" |
| 234 | +nojq="$(mktemp -d)" |
| 235 | +for b in bash env cat sed head grep git dirname basename; do |
| 236 | + p="$(command -v "$b")" && ln -s "$p" "$nojq/$b" |
| 237 | +done |
| 238 | +check block 'no jq: into $MAIN' "$(payload "$MAIN/pkg/x.ts")" PATH="$nojq" |
| 239 | +check allow 'no jq: into $WT' "$(payload "$WT/pkg/x.ts")" PATH="$nojq" |
| 240 | +check allow 'no jq: into $PLAIN' "$(payload "$PLAIN/notes.md")" PATH="$nojq" |
| 241 | +check block 'no jq: new file, new dir in $MAIN' "$(payload "$MAIN/brand/new/f.ts")" PATH="$nojq" |
| 242 | +mkdir -p "$MAIN/a b" "$WT/a b" |
| 243 | +check block 'no jq: path containing a space, $MAIN' "$(payload "$MAIN/a b/c.ts")" PATH="$nojq" |
| 244 | +check allow 'no jq: path containing a space, $WT' "$(payload "$WT/a b/c.ts")" PATH="$nojq" |
| 245 | +# a decoy "file_path" inside a Write payload's content is JSON-escaped, so the text scan |
| 246 | +# does not mistake it for the real key |
| 247 | +decoy="$(jq -nc --arg f "$MAIN/pkg/x.ts" --arg c "see \"file_path\": \"$PLAIN/decoy\" in the docs" \ |
| 248 | + '{tool_name:"Write",tool_input:{content:$c,file_path:$f}}')" |
| 249 | +check block 'no jq: escaped decoy file_path in content loses to the real key' "$decoy" PATH="$nojq" |
| 250 | +check block 'with jq: same decoy payload' "$decoy" |
| 251 | + |
| 252 | +echo "== wiring: settings.json must route Edit, Write and NotebookEdit to this hook ==" |
| 253 | +# The hook is deliberately tool-agnostic, so the matcher is the ONLY thing that decides which |
| 254 | +# tools it sees. Nothing else in the repo checks that. Additions to the matcher are fine; |
| 255 | +# a removal is what this pins. |
| 256 | +if [ -f "$settings" ]; then |
| 257 | + matcher="$(jq -r '[.hooks.PreToolUse[]? | select([.hooks[]?.command] | join(" ") | contains("guard-main-checkout.sh")) |
| 258 | + | .matcher] | join(" ")' "$settings" 2>/dev/null || printf '')" |
| 259 | + for tool in Edit Write NotebookEdit; do |
| 260 | + case "$matcher" in |
| 261 | + *"$tool"*) pass=$((pass + 1)); printf ' ok wired %s -> guard-main-checkout.sh\n' "$tool" ;; |
| 262 | + *) fail=$((fail + 1)); printf ' FAIL %s is not routed to guard-main-checkout.sh (matcher: %s)\n' "$tool" "$matcher" ;; |
| 263 | + esac |
| 264 | + done |
| 265 | +else |
| 266 | + fail=$((fail + 1)); printf ' FAIL settings.json not found at %s\n' "$(short "$settings")" |
| 267 | +fi |
| 268 | + |
| 269 | +# ── KNOWN HOLES ───────────────────────────────────────────────────────────────────────── |
| 270 | +# The cases below pin what the hook does TODAY, and what it does today is WRONG. They are |
| 271 | +# here so the matrix says the hole out loud rather than being silent about it, and so that |
| 272 | +# fixing it is a mechanical edit to this file. They are NOT statements of intended |
| 273 | +# behaviour. Each names the issue that must flip it. |
| 274 | +echo "== KNOWN HOLE #7259: any git-dir path containing /worktrees/ reads as a linked worktree ==" |
| 275 | +# `case "$gitdir" in */worktrees/*) exit 0` is a substring match on a path, not a test for a |
| 276 | +# linked worktree. $ODD is a PRIMARY checkout that merely lives under a directory named |
| 277 | +# `worktrees`. git prints a RELATIVE git-dir (`.git`) at a repo's toplevel and an ABSOLUTE |
| 278 | +# one from any subdirectory, so the same unguarded checkout gets opposite verdicts by depth. |
| 279 | +# When #7259 is fixed both of these become `block`. (Same defect as the sibling repo's |
| 280 | +# objectstack-ai/objectstack#11809 — the hooks share these lines; fix them together.) |
| 281 | +# The deciding detail, measured rather than assumed: it is the NEAREST EXISTING ANCESTOR |
| 282 | +# that is handed to git, so a path whose nearest existing ancestor is the repo toplevel gets |
| 283 | +# the relative git-dir and blocks, while anything resolving to a subdirectory gets the |
| 284 | +# absolute one and slips through. |
| 285 | +expect block "$ODD/README.md" # correct today, but only because git-dir was relative |
| 286 | +expect block "$ODD/brand/new/f.ts" # ditto — resolves up to the toplevel |
| 287 | +expect allow "$ODD/pkg/x.ts" # ⛔ WRONG — an unguarded edit into a PRIMARY checkout |
| 288 | +expect allow "$ODD/pkg/brand/new/f.ts" # ⛔ WRONG — same hole, reached through the ancestor walk |
| 289 | + |
| 290 | +echo "== KNOWN HOLE #7260: NotebookEdit's path key is notebook_path, which this hook never reads ==" |
| 291 | +# The matcher routes NotebookEdit here, but the hook extracts only .tool_input.file_path, so |
| 292 | +# every notebook edit takes the no-path branch and is judged by CLAUDE_PROJECT_DIR instead of |
| 293 | +# by the file. The verdict below is a constant per session and is wrong in both directions. |
| 294 | +# When #7260 is fixed, these become block / allow / allow by the notebook's own path. |
| 295 | +# (Same defect as objectstack-ai/objectstack#11810 — the hooks share these lines.) |
| 296 | +nbpay() { jq -nc --arg f "$1" '{tool_name:"NotebookEdit",tool_input:{notebook_path:$f,new_source:"x",edit_mode:"replace"}}'; } |
| 297 | +PROJ="$MAIN" |
| 298 | +check block 'NotebookEdit into $MAIN, CLAUDE_PROJECT_DIR=$MAIN' "$(nbpay "$MAIN/pkg/x.ipynb")" |
| 299 | +check block 'NotebookEdit into $WT, CLAUDE_PROJECT_DIR=$MAIN' "$(nbpay "$WT/pkg/x.ipynb")" # ⛔ WRONG — refuses the mandated location |
| 300 | +check block 'NotebookEdit into $PLAIN, CLAUDE_PROJECT_DIR=$MAIN' "$(nbpay "$PLAIN/x.ipynb")" # ⛔ WRONG — refuses a file in no repo |
| 301 | +PROJ="$WT" |
| 302 | +check allow 'NotebookEdit into $MAIN, CLAUDE_PROJECT_DIR=$WT' "$(nbpay "$MAIN/pkg/x.ipynb")" # ⛔ WRONG — unguarded edit into the shared checkout |
| 303 | +PROJ="$PLAIN" |
| 304 | +check allow 'NotebookEdit into $MAIN, CLAUDE_PROJECT_DIR=$PLAIN' "$(nbpay "$MAIN/pkg/x.ipynb")" # ⛔ WRONG — same, from a session rooted outside any repo |
| 305 | + |
| 306 | +echo "== BOUNDARY: the jq-less fallback is a text scan, not a JSON parser ==" |
| 307 | +# Not filed as a defect: jq is present wherever this hook runs, and Claude Code emits plain |
| 308 | +# UTF-8 paths, never \u-escaped ones. Recorded so that the first thing to fix is known if the |
| 309 | +# fallback ever becomes load-bearing. With jq the same payload is judged correctly. |
| 310 | +esc="$(printf '%s' "$MAIN/pkg/x.ts" | sed 's/\//\\u002f/g')" |
| 311 | +uni="$(printf '{"tool_name":"Edit","tool_input":{"file_path":"%s"}}' "$esc")" |
| 312 | +check block 'with jq: /-escaped path into $MAIN' "$uni" |
| 313 | +check allow 'no jq: /-escaped path yields no path' "$uni" PATH="$nojq" |
| 314 | + |
| 315 | +printf '\n%s passed, %s failed' "$pass" "$fail" |
| 316 | +[ "$skip" -gt 0 ] && printf ', %s skipped' "$skip" |
| 317 | +printf '\n' |
| 318 | +[ "$fail" -eq 0 ] |
| 319 | + |
| 320 | +# ── NON-VACUITY ───────────────────────────────────────────────────────────────────────── |
| 321 | +# Each class above was shown to fail against a mutated copy of the hook before being trusted |
| 322 | +# to pass against the real one. Reproduce any of them without touching the real hook: |
| 323 | +# |
| 324 | +# cp .claude/hooks/guard-main-checkout.sh /tmp/mutant.sh |
| 325 | +# # e.g. delete the linked-worktree escape, which should redden every `allow` in a worktree: |
| 326 | +# perl -0pi -e 's{^\s*\*/worktrees/\*\) exit 0 ;;\n}{}m' /tmp/mutant.sh |
| 327 | +# GUARD_MAIN_CHECKOUT_HOOK=/tmp/mutant.sh .claude/hooks/guard-main-checkout.selftest.sh |
| 328 | +# |
| 329 | +# The mutations used, one per class: drop the */worktrees/* arm (core verdict) · drop the |
| 330 | +# nearest-existing-ancestor walk (new-file class) · replace dirname "$file" with |
| 331 | +# CLAUDE_PROJECT_DIR (the file's-own-repo class) · drop the OS_ALLOW_MAIN_EDITS line (escape |
| 332 | +# hatch) · turn the no-path else branch into exit 0 (the fails-closed class) · rename the key |
| 333 | +# in the grep fallback (the jq-less class) · change the final exit 2 to exit 0 (every block). |
0 commit comments