From dcd9ef96669644f8f2b0d12aa6652dc80265c38b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Cruz?= Date: Sun, 29 Nov 2026 13:06:44 +0000 Subject: [PATCH] refactor: export the escaping functions under their own names `escape.js` exported `escapeCommand()`, `escapeArgument()` and `escapeBatchArgument()` as `command`, `argument` and `batchArgument`, used through `import * as escape`. They're now exported as they are, and `parse.js` and the tests import them, and the validation functions, by name instead of through namespace imports. --- src/parse.js | 18 +++++++-------- src/util/escape.js | 12 ++++------ src/util/escape.test.js | 48 +++++++++++++++++++-------------------- src/util/validate.test.js | 20 ++++++++-------- 4 files changed, 47 insertions(+), 51 deletions(-) diff --git a/src/parse.js b/src/parse.js index 0a4bbbb..f894ead 100644 --- a/src/parse.js +++ b/src/parse.js @@ -1,7 +1,7 @@ import path from 'path'; import { resolveCommand } from './util/command.js'; -import * as escape from './util/escape.js'; -import * as validate from './util/validate.js'; +import { escapeCommand, escapeArgument, escapeBatchArgument } from './util/escape.js'; +import { assertValidCommand, assertValidArgument } from './util/validate.js'; import { readShebang } from './util/shebang.js'; const isWin = process.platform === 'win32'; @@ -36,7 +36,7 @@ function parseNonShell(parsed) { // Reject commands that can't be escaped for cmd.exe, e.g. with double quotes or trailing dots // This is done up front, so that they always throw the same error, whether or not they resolve to a file - validate.assertValidCommand(parsed.command); + assertValidCommand(parsed.command); // Resolve the command, adding support for shebangs resolveFiles(parsed); @@ -52,7 +52,7 @@ function parseNonShell(parsed) { // the interpreter of a shebang script [parsed.command, parsed.commandFile, parsed.executableFile] .filter(Boolean) - .forEach((file) => validate.assertValidCommand(file)); + .forEach((file) => assertValidCommand(file)); // We don't need a shell if the command filename is an executable const needsShell = !isExecutableRegExp.test(parsed.executableFile); @@ -66,17 +66,17 @@ function parseNonShell(parsed) { parsed.command = path.normalize(parsed.command); // Arguments can't contain values that can't be escaped for cmd.exe either, e.g. line breaks - parsed.args.forEach((arg) => validate.assertValidArgument(arg)); + parsed.args.forEach((arg) => assertValidArgument(arg)); // Escape command & arguments // Batch files need a different escaping strategy, see `src/util/escape.js` for more information if (isBatchFile(parsed.executableFile)) { // Run the batch file by its resolved path, so that cmd.exe runs exactly the file that was detected - parsed.command = escape.command(parsed.executableFile); - parsed.args = parsed.args.map((arg) => escape.batchArgument(arg)); + parsed.command = escapeCommand(parsed.executableFile); + parsed.args = parsed.args.map((arg) => escapeBatchArgument(arg)); } else { - parsed.command = escape.command(parsed.command); - parsed.args = parsed.args.map((arg) => escape.argument(arg)); + parsed.command = escapeCommand(parsed.command); + parsed.args = parsed.args.map((arg) => escapeArgument(arg)); } const shellCommand = [parsed.command].concat(parsed.args).join(' '); diff --git a/src/util/escape.js b/src/util/escape.js index 6a51421..5bb5b5e 100644 --- a/src/util/escape.js +++ b/src/util/escape.js @@ -41,7 +41,7 @@ function quoteArgument(arg, escapedQuote) { return `${quoted}${'\\'.repeat(backslashes * 2)}"`; } -// The command must have been validated with `validate.assertValidCommand()`, so it can't contain double quotes, +// The command must have been validated with `assertValidCommand()`, so it can't contain double quotes, // `^` or `%`, and only needs to be quoted, for both batch files and other commands function escapeCommand(arg) { // Quote the command, so that cmd.exe treats it as a single name @@ -51,7 +51,7 @@ function escapeCommand(arg) { return `"${arg}"`; } -// The argument must have been validated with `validate.assertValidArgument()` +// The argument must have been validated with `assertValidArgument()` function escapeArgument(arg) { // Convert to string arg = `${arg}`; @@ -73,7 +73,7 @@ function escapeArgument(arg) { // Instead of escaping meta chars with ^, we rely on double quotes, which keep meta chars literal in both parses // This is the same approach taken by Rust after BatBadBut (CVE-2024-24576) -// The argument must have been validated with `validate.assertValidArgument()` +// The argument must have been validated with `assertValidArgument()` function escapeBatchArgument(arg) { // Convert to string arg = `${arg}`; @@ -87,8 +87,4 @@ function escapeBatchArgument(arg) { return arg; } -export { - escapeCommand as command, - escapeArgument as argument, - escapeBatchArgument as batchArgument, -}; +export { escapeCommand, escapeArgument, escapeBatchArgument }; diff --git a/src/util/escape.test.js b/src/util/escape.test.js index 6f8fc29..6d20538 100644 --- a/src/util/escape.test.js +++ b/src/util/escape.test.js @@ -1,49 +1,49 @@ import { describe, it } from 'node:test'; import assert from 'node:assert/strict'; -import * as escape from './escape.js'; +import { escapeCommand, escapeArgument, escapeBatchArgument } from './escape.js'; describe('escape', () => { it('should quote batch arguments, doubling quotes and neutralizing %', () => { - assert.equal(escape.batchArgument('hello'), '"hello"'); - assert.equal(escape.batchArgument(''), '""'); - assert.equal(escape.batchArgument(1234), '"1234"'); - assert.equal(escape.batchArgument('a & b'), '"a & b"'); - assert.equal(escape.batchArgument('a"b'), '"a""b"'); - assert.equal(escape.batchArgument('a\\"b'), '"a\\\\""b"'); - assert.equal(escape.batchArgument('C:\\dir\\'), '"C:\\dir\\\\"'); - assert.equal(escape.batchArgument('%PATH%'), '"%%cd:~,%PATH%%cd:~,%"'); - assert.equal(escape.batchArgument('x^y!'), '"x^y!"'); + assert.equal(escapeBatchArgument('hello'), '"hello"'); + assert.equal(escapeBatchArgument(''), '""'); + assert.equal(escapeBatchArgument(1234), '"1234"'); + assert.equal(escapeBatchArgument('a & b'), '"a & b"'); + assert.equal(escapeBatchArgument('a"b'), '"a""b"'); + assert.equal(escapeBatchArgument('a\\"b'), '"a\\\\""b"'); + assert.equal(escapeBatchArgument('C:\\dir\\'), '"C:\\dir\\\\"'); + assert.equal(escapeBatchArgument('%PATH%'), '"%%cd:~,%PATH%%cd:~,%"'); + assert.equal(escapeBatchArgument('x^y!'), '"x^y!"'); }); it('should neutralize % instead of escaping it with ^', () => { // ^% doesn't prevent %VAR% expansion if a variable named `VAR^` exists - assert.equal(escape.argument('%x%'), '^"%%cd:~,%x%%cd:~,%^"'); - assert.equal(escape.argument('a,b'), '^"a^,b^"'); + assert.equal(escapeArgument('%x%'), '^"%%cd:~,%x%%cd:~,%^"'); + assert.equal(escapeArgument('a,b'), '^"a^,b^"'); }); it('should double backslashes only before a quote or at the end', () => { - assert.equal(escape.argument('a\\b'), '^"a\\b^"'); - assert.equal(escape.argument('a\\\\"b'), '^"a\\\\\\\\\\^"b^"'); - assert.equal(escape.argument('C:\\dir\\\\'), '^"C:\\dir\\\\\\\\^"'); - assert.equal(escape.batchArgument('a\\b'), '"a\\b"'); - assert.equal(escape.batchArgument('a\\\\"b'), '"a\\\\\\\\""b"'); - assert.equal(escape.batchArgument('C:\\dir\\\\'), '"C:\\dir\\\\\\\\"'); + assert.equal(escapeArgument('a\\b'), '^"a\\b^"'); + assert.equal(escapeArgument('a\\\\"b'), '^"a\\\\\\\\\\^"b^"'); + assert.equal(escapeArgument('C:\\dir\\\\'), '^"C:\\dir\\\\\\\\^"'); + assert.equal(escapeBatchArgument('a\\b'), '"a\\b"'); + assert.equal(escapeBatchArgument('a\\\\"b'), '"a\\\\\\\\""b"'); + assert.equal(escapeBatchArgument('C:\\dir\\\\'), '"C:\\dir\\\\\\\\"'); }); it('should escape long sequences of backslashes in linear time', () => { const arg = `${'\\'.repeat(1e6)}"`; const start = Date.now(); - escape.argument(arg); - escape.batchArgument(arg); + escapeArgument(arg); + escapeBatchArgument(arg); assert.ok(Date.now() - start < 1000); }); it('should quote commands, so that cmd.exe treats them as a single name', () => { - assert.equal(escape.command('npm install'), '"npm install"'); - assert.equal(escape.command('foo\tbar,baz;qux=quux'), '"foo\tbar,baz;qux=quux"'); - assert.equal(escape.command('a&b'), '"a&b"'); - assert.equal(escape.command('C:\\foo bar\\x&y.bat'), '"C:\\foo bar\\x&y.bat"'); + assert.equal(escapeCommand('npm install'), '"npm install"'); + assert.equal(escapeCommand('foo\tbar,baz;qux=quux'), '"foo\tbar,baz;qux=quux"'); + assert.equal(escapeCommand('a&b'), '"a&b"'); + assert.equal(escapeCommand('C:\\foo bar\\x&y.bat'), '"C:\\foo bar\\x&y.bat"'); }); }); diff --git a/src/util/validate.test.js b/src/util/validate.test.js index 5a9b3b3..8c77948 100644 --- a/src/util/validate.test.js +++ b/src/util/validate.test.js @@ -1,6 +1,6 @@ import { describe, it } from 'node:test'; import assert from 'node:assert/strict'; -import * as validate from './validate.js'; +import { assertValidCommand, assertValidArgument } from './validate.js'; const invalidValueError = { name: 'TypeError', code: 'ERR_INVALID_ARG_VALUE' }; @@ -8,54 +8,54 @@ describe('validate', () => { describe('assertValidCommand()', () => { it('should accept commands and paths', () => { for (const command of ['node', 'foo.bat', 'C:\\foo bar\\x&y!.bat', '..\\foo', './foo', 'a,b;c=d']) { - validate.assertValidCommand(command); + assertValidCommand(command); } }); it('should reject line breaks', () => { for (const command of ['foo\ncalc.exe', 'foo\rcalc.exe']) { - assert.throws(() => validate.assertValidCommand(command), invalidValueError); + assert.throws(() => assertValidCommand(command), invalidValueError); } }); it('should reject double quotes', () => { for (const command of ['"foo"', 'foo"', '"foo.bat"', 'C:\\"foo bar"\\x.exe']) { - assert.throws(() => validate.assertValidCommand(command), invalidValueError); + assert.throws(() => assertValidCommand(command), invalidValueError); } }); it('should reject ^ and %', () => { for (const command of ['C:\\foo\\%CD%.bat', 'C:\\100%\\x.exe', 'C:\\a^b\\x.bat', 'x^y', '%PATH%']) { - assert.throws(() => validate.assertValidCommand(command), invalidValueError); + assert.throws(() => assertValidCommand(command), invalidValueError); } }); it('should reject a trailing dot or whitespace', () => { for (const command of ['foo.bat ', 'foo.bat.', 'foo ', 'foo.', 'foo\t', '..']) { - assert.throws(() => validate.assertValidCommand(command), invalidValueError); + assert.throws(() => assertValidCommand(command), invalidValueError); } }); it('should not include the command in the error message', () => { - assert.throws(() => validate.assertValidCommand('C:\\a^b\\secret.bat'), (err) => !err.message.includes('secret')); + assert.throws(() => assertValidCommand('C:\\a^b\\secret.bat'), (err) => !err.message.includes('secret')); }); }); describe('assertValidArgument()', () => { it('should accept arguments, including non-strings', () => { for (const arg of ['foo', '', '" & echo injected & "', '%PATH%', 'x^y', 1234]) { - validate.assertValidArgument(arg); + assertValidArgument(arg); } }); it('should reject line breaks', () => { for (const arg of ['safe.js\ncalc.exe', 'safe.js\rcalc.exe', 'safe.js\r\ncalc.exe', '\n']) { - assert.throws(() => validate.assertValidArgument(arg), invalidValueError); + assert.throws(() => assertValidArgument(arg), invalidValueError); } }); it('should not include the argument in the error message', () => { - assert.throws(() => validate.assertValidArgument('secret\n'), (err) => !err.message.includes('secret')); + assert.throws(() => assertValidArgument('secret\n'), (err) => !err.message.includes('secret')); }); }); });