Skip to content
31 changes: 27 additions & 4 deletions scripts/hooks/block-no-verify.js
Original file line number Diff line number Diff line change
Expand Up @@ -815,20 +815,43 @@ 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));
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
}
if (written.length > 1) written.push({ text: written.map(source => source.text).join(''), opaque: written.some(source => source.opaque) });
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
Comment thread
greptile-apps[bot] marked this conversation as resolved.
Resolved
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');
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
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) {
const sources = [];
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: 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.
Expand Down
5 changes: 4 additions & 1 deletion scripts/hooks/lib/shell-scan.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
8 changes: 8 additions & 0 deletions tests/hooks/block-no-verify.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,14 @@ 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'],
['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'"],
Expand Down