diff --git a/scripts/lib/shell-split.js b/scripts/lib/shell-split.js index 8a26587c8..c1c26c919 100644 --- a/scripts/lib/shell-split.js +++ b/scripts/lib/shell-split.js @@ -49,6 +49,12 @@ function readWord(command, start) { let i = start; for (; i < command.length; i++) { const ch = command[i]; + // $$ is the process-ID parameter, so a quote right after it is not $'...' + if (!quote && ch === '$' && command[i + 1] === '$') { + word += '$$'; + i++; + continue; + } ANSI_C_QUOTED_RE.lastIndex = i; const ansiC = !quote && ch === '$' ? ANSI_C_QUOTED_RE.exec(command) : null; if (quote) { @@ -261,6 +267,13 @@ function splitShellSegments(command) { continue; } + // $$ is the process-ID parameter, so a quote right after it is not $'...' + if (ch === '$' && command[i + 1] === '$') { + current += '$$'; + i++; + continue; + } + // Opening quote. Keep ANSI-C quoting distinct from literal single quotes. if (ch === '$' && command[i + 1] === "'") { quote = "$'"; diff --git a/tests/hooks/pre-bash-dev-server-block.test.js b/tests/hooks/pre-bash-dev-server-block.test.js index 19adefc71..d6f97b132 100644 --- a/tests/hooks/pre-bash-dev-server-block.test.js +++ b/tests/hooks/pre-bash-dev-server-block.test.js @@ -56,6 +56,18 @@ function runTests() { } }) ? passed++ : failed++); + (test('blocks dev launch after a $$ that a quote follows', () => { + for (const command of ["echo $$'\\'; npm run dev", "cat <<$$'EOF'\nbody\n$$EOF\nnpm run dev"]) { + const result = runScript(command); + assert.strictEqual(result.code, 2, `Expected exit code 2 for ${JSON.stringify(command)}, got ${result.code}`); + } + }) ? passed++ : failed++); + + (test('allows a dev command quoted after a $$', () => { + const result = runScript("echo $$'; npm run dev'"); + assert.strictEqual(result.code, 0, `Expected exit code 0, got ${result.code}`); + }) ? passed++ : failed++); + (test('allows a dev command mentioned inside an ANSI-C quoted string', () => { const result = runScript("echo $'x\\' && npm run dev'"); assert.strictEqual(result.code, 0, `Expected exit code 0, got ${result.code}`); diff --git a/tests/lib/shell-split.test.js b/tests/lib/shell-split.test.js index 794a69a09..cee8b00e6 100644 --- a/tests/lib/shell-split.test.js +++ b/tests/lib/shell-split.test.js @@ -91,6 +91,26 @@ test('ANSI-C quotes keep escaped apostrophes and shell operators as data', () => assert.strictEqual(text.stdout, "x' && npm run dev\n"); } }); +test('$$ before a quote is the process ID, not the start of ANSI-C quoting', () => { + const launch = "echo $$'\\'; npm run dev"; + const heredoc = "cat <<$$'EOF'\nbody\n$$EOF\nnpm run dev"; + const literalInstruction = "echo $$'; npm run dev'"; + assert.deepStrictEqual(splitShellSegments(launch), ["echo $$'\\'", 'npm run dev']); + assert.deepStrictEqual(splitShellSegments(heredoc), ["cat <<$$'EOF'", 'npm run dev']); + assert.deepStrictEqual(splitShellSegments(literalInstruction), [literalInstruction]); + if (process.platform !== 'win32') { + const run = command => spawnSync('/bin/bash', ['-c', command.replace('npm run dev', "printf 'ran\\n'")], { encoding: 'utf8' }); + const ran = run(launch); + assert.strictEqual(ran.status, 0, ran.stderr); + assert.ok(ran.stdout.endsWith('\\\nran\n'), ran.stdout); + const body = run(heredoc); + assert.strictEqual(body.status, 0, body.stderr); + assert.strictEqual(body.stdout, 'body\nran\n'); + const text = spawnSync('/bin/bash', ['-c', literalInstruction], { encoding: 'utf8' }); + assert.strictEqual(text.status, 0, text.stderr); + assert.ok(text.stdout.endsWith('; npm run dev\n'), text.stdout); + } +}); // Escaped operators outside quotes console.log('\nEscaped operators outside quotes:');