diff --git a/hooks/extract-guard.sh b/hooks/extract-guard.sh index b3feda3..bb7a079 100755 --- a/hooks/extract-guard.sh +++ b/hooks/extract-guard.sh @@ -23,9 +23,12 @@ # 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=; a tool name built by $'\x74' -# escapes (the prefilter strips only quotes and backslashes); 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. +# escapes (the prefilter strips only quotes and backslashes) or by expansion +# (t$'a'r, t$''ar, t$"a"r, t${x}ar, an empty $() or backticks inside a +# name): mostly stopped by the prefilter, the soft_deny line is the net; +# 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, `dpkg -x ` with no directory, a glob or brace destination, and # a quoted script run after a cd to a system dir (cd / && ssh vm "tar xf a": @@ -231,6 +234,7 @@ function token( c) { c = substr(txt, i, 1) if (c == "'" || c == "\"" || c == "\\" || c == "`") quoted(c) else if (c == "$" && substr(txt, i + 1, 1) == "'") ansi() + else if (c == "$" && substr(txt, i + 1, 1) == "\"") { i++; quoted("\"") } 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++ } @@ -429,7 +433,7 @@ END { AWK size=$(printf '%s' "$cmd" | wc -c | tr -d ' ') -if [ "$size" -gt 262144 ]; then +if [ "$size" -gt 65536 ]; then reason="extract-guard: refused: command too large to check; run it yourself if it is safe: ! (CLAUDE.md ยง Destructive tools & data loss)" else reason=$(printf '%s' "$cmd" | LC_ALL=C awk "$PROG" 2>/dev/null) || { diff --git a/lib/tests/extract-guard.test.sh b/lib/tests/extract-guard.test.sh index 31083ff..f710ab2 100644 --- a/lib/tests/extract-guard.test.sh +++ b/lib/tests/extract-guard.test.sh @@ -77,6 +77,7 @@ deny 't'"'a'"'r xf a.tar -C /' deny '"t"ar xf a.tar -C /' deny $'ta\\\nr xf a.tar -C /' deny $'$\'tar\' xf a.tar -C /' +deny '$"tar" xf a.tar -C /' deny $'tar xf a.tar -C $\'\\x2f\'' deny $'echo \x01# ; tar xf a.tar -C /' deny 'tar xf a.tar -C /u?r' @@ -163,12 +164,14 @@ 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" -big=$(awk 'BEGIN { for (i = 0; i < 30000; i++) print "echo tar xf" }') +big=$(awk 'BEGIN { for (i = 0; i < 7000; i++) print "echo tar xf" }') SECONDS=0; run "$big"; el=$SECONDS -check cap-300k-deny "$RC $(decision)" "0 deny" -check cap-300k-reason "$(printf '%s' "$OUT" | jq -r \ +check cap-70k-deny "$RC $(decision)" "0 deny" +check cap-70k-reason "$(printf '%s' "$OUT" | jq -r \ '.hookSpecificOutput.permissionDecisionReason' | grep -c 'too large')" 1 -check cap-300k-fast "$([ "$el" -le 2 ] && echo ok || echo "slow ${el}s")" ok +check cap-70k-fast "$([ "$el" -le 2 ] && echo ok || echo "slow ${el}s")" ok +small=$(awk 'BEGIN { for (i = 0; i < 4000; i++) print "echo hello wor" }') +run "$small"; check cap-60k-silent "$RC [$OUT]" "0 []" deep="bash -c \"bash -c 'bash -c \\\"bash -c \\\\\\\"tar xf a\\\\\\\"\\\"'\"" run "$deep"; check nesting-deny "$RC $(decision)" "0 deny" check nesting-reason-names-rule "$(printf '%s' "$OUT" | jq -r \