Skip to content

Commit a6385a7

Browse files
bradygasterCopilot
andcommitted
test(ci): reject continued and dynamic compiler installers
Closes #2103 Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
1 parent 332efec commit a6385a7

1 file changed

Lines changed: 123 additions & 7 deletions

File tree

‎test/gh-aw-quality.test.ts‎

Lines changed: 123 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3534,6 +3534,72 @@ describe('gh-aw: CI compiler pin contract', () => {
35343534
},
35353535
] as const;
35363536

3537+
// This is a deliberately bounded recognizer, not a general shell parser.
3538+
// Unsupported here-documents fail closed; only literal cat-to-stdout is inert.
3539+
function shellCommands(run: string): string[] {
3540+
const commands: string[] = [];
3541+
let command = '';
3542+
let quote = '';
3543+
let wordStart = true;
3544+
for (let i = 0; i < run.length; i++) {
3545+
const char = run[i];
3546+
if (char === '\\' && quote !== "'") {
3547+
const next = run[++i];
3548+
if (next === undefined) throw new Error('Dangling shell escape');
3549+
if (next !== '\n' || quote) {
3550+
command += char + next;
3551+
wordStart = false;
3552+
}
3553+
} else if (char === quote) {
3554+
quote = '';
3555+
command += char;
3556+
} else if (!quote && (char === "'" || char === '"')) {
3557+
quote = char;
3558+
command += char;
3559+
wordStart = false;
3560+
} else if (!quote && char === '#' && wordStart) {
3561+
while (i + 1 < run.length && run[i + 1] !== '\n') i++;
3562+
} else if (!quote && char === '\n') {
3563+
const heredoc = command.match(/^cat <<(['"])([A-Za-z_][A-Za-z0-9_]*)\1$/);
3564+
if (heredoc) {
3565+
const end = run.indexOf(`\n${heredoc[2]}\n`, i);
3566+
if (end === -1) throw new Error('Unterminated literal heredoc');
3567+
command += run.slice(i, end + heredoc[2].length + 1);
3568+
i = end + heredoc[2].length + 1;
3569+
}
3570+
if (command.trim()) commands.push(command.trim());
3571+
command = '';
3572+
wordStart = true;
3573+
} else {
3574+
command += char;
3575+
if (!quote) wordStart = /[\s;|&()<>]/.test(char);
3576+
}
3577+
}
3578+
if (quote) throw new Error('Unterminated shell quote');
3579+
if (command.trim()) commands.push(command.trim());
3580+
return commands;
3581+
}
3582+
3583+
function isInstallerOrAmbiguous(command: string): boolean {
3584+
if (/^cat <<(['"])([A-Za-z_][A-Za-z0-9_]*)\1\n[\s\S]*\n\2$/.test(command)) return false;
3585+
if (/^(?:echo|printf)(?:\s+(?:'[^']*'|"(?:\\[\s\S]|[^"\\$`])*"))+$/.test(command)) return false;
3586+
// Remove word-splicing quotes/escapes for detection only, never for approval.
3587+
// A repository operand alone is enough: dynamic command/argument construction
3588+
// must not evade detection merely by hiding "gh extension install".
3589+
const visible = command.replace(/['"\\]/g, '');
3590+
const syntax = command.replace(/'[^']*'|"(?:\\[\s\S]|[^"\\])*"|\\[\s\S]/g,
3591+
token => token.startsWith('"') && /[$`]/.test(token) ? '$' : 'literal');
3592+
const dynamicCommand = '(?![A-Za-z_]\\w*=)[^\\s;&|()]*[$`]';
3593+
return /github\/gh-aw|\bgh\s+(?:extension|ext)\b/.test(visible) ||
3594+
/\bgh\s+[^\s]*[$`]/.test(visible) ||
3595+
new RegExp(`(?:^|[;&|(){}\\n])\\s*(?:(?:if|then|else|do|!|command|exec|env|sudo|--?[\\w-]+|[A-Za-z_]\\w*=[^()\\s]+)\\s+)*${dynamicCommand}`).test(syntax) ||
3596+
new RegExp(`(?:\\$\\(|\`)\\s*${dynamicCommand}`).test(visible) ||
3597+
/\b(?:command|exec|env|sudo)\s[^;\n]*[$`]/.test(syntax) ||
3598+
/(?:^|[;&|()\n])\s*(?:eval|source|\.)\s/.test(syntax) ||
3599+
/\b(?:bash|sh|zsh|ksh)\s+.*(?:-[a-z]*c\b|[$`])/.test(visible) ||
3600+
/<</.test(syntax) || /\\\n/.test(command);
3601+
}
3602+
35373603
function assertCompilerPins(source: string): void {
35383604
const workflow = parseDocument(source);
35393605
expect(workflow.errors).toEqual([]);
@@ -3550,17 +3616,12 @@ describe('gh-aw: CI compiler pin contract', () => {
35503616
const run = step.get('run');
35513617
if (run === undefined) continue;
35523618
if (typeof run !== 'string') throw new Error('CI run must be a string');
3553-
const commands = run.split('\n').map(line => line.replace(
3554-
/'[^']*'|"(?:\\.|[^"\\])*"|\\.|(^|\s)#.*$/g,
3555-
(token, comment) => comment === undefined ? token : '',
3556-
).trim()).filter(Boolean);
3619+
const commands = shellCommands(`${run}\n`);
35573620
const name = step.get('name');
35583621
// Include named slots even if their install was removed or replaced by output.
35593622
// Unexpected install-like scripts fail closed; never accept a shell wrapper
35603623
// merely because it contains the pinned command as a substring.
3561-
if (installs.some(install => install.name === name) || commands.some(line =>
3562-
!/^(?:echo|printf)(?:\s+(?:'[^']*'|"[^"$`]*"))+$/.test(line) &&
3563-
/\bgh\s+(?:extension|ext)\s+install\b.*\bgithub\/gh-aw\b/.test(line))) {
3624+
if (installs.some(install => install.name === name) || commands.some(isInstallerOrAmbiguous)) {
35643625
installSteps.push({ job: String(key), name, commands });
35653626
}
35663627
}
@@ -3598,6 +3659,26 @@ describe('gh-aw: CI compiler pin contract', () => {
35983659
assertCompilerPins(workflow.toString());
35993660
});
36003661

3662+
it.each([
3663+
['continued comment', '# gh extension \\\necho "install github/gh-aw"'],
3664+
['single-quoted backslash', "printf '%s' 'gh extension \\\ninstall github/gh-aw'"],
3665+
['double-quoted backslash', 'echo "gh extension \\\ninstall github/gh-aw"'],
3666+
['escaped backslash', String.raw`echo "gh extension \\"`],
3667+
['literal heredoc', "cat <<'INSTALL'\ngh extension \\\ninstall github/gh-aw\nINSTALL"],
3668+
['heredoc quote and comment text', "cat <<'INSTALL'\n' \" # \\\n$(gh extension install github/gh-aw)\nINSTALL"],
3669+
])('ignores inert %s without folding literal text', (_label, run) => {
3670+
const workflow = parseDocument(ci);
3671+
workflow.addIn(['jobs', 'changes', 'steps'], { name: 'Literal example', run });
3672+
assertCompilerPins(workflow.toString());
3673+
if (!run.startsWith('#')) expect(shellCommands(`${run}\n`)).toEqual([run]);
3674+
});
3675+
3676+
it('accepts an unquoted continuation of the standalone approved command', () => {
3677+
assertCompilerPins(mutateInstall('test', step => {
3678+
step.set('run', installs[0].commands[0].replace('extension ', 'extension \\\n'));
3679+
}));
3680+
});
3681+
36013682
describe.each(installs)('$job install step', ({ job, commands }) => {
36023683
it.each([
36033684
['unpinned', 'gh extension install github/gh-aw'],
@@ -3611,6 +3692,10 @@ describe('gh-aw: CI compiler pin contract', () => {
36113692
['preceding command', `echo preparing\n${commands[0]}`],
36123693
['appended command', `${commands[0]}; gh extension install github/gh-aw`],
36133694
['heredoc text', `cat <<'INSTALL'\n${commands[0]}\nINSTALL`],
3695+
['quoted continuation', `'${commands[0].replace('extension ', 'extension \\\n')}'`],
3696+
['escaped backslash is not continuation', commands[0].replace('extension ', 'extension \\\\\n')],
3697+
['comment backslash is not continuation', commands[0].replace('extension ', 'extension # comment \\\n')],
3698+
['space after backslash is not continuation', commands[0].replace('extension ', 'extension \\ \n')],
36143699
])('rejects %s', (_label, command) => {
36153700
const source = mutateInstall(job, step => {
36163701
step.set('run', [command, ...commands.slice(1)].join('\n'));
@@ -3643,6 +3728,37 @@ describe('gh-aw: CI compiler pin contract', () => {
36433728
['extra wrapped install', 'changes', `bash -c '${commands[0]}'`],
36443729
['extra install after quoted hash', 'changes', `echo "#"; ${commands[0]}`],
36453730
['extra install in output substitution', 'changes', `echo "$(${commands[0]})"`],
3731+
['continued installer', 'changes', 'gh extension \\\ninstall github/gh-aw'],
3732+
['continued command word', 'changes', 'g\\\nh ext\\\nension install github/gh-aw'],
3733+
['continued repository', 'changes', 'gh extension install github/gh-\\\naw'],
3734+
['continued wrapper', 'changes', 'command gh extension \\\ninstall github/gh-aw'],
3735+
['continued conditional', 'changes', 'if true; then gh extension \\\ninstall github/gh-aw; fi'],
3736+
['continued substitution', 'changes', 'echo "$(gh extension \\\ninstall github/gh-aw)"'],
3737+
['backtick substitution', 'changes', 'echo "`gh extension install github/gh-aw`"'],
3738+
['quoted word splicing', 'changes', `g'h' exten"sion" install github/gh-"aw"`],
3739+
['dynamic command', 'changes', 'cli=gh\n"$cli" extension install github/gh-aw'],
3740+
['dynamic arguments', 'changes', 'gh "$verb" "$action" "$repository"'],
3741+
['partly dynamic subcommand', 'changes', 'gh exten"$suffix" "$action" "$repository"'],
3742+
['partly dynamic executable', 'changes', 'g"$suffix" "$verb" "$action" "$repository"'],
3743+
['fully dynamic command', 'changes', '"$INSTALLER"'],
3744+
['dynamic command in group', 'changes', '{ "$INSTALLER"; }'],
3745+
['dynamic command in substitution', 'changes', 'echo "$("$INSTALLER")"'],
3746+
['dynamic command in backticks', 'changes', 'echo "`$INSTALLER`"'],
3747+
['dynamic command after assignment', 'changes', 'ENV=value "$INSTALLER"'],
3748+
['dynamic env wrapper', 'changes', 'env -i "$INSTALLER"'],
3749+
['dynamic wrapper with option operand', 'changes', 'env -u PATH "$INSTALLER"'],
3750+
['dynamic shell wrapper', 'changes', 'bash -c "$INSTALLER"'],
3751+
['eval wrapper', 'changes', 'eval "$INSTALLER"'],
3752+
['unquoted heredoc substitution', 'changes', 'cat <<INSTALL\n$(gh extension \\\ninstall github/gh-aw)\nINSTALL'],
3753+
['executable heredoc', 'changes', "bash <<'INSTALL'\ngh extension \\\ninstall github/gh-aw\nINSTALL"],
3754+
['literal piped to shell', 'changes', `printf '%s' '${commands[0]}' | bash`],
3755+
['literal heredoc piped to shell', 'changes', "cat <<'INSTALL' | bash\ngh extension \\\ninstall github/gh-aw\nINSTALL"],
3756+
['installer after literal heredoc', 'changes', "cat <<'INSTALL'\nliteral \\\nINSTALL\ngh extension \\\ninstall github/gh-aw"],
3757+
['installer after comment backslash', 'changes', '# example \\\ngh extension install github/gh-aw'],
3758+
['installer after quoted backslash', 'changes', "echo '\\'\ngh extension install github/gh-aw"],
3759+
['installer after escaped backslash', 'changes', 'echo \\\\\ngh extension install github/gh-aw'],
3760+
['installer after escaped space hash', 'changes', 'echo \\ #; gh extension install github/gh-aw'],
3761+
['double-quoted continuations', 'changes', '"g\\\nh" "exten\\\nsion" install "github/gh-\\\naw"'],
36463762
])('rejects %s', (_label, targetJob, command) => {
36473763
const workflow = parseDocument(ci);
36483764
workflow.addIn(['jobs', targetJob, 'steps'], {

0 commit comments

Comments
 (0)