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
4 changes: 3 additions & 1 deletion src/normalize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,9 @@ export function normalizeSpawnCommand(
return arg;
});

args = ['/d', '/s', '/c', `"${[command, ...args].join(' ')}"`];
// /d: disable autorun, /s: strip outer quotes of the /c string,
// /v:off: disable delayed expansion, /c: run the command then exit
args = ['/d', '/s', '/v:off', '/c', `"${[command, ...args].join(' ')}"`];
command = options.env?.comspec ?? 'cmd.exe';
// Tell node's spawn that the arguments are already escaped
options = {...options, windowsVerbatimArguments: true};
Expand Down
10 changes: 5 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', '/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', '/c', `"${relativePath}"`],
args: ['/d', '/s', '/v:off', '/c', `"${relativePath}"`],
options: {windowsVerbatimArguments: true}
});
});
Expand Down Expand Up @@ -98,15 +98,15 @@ 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[3]).toBe(
expect(normalized.args[4]).toBe(
`"${cmdPath} ^^^"\\^^^"^^^&injected^^^""`
);
});

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

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

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