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
6 changes: 4 additions & 2 deletions src/normalize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,10 @@ export function normalizeSpawnCommand(
// This is necessary otherwise it will always fail with ENOENT in those cases
command = normalizePath(command);

// Escape command & arguments
command = command.replace(metaCharsRegExp, '^$1');
// Quote the command for windows so it doesn't bother expanding meta chars
command = `"${command.replace(/%/g, '%%cd:~,%')}"`;

// Escape arguments
args = args.map((arg) => {
// Algorithm below is based on https://qntm.org/cmd
// It's slightly altered to disable JS backtracking to avoid hanging on specially crafted input
Expand Down
26 changes: 22 additions & 4 deletions src/test/main_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,7 @@ if (isWindows) {
test('does not throw spawn errors', async () => {
const result = await x('definitelyNonExistent');
expect(result.stderr).toBe(
"'definitelyNonExistent' is not recognized as an internal" +
'\'"definitelyNonExistent"\' is not recognized as an internal' +
' or external command,\r\noperable program or batch file.\r\n'
);
expect(result.stdout).toBe('');
Expand All @@ -419,6 +419,24 @@ if (isWindows) {
expect(result.stdout).toContain('&echo INJECTED');
expect(result.stdout.split(/\r?\n/)).not.toContain('INJECTED');
});

test.for([
['spaces', 'tinyexec program files (x86)-'],
['variables', 'tinyexec-%CD%-']
])('runs batch files in directories with %s', async ([, prefix]) => {
const dir = fs.mkdtempSync(path.join(os.tmpdir(), prefix));
try {
const scriptPath = path.join(dir, 'tool.cmd');
fs.writeFileSync(scriptPath, '@echo ran %*\r\n');

const result = await x(scriptPath, ['hello']);

expect(result.exitCode).toBe(0);
expect(result.stdout).toBe('ran "hello"\r\n');
} finally {
fs.rmSync(dir, {recursive: true, force: true});
}
});
});

describe('exec (windows) (async)', () => {
Expand All @@ -440,7 +458,7 @@ if (isWindows) {
} catch (err) {
expect.assert(err instanceof NonZeroExitError);
expect(err.output?.stderr).toBe(
"'definitelyNonExistent' is not recognized as an internal" +
'\'"definitelyNonExistent"\' is not recognized as an internal' +
' or external command,\r\noperable program or batch file.\r\n'
);
expect(err.output?.stdout).toBe('');
Expand Down Expand Up @@ -490,7 +508,7 @@ if (isWindows) {
}

expect(lines).toEqual([
"'nonexistentforsure' is not recognized as an internal or " +
'\'"nonexistentforsure"\' is not recognized as an internal or ' +
'external command,',
'operable program or batch file.'
]);
Expand Down Expand Up @@ -547,7 +565,7 @@ if (isWindows) {
}

expect(lines).toEqual([
"'nonexistentforsure' is not recognized as an internal or " +
'\'"nonexistentforsure"\' is not recognized as an internal or ' +
'external command,',
'operable program or batch file.'
]);
Expand Down
22 changes: 17 additions & 5 deletions src/test/normalize_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ describe('normalizeSpawnCommand', () => {

expect(normalized).toEqual({
command: 'cmd.exe',
args: ['/d', '/s', '/v:off', '/c', '"nonexistent ^"hi^""'],
args: ['/d', '/s', '/v:off', '/c', '""nonexistent" ^"hi^""'],
options: {windowsVerbatimArguments: true}
});
});
Expand All @@ -56,7 +56,7 @@ describe('normalizeSpawnCommand', () => {
const normalized = normalizeSpawnCommand(relativePath, []);
expect(normalized).toEqual({
command: 'cmd.exe',
args: ['/d', '/s', '/v:off', '/c', `"${relativePath}"`],
args: ['/d', '/s', '/v:off', '/c', `""${relativePath}""`],
options: {windowsVerbatimArguments: true}
});
});
Expand Down Expand Up @@ -99,14 +99,26 @@ describe('normalizeSpawnCommand', () => {
// the batch interpreter consumes a second layer of `^` escapes when it
// expands `%*`, so `&` must still be escaped at that point
expect(normalized.args[4]).toBe(
`"${cmdPath} ^^^"\\^^^"^^^&injected^^^""`
`""${cmdPath}" ^^^"\\^^^"^^^&injected^^^""`
);
});

test('single escapes meta chars in args to a non-batch command', () => {
const normalized = normalizeSpawnCommand('nonexistent', ['"&injected']);

expect(normalized.args[4]).toBe('"nonexistent ^"\\^"^&injected^""');
expect(normalized.args[4]).toBe('""nonexistent" ^"\\^"^&injected^""');
});

test('passes the command to cmd.exe as a single quoted token', () => {
const normalized = normalizeSpawnCommand('a b&c(d)', ['arg']);

expect(normalized.args[4]).toBe('""a b&c(d)" ^"arg^""');
});

test('prevents variable expansion in the command', () => {
const normalized = normalizeSpawnCommand('a%CD%b', []);

expect(normalized.args[4]).toBe('""a%%cd:~,%CD%%cd:~,%b""');
});

test('handles relative commands without extension', () => {
Expand All @@ -117,7 +129,7 @@ describe('normalizeSpawnCommand', () => {
const normalized = normalizeSpawnCommand(relativePath, []);
expect(normalized).toEqual({
command: 'cmd.exe',
args: ['/d', '/s', '/v:off', '/c', `"${relativePath}"`],
args: ['/d', '/s', '/v:off', '/c', `""${relativePath}""`],
options: {windowsVerbatimArguments: true}
});
});
Expand Down