diff --git a/scripts/hooks/block-no-verify.js b/scripts/hooks/block-no-verify.js index 5b1cfcee2..c6844d26a 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,33 +826,220 @@ function commandRole(words, budget) { return { kind: 'opaque', stdin: true }; } -// Literal producers only. Unmodeled transformations remain conservative rather -// than executing a formatter, interpreter, shell or user-supplied command. -function pipelineSources(command, budget) { +// 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. `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(); + 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']); + +// 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`. 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.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; +} + +// 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 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. +function echoText(words) { + 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`; +} + +// 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. 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 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 { + 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 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, budget) { + if (name === 'echo') return echoText(words); + if (name === 'printf' && words[1]?.value !== '-v') return printfText(words, budget); + 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, 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; + 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. 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) { - 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: 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 (!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 94a23a32a..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; } @@ -449,12 +451,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; @@ -477,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) { @@ -506,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 2cf25a1e1..328571993 100644 --- a/tests/hooks/block-no-verify.test.js +++ b/tests/hooks/block-no-verify.test.js @@ -235,6 +235,16 @@ 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"], + ['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'], + ['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'"], @@ -276,6 +286,8 @@ 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"], + ['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'], @@ -2206,6 +2218,56 @@ 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`. 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", + "{ 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", + "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)$/, '| /bin/bash -c \'git() { echo "GIT $*"; }; . /dev/stdin\''); + const bash = spawnSync('/bin/bash', ['-c', shown], { encoding: 'utf8' }); + 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}`);