From 5507972d3a98c6c29b99fab50253080628ff1983 Mon Sep 17 00:00:00 2001 From: bchanot Date: Thu, 5 Nov 2026 00:24:45 +0100 Subject: [PATCH] feat(hooks): extract-guard refuses package/archive extraction into system dirs PreToolUse Bash|Monitor hook: dpkg/dpkg-deb -x, tar x, unzip, cpio -i and cp -r into /, /usr, /etc or another system root, including inside quoted ssh, vaas-cli ssh and bash -c scripts; doctrine bullet in Destructive tools. --- CLAUDE.global.md | 8 + hooks/extract-guard.sh | 386 ++++++++++++++++++++++++++++++++ lib/tests/extract-guard.test.sh | 176 +++++++++++++++ 3 files changed, 570 insertions(+) create mode 100755 hooks/extract-guard.sh create mode 100644 lib/tests/extract-guard.test.sh diff --git a/CLAUDE.global.md b/CLAUDE.global.md index ad0bb8e..b858626 100644 --- a/CLAUDE.global.md +++ b/CLAUDE.global.md @@ -221,6 +221,14 @@ days of work never pushed. - Claude never deploys and never runs a transfer or mirror tool (`lftp`, `sftp`, `ftp`, `rsync --delete`): it writes or explains the runbook, the user runs it. A test is a dev server on this machine, nothing more. +- Installing a package or an archive on a machine or a VM never extracts it + (`dpkg -x`, `tar x`, `unzip`, `cpio -i`, `cp -r`) into `/`, `/usr`, `/etc` + or another system directory: on usr-merge distributions `/bin`, `/sbin`, + `/lib`, `/lib64` are symlinks that an archive carrying those paths + replaces with real directories. List it first (`dpkg -c`, `tar tf`), + extract into a literal `mktemp -d` path and use PATH or PYTHONPATH; no + package manager on the target → say so to the user, never improvise. + `hooks/extract-guard.sh` refuses these commands. - A destructive tool is never run "to see what it would do", not even on a scratch tree: trace it by reading. If a run is unavoidable, the target is a fresh `mktemp -d` path written literally in the same command, after a diff --git a/hooks/extract-guard.sh b/hooks/extract-guard.sh new file mode 100755 index 0000000..57bbf3f --- /dev/null +++ b/hooks/extract-guard.sh @@ -0,0 +1,386 @@ +#!/usr/bin/env bash +# extract-guard.sh — PreToolUse (Bash|Monitor): refuse a package/archive +# extraction into / or a system directory (usr-merge incident: dpkg -x into +# / replaced /bin /lib symlinks with real dirs). Watched: dpkg -x, dpkg-deb +# -x/-R, tar x, unzip, cpio -i, recursive cp. System dirs: / /usr /etc /bin +# /sbin /lib* /boot /opt /var /srv /root /sys /proc /dev (not /var/tmp, +# /var/folders, $HOME). An unresolvable destination ($VAR, $(..)) is refused. +# +# Deny form: JSON on stdout, exit 0 (shape of hooks/push-guard.sh, BDR-111). +# Silent on every allowed command. jq, awk or grep missing: one stderr +# warning, allow. A command that names no watched word never reaches awk; +# past that prefilter an awk failure denies (fail closed). +# +# The parser is one POSIX awk program (bash 3.2 safe). Its tokenizer is +# adapted from push-guard.sh simple_cmds at fe7d5cd: newlines inside quotes +# stay in the word, redirections and `<<<` are words of their own, `$(..)` +# and backticks and heredoc bodies are queued for a scan of their own, and +# every quoted word holding a blank is re-scanned (no carrier list: ssh, +# vaas-cli ssh, bash -c, sudo sh -c, find -exec all fall out of it). +# +# MISSES: a destination built outside the text (script file, Makefile, alias, +# variable set in an earlier call); old-style tar `C` letter (tar xfC a /usr), +# --dir= abbreviations, --one-top-level=; archive members escaping a safe -C +# (../../usr, -P); a cd to a system dir in a remote shell then an extraction +# with no destination; option values read as tool words. ACCEPTED FALSE +# POSITIVES: text holding a complete extraction into a system dir, or `dpkg +# -x ` with no directory. The autoMode soft_deny rule is the net. +set -u + +for t in jq awk grep; do + command -v "$t" >/dev/null 2>&1 || { + echo "extract-guard: $t missing, guard inactive" >&2 + exit 0 + } +done + +cmd=$(jq -r '.tool_input.command // empty' 2>/dev/null) || exit 0 +[ -n "$cmd" ] || exit 0 + +WATCHED='(^|[^[:alnum:]_.-])(dpkg|dpkg-deb|tar|bsdtar|gtar|unzip|cpio|cp)([^[:alnum:]_-]|$)' +printf '%s' "$cmd" | grep -qE "$WATCHED" || exit 0 + +IFS= read -r -d '' PROG <<'AWK' || true +function base(s) { sub(/^.*\//, "", s); return s } +function hasblank(w) { return index(w, " ") || index(w, "\t") || index(w, "\n") } +function deny(msg) { print msg; exit 0 } + +function enqueue(t, d, dir) { + if (d > 3) { + if (t ~ WRE) deny("extract-guard: refused: nesting too deep to check a command that names an extraction tool; run it yourself if it is safe: ! ") + return + } + if (++nenq > 300) deny("extract-guard: refused: command too large to check; run it yourself if it is safe: ! ") + qn++; QT[qn] = t; QD[qn] = d; QDIR[qn] = dir +} + +function endword() { + if (have) { nrw++; RW[nrw] = word; RQ[nrw] = wq } + word = ""; have = 0; wq = 0 +} +function endcmd() { if (nrw) process(); nrw = 0 } + +# closing quote q of the quote opened at k; n + 1 when unclosed +function qend(k, q, m, ch) { + for (m = k + 1; m <= n; m++) { + ch = substr(txt, m, 1) + if (q == "\"" && ch == "\\") { m++; continue } + if (ch == q) return m + } + return n + 1 +} + +# $( ... ) at i: balanced inner text queued, the word keeps a "$" marker +function subst( k, dp, ch) { + dp = 1; k = i + 2 + while (k <= n && dp > 0) { + ch = substr(txt, k, 1) + if (ch == "'" || ch == "\"") k = qend(k, ch) + 1 + else if (ch == "\\") k += 2 + else { if (ch == "(") dp++; else if (ch == ")") dp--; k++ } + } + if (dp == 0) { enqueue(substr(txt, i + 2, k - i - 3), cdep + 1, DIR); i = k } + else { enqueue(substr(txt, i + 2), cdep + 1, DIR); i = n + 1 } + word = word "$"; have = 1 +} + +# backtick command substitution at i +function bq( k, ch) { + for (k = i + 1; k <= n; k++) { + ch = substr(txt, k, 1) + if (ch == "\\") { k++; continue } + if (ch == "`") break + } + enqueue(substr(txt, i + 1, k - i - 1), cdep + 1, DIR) + word = word "$"; have = 1; i = k + 1 +} + +function dquote( c, d) { + while (i <= n) { + c = substr(txt, i, 1) + if (c == "\"") { i++; return } + d = substr(txt, i + 1, 1) + if (c == "\\" && i < n && index("\"\\$`\n", d)) { + if (d != "\n") word = word d + i += 2 + } else if (c == "$" && d == "(") subst() + else if (c == "`") bq() + else { word = word c; i++ } + } +} + +function quoted(c, j, d) { + if (c == "'") { + have = 1; wq = 1 + j = index(substr(txt, i + 1), "'") + if (j == 0) { word = word substr(txt, i + 1); i = n + 1 } + else { word = word substr(txt, i + 1, j - 1); i += j + 1 } + } else if (c == "\"") { have = 1; wq = 1; i++; dquote() + } else if (c == "`") bq() + else { + d = substr(txt, i + 1, 1) + if (i < n && d != "\n") { word = word d; have = 1; wq = 1 } + i += 2 + } +} + +# heredoc delimiter after << (quotes and backslashes removed) +function read_delim( q, d, ch) { + if (substr(txt, i, 1) == "-") i++ + while (substr(txt, i, 1) == " " || substr(txt, i, 1) == "\t") i++ + d = "" + while (i <= n) { + ch = substr(txt, i, 1) + if (index(" \t\n;&|()<>", ch)) break + if (ch == "'" || ch == "\"") { + q = ch; i++ + while (i <= n && substr(txt, i, 1) != q) { d = d substr(txt, i, 1); i++ } + i++; continue + } + if (ch == "\\") { d = d substr(txt, i + 1, 1); i += 2; continue } + d = d ch; i++ + } + hd[++nh] = d +} + +# heredoc bodies start after the newline that ended the command line +function read_heredocs( k, e, ln, chk, body) { + for (k = 1; k <= nh; k++) { + body = "" + while (i <= n) { + e = index(substr(txt, i), "\n") + if (e == 0) { ln = substr(txt, i); i = n + 1 } + else { ln = substr(txt, i, e - 1); i += e } + chk = ln; sub(/^\t+/, "", chk) + if (chk == hd[k]) break + body = body ln "\n" + } + enqueue(body, cdep + 1, DIR) + } + nh = 0 +} + +# a redirection operator becomes a word of its own (fd prefix kept) +function redir( c, d, op) { + if (!(have && !wq && word ~ /^[0-9]+$/)) endword() + c = substr(txt, i, 1); d = substr(txt, i + 1, 1) + if (c == "&") { op = "&>"; i += 2; if (substr(txt, i, 1) == ">") { op = op ">"; i++ } } + else { + op = c; i++ + if (c == ">" && (d == ">" || d == "|")) { op = op d; i++ } + else if (d == "&") { + op = op d; i++ + while (i <= n && index("0123456789-", substr(txt, i, 1))) { op = op substr(txt, i, 1); i++ } + } else if (c == "<" && d == ">") { op = op d; i++ } + } + word = word op; have = 1; endword() +} + +function op_item(c) { + if (c == ";" || c == "|" || c == "(" || c == ")") { endword(); endcmd(); i++ } + else if (c == "&" && substr(txt, i + 1, 1) != ">") { endword(); endcmd(); i++ } + else if (c == "<" && substr(txt, i + 1, 2) == "<<") { + endword(); word = "<<<"; have = 1; endword(); i += 3 + } else if (c == "<" && substr(txt, i + 1, 1) == "<") { endword(); i += 2; read_delim() } + else redir() +} + +function token( c) { + c = substr(txt, i, 1) + if (c == "'" || c == "\"" || c == "\\" || c == "`") quoted(c) + else if (c == "$" && substr(txt, i + 1, 1) == "(") subst() + else if (c == "\n") { endword(); endcmd(); i++; if (nh) read_heredocs() } + else if (c == " " || c == "\t") { endword(); i++ } + else if (c == "#" && !have) { while (i <= n && substr(txt, i, 1) != "\n") i++ } + else if (index(";|()<>&", c)) op_item(c) + else { word = word c; have = 1; i++ } +} + +function scan(t, d, dir) { + txt = t; n = length(t); i = 1; cdep = d; DIR = dir + word = ""; have = 0; wq = 0; nrw = 0; nh = 0 + while (i <= n) token() + endword(); endcmd() +} + +# absolute normalized path; "$" = unresolvable; "" = relative without a cwd +function norm(p, nc, k, c, sp, out) { + if (p ~ /^~(\/|$)/) p = "/HOME" substr(p, 2) + else if (p ~ /^[$]HOME(\/|$)/) p = "/HOME" substr(p, 6) + else if (p ~ /^[$][{]HOME[}](\/|$)/) p = "/HOME" substr(p, 8) + if (substr(p, 1, 1) == "~" || index(p, "$") || index(p, "`")) return "$" + if (substr(p, 1, 1) != "/") { + if (DIR == "" || DIR == "$") return DIR + p = DIR "/" p + } + nc = split(p, parts, "/"); sp = 0 + for (k = 1; k <= nc; k++) { + c = parts[k] + if (c == "" || c == ".") continue + if (c == "..") { if (sp > 0) sp--; continue } + stack[++sp] = c + } + out = "" + for (k = 1; k <= sp; k++) out = out "/" stack[k] + return out == "" ? "/" : out +} + +# 1 system, 2 unresolvable, 0 fine +function is_system(p, q, nc) { + q = norm(p) + if (q == "$") return 2 + if (q == "") return 0 + if (q == "/") return 1 + nc = split(q, parts, "/") + if (parts[2] == "HOME") return 0 + if (parts[2] == "var" && (parts[3] == "tmp" || parts[3] == "folders")) return 0 + return (parts[2] in SYS) ? 1 : 0 +} + +function track_cd( j) { + if (base(W[1]) == "popd") { DIR = ""; return } + if (base(W[1]) != "cd" && base(W[1]) != "pushd") return + j = 2 + while (j <= nw && W[j] ~ /^(-P|-L|-e|-@|--)$/) j++ + DIR = (j > nw || W[j] == "-") ? "" : norm(W[j]) +} + +# dest option at A[j]: -s , -s, bundle ending in s, --l , --l= +function dopt(j, s, l, w, wl) { + w = A[j]; DSKIP = 0 + if (w ~ /^-[A-Za-z]+$/ && substr(w, length(w)) == s) { DV = A[j + 1]; DSKIP = 1; return 1 } + if (l != "") { + if (w == "--" l) { DV = A[j + 1]; DSKIP = 1; return 1 } + wl = "--" l "=" + if (substr(w, 1, length(wl)) == wl) { DV = substr(w, length(wl) + 1); return 1 } + } + if (substr(w, 1, 2) == "-" s && length(w) > 2 && w !~ /^-[A-Za-z]+$/) { DV = substr(w, 3); return 1 } + return 0 +} +function adddest() { D[++nd] = DV } + +function det_tar( j, w, x, t) { + if (A[1] ~ /^[A-Za-z]+$/) { x = (A[1] ~ /x/); t = (A[1] ~ /t/) } + for (j = 1; j <= na; j++) { + w = A[j] + if (w == "--extract" || w == "--get") x = 1 + else if (w == "--list") t = 1 + else if (w ~ /^-[A-Za-z]+$/) { if (w ~ /x/) x = 1; if (w ~ /t/) t = 1 } + if (dopt(j, "C", "directory")) { adddest(); j += DSKIP } + } + ext = (x && !t) +} + +function det_dpkg(b, j, w, seen, nn, n2) { + for (j = 1; j <= na; j++) { + w = A[j] + if (!seen) { + seen = (w == "-x" || w == "-X" || w == "--extract" || w == "--vextract" || (b == "dpkg-deb" && (w == "-R" || w == "--raw-extract"))) + } else if (w !~ /^-/) { nn++; if (nn == 2) n2 = w } + } + if (!seen || nn == 0) return + ext = 1 + if (nn >= 2) { DV = n2; adddest() } else absent = 1 +} + +function det_unzip( j, w, lst) { + for (j = 1; j <= na; j++) { + w = A[j] + if ((w ~ /^-[A-Za-z]+$/ && w ~ /[lvtzZ]/) || w ~ /^-Z/) lst = 1 + if (dopt(j, "d", "")) { adddest(); j += DSKIP } + } + ext = !lst +} + +function det_cpio( j, w, x, t) { + for (j = 1; j <= na; j++) { + w = A[j] + if (w == "--extract") x = 1 + else if (w ~ /^-[A-Za-z]+$/) { if (w ~ /i/) x = 1; if (w ~ /t/) t = 1 } + if (dopt(j, "D", "directory")) { adddest(); j += DSKIP } + } + ext = (x && !t) +} + +function det_cp( j, w, rec, last) { + for (j = 1; j <= na; j++) { + w = A[j] + if (w == "--recursive" || w == "--archive") rec = 1 + else if (w ~ /^-[A-Za-z]+$/ && w ~ /[rRa]/) rec = 1 + if (dopt(j, "t", "target-directory")) { adddest(); j += DSKIP; continue } + if (w !~ /^-/) last = w + } + if (!rec) return + if (nd == 0 && last != "") { DV = last; adddest() } + ext = (nd > 0) +} + +function why(b, dest, kind) { + gsub(/\n/, " ", dest) + if (kind == 2) + return "extract-guard: refused: " b " destination " dest " cannot be checked; write the literal path, run mktemp -d alone and extract into the path it prints (CLAUDE.md § Destructive tools & data loss); a real install is the user's: ! " + return "extract-guard: refused: " b " writes into system directory " dest " (CLAUDE.md § Destructive tools & data loss). List it first (dpkg -c, tar tf, unzip -l), run mktemp -d alone and extract into the literal path it prints, then use PATH or PYTHONPATH; a real system install is the user's: ! " +} + +function verdict(b, j, s) { + if (!ext) return + if (absent) deny(why(b, "(none)", 2)) + if (nd == 0) { if (DIR == "") return; D[++nd] = DIR } + for (j = 1; j <= nd; j++) { + s = is_system(D[j]) + if (s) deny(why(b, D[j], s)) + } +} + +function detect(b, k, j) { + na = 0; nd = 0; ext = 0; absent = 0 + for (j = k + 1; j <= nw; j++) A[++na] = W[j] + A[na + 1] = "" + if ((work += na + 1) > 300000) deny("extract-guard: refused: command too large to check; run it yourself if it is safe: ! ") + if (b ~ /^(tar|bsdtar|gtar)$/) det_tar() + else if (b == "dpkg" || b == "dpkg-deb") det_dpkg(b) + else if (b == "unzip") det_unzip() + else if (b == "cpio") det_cpio() + else det_cp() + verdict(b) +} + +# one simple command: drop redirections, track cd, run each watched tool +function process( k, w, skip) { + if (++nrec > 5000) deny("extract-guard: refused: command too large to check; run it yourself if it is safe: ! ") + nw = 0 + for (k = 1; k <= nrw; k++) { + w = RW[k] + if (hasblank(w)) enqueue(w, cdep + 1, DIR) + if (skip) { skip = 0; continue } + if (!RQ[k] && (w ~ /^[0-9]*[<>]/ || w ~ /^&>/)) { skip = (w ~ BARE); continue } + if (w == "{}" || w == "+" || (RQ[k] && w == ";")) continue + W[++nw] = w + } + track_cd() + for (k = 1; k <= nw; k++) if (base(W[k]) in WATCH) detect(base(W[k]), k) +} + +BEGIN { + RS = "\001" + split("usr etc bin sbin lib lib32 lib64 libx32 boot opt var srv root sys proc dev", sy, " ") + for (j in sy) SYS[sy[j]] = 1 + split("dpkg dpkg-deb tar bsdtar gtar unzip cpio cp", wt, " ") + for (j in wt) WATCH[wt[j]] = 1 + WRE = "(^|[^[:alnum:]_.-])(dpkg|dpkg-deb|tar|bsdtar|gtar|unzip|cpio|cp)([^[:alnum:]_-]|$)" + BARE = "^[0-9]*(>>|>[|]|>&|>|<&|<>|<<<|<|&>>|&>)$" +} +{ all = all $0 } +END { + scan(all, 0, "") + while (qh < qn) { qh++; scan(QT[qh], QD[qh], QDIR[qh]) } +} +AWK + +reason=$(printf '%s' "$cmd" | LC_ALL=C awk "$PROG" 2>/dev/null) || { + word=$(printf '%s' "$cmd" | grep -oE 'dpkg-deb|dpkg|bsdtar|gtar|tar|unzip|cpio|cp' | head -n 1) + reason="extract-guard: refused: the checker failed on a command that names $word; run it yourself if it is safe: ! " +} +[ -n "$reason" ] || exit 0 +jq -cn --arg r "$reason" '{hookSpecificOutput:{hookEventName:"PreToolUse",permissionDecision:"deny",permissionDecisionReason:$r}}' +exit 0 diff --git a/lib/tests/extract-guard.test.sh b/lib/tests/extract-guard.test.sh new file mode 100644 index 0000000..5cab0b7 --- /dev/null +++ b/lib/tests/extract-guard.test.sh @@ -0,0 +1,176 @@ +#!/usr/bin/env bash +# lib/tests/extract-guard.test.sh +# hooks/extract-guard.sh: denies an extraction into / or a system dir, +# silent on everything else. Also locks the settings.json wiring (last). +# shellcheck disable=SC2016 # single-quoted forms are the commands under test +set -u +ROOT="$(cd "$(dirname "$0")/../.." && pwd)" +H="$ROOT/hooks/extract-guard.sh" +WORK=$(mktemp -d) +trap 'rm -rf "$WORK"' EXIT +pass=0; fail=0 +check() { if [ "$2" = "$3" ]; then pass=$((pass+1)); else fail=$((fail+1)); + printf 'FAIL %s: got[%s] want[%s]\n' "$1" "$2" "$3"; fi; } + +OUT=""; RC=0 +run() { # run + OUT=$(jq -n --arg c "$1" '{tool_name:"Bash",tool_input:{command:$c}}' \ + | bash "$H" 2>/dev/null); RC=$? +} +decision() { printf '%s' "$OUT" \ + | jq -r '.hookSpecificOutput.permissionDecision // "none"' 2>/dev/null; } +deny() { run "$1"; check "deny[$1]" "$RC $(decision)" "0 deny"; } +allow() { run "$1"; check "allow[$1]" "$RC [$OUT]" "0 []"; } + +# ── deny ── +deny 'dpkg -x p.deb /' +deny 'dpkg-deb --extract p.deb /usr' +deny 'dpkg -x p.deb' +deny $'dpkg -x p.deb \\\n/' +deny 'tar -xzf a.tgz -C /usr' +deny 'tar -xf a.tar -C "/usr"' +deny 'tar -C /usr -xzf a.tgz' +deny 'tar -z -xvf a.tgz -C /' +deny 'tar xzf a.tgz -C/' +deny 'tar -xvC / -f a.tar' +deny 'tar -xf a.tar --directory=/etc' +deny 'tar -C /tmp/w -C / -xf a.tar' +deny $'tar -xzf a.tgz \\\n -C /usr' +deny 'cd / && tar xf a.tar' +deny 'cd / && cd usr && tar xf a.tar' +deny 'pushd /usr && unzip a.zip' +deny "unzip a.zip -d '/opt'" +deny 'unzip -od /usr a.zip' +deny 'cpio -idm -D /usr < a.cpio' +deny 'cd / && cpio -vidm < a.cpio' +deny 'cp -r build /opt/app' +deny 'cp -r build /opt/app 2>/dev/null' +deny 'cp -rt /usr/lib src' +deny 'cp -a src "/usr/local/x"' +deny 'find dist -exec cp -r {} /var/www/html \;' +deny "ssh vm 'dpkg -x p.deb /'" +deny "ssh -i k -p 2222 root@vm 'tar -xf /tmp/a.tar -C /'" +deny 'vaas-cli ssh vm -- tar -xzf a.tgz -C /usr' +deny "vaas-cli ssh vm 'dpkg -x /tmp/p.deb /'" +deny $'vaas-cli ssh vm "\nset -e\ncd /\ntar xf /tmp/a.tar\n"' +deny 'bash -c "cd /usr/lib && unzip a.zip"' +deny "sudo sh -c 'cd / && tar xzf /tmp/a.tgz'" +deny "timeout 600 vaas-cli ssh vm 'dpkg -x p.deb /'" +deny $'ssh vm bash -s <<\'EOF\' 2>&1 | tail -5\ntar -xf a.tar -C /\nEOF' +deny $'cat <<< "$x"\ntar -xf a.tar -C /' +deny 'tar -xf $(ls *.tar) -C /' +deny 'tar -xf `ls *.tar` -C /usr' +deny 'out="$(vaas-cli ssh vm '"'dpkg -x /tmp/p.deb /'"')"' +deny 'tar -xf a.tar -C "$DEST"' +deny 'tar -xf a.tar -C ~/../../usr' +deny 'stdbuf -oL tar -xf a.tar -C /' +deny 'busybox tar -xf a.tar -C /' +deny 'for f in *.deb; do dpkg -x $f /; done' +deny 'cd "$work" && tar xf a.tar' +deny "bash -c \"ssh vm 'sh -c \\\"bash -c \\\\\\\"tar xf a -C /\\\\\\\"\\\"'\"" + +# two violations: exactly one JSON object +run 'dpkg -x a.deb / ; tar -xf a.tar -C /usr' +check two-violations "$(printf '%s' "$OUT" | jq -s length)" 1 + +# ── allow ── +allow 'tar -C /tmp/x -xf a.tar' +allow 'tar -cf /tmp/a.tar /usr' +allow 'tar tf a.tar' +allow 'tar -tvf a.tar -C /' +allow 'tar -xf a.tar' +allow 'tar -xf a.tar -C ~/x' +allow 'tar -xf a.tar -C "$HOME/x"' +allow 'tar -xf a.tar -C /var/folders/ab/T/tmp.X' +allow 'tar -xf a.tar -C /var/tmp/w' +allow 'dpkg -c p.deb' +allow 'dpkg -R -i debs/' +allow 'unzip -l a.zip' +allow 'unzip -Z1 a.zip' +allow 'cpio -itv < a.cpio' +allow 'unzip a.zip -d build' +allow 'cp a.txt /etc/hosts' +allow 'cp -r src dist' +allow 'cp --parents a /tmp/x' +allow 'cp -r a b > /dev/null' +allow 'grep -rn "dpkg -x" docs' +allow 'echo "tar xf a"' +allow "vaas-cli ssh vm 'tar -xf a.tar -C /tmp/w'" +allow "vaas-cli ssh vm 'unzip a.zip -d build'" +allow "vaas-cli ssh vm 'command -v unzip || sudo apt-get install -y unzip'" +allow 'sudo apt install tar xz-utils' +allow 'git commit -m "doc: list an archive with tar tf before extracting"' +allow 'bash ~/.claude/lib/memory.sh add learning --title "dpkg -x into a system dir breaks usr-merge" --body-file /tmp/b' + +# quiet corpus: ordinary commands, silent rc 0 +while IFS= read -r c; do allow "$c"; done <<'EOF' +ls -la /usr/lib | grep tar +jq -r '.a[] | select(.x > 1)' f.json +awk '{ print $1 }' data.txt +echo "a" "b" +git status --short +git log --oneline -n 5 +cat /etc/hosts +grep -rn "cp" src +make test suite=memory +npm run build && npm test +sed -n '1,20p' README.md +find . -name '*.sh' -exec shellcheck {} + +curl -sS https://example.org/x -o /tmp/x.json +mkdir -p /tmp/w && cd /tmp/w && ls +python3 -I -c 'print(1)' +docker ps --format '{{.Names}}' +ssh vm 'uname -a' +rsync -n a/ b/ +cp a.txt b.txt +git commit -m "$(printf 'fix: a\n\nbody')" +EOF +run $'git commit -m "$(cat <<\'EOF\'\nfeat: x\n\nbody about archives\nEOF\n)"' +check quiet-heredoc-commit "$RC [$OUT]" "0 []" + +# ── timed flood cases ── +flood=$(awk 'BEGIN { for (i = 1; i <= 5001; i++) print "echo " i; print "tar xf a" }') +SECONDS=0; run "$flood"; el=$SECONDS +check flood-5001-deny "$RC $(decision)" "0 deny" +check flood-5001-fast "$([ "$el" -le 2 ] && echo ok || echo "slow ${el}s")" ok +plain=$(awk 'BEGIN { for (i = 1; i <= 5000; i++) print "echo line " i }') +SECONDS=0; run "$plain"; el=$SECONDS +check plain-5000-allow "$RC [$OUT]" "0 []" +check plain-5000-fast "$([ "$el" -le 1 ] && echo ok || echo "slow ${el}s")" ok +many=$(awk "BEGIN { for (i = 1; i <= 299; i++) print \"bash -c 'cp -r a b'\" }") +SECONDS=0; run "$many"; el=$SECONDS +check rescan-299-allow "$RC [$OUT]" "0 []" +check rescan-299-fast "$([ "$el" -le 3 ] && echo ok || echo "slow ${el}s")" ok +over=$(awk "BEGIN { for (i = 1; i <= 301; i++) print \"bash -c 'cp -r a b'\" }") +run "$over"; check rescan-301-deny "$RC $(decision)" "0 deny" +deep="bash -c \"bash -c 'bash -c \\\"bash -c \\\\\\\"tar xf a\\\\\\\"\\\"'\"" +run "$deep"; check nesting-deny "$RC $(decision)" "0 deny" + +# ── plumbing ── +BASH_BIN=$(command -v bash) +mkdir -p "$WORK/shim" +for t in awk grep head; do ln -s "$(command -v "$t")" "$WORK/shim/$t"; done +err=$(printf '%s' '{"tool_input":{"command":"tar xf a -C /"}}' \ + | PATH="$WORK/shim" "$BASH_BIN" "$H" 2>&1 >"$WORK/o"); rc=$? +check nojq "$rc [$(cat "$WORK/o")] [$err]" \ + "0 [] [extract-guard: jq missing, guard inactive]" +run 'tar xf a -C /' +check deny-json-shape "$(printf '%s' "$OUT" | jq -r \ + '.hookSpecificOutput | .hookEventName + " " + .permissionDecision')" \ + "PreToolUse deny" +check deny-reason-names-rule "$(printf '%s' "$OUT" \ + | jq -r '.hookSpecificOutput.permissionDecisionReason' \ + | grep -c 'Destructive tools & data loss')" 1 +out=$(printf 'not json' | bash "$H" 2>/dev/null); check badjson "$? [$out]" "0 []" +out=$(printf '{}' | bash "$H" 2>/dev/null); check nocmd "$? [$out]" "0 []" + +# ── wiring (last): settings.json carries the entry and the soft_deny line ── +S="$ROOT/settings.json" +check wiring-entry "$(jq -e '.hooks.PreToolUse[] + | select(any(.hooks[]; .command=="bash ~/.claude/hooks/extract-guard.sh")) + | .matcher=="Bash|Monitor" and .hooks[0].timeout==10' "$S" 2>&1)" true +check wiring-soft-deny "$(jq -r '[.autoMode.soft_deny[] + | select(startswith("Extracting, unpacking or recursively copying"))] + | length' "$S")" 1 + +printf 'PASS=%s FAIL=%s\n' "$pass" "$fail"; [ "$fail" -eq 0 ]