From 30eb570117aaf5fa1f550e4f71c6b9fd3fcd79f7 Mon Sep 17 00:00:00 2001 From: L4XB Date: Thu, 5 Nov 2026 13:07:21 +0100 Subject: [PATCH] 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. --- scripts/hooks/pre-bash-dev-server-block.js | 8 ++++++-- scripts/lib/shell-split.js | 6 ++++-- tests/hooks/pre-bash-dev-server-block.test.js | 6 ++++++ tests/lib/shell-split.test.js | 19 +++++++++++++++++++ 4 files changed, 35 insertions(+), 4 deletions(-) diff --git a/scripts/hooks/pre-bash-dev-server-block.js b/scripts/hooks/pre-bash-dev-server-block.js index a92036204..a1a868cb0 100755 --- a/scripts/hooks/pre-bash-dev-server-block.js +++ b/scripts/hooks/pre-bash-dev-server-block.js @@ -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); + } } } diff --git a/scripts/lib/shell-split.js b/scripts/lib/shell-split.js index c1c26c919..d25684d99 100644 --- a/scripts/lib/shell-split.js +++ b/scripts/lib/shell-split.js @@ -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 diff --git a/tests/hooks/pre-bash-dev-server-block.test.js b/tests/hooks/pre-bash-dev-server-block.test.js index 48f723dc2..aaccc316f 100644 --- a/tests/hooks/pre-bash-dev-server-block.test.js +++ b/tests/hooks/pre-bash-dev-server-block.test.js @@ -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 < { + 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', () => {