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}`);