fix(hooks): extract-guard keeps <name> placeholders as words

This commit is contained in:
bchanot committed 2026-11-05 14:43:48 +01:00
1 parent 8d5a7bdb07
commit 0ca453f044
2 files changed
+27 -2

No files matched your search

+18 -2
View File
@@ -10,7 +10,7 @@
# Silent on every allowed command. jq, awk, grep, sed, tr or wc missing: one
# stderr warning, allow. A command that names no watched word (also with
# quotes and backslashes removed) never reaches awk; past that prefilter a
# command over 256 KiB or an awk failure denies (fail closed). The cap bounds
# command over 64 KiB or an awk failure denies (fail closed). The cap bounds
# the awk tokenizer, which rebuilds a word by concatenation.
#
# The parser is one POSIX awk program (bash 3.2 safe). Its tokenizer is
@@ -219,7 +219,23 @@ function redir( c, d, op) {
word = word op; have = 1; endword()
}
function op_item(c) {
# length of an angle-bracket placeholder (<pkg>, <root-dir>) at i, else 0:
# no valid redirection has that shape: kept as an ordinary (quoted) word
function placeholder( k, ch) {
if (have || substr(txt, i, 1) != "<") return 0
if (substr(txt, i + 1, 1) !~ /^[A-Za-z]$/) return 0
for (k = i + 2; k <= n && k <= i + 41; k++) {
ch = substr(txt, k, 1)
if (ch == ">") break
if (ch !~ /^[A-Za-z0-9_.-]$/) return 0
}
if (substr(txt, k, 1) != ">") return 0
if (k < n && !index(" \t\n;|()&", substr(txt, k + 1, 1))) return 0
return k - i + 1
}
function op_item(c, pl) {
if ((pl = placeholder())) { word = substr(txt, i, pl); have = 1; wq = 1; i += pl; return }
if (c == "(") { endword(); endcmd(); SD[++sdn] = DIR; i++ }
else if (c == ")") { endword(); endcmd(); if (sdn > 0) DIR = SD[sdn--]; i++ }
else if (c == ";" || c == "|") { endword(); endcmd(); i++ }
+9
View File
@@ -149,6 +149,15 @@ EOF
run $'git commit -m "$(cat <<\'EOF\'\nfeat: x\n\nbody about archives\nEOF\n)"'
check quiet-heredoc-commit "$RC [$OUT]" "0 []"
# angle-bracket placeholders are words, real redirections are not
allow 'dpkg -x <pkg> <dir>'
allow 'tar -xf <archive> -C <dir>'
allow $'git commit -F - <<\'EOF\'\nnever run dpkg -x <pkg> <rootdir>\nEOF'
deny 'dpkg -x p.deb /'
deny 'cp -r a /opt/app >log'
deny 'dpkg -x <pkg> /'
deny 'dpkg -x <pkg> </dev/null'
# ── 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