From b8ce049abbb4766ea9a9b3bcf16e447ebafa3af9 Mon Sep 17 00:00:00 2001 From: L4XB Date: Fri, 23 Oct 2026 07:19:32 +0200 Subject: [PATCH 1/7] fix(hooks): block a bypass that a brace group or subshell pipes into a shell pipelineSources() read the literal output of the commands that feed a shell, but a brace group or subshell producer has no words of its own, so nothing it printed reached the check. { echo 'git commit --no-verify -m x'; } | sh and (echo 'git push --no-verify'; printf x) | bash passed the guard, while echo 'git commit --no-verify -m x' | sh is blocked. shell-scan now keeps the source text of a closed scope, and pipelineSources() reads the commands in it that write to the group's output, as it already does for a plain producer. --- scripts/hooks/block-no-verify.js | 7 +++++++ scripts/hooks/lib/shell-scan.js | 5 ++++- tests/hooks/block-no-verify.test.js | 4 ++++ 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/scripts/hooks/block-no-verify.js b/scripts/hooks/block-no-verify.js index 5b1cfcee2..75755260f 100644 --- a/scripts/hooks/block-no-verify.js +++ b/scripts/hooks/block-no-verify.js @@ -820,6 +820,13 @@ function commandRole(words, budget) { function pipelineSources(command, budget) { const sources = []; for (let current = command; current; current = current.pipeFrom) { + // A brace group or subshell writes what its own commands print into the + // pipe: `{ echo 'git push --no-verify'; } | sh` feeds sh like `echo ... | sh`. + if (current.closedScope) { + for (const inner of scanShell(current.closedScope.body, budget).commands) { + if (!inner.pipeTo) sources.push(...pipelineSources(inner, budget)); + } + } budget.spend(current.words.length + 1); const { words } = executableWords(current.words, budget); for (const word of words) budget.spend(word.value.length + 3); diff --git a/scripts/hooks/lib/shell-scan.js b/scripts/hooks/lib/shell-scan.js index 94a23a32a..953833485 100644 --- a/scripts/hooks/lib/shell-scan.js +++ b/scripts/hooks/lib/shell-scan.js @@ -449,12 +449,15 @@ function markFunctionDefinition(scan) { } function openScope(scan, c, incomingPipe) { - scan.scope = { parent: scan.scope, isolated: c === '(' || incomingPipe, conditional: false, pipelineLast: c === '{' && incomingPipe }; + scan.scope = { parent: scan.scope, isolated: c === '(' || incomingPipe, conditional: false, pipelineLast: c === '{' && incomingPipe, bodyStart: scan.i + 1 }; scan.current.scope = scan.scope; } function closeScope(scan) { const closedScope = scan.scope; + // The source text between the braces or parentheses, for a reader of the + // group's output. + closedScope.body = scan.input.slice(closedScope.bodyStart, scan.i); scan.scope = closedScope.parent; scan.current.scope = scan.scope; scan.current.closedScope = closedScope; diff --git a/tests/hooks/block-no-verify.test.js b/tests/hooks/block-no-verify.test.js index 2cf25a1e1..5dac62720 100644 --- a/tests/hooks/block-no-verify.test.js +++ b/tests/hooks/block-no-verify.test.js @@ -235,6 +235,10 @@ const executingPayloads = [ ['word-internal hash does not hide a later push bypass', 'printf %s foo#bar && git push --no-verify'], ['pipe echo data to bash', "echo 'git commit -n -m x' | bash"], ['pipe printf data to sh', "printf '%s\\n' 'git commit --no-verify -m x' | sh"], + ['pipe brace group data to sh', "{ echo 'git commit --no-verify -m x'; } | sh"], + ['pipe subshell data to bash', "(echo 'git push --no-verify'; printf x) | bash"], + ['pipe nested group data to sh', "{ (printf '%s\\n' 'git commit -n -m x'); } | sh"], + ['pipe group pipeline data to sh', "{ echo 'git push --no-verify' | grep git; } | sh"], ['execute data through xargs and bash -c', "printf '%s\\n' 'git commit -n -m x' | xargs -I CMD bash -c CMD"], ['execute command substitution text through bash', "echo '$(git commit -n -m x)' | bash"], ['execute bash here-string', "bash <<< 'git commit -n -m x'"], From fc557e57ff63ea5498e47c04f9a1e397f6cd8e07 Mon Sep 17 00:00:00 2001 From: L4XB Date: Fri, 23 Oct 2026 07:33:24 +0200 Subject: [PATCH 2/7] fix(hooks): also read a group's writes joined in the order the shell receives them The commands in a group write one stream into the pipe, so a bypass split across them, as in { printf 'git pu'; printf 'sh --no-verify'; } | sh, was read as two harmless fragments. groupSources() now adds the outputs joined in order next to the separate ones. The group handling moves out of pipelineSources() to keep it under the complexity limit. --- scripts/hooks/block-no-verify.js | 21 ++++++++++++++------- tests/hooks/block-no-verify.test.js | 2 ++ 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/scripts/hooks/block-no-verify.js b/scripts/hooks/block-no-verify.js index 75755260f..1eff156d7 100644 --- a/scripts/hooks/block-no-verify.js +++ b/scripts/hooks/block-no-verify.js @@ -815,18 +815,25 @@ function commandRole(words, budget) { return { kind: 'opaque', stdin: true }; } +// A brace group or subshell writes what its own commands print into the pipe: +// `{ echo 'git push --no-verify'; } | sh` feeds sh like `echo ... | sh`. The +// writes form one stream, so they are also read joined in order: +// `{ printf 'git pu'; printf 'sh --no-verify'; } | sh`. +function groupSources(scope, budget) { + const written = []; + for (const inner of scanShell(scope.body, budget).commands) { + if (!inner.pipeTo) written.push(...pipelineSources(inner, budget)); + } + if (written.length > 1) written.push({ text: written.map(source => source.text).join(''), opaque: written.some(source => source.opaque) }); + return written; +} + // Literal producers only. Unmodeled transformations remain conservative rather // than executing a formatter, interpreter, shell or user-supplied command. function pipelineSources(command, budget) { const sources = []; for (let current = command; current; current = current.pipeFrom) { - // A brace group or subshell writes what its own commands print into the - // pipe: `{ echo 'git push --no-verify'; } | sh` feeds sh like `echo ... | sh`. - if (current.closedScope) { - for (const inner of scanShell(current.closedScope.body, budget).commands) { - if (!inner.pipeTo) sources.push(...pipelineSources(inner, budget)); - } - } + if (current.closedScope) sources.push(...groupSources(current.closedScope, budget)); budget.spend(current.words.length + 1); const { words } = executableWords(current.words, budget); for (const word of words) budget.spend(word.value.length + 3); diff --git a/tests/hooks/block-no-verify.test.js b/tests/hooks/block-no-verify.test.js index 5dac62720..1bbec6131 100644 --- a/tests/hooks/block-no-verify.test.js +++ b/tests/hooks/block-no-verify.test.js @@ -239,6 +239,8 @@ const executingPayloads = [ ['pipe subshell data to bash', "(echo 'git push --no-verify'; printf x) | bash"], ['pipe nested group data to sh', "{ (printf '%s\\n' 'git commit -n -m x'); } | sh"], ['pipe group pipeline data to sh', "{ echo 'git push --no-verify' | grep git; } | sh"], + ['pipe consecutive group writes to sh', "{ printf 'git pu'; printf 'sh --no-verify'; } | sh"], + ['pipe echo -n and echo group writes to bash', "{ echo -n 'git commit '; echo '--no-verify -m x'; } | bash"], ['execute data through xargs and bash -c', "printf '%s\\n' 'git commit -n -m x' | xargs -I CMD bash -c CMD"], ['execute command substitution text through bash', "echo '$(git commit -n -m x)' | bash"], ['execute bash here-string', "bash <<< 'git commit -n -m x'"], From 703fc9e3b8f28f09f5ef2d683c94476a44c27ba5 Mon Sep 17 00:00:00 2001 From: L4XB Date: Fri, 23 Oct 2026 07:40:27 +0200 Subject: [PATCH 3/7] fix(hooks): read the whitespace escapes in printf's format when it feeds a shell printf writes \n, \t and the other whitespace escapes of its format as the characters they name, but the producer text kept them as a backslash and a letter. printf 'git push --no-verify\n' | sh was read as the flag --no-verifyn and passed, with or without a group around it. printfText() now decodes the whitespace escapes and \\. Numeric escapes stay encoded, as for $'...' in shell-scan. --- scripts/hooks/block-no-verify.js | 17 +++++++++++++---- tests/hooks/block-no-verify.test.js | 2 ++ 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/scripts/hooks/block-no-verify.js b/scripts/hooks/block-no-verify.js index 1eff156d7..01d215113 100644 --- a/scripts/hooks/block-no-verify.js +++ b/scripts/hooks/block-no-verify.js @@ -828,6 +828,18 @@ function groupSources(scope, budget) { return written; } +// printf writes the whitespace escapes of its format as the characters they +// name, so `printf 'git push --no-verify\n'` ends its flag at the newline. As for +// $'...' in shell-scan, only word boundaries are decoded, not numeric escapes. +const PRINTF_SPACE_ESCAPES = { n: '\n', t: '\t', r: '\r', v: '\v', f: '\f', '\\': '\\' }; + +function printfText(words) { + const format = words[1]?.value || ''; + if (format === '%s' || format === '%s\\n') return words.slice(2).map(word => word.value).join('\n'); + const decoded = format.replace(/\\([ntrvf\\])/g, (escape, c) => PRINTF_SPACE_ESCAPES[c]); + return [decoded, ...words.slice(2).map(word => word.value)].join(' '); +} + // Literal producers only. Unmodeled transformations remain conservative rather // than executing a formatter, interpreter, shell or user-supplied command. function pipelineSources(command, budget) { @@ -839,10 +851,7 @@ function pipelineSources(command, budget) { for (const word of words) budget.spend(word.value.length + 3); const name = basename(words[0]?.value || ''); if (name === 'echo') sources.push({ text: words.slice(1).filter(word => !/^-[neE]+$/.test(word.value)).map(word => word.value).join(' ') }); - if (name === 'printf') { - const format = words[1]?.value || ''; - if (format !== '-v') sources.push({ text: (format === '%s' || format === '%s\\n') ? words.slice(2).map(word => word.value).join('\n') : words.slice(1).map(word => word.value).join(' ') }); - } + if (name === 'printf' && words[1]?.value !== '-v') sources.push({ text: printfText(words) }); if (!DATA_COMMANDS.has(name)) { // Foreign transformations can introduce literal bypasses into executable // stdin. Treat their punctuation as delimiters, not as proved shell syntax. diff --git a/tests/hooks/block-no-verify.test.js b/tests/hooks/block-no-verify.test.js index 1bbec6131..383e97891 100644 --- a/tests/hooks/block-no-verify.test.js +++ b/tests/hooks/block-no-verify.test.js @@ -241,6 +241,8 @@ const executingPayloads = [ ['pipe group pipeline data to sh', "{ echo 'git push --no-verify' | grep git; } | sh"], ['pipe consecutive group writes to sh', "{ printf 'git pu'; printf 'sh --no-verify'; } | sh"], ['pipe echo -n and echo group writes to bash', "{ echo -n 'git commit '; echo '--no-verify -m x'; } | bash"], + ['pipe printf with a newline escape to sh', "printf 'git push --no-verify\\n' | sh"], + ['pipe split printf writes ending in a newline escape to sh', '{ printf "git "; printf "push --no-verify\\n"; } | sh'], ['execute data through xargs and bash -c', "printf '%s\\n' 'git commit -n -m x' | xargs -I CMD bash -c CMD"], ['execute command substitution text through bash', "echo '$(git commit -n -m x)' | bash"], ['execute bash here-string', "bash <<< 'git commit -n -m x'"], From 91fc34e6d6c506dab11d7567df7c7ab2fa0d5aa5 Mon Sep 17 00:00:00 2001 From: L4XB Date: Fri, 23 Oct 2026 07:45:32 +0200 Subject: [PATCH 4/7] fix(hooks): end echo output with its newline when group writes are joined echo writes a newline unless -n is given, so { echo 'git pu'; echo 'sh --no-verify'; } | sh feeds sh two separate lines. The joined read glued them into git push --no-verify and blocked the group. echoText() and printf's %s\n branch now end their text with the newline they write, while echo -n and printf without one still join directly. --- scripts/hooks/block-no-verify.js | 13 +++++++++++-- tests/hooks/block-no-verify.test.js | 1 + 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/scripts/hooks/block-no-verify.js b/scripts/hooks/block-no-verify.js index 01d215113..ea466fcd8 100644 --- a/scripts/hooks/block-no-verify.js +++ b/scripts/hooks/block-no-verify.js @@ -833,9 +833,18 @@ function groupSources(scope, budget) { // $'...' in shell-scan, only word boundaries are decoded, not numeric escapes. const PRINTF_SPACE_ESCAPES = { n: '\n', t: '\t', r: '\r', v: '\v', f: '\f', '\\': '\\' }; +// echo ends its output with a newline unless an -n option asks it not to, so +// two echoes in a group write two lines, not one joined command. +function echoText(words) { + const options = words.slice(1).filter(word => /^-[neE]+$/.test(word.value)); + const text = words.slice(1).filter(word => !options.includes(word)).map(word => word.value).join(' '); + return options.some(word => word.value.includes('n')) ? text : `${text}\n`; +} + function printfText(words) { const format = words[1]?.value || ''; - if (format === '%s' || format === '%s\\n') return words.slice(2).map(word => word.value).join('\n'); + if (format === '%s') return words.slice(2).map(word => word.value).join('\n'); + if (format === '%s\\n') return words.slice(2).map(word => `${word.value}\n`).join(''); const decoded = format.replace(/\\([ntrvf\\])/g, (escape, c) => PRINTF_SPACE_ESCAPES[c]); return [decoded, ...words.slice(2).map(word => word.value)].join(' '); } @@ -850,7 +859,7 @@ function pipelineSources(command, budget) { const { words } = executableWords(current.words, budget); for (const word of words) budget.spend(word.value.length + 3); const name = basename(words[0]?.value || ''); - if (name === 'echo') sources.push({ text: words.slice(1).filter(word => !/^-[neE]+$/.test(word.value)).map(word => word.value).join(' ') }); + if (name === 'echo') sources.push({ text: echoText(words) }); if (name === 'printf' && words[1]?.value !== '-v') sources.push({ text: printfText(words) }); if (!DATA_COMMANDS.has(name)) { // Foreign transformations can introduce literal bypasses into executable diff --git a/tests/hooks/block-no-verify.test.js b/tests/hooks/block-no-verify.test.js index 383e97891..16cd94397 100644 --- a/tests/hooks/block-no-verify.test.js +++ b/tests/hooks/block-no-verify.test.js @@ -284,6 +284,7 @@ const nonLeakingPayloads = [ ['allows the distinct executable with a literal escaped dot', '"git""\\.exe" commit -n -m x'], // A complete echo operand is data; accepted role repair intentionally corrects the authored broad-blocking expectation. ['allows quoted echo data (corrected author expectation)', 'echo "git commit -n"'], + ['allows two echo lines in a group piped to sh', "{ echo 'git pu'; echo 'sh --no-verify'; } | sh"], ['python heredoc string with later bash -n', 'python3 - <<\'PY\'\nold="git add -A\\nif ! git diff --cached --quiet; then\\n git commit -q -m \\"vault sync"\nPY\nbash -n vault-sync.sh'], ['assignment string with later bash -n', 'old="git commit -q -m x"; bash -n x.sh'], ['plain commit followed by later-line bash -n', 'git commit -m x\nbash -n s.sh'], From e7c5ce3870fdd87ae494c882db5565c420b75861 Mon Sep 17 00:00:00 2001 From: L4XB Date: Fri, 23 Oct 2026 08:01:23 +0200 Subject: [PATCH 5/7] fix(hooks): read echo options only before the first operand, and join printf %s arguments echo takes -n, -e and -E only before its first operand, so echo 'git commit -m x' -n prints git commit -m x -n and runs the bypass when it is piped into sh. The producer text removed the -n wherever it stood and lost the flag. printf '%s' reuses its format for every argument without a separator, so printf '%s' 'git pu' 'sh --no-verify' writes git push --no-verify, but the arguments were joined with newlines. Both passed on main as well. --- scripts/hooks/block-no-verify.js | 14 ++++++++------ tests/hooks/block-no-verify.test.js | 3 +++ 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/scripts/hooks/block-no-verify.js b/scripts/hooks/block-no-verify.js index ea466fcd8..45f38c5e8 100644 --- a/scripts/hooks/block-no-verify.js +++ b/scripts/hooks/block-no-verify.js @@ -833,17 +833,19 @@ function groupSources(scope, budget) { // $'...' in shell-scan, only word boundaries are decoded, not numeric escapes. const PRINTF_SPACE_ESCAPES = { n: '\n', t: '\t', r: '\r', v: '\v', f: '\f', '\\': '\\' }; -// echo ends its output with a newline unless an -n option asks it not to, so -// two echoes in a group write two lines, not one joined command. +// echo reads options only before its first operand, so `echo 'git commit' -n` +// prints `git commit -n`. Its output ends with a newline unless an -n option +// asks it not to, so two echoes in a group write two lines, not one command. function echoText(words) { - const options = words.slice(1).filter(word => /^-[neE]+$/.test(word.value)); - const text = words.slice(1).filter(word => !options.includes(word)).map(word => word.value).join(' '); - return options.some(word => word.value.includes('n')) ? text : `${text}\n`; + let first = 1; + while (first < words.length && /^-[neE]+$/.test(words[first].value)) first++; + const text = words.slice(first).map(word => word.value).join(' '); + return words.slice(1, first).some(word => word.value.includes('n')) ? text : `${text}\n`; } function printfText(words) { const format = words[1]?.value || ''; - if (format === '%s') return words.slice(2).map(word => word.value).join('\n'); + if (format === '%s') return words.slice(2).map(word => word.value).join(''); if (format === '%s\\n') return words.slice(2).map(word => `${word.value}\n`).join(''); const decoded = format.replace(/\\([ntrvf\\])/g, (escape, c) => PRINTF_SPACE_ESCAPES[c]); return [decoded, ...words.slice(2).map(word => word.value)].join(' '); diff --git a/tests/hooks/block-no-verify.test.js b/tests/hooks/block-no-verify.test.js index 16cd94397..9a338f8b8 100644 --- a/tests/hooks/block-no-verify.test.js +++ b/tests/hooks/block-no-verify.test.js @@ -243,6 +243,8 @@ const executingPayloads = [ ['pipe echo -n and echo group writes to bash', "{ echo -n 'git commit '; echo '--no-verify -m x'; } | bash"], ['pipe printf with a newline escape to sh', "printf 'git push --no-verify\\n' | sh"], ['pipe split printf writes ending in a newline escape to sh', '{ printf "git "; printf "push --no-verify\\n"; } | sh'], + ['pipe printf %s arguments joined to sh', "printf '%s' 'git pu' 'sh --no-verify' | sh"], + ['pipe echo with a trailing -n operand to sh', "echo 'git commit -m x' -n | sh"], ['execute data through xargs and bash -c', "printf '%s\\n' 'git commit -n -m x' | xargs -I CMD bash -c CMD"], ['execute command substitution text through bash', "echo '$(git commit -n -m x)' | bash"], ['execute bash here-string', "bash <<< 'git commit -n -m x'"], @@ -285,6 +287,7 @@ const nonLeakingPayloads = [ // A complete echo operand is data; accepted role repair intentionally corrects the authored broad-blocking expectation. ['allows quoted echo data (corrected author expectation)', 'echo "git commit -n"'], ['allows two echo lines in a group piped to sh', "{ echo 'git pu'; echo 'sh --no-verify'; } | sh"], + ['allows an echo -n operand that does not join two group lines', "{ echo 'git pu' -n; echo 'sh --no-verify'; } | sh"], ['python heredoc string with later bash -n', 'python3 - <<\'PY\'\nold="git add -A\\nif ! git diff --cached --quiet; then\\n git commit -q -m \\"vault sync"\nPY\nbash -n vault-sync.sh'], ['assignment string with later bash -n', 'old="git commit -q -m x"; bash -n x.sh'], ['plain commit followed by later-line bash -n', 'git commit -m x\nbash -n s.sh'], From bf71cdfff18372e3473c449f073107f582d596fc Mon Sep 17 00:00:00 2001 From: L4XB Date: Thu, 5 Nov 2026 12:27:55 +0100 Subject: [PATCH 6/7] fix(hooks): follow each producer's descriptors into the shell A group or pipeline producer counted whatever its redirections said, so `{ echo 'git push --no-verify' >/dev/null | cat; } | bash` was blocked although bash reads nothing, and printf filled no %s, so `printf 'git %s --no-verify\n' push | sh` got through. shell-scan now keeps a redirection's descriptor, and a stage's output counts only when its descriptors, the stages after it and an `exec` earlier in its group take it to the shell; an `exec` is carried only where it adds output. printf fills %s, %b and %c and reuses its format; other conversions are read like an unmodeled command. The header states what the model covers. --- scripts/hooks/block-no-verify.js | 199 +++++++++++++++++++++++----- scripts/hooks/lib/shell-scan.js | 12 +- tests/hooks/block-no-verify.test.js | 36 +++++ 3 files changed, 214 insertions(+), 33 deletions(-) diff --git a/scripts/hooks/block-no-verify.js b/scripts/hooks/block-no-verify.js index 45f38c5e8..acc4a3dd8 100644 --- a/scripts/hooks/block-no-verify.js +++ b/scripts/hooks/block-no-verify.js @@ -8,6 +8,17 @@ * Replaces the previous npx-based invocation that failed in pnpm-only projects * (EBADDEVENGINES) and could not be disabled via ECC_DISABLED_HOOKS. * + * Output piped into a shell (`... | sh`) is read through a static model, not + * run: literal echo and printf output (printf's %s, %b, %c and %% conversions, + * with the format reused for extra arguments), here-documents and here-strings, + * brace groups and subshells, and the redirections and `exec` that decide + * whether that output reaches the shell at all. A bypass built at run time is + * outside the model and is not blocked: values of variables and command + * substitutions, shell functions, other printf conversions (%d, a width or a + * precision), numeric escapes such as \x2d or \055, and transformations such as + * `base64 -d` or `rev`. The hook stops the usual ways an agent writes a bypass; + * it does not prove that none can run. + * * Exit codes: * 0 = allow (not a git command or no bypass flags) * 2 = block (bypass flag detected) @@ -815,14 +826,74 @@ function commandRole(words, budget) { return { kind: 'opaque', stdin: true }; } +// Where output goes, as far as the redirections show it: 'out' is the shell that +// reads the pipeline, 'next' the stdin of the next stage, 'away' a file, +// /dev/null or the terminal, and 'unknown' a target the scan cannot name, which +// is read as reaching the shell. +const TO_READER = new Map([['1', 'out'], ['2', 'away']]); + +function reaches(target, downstream) { + return target === 'out' || target === 'unknown' || (target === 'next' && downstream); +} + +// A file name that is one of the shell's own descriptors. +function fileTarget(path, fds) { + const fd = { '/dev/stdout': '1', '/dev/stderr': '2' }[path] ?? path.match(/^\/(?:dev\/fd|proc\/self\/fd)\/(\d+)$/)?.[1]; + return fd === undefined ? 'away' : (fds.get(fd) ?? 'away'); +} + +// The descriptors after a command's output redirections, applied left to right +// as the shell does: `2>&1 >/dev/null` sends stderr to the old stdout and +// stdout away. +function redirectedFds(fds, redirects, budget) { + const result = new Map(fds); + for (const redirect of redirects) { + budget.spend(); + const { operator, word } = redirect; + if (operator !== '>' && operator !== '>>' && operator !== '>&') continue; + const fd = redirect.fd ?? '1'; + const target = word?.value ?? ''; + if (!word || word.dynamic) { + result.set(fd, 'unknown'); + } else if (operator === '>&' && target === '-') { + result.set(fd, 'away'); + } else if (operator === '>&' && /^\d+$/.test(target)) { + // A descriptor that was never opened fails the command, so nothing is written. + result.set(fd, result.get(target) ?? 'away'); + } else { + const file = fileTarget(target, result); + result.set(fd, file); + // `>&file` sends stdout and stderr there, like `&>file`. + if (operator === '>&' && redirect.fd === null) result.set('2', file); + } + } + return result; +} + +// Commands that print without reading their stdin, so what is piped into them +// stops there. +const IGNORES_STDIN = new Set(['echo', 'printf', ':', 'true', 'false']); + // A brace group or subshell writes what its own commands print into the pipe: // `{ echo 'git push --no-verify'; } | sh` feeds sh like `echo ... | sh`. The // writes form one stream, so they are also read joined in order: -// `{ printf 'git pu'; printf 'sh --no-verify'; } | sh`. -function groupSources(scope, budget) { +// `{ printf 'git pu'; printf 'sh --no-verify'; } | sh`. An `exec` in the group +// changes the descriptors of the commands after it, but only where it sends +// more output to the shell: an `exec >/dev/null` that a condition skips must not +// hide what follows, so a redirection away is not carried. +function groupSources(scope, budget, fds) { const written = []; + let groupFds = fds; for (const inner of scanShell(scope.body, budget).commands) { - if (!inner.pipeTo) written.push(...pipelineSources(inner, budget)); + if (inner.words.length === 1 && inner.words[0].value === 'exec' && !inner.pipeFrom && !inner.pipeTo) { + const redirected = redirectedFds(groupFds, inner.redirects, budget); + groupFds = new Map(groupFds); + for (const [fd, target] of redirected) { + if (reaches(target, false) && !reaches(groupFds.get(fd), false)) groupFds.set(fd, target); + } + continue; + } + if (!inner.pipeTo) written.push(...pipelineSources(inner, budget, groupFds)); } if (written.length > 1) written.push({ text: written.map(source => source.text).join(''), opaque: written.some(source => source.opaque) }); return written; @@ -833,6 +904,10 @@ function groupSources(scope, budget) { // $'...' in shell-scan, only word boundaries are decoded, not numeric escapes. const PRINTF_SPACE_ESCAPES = { n: '\n', t: '\t', r: '\r', v: '\v', f: '\f', '\\': '\\' }; +function decodeSpaceEscapes(text) { + return text.replace(/\\([ntrvf\\])/g, (escape, c) => PRINTF_SPACE_ESCAPES[c]); +} + // echo reads options only before its first operand, so `echo 'git commit' -n` // prints `git commit -n`. Its output ends with a newline unless an -n option // asks it not to, so two echoes in a group write two lines, not one command. @@ -843,39 +918,103 @@ function echoText(words) { return words.slice(1, first).some(word => word.value.includes('n')) ? text : `${text}\n`; } +// What printf writes: each %s, %b and %c takes the next argument, and the format +// is used again while arguments remain, as printf does; %% is a percent sign, and +// a conversion with no argument left is empty. Null for any other conversion +// (%d, a width or a precision): printf is then read like an unmodeled command. function printfText(words) { - const format = words[1]?.value || ''; - if (format === '%s') return words.slice(2).map(word => word.value).join(''); - if (format === '%s\\n') return words.slice(2).map(word => `${word.value}\n`).join(''); - const decoded = format.replace(/\\([ntrvf\\])/g, (escape, c) => PRINTF_SPACE_ESCAPES[c]); - return [decoded, ...words.slice(2).map(word => word.value)].join(' '); + let index = 1; + if (words[index]?.value === '--') index++; + const format = words[index]?.value ?? ''; + const args = words.slice(index + 1).map(word => word.value); + const conversions = format.match(/%./g) || []; + if (conversions.some(conversion => !['%s', '%b', '%c', '%%'].includes(conversion))) return null; + const consumes = conversions.some(conversion => conversion !== '%%'); + let output = ''; + let next = 0; + do { + output += format.replace(/%(.)/g, (conversion, kind) => { + if (kind === '%') return '%'; + const arg = args[next++] ?? ''; + if (kind === 'c') return arg.slice(0, 1); + return kind === 'b' ? decodeSpaceEscapes(arg) : arg; + }); + } while (consumes && next < args.length); + return decodeSpaceEscapes(output); +} + +// A group writes to where its stage's descriptors point: the shell, or away. +function groupContext(stageFds, downstream) { + const fds = new Map(); + for (const [fd, target] of stageFds) { + if (!reaches(target, downstream)) fds.set(fd, 'away'); + else fds.set(fd, target === 'unknown' ? 'unknown' : 'out'); + } + return fds; +} + +// Foreign transformations can introduce literal bypasses into executable stdin. +// Treat their punctuation as delimiters, not as proved shell syntax. This +// deliberately may refuse a transformation that removes a bypass; it does not +// evaluate sed/interpreters or detect arbitrary generated source. +function opaqueSource(words, budget) { + const text = words.map(word => word.value).join(' '); + budget.spend(2 * text.length + 1); + return { text: text.replace(/[^\w$=.+-]/g, ' '), opaque: true }; +} + +// The text echo or printf writes; null for a printf conversion the model lacks, +// undefined for any other command. +function literalText(name, words) { + if (name === 'echo') return echoText(words); + if (name === 'printf' && words[1]?.value !== '-v') return printfText(words); + return undefined; +} + +function hereSources(redirects) { + const sources = []; + for (const redirect of redirects) { + if (redirect.operator === '<<<') sources.push({ text: redirect.word.value }); + else if (redirect.operator === '<<' || redirect.operator === '<<-') sources.push({ text: redirect.body }); + } + return sources; +} + +// What one pipeline stage adds to the shell's input, and whether what is piped +// into the stage gets there too. +function stageSources(current, budget, stageFds, downstream) { + const sources = []; + const stdoutReaches = reaches(stageFds.get('1'), downstream); + if (current.closedScope) sources.push(...groupSources(current.closedScope, budget, groupContext(stageFds, downstream))); + budget.spend(current.words.length + 1); + const { words } = executableWords(current.words, budget); + for (const word of words) budget.spend(word.value.length + 3); + const name = basename(words[0]?.value || ''); + const literal = literalText(name, words); + if (stdoutReaches && typeof literal === 'string') sources.push({ text: literal }); + // An unmodeled command may also write to stderr, so either stream counts. + const unmodeled = !DATA_COMMANDS.has(name) || literal === null; + if (unmodeled && (stdoutReaches || reaches(stageFds.get('2'), downstream))) sources.push(opaqueSource(words, budget)); + const forwards = stdoutReaches && !IGNORES_STDIN.has(name); + if (forwards) sources.push(...hereSources(current.redirects)); + // Its own stdin redirection replaces the pipe, so the stage before it feeds nothing. + const ownStdin = current.redirects.some(redirect => redirect.operator.startsWith('<') && (redirect.fd ?? '0') === '0'); + return { sources, downstream: forwards && !ownStdin }; } // Literal producers only. Unmodeled transformations remain conservative rather -// than executing a formatter, interpreter, shell or user-supplied command. -function pipelineSources(command, budget) { +// than executing a formatter, interpreter, shell or user-supplied command. A +// stage counts only when its output reaches the shell: through the stages after +// it, unless a redirection sends it elsewhere, as in `echo ... >/dev/null | cat`. +function pipelineSources(command, budget, fds = TO_READER) { const sources = []; + // Whether what is written into the current stage's stdin gets to the shell. + let downstream = true; for (let current = command; current; current = current.pipeFrom) { - if (current.closedScope) sources.push(...groupSources(current.closedScope, budget)); - budget.spend(current.words.length + 1); - const { words } = executableWords(current.words, budget); - for (const word of words) budget.spend(word.value.length + 3); - const name = basename(words[0]?.value || ''); - if (name === 'echo') sources.push({ text: echoText(words) }); - if (name === 'printf' && words[1]?.value !== '-v') sources.push({ text: printfText(words) }); - if (!DATA_COMMANDS.has(name)) { - // Foreign transformations can introduce literal bypasses into executable - // stdin. Treat their punctuation as delimiters, not as proved shell syntax. - // This deliberately may refuse a transformation that removes a bypass; it - // does not evaluate sed/interpreters or detect arbitrary generated source. - const text = words.map(word => word.value).join(' '); - budget.spend(2 * text.length + 1); - sources.push({ text: text.replace(/[^\w$=.+-]/g, ' '), opaque: true }); - } - for (const redirect of current.redirects) { - if (redirect.operator === '<<<') sources.push({ text: redirect.word.value }); - else if (redirect.operator === '<<' || redirect.operator === '<<-') sources.push({ text: redirect.body }); - } + const stageFds = redirectedFds(current === command ? fds : new Map([...fds, ['1', 'next']]), current.redirects, budget); + const stage = stageSources(current, budget, stageFds, downstream); + sources.push(...stage.sources); + downstream = stage.downstream; } return sources; } diff --git a/scripts/hooks/lib/shell-scan.js b/scripts/hooks/lib/shell-scan.js index 953833485..1885d095f 100644 --- a/scripts/hooks/lib/shell-scan.js +++ b/scripts/hooks/lib/shell-scan.js @@ -343,10 +343,12 @@ function flushWord(scan) { word.end = scan.i; word.raw = scan.input.slice(word.start, scan.i); if (scan.pendingRedirect) { - const redirect = { operator: scan.pendingRedirect, word, body: '' }; + // `fd` is the descriptor written before the operator (`2>&1`), or null for the default. + const redirect = { operator: scan.pendingRedirect, word, body: '', fd: scan.pendingDescriptor ?? null }; current.redirects.push(redirect); if (scan.pendingRedirect === '<<' || scan.pendingRedirect === '<<-') scan.pendingHeredocs.push({ redirect, owner: current }); scan.pendingRedirect = null; + scan.pendingDescriptor = null; } else current.words.push(word); scan.word = null; } @@ -480,8 +482,11 @@ function scanOperator(scan, c, braceKeyword) { } // An immediately adjacent numeric word is a descriptor, not an argv word. +// A word of digits right before a redirection operator is its descriptor, not an argument. function dropDescriptorWord(scan) { - if (scan.word && /^\d+$/.test(scan.word.value)) scan.word = null; + const descriptor = scan.word && /^\d+$/.test(scan.word.value) ? scan.word.value : null; + if (descriptor !== null) scan.word = null; + return descriptor; } function queueHeredoc(scan, delimiter) { @@ -509,8 +514,9 @@ function scanRedirect(scan, c) { const delimiter = heredocDelimiter(input, scan.i, scan.budget); if (delimiter) { queueHeredoc(scan, delimiter); return; } } - dropDescriptorWord(scan); + const descriptor = dropDescriptorWord(scan); flushWord(scan); + scan.pendingDescriptor = descriptor; scan.pendingRedirect = redirectOperator(input, scan.i, c); scan.i += scan.pendingRedirect.length; } diff --git a/tests/hooks/block-no-verify.test.js b/tests/hooks/block-no-verify.test.js index 9a338f8b8..793660fb5 100644 --- a/tests/hooks/block-no-verify.test.js +++ b/tests/hooks/block-no-verify.test.js @@ -2218,6 +2218,42 @@ for (const [name, command, expected] of sedCases) { })) passed++; else failed++; } +// --- Producer output follows the shell's descriptors (checked against bash) --- + +// Each command ends in `| bash` or `| sh`. With that receiver replaced by `cat`, +// bash itself shows what the shell would have read, and the hook must block +// exactly when that text runs `git push` or `git commit` with --no-verify. +const producerCases = [ + "{ exec 2>&1; echo 'git push --no-verify' >&2; } | bash", + "( exec 3>&1; echo 'git push --no-verify' >&3 ) | sh", + "{ echo 'git push --no-verify' >&2; } 2>&1 | bash", + "{ echo 'git push --no-verify' >&2; } | bash", + "{ echo 'git push --no-verify' >/dev/null | cat; } | bash", + "echo 'git push --no-verify' >&2 | bash", + "echo 'git push --no-verify' 2>&1 >/dev/null | bash", + "echo 'git push --no-verify' >/dev/stdout | bash", + "echo 'git push --no-verify' | cat >/dev/null | bash", + "echo 'git push --no-verify' | echo hi | bash", + "echo 'git push --no-verify' | cat <<< 'ls' | bash", + "printf 'git %s --no-verify\\n' push | sh", + "printf -- 'git %b --no-verify\\n' push | sh", + "printf '%s %s %s\\n' git push --no-verify | sh", + "printf 'git push %s\\n' --no-verify | sh", + "printf '%%s git push\\n' | sh", + "{ printf 'git pu'; printf 'sh --no-verify'; } | sh", +]; +if (process.platform !== 'win32') { + for (const command of producerCases) { + if (test(`producer output as bash reads it: ${command}`, () => { + const shown = command.replace(/\|\s*(bash|sh)$/, '| cat'); + const bash = spawnSync('/bin/bash', ['-c', shown], { encoding: 'utf8' }); + assert.strictEqual(bash.status, 0, bash.stderr); + const bypass = /\bgit\s+(?:push|commit)\b[^\n]*\s--no-verify\b/.test(bash.stdout); + assert.strictEqual(runHook({ tool_input: { command } }).code, bypass ? 2 : 0, `bash reads ${JSON.stringify(bash.stdout)}`); + })) passed++; else failed++; + } +} + console.log('─'.repeat(50)); console.log(`Passed: ${passed} Failed: ${failed}`); From 31866936b9a0b5a4ffcb6af170ac46bfe24f5806 Mon Sep 17 00:00:00 2001 From: L4XB Date: Thu, 5 Nov 2026 12:59:29 +0100 Subject: [PATCH 7/7] fix(hooks): keep printf arguments literal, model descriptor copies, charge expansion printf decoded escapes in %s arguments too, so `printf '%s' 'gi\t push --no-verify' | sh` read as `gi push` and passed, while the shell reads `gi\t` as `git`. Escapes are now decoded in the format and in %b arguments only. `1<&3` and the move `1>&3-` copy a descriptor like `1>&3`, so restoring a saved stdout is followed. The format-reuse loop charges the work budget before each piece, so a short command cannot expand into a huge string. The bash-backed tests now run what the shell reads with a stub git, which also sees how the shell unescapes words. --- scripts/hooks/block-no-verify.js | 87 +++++++++++++++++++---------- tests/hooks/block-no-verify.test.js | 28 +++++++--- 2 files changed, 77 insertions(+), 38 deletions(-) diff --git a/scripts/hooks/block-no-verify.js b/scripts/hooks/block-no-verify.js index acc4a3dd8..c6844d26a 100644 --- a/scripts/hooks/block-no-verify.js +++ b/scripts/hooks/block-no-verify.js @@ -844,32 +844,48 @@ function fileTarget(path, fds) { // The descriptors after a command's output redirections, applied left to right // as the shell does: `2>&1 >/dev/null` sends stderr to the old stdout and -// stdout away. +// stdout away. `N>&M` and `N<&M` both make N a copy of M, and `N>&M-` moves M +// to N and closes M. function redirectedFds(fds, redirects, budget) { const result = new Map(fds); for (const redirect of redirects) { budget.spend(); - const { operator, word } = redirect; - if (operator !== '>' && operator !== '>>' && operator !== '>&') continue; - const fd = redirect.fd ?? '1'; - const target = word?.value ?? ''; - if (!word || word.dynamic) { - result.set(fd, 'unknown'); - } else if (operator === '>&' && target === '-') { - result.set(fd, 'away'); - } else if (operator === '>&' && /^\d+$/.test(target)) { - // A descriptor that was never opened fails the command, so nothing is written. - result.set(fd, result.get(target) ?? 'away'); - } else { - const file = fileTarget(target, result); - result.set(fd, file); - // `>&file` sends stdout and stderr there, like `&>file`. - if (operator === '>&' && redirect.fd === null) result.set('2', file); - } + applyRedirect(result, redirect); } return result; } +// A descriptor that was never opened fails the command, so nothing is written. +function copyDescriptor(fds, fd, source, move) { + const target = fds.get(source) ?? 'away'; + if (move && source !== fd) fds.set(source, 'away'); + fds.set(fd, target); +} + +function applyRedirect(fds, redirect) { + const { operator, word } = redirect; + const copies = operator === '>&' || (operator === '<&' && redirect.fd !== null); + if (operator !== '>' && operator !== '>>' && !copies) return; + const fd = redirect.fd ?? '1'; + const target = word?.value ?? ''; + const copied = copies ? /^(\d+)(-?)$/.exec(target) : null; + if (!word || word.dynamic) { + fds.set(fd, 'unknown'); + } else if (copies && target === '-') { + fds.set(fd, 'away'); + } else if (copied) { + copyDescriptor(fds, fd, copied[1], copied[2] === '-'); + } else if (operator === '<&') { + // Not a descriptor: the scan does not model what this reads into an output. + fds.set(fd, 'unknown'); + } else { + const file = fileTarget(target, fds); + fds.set(fd, file); + // `>&file` sends stdout and stderr there, like `&>file`. + if (operator === '>&' && redirect.fd === null) fds.set('2', file); + } +} + // Commands that print without reading their stdin, so what is piped into them // stops there. const IGNORES_STDIN = new Set(['echo', 'printf', ':', 'true', 'false']); @@ -920,27 +936,36 @@ function echoText(words) { // What printf writes: each %s, %b and %c takes the next argument, and the format // is used again while arguments remain, as printf does; %% is a percent sign, and -// a conversion with no argument left is empty. Null for any other conversion -// (%d, a width or a precision): printf is then read like an unmodeled command. -function printfText(words) { +// a conversion with no argument left is empty. Escapes are decoded in the format +// and in %b arguments only: %s and %c write their argument as it is. Null for any +// other conversion (%d, a width or a precision): printf is then read like an +// unmodeled command. The budget is charged before each piece is added, so a +// short command cannot expand into a huge string. +function printfText(words, budget) { let index = 1; if (words[index]?.value === '--') index++; const format = words[index]?.value ?? ''; const args = words.slice(index + 1).map(word => word.value); - const conversions = format.match(/%./g) || []; + const pieces = format.split(/(%.)/); + const conversions = pieces.filter((piece, i) => i % 2 === 1); if (conversions.some(conversion => !['%s', '%b', '%c', '%%'].includes(conversion))) return null; const consumes = conversions.some(conversion => conversion !== '%%'); let output = ''; let next = 0; do { - output += format.replace(/%(.)/g, (conversion, kind) => { - if (kind === '%') return '%'; - const arg = args[next++] ?? ''; - if (kind === 'c') return arg.slice(0, 1); - return kind === 'b' ? decodeSpaceEscapes(arg) : arg; + pieces.forEach((piece, i) => { + let text; + if (i % 2 === 0) text = decodeSpaceEscapes(piece); + else if (piece === '%%') text = '%'; + else { + const arg = args[next++] ?? ''; + text = piece === '%c' ? arg.slice(0, 1) : (piece === '%b' ? decodeSpaceEscapes(arg) : arg); + } + budget.spend(text.length + 1); + output += text; }); } while (consumes && next < args.length); - return decodeSpaceEscapes(output); + return output; } // A group writes to where its stage's descriptors point: the shell, or away. @@ -965,9 +990,9 @@ function opaqueSource(words, budget) { // The text echo or printf writes; null for a printf conversion the model lacks, // undefined for any other command. -function literalText(name, words) { +function literalText(name, words, budget) { if (name === 'echo') return echoText(words); - if (name === 'printf' && words[1]?.value !== '-v') return printfText(words); + if (name === 'printf' && words[1]?.value !== '-v') return printfText(words, budget); return undefined; } @@ -990,7 +1015,7 @@ function stageSources(current, budget, stageFds, downstream) { const { words } = executableWords(current.words, budget); for (const word of words) budget.spend(word.value.length + 3); const name = basename(words[0]?.value || ''); - const literal = literalText(name, words); + const literal = literalText(name, words, budget); if (stdoutReaches && typeof literal === 'string') sources.push({ text: literal }); // An unmodeled command may also write to stderr, so either stream counts. const unmodeled = !DATA_COMMANDS.has(name) || literal === null; diff --git a/tests/hooks/block-no-verify.test.js b/tests/hooks/block-no-verify.test.js index 793660fb5..328571993 100644 --- a/tests/hooks/block-no-verify.test.js +++ b/tests/hooks/block-no-verify.test.js @@ -2220,9 +2220,10 @@ for (const [name, command, expected] of sedCases) { // --- Producer output follows the shell's descriptors (checked against bash) --- -// Each command ends in `| bash` or `| sh`. With that receiver replaced by `cat`, -// bash itself shows what the shell would have read, and the hook must block -// exactly when that text runs `git push` or `git commit` with --no-verify. +// Each command ends in `| bash` or `| sh`. That receiver is replaced by a bash +// that runs what it reads with `git` defined as a function printing its +// arguments, and the hook must block exactly when the shell would have run +// `git push` or `git commit` with --no-verify. const producerCases = [ "{ exec 2>&1; echo 'git push --no-verify' >&2; } | bash", "( exec 3>&1; echo 'git push --no-verify' >&3 ) | sh", @@ -2241,19 +2242,32 @@ const producerCases = [ "printf 'git push %s\\n' --no-verify | sh", "printf '%%s git push\\n' | sh", "{ printf 'git pu'; printf 'sh --no-verify'; } | sh", + "printf '%s' 'gi\\t push --no-verify' | sh", + "printf '%b' 'git push\\t--no-verify' | sh", + "echo 'git push --no-verify' 3>&1 >/dev/null 1<&3 | sh", + "echo 'git push --no-verify' 3>&1 >/dev/null 1>&3- | sh", ]; if (process.platform !== 'win32') { for (const command of producerCases) { if (test(`producer output as bash reads it: ${command}`, () => { - const shown = command.replace(/\|\s*(bash|sh)$/, '| cat'); + const shown = command.replace(/\|\s*(bash|sh)$/, '| /bin/bash -c \'git() { echo "GIT $*"; }; . /dev/stdin\''); const bash = spawnSync('/bin/bash', ['-c', shown], { encoding: 'utf8' }); - assert.strictEqual(bash.status, 0, bash.stderr); - const bypass = /\bgit\s+(?:push|commit)\b[^\n]*\s--no-verify\b/.test(bash.stdout); - assert.strictEqual(runHook({ tool_input: { command } }).code, bypass ? 2 : 0, `bash reads ${JSON.stringify(bash.stdout)}`); + const bypass = /^GIT (?:push|commit)\b.*\s--no-verify\b/m.test(bash.stdout); + assert.strictEqual(runHook({ tool_input: { command } }).code, bypass ? 2 : 0, `the shell runs ${JSON.stringify(bash.stdout)}`); })) passed++; else failed++; } } +if (test('printf format reuse is charged to the work budget before it expands', () => { + // About 4 KB of command expands to about 2 MB when the format is reused per argument. + const command = `printf '${'x'.repeat(2000)}%s' ${Array(1000).fill('a').join(' ')} | sh`; + const started = Date.now(); + const result = runHook({ tool_input: { command } }); + assert.strictEqual(result.code, 2); + assert.match(result.stderr, /work budget exceeded/); + assert.ok(Date.now() - started < 2000, 'expected the budget to stop the expansion early'); +})) passed++; else failed++; + console.log('─'.repeat(50)); console.log(`Passed: ${passed} Failed: ${failed}`);