mirror of
https://github.com/affaan-m/ECC.git
synced 2026-11-17 05:22:17 +00:00
fix(hooks): read NUL-ended delimiters, Bash blanks before #, and long bodies
Bash ends a decoded $'...' string at its first NUL, so `cat <<$'EOF\0X'` ends at the line EOF, but the splitter looked for EOF\0X and swallowed the commands after the body. A carriage return or a no-break space is not a blank to Bash, so `echo foo\r# && npm run dev` runs the dev server, but the splitter read the # as a comment. And spreading a long substitution's segments into push() threw RangeError, which the hook's catch turned into an allow. Truncate the decoded part at the NUL, use Bash's blanks for word starts, and append the segments one at a time.
This commit is contained in:
4 files changed
+35
-4
No files matched your search
@@ -204,8 +204,12 @@ function collectCheckSegments(cmd) {
|
||||
|
||||
for (const body of [...extractCommandSubstitutions(current), ...extractSubshellGroups(current)]) {
|
||||
const bodySegments = splitShellSegments(body);
|
||||
segments.push(...bodySegments);
|
||||
queue.push(body, ...bodySegments);
|
||||
// One at a time: spreading a long command's segments into push() exceeds the argument limit.
|
||||
queue.push(body);
|
||||
for (const segment of bodySegments) {
|
||||
segments.push(segment);
|
||||
queue.push(segment);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -67,7 +67,8 @@ function readWord(command, start) {
|
||||
// $'...' decodes its escapes, so $'E\x4fF' is EOF
|
||||
const decoded = decodeAnsiC(ansiC[1]);
|
||||
if (decoded === null) utf8 = false;
|
||||
else word += decoded;
|
||||
// Bash ends the decoded string at a NUL, so $'EOF\0X' is EOF
|
||||
else word += decoded.split('\0', 1)[0];
|
||||
quoted = true;
|
||||
i += ansiC[0].length - 1;
|
||||
} else if (ch === "'" || ch === '"') {
|
||||
@@ -165,7 +166,8 @@ function startsWord(command, index, current) {
|
||||
while (before >= 1 && command[before] === '\n' && command[before - 1] === '\\' && !isEscaped(command, before - 1)) {
|
||||
before -= 2;
|
||||
}
|
||||
return before < 0 || (/[\s;&|(]/.test(command[before]) && !isEscaped(command, before));
|
||||
// Bash's blanks are a space and a tab; a carriage return or a no-break space is part of a word.
|
||||
return before < 0 || (/[ \t\n;&|(]/.test(command[before]) && !isEscaped(command, before));
|
||||
}
|
||||
|
||||
// The end of a span copied as is, or -1: a comment, which runs to the end of its line, or an
|
||||
|
||||
@@ -324,6 +324,12 @@ function runTests() {
|
||||
}
|
||||
}) ? passed++ : failed++);
|
||||
|
||||
(test('blocks a dev command in a substitution with a very long body', () => {
|
||||
const command = `echo "$(npm run dev; ${'true; '.repeat(150000)})"`;
|
||||
const result = runScript(command);
|
||||
assert.strictEqual(result.code, 2, `Expected exit code 2, got ${result.code}`);
|
||||
}) ? passed++ : failed++);
|
||||
|
||||
(test('blocks nested dev commands in expanded here-document substitutions', () => {
|
||||
for (const command of [
|
||||
'cat <<EOF\n\'$(echo "$(npm run dev)")\'\nEOF',
|
||||
|
||||
@@ -112,6 +112,25 @@ test('$$ before a quote is the process ID, not the start of ANSI-C quoting', ()
|
||||
}
|
||||
});
|
||||
|
||||
test('bash ends a decoded ANSI-C delimiter at a NUL', () => {
|
||||
const command = "cat <<$'EOF\\0X'\nbody\nEOF\nnpm run dev";
|
||||
assert.deepStrictEqual(splitShellSegments(command), ["cat <<$'EOF\\0X'", 'npm run dev']);
|
||||
if (process.platform !== 'win32') {
|
||||
const ran = spawnSync('/bin/bash', ['-c', command.replace('npm run dev', "printf 'ran\\n'")], { encoding: 'utf8' });
|
||||
assert.strictEqual(ran.stdout, 'body\nran\n', ran.stderr);
|
||||
}
|
||||
});
|
||||
test('a # after a carriage return or a no-break space is part of the word', () => {
|
||||
for (const blankLike of ['\r', ' ']) {
|
||||
const command = `echo foo${blankLike}# && npm run dev`;
|
||||
assert.deepStrictEqual(splitShellSegments(command), [`echo foo${blankLike}#`, 'npm run dev']);
|
||||
if (process.platform !== 'win32') {
|
||||
const ran = spawnSync('/bin/bash', ['-c', command.replace('npm run dev', "printf 'ran\\n'")], { encoding: 'utf8' });
|
||||
assert.ok(ran.stdout.endsWith('ran\n'), JSON.stringify(ran.stdout));
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
// Escaped operators outside quotes
|
||||
console.log('\nEscaped operators outside quotes:');
|
||||
test('escaped && outside quotes not split', () => {
|
||||
|
||||
Reference in new issue
Block a user