mirror of
https://github.com/affaan-m/ECC.git
synced 2026-11-17 05:22:17 +00:00
fix(hooks): read $$ as the process ID before looking for $'...'
In `echo $$'\'; npm run dev`, Bash reads `$$` as the process-ID parameter and the quote after it as an ordinary single quote, so it runs `npm run dev`. The splitter took the second `$` as the start of ANSI-C quoting, which hid the semicolon, and a here-document delimiter such as `$$'EOF'` was read as `$EOF`, so the body never ended. Consume the `$$` pair first in both places.
This commit is contained in:
3 files changed
+45
No files matched your search
@@ -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 = "$'";
|
||||
|
||||
@@ -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}`);
|
||||
|
||||
@@ -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:');
|
||||
|
||||
Reference in new issue
Block a user