diff --git a/hooks/extract-guard.sh b/hooks/extract-guard.sh index bb7a079..1b29df0 100755 --- a/hooks/extract-guard.sh +++ b/hooks/extract-guard.sh @@ -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 (, ) 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++ } diff --git a/lib/tests/extract-guard.test.sh b/lib/tests/extract-guard.test.sh index f710ab2..eff5440 100644 --- a/lib/tests/extract-guard.test.sh +++ b/lib/tests/extract-guard.test.sh @@ -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 ' +allow 'tar -xf -C ' +allow $'git commit -F - <<\'EOF\'\nnever run dpkg -x \nEOF' +deny 'dpkg -x p.deb /' +deny 'cp -r a /opt/app >log' +deny 'dpkg -x /' +deny 'dpkg -x