Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions src/parse.js
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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(' ');
Expand Down
12 changes: 4 additions & 8 deletions src/util/escape.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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}`;
Expand All @@ -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}`;
Expand All @@ -87,8 +87,4 @@ function escapeBatchArgument(arg) {
return arg;
}

export {
escapeCommand as command,
escapeArgument as argument,
escapeBatchArgument as batchArgument,
};
export { escapeCommand, escapeArgument, escapeBatchArgument };
48 changes: 24 additions & 24 deletions src/util/escape.test.js
Original file line number Diff line number Diff line change
@@ -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"');
});
});
20 changes: 10 additions & 10 deletions src/util/validate.test.js
Original file line number Diff line number Diff line change
@@ -1,61 +1,61 @@
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' };

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'));
});
});
});