Skip to content
Myr

command-resolution.js

This file

Path

test/arguments/command-resolution.js

565 lines · 26.2 KiB · JavaScript

Last changed by

Fix arguments of Windows batch files that read them with `%~1`Sindre Sorhusbefore adoption93bddc4

History of this file

Take it

TweaksSindre Sorhusbefore adoption698a1b8

lines 1–4why, one step back
1import assert from 'node:assert/strict';
2import path from 'node:path';3import process from 'node:process';4import test from 'node:test';

Fix `preferLocal` argument escaping edge-case on Windows (#1259)Sindre Sorhusbefore adoptione771733

lines 5–10why, one step back
5import {
6 cp,7 mkdir,8 unlink,9 writeFile,10} from 'node:fs/promises';

TweaksSindre Sorhusbefore adoption698a1b8

line 11why, one step back
11import {assertThrows, assertRejects} from '../helpers/assert.js';

Move away from `cross-spawn` (#1251)Sindre Sorhusbefore adoption4cafc3f

lines 12–27why, one step back
12import {execa, execaSync} from '../../index.js';
13import {setFixtureDirectory, FIXTURES_DIRECTORY} from '../helpers/fixtures-directory.js';1415setFixtureDirectory();1617const isWindows = process.platform === 'win32';1819// These tests cover the command resolution and argument escaping performed by20// `lib/arguments/command-file.js`, which allows running scripts, `.cmd`/`.bat` files21// and shebang scripts without a shell, and prevents command injection on Windows.22// Most assertions use a roundtrip: an argument is passed in, the subprocess prints23// its own `process.argv` back (see `test/fixtures/echo.js`), and we check it comes24// back byte-identical. This exercises the escaping logic on whichever OS runs the25// test, so Windows escaping is genuinely validated on CI.2627// `echo.js` prints `process.argv.slice(2).join('\n')`

TweaksSindre Sorhusbefore adoption698a1b8

line 28why, one step back
28const testRoundtrip = async commandArguments => {

Move away from `cross-spawn` (#1251)Sindre Sorhusbefore adoption4cafc3f

lines 29–31why, one step back
29 const expectedStdout = commandArguments.map(String).join('\n');
3031 const {stdout} = await execa('echo.js', commandArguments);

TweaksSindre Sorhusbefore adoption698a1b8

line 32why, one step back
32 assert.equal(stdout, expectedStdout);

Move away from `cross-spawn` (#1251)Sindre Sorhusbefore adoption4cafc3f

lines 33–34why, one step back
33
34 const {stdout: stdoutSync} = execaSync('echo.js', commandArguments);

TweaksSindre Sorhusbefore adoption698a1b8

line 35why, one step back
35 assert.equal(stdoutSync, expectedStdout);

Move away from `cross-spawn` (#1251)Sindre Sorhusbefore adoption4cafc3f

lines 36–37why, one step back
36};
37

TweaksSindre Sorhusbefore adoption698a1b8

lines 38–40why, one step back
38test('Empty arguments and arguments with spaces', () => testRoundtrip(['foo', '', 'bar', 'André Cruz']));
39test('Non-string arguments are coerced', () => testRoundtrip([1234]));40test('Arguments with shell special characters', () => testRoundtrip([

Move away from `cross-spawn` (#1251)Sindre Sorhusbefore adoption4cafc3f

lines 41–71why, one step back
41 'foo',
42 '()',43 'foo',44 '[]',45 'foo',46 '%!',47 'foo',48 '^<',49 'foo',50 '>&',51 'foo',52 '|;',53 'foo',54 ', ',55 'foo',56 '!=',57 'foo',58 '\\*',59 'foo',60 '"f"',61 'foo',62 '?.',63 'foo',64 '=`',65 'foo',66 '\'',67 'foo',68 '\\"',69 'bar\\',70 '"foo|bar>baz"',71 '"(foo|bar>baz|foz)"',

TweaksSindre Sorhusbefore adoption698a1b8

line 72why, one step back
72]));

Move away from `cross-spawn` (#1251)Sindre Sorhusbefore adoption4cafc3f

line 73why, one step back
73// Backslashes and double quotes need specific escaping on Windows, so their combinations are the trickiest.

TweaksSindre Sorhusbefore adoption698a1b8

lines 74–75why, one step back
74test('Arguments with backslashes', () => testRoundtrip(['a\\b', 'a\\\\b', '\\', '\\\\', 'a\\', 'trailing\\']));
75test('Arguments with backslashes and double quotes', () => testRoundtrip(['a\\"b', '\\"', 'a"\\', '\\\\"', '"\\\\"']));

Move away from `cross-spawn` (#1251)Sindre Sorhusbefore adoption4cafc3f

line 76why, one step back
76// Backslashes, double quotes and shell metacharacters combined in a single argument are the trickiest to escape on Windows.

TweaksSindre Sorhusbefore adoption698a1b8

lines 77–78why, one step back
77test('Arguments combining backslashes, quotes and metacharacters', () => testRoundtrip(['a\\"&b', '\\"()\\', 'c\\\\"|d', '%x%\\', '">&<"']));
78test('Arguments with tabs and whitespace', () => testRoundtrip(['a\tb', '\t', ' spaces ', ' ']));

Move away from `cross-spawn` (#1251)Sindre Sorhusbefore adoption4cafc3f

line 79why, one step back
79// Several consecutive empty and whitespace-only arguments must each be preserved as a distinct argument.

TweaksSindre Sorhusbefore adoption698a1b8

lines 80–82why, one step back
80test('Arguments that are empty or whitespace-only', () => testRoundtrip(['', '', 'a', '', ' ', '\t', '']));
81test('Arguments that look like flags', () => testRoundtrip(['-e', '--foo=bar', '--', '-']));82test('Arguments with Unicode characters', () => testRoundtrip(['café', '🎉', '日本語', 'naïve']));

Move away from `cross-spawn` (#1251)Sindre Sorhusbefore adoption4cafc3f

line 83why, one step back
83// eslint-disable-next-line no-template-curly-in-string

TweaksSindre Sorhusbefore adoption698a1b8

line 84why, one step back
84test('Arguments with shell variable syntax', () => testRoundtrip(['$HOME', '${x}', '~', 'a=b,c', '`whoami`']));

Move away from `cross-spawn` (#1251)Sindre Sorhusbefore adoption4cafc3f

line 85why, one step back
85// Very long arguments must not be truncated nor corrupted by the escaping.

TweaksSindre Sorhusbefore adoption698a1b8

line 86why, one step back
86test('Very long arguments', () => testRoundtrip(['a'.repeat(10_000), `${'b'.repeat(1000)}\\"${'c'.repeat(1000)}`]));

Move away from `cross-spawn` (#1251)Sindre Sorhusbefore adoption4cafc3f

lines 87–90why, one step back
87// A long run of consecutive backslashes right before a double quote (or the end of the
88// argument) must be doubled as a whole. This is both the input a length-dependent escaping89// bug corrupts and the one a naive regex backtracks quadratically over, so a correct90// roundtrip here guards correctness and the single-pass linear escaping at once.

TweaksSindre Sorhusbefore adoption698a1b8

line 91why, one step back
91test('Arguments with long runs of backslashes before quotes', () => testRoundtrip([

Move away from `cross-spawn` (#1251)Sindre Sorhusbefore adoption4cafc3f

lines 92–94why, one step back
92 `${'\\'.repeat(1000)}"`,
93 '\\'.repeat(1000),94 `a${'\\'.repeat(500)}"b${'\\'.repeat(500)}`,

TweaksSindre Sorhusbefore adoption698a1b8

line 95why, one step back
95]));

Move away from `cross-spawn` (#1251)Sindre Sorhusbefore adoption4cafc3f

lines 96–103why, one step back
96// Runs of backslashes of every length from 1 to 6, each right before a double quote, then
97// each at the very end of the argument. The bug this guards against doubled only the last98// backslash of a run instead of the whole run, so it corrupted every length above 1.99// Passing each length as its own argument pins down exactly which lengths would break.100const backslashRunArguments = [101 ...Array.from({length: 6}, (_, index) => `${'\\'.repeat(index + 1)}"x`),102 ...Array.from({length: 6}, (_, index) => `end${'\\'.repeat(index + 1)}`),103];

TweaksSindre Sorhusbefore adoption698a1b8

line 104why, one step back
104test('Arguments with backslash runs of every length', () => testRoundtrip(backslashRunArguments));

Move away from `cross-spawn` (#1251)Sindre Sorhusbefore adoption4cafc3f

line 105why, one step back
105// Consecutive double quotes must each be escaped independently, not collapsed nor paired.

TweaksSindre Sorhusbefore adoption698a1b8

line 106why, one step back
106test('Arguments with consecutive double quotes', () => testRoundtrip(['""', '"""', 'a""b', '""""']));

Move away from `cross-spawn` (#1251)Sindre Sorhusbefore adoption4cafc3f

lines 107–109why, one step back
107// A line break can be passed as an argument to a directly executable file (unlike a `.cmd`
108// on Windows, see below). `echo-argv.js` prints its arguments as JSON, so this verifies the109// line break stays within a single argument rather than splitting it in two.

TweaksSindre Sorhusbefore adoption698a1b8

line 110why, one step back
110test('Line breaks are kept within a single argument for executables', async () => {

Move away from `cross-spawn` (#1251)Sindre Sorhusbefore adoption4cafc3f

lines 111–112why, one step back
111 const commandArguments = ['a\nb', 'a\r\nb', 'c'];
112 const {stdout} = await execa('echo-argv.js', commandArguments);

TweaksSindre Sorhusbefore adoption698a1b8

line 113why, one step back
113 assert.deepEqual(JSON.parse(stdout), commandArguments);

Move away from `cross-spawn` (#1251)Sindre Sorhusbefore adoption4cafc3f

lines 114–115why, one step back
114
115 const {stdout: stdoutSync} = execaSync('echo-argv.js', commandArguments);

TweaksSindre Sorhusbefore adoption698a1b8

line 116why, one step back
116 assert.deepEqual(JSON.parse(stdoutSync), commandArguments);

Move away from `cross-spawn` (#1251)Sindre Sorhusbefore adoption4cafc3f

lines 117–120why, one step back
117});
118119// A command can be resolved from different kinds of file paths. Forward slashes120// work even on Windows, where they are normalized to backslashes.

TweaksSindre Sorhusbefore adoption698a1b8

line 121why, one step back
121const testResolvesCommand = async (file, options) => {

Move away from `cross-spawn` (#1251)Sindre Sorhusbefore adoption4cafc3f

line 122why, one step back
122 const {stdout} = await execa(file, ['foo'], options);

TweaksSindre Sorhusbefore adoption698a1b8

line 123why, one step back
123 assert.equal(stdout, 'foo');

Move away from `cross-spawn` (#1251)Sindre Sorhusbefore adoption4cafc3f

lines 124–125why, one step back
124
125 const {stdout: stdoutSync} = execaSync(file, ['foo'], options);

TweaksSindre Sorhusbefore adoption698a1b8

line 126why, one step back
126 assert.equal(stdoutSync, 'foo');

Move away from `cross-spawn` (#1251)Sindre Sorhusbefore adoption4cafc3f

lines 127–128why, one step back
127};
128

TweaksSindre Sorhusbefore adoption698a1b8

lines 129–133why, one step back
129test('Runs a command given as an absolute path', () => testResolvesCommand(path.join(FIXTURES_DIRECTORY, 'echo.js')));
130test('Runs a command given as a relative path with a custom cwd', () => testResolvesCommand('./echo.js', {cwd: FIXTURES_DIRECTORY}));131test('Runs a command given as a relative POSIX-style subpath', () => testResolvesCommand('test/fixtures/echo.js'));132test('Runs a command whose path contains a space', () => testResolvesCommand('command with space.js'));133test('Runs a command whose absolute path contains a space', () => testResolvesCommand(path.join(FIXTURES_DIRECTORY, 'command with space.js')));

Move away from `cross-spawn` (#1251)Sindre Sorhusbefore adoption4cafc3f

lines 134–137why, one step back
134
135// A shebang script can be run without explicitly prepending its interpreter.136// This is emulated on Windows, where shebangs are not natively supported.137// The whole test suite implicitly relies on this (e.g. `echo.js`), so we assert it explicitly.

TweaksSindre Sorhusbefore adoption698a1b8

line 138why, one step back
138test('Runs a shebang script without specifying its interpreter', async () => {

Move away from `cross-spawn` (#1251)Sindre Sorhusbefore adoption4cafc3f

line 139why, one step back
139 const {stdout} = await execa('echo.js', ['foo']);

TweaksSindre Sorhusbefore adoption698a1b8

line 140why, one step back
140 assert.equal(stdout, 'foo');

Move away from `cross-spawn` (#1251)Sindre Sorhusbefore adoption4cafc3f

lines 141–142why, one step back
141
142 const {stdout: stdoutSync} = execaSync('echo.js', ['foo']);

TweaksSindre Sorhusbefore adoption698a1b8

line 143why, one step back
143 assert.equal(stdoutSync, 'foo');

Move away from `cross-spawn` (#1251)Sindre Sorhusbefore adoption4cafc3f

lines 144–148why, one step back
144});
145146// A space between `#!` and the interpreter path (e.g. `#! /usr/bin/env node`) is a valid,147// real-world shebang variation. The reimplemented shebang parser must trim it, otherwise148// on Windows the interpreter would resolve as ` /usr/bin/env` and fail with ENOENT.

TweaksSindre Sorhusbefore adoption698a1b8

line 149why, one step back
149test('Runs a shebang script with a space after the "#!"', async () => {

Move away from `cross-spawn` (#1251)Sindre Sorhusbefore adoption4cafc3f

line 150why, one step back
150 const {stdout} = await execa('echo-space-shebang.js', ['foo']);

TweaksSindre Sorhusbefore adoption698a1b8

line 151why, one step back
151 assert.equal(stdout, 'foo');

Move away from `cross-spawn` (#1251)Sindre Sorhusbefore adoption4cafc3f

lines 152–153why, one step back
152
153 const {stdout: stdoutSync} = execaSync('echo-space-shebang.js', ['foo']);

TweaksSindre Sorhusbefore adoption698a1b8

line 154why, one step back
154 assert.equal(stdoutSync, 'foo');

Move away from `cross-spawn` (#1251)Sindre Sorhusbefore adoption4cafc3f

lines 155–156why, one step back
155});
156

Fix shebang parsing reading past the end of a small fileSindre Sorhusbefore adoption7c1a631

lines 157–166why, one step back
157/*
158Windows does not support shebangs natively, so Execa parses them itself, by reading the file's first bytes into a fixed-size buffer.159A file which only contains a shebang line, without a trailing newline, is shorter than that buffer, which is zero-filled.160Only the bytes actually read must be parsed, otherwise the interpreter would include those `\0` characters, which does not resolve to any file.161The file is created by the test since Unix cannot execute it, so it must not be used by any other test.162*/163if (isWindows) {164 test('Runs a shebang script shorter than the shebang buffer', async t => {165 const filePath = path.join(FIXTURES_DIRECTORY, 'shebang-only.js');166 await writeFile(filePath, '#!/usr/bin/env node');

TweaksSindre Sorhusbefore adoption698a1b8

line 167why, one step back
167 t.after(() => unlink(filePath));

Fix shebang parsing reading past the end of a small fileSindre Sorhusbefore adoption7c1a631

lines 168–169why, one step back
168
169 const {exitCode, stdout} = await execa('shebang-only.js');

TweaksSindre Sorhusbefore adoption698a1b8

lines 170–171why, one step back
170 assert.equal(exitCode, 0);
171 assert.equal(stdout, '');

Fix shebang parsing reading past the end of a small fileSindre Sorhusbefore adoption7c1a631

lines 172–174why, one step back
172 });
173}174

Move away from `cross-spawn` (#1251)Sindre Sorhusbefore adoption4cafc3f

lines 175–176why, one step back
175// Neither the caller's arguments array nor its options object should be mutated,
176// whether or not a shell is used (each path clones the arguments internally).

TweaksSindre Sorhusbefore adoption698a1b8

line 177why, one step back
177const testNoMutation = async options => {

Move away from `cross-spawn` (#1251)Sindre Sorhusbefore adoption4cafc3f

lines 178–181why, one step back
178 const commandArguments = ['foo', 'bar'];
179 const optionsCopy = {...options};180181 await execa('echo.js', commandArguments, options);

TweaksSindre Sorhusbefore adoption698a1b8

lines 182–183why, one step back
182 assert.deepEqual(commandArguments, ['foo', 'bar']);
183 assert.deepEqual(options, optionsCopy);

Move away from `cross-spawn` (#1251)Sindre Sorhusbefore adoption4cafc3f

lines 184–185why, one step back
184
185 execaSync('echo.js', commandArguments, options);

TweaksSindre Sorhusbefore adoption698a1b8

lines 186–187why, one step back
186 assert.deepEqual(commandArguments, ['foo', 'bar']);
187 assert.deepEqual(options, optionsCopy);

Move away from `cross-spawn` (#1251)Sindre Sorhusbefore adoption4cafc3f

lines 188–189why, one step back
188};
189

TweaksSindre Sorhusbefore adoption698a1b8

lines 190–191why, one step back
190test('Does not mutate arguments nor options', () => testNoMutation({}));
191test('Does not mutate arguments nor options with a shell', () => testNoMutation({shell: true}));

Move away from `cross-spawn` (#1251)Sindre Sorhusbefore adoption4cafc3f

lines 192–193why, one step back
192
193if (isWindows) {

Fix `preferLocal` argument escaping edge-case on Windows (#1259)Sindre Sorhusbefore adoptione771733

lines 194–201why, one step back
194 const nodeOnlyOptions = {
195 extendEnv: false,196 env: {197 Path: path.dirname(process.execPath),198 PathExt: '.EXE',199 },200 };201

Move away from `cross-spawn` (#1251)Sindre Sorhusbefore adoption4cafc3f

line 202why, one step back
202 // A bare command name without an extension is resolved using `PATHEXT`.

TweaksSindre Sorhusbefore adoption698a1b8

line 203why, one step back
203 test('Resolves command extension using PATHEXT (sync)', () => {

Move away from `cross-spawn` (#1251)Sindre Sorhusbefore adoption4cafc3f

line 204why, one step back
204 const {stdout} = execaSync('hello');

TweaksSindre Sorhusbefore adoption698a1b8

line 205why, one step back
205 assert.equal(stdout, 'Hello World');

Move away from `cross-spawn` (#1251)Sindre Sorhusbefore adoption4cafc3f

lines 206–207why, one step back
206 });
207

Fix `preferLocal` argument escaping edge-case on Windows (#1259)Sindre Sorhusbefore adoptione771733

lines 208–217why, one step back
208 test('Uses PATHEXT extension order for direct executables', async t => {
209 const binaryDirectory = path.join(FIXTURES_DIRECTORY, 'node_modules', '.bin');210 await mkdir(binaryDirectory, {recursive: true});211 const command = 'node-extension-order';212 const executablePath = path.join(binaryDirectory, `${command}.exe`);213 const comPath = path.join(binaryDirectory, `${command}.com`);214 await Promise.all([215 cp(process.execPath, executablePath),216 cp(process.execPath, comPath),217 ]);

TweaksSindre Sorhusbefore adoption698a1b8

line 218why, one step back
218 t.after(async () => {

Fix `preferLocal` argument escaping edge-case on Windows (#1259)Sindre Sorhusbefore adoptione771733

lines 219–232why, one step back
219 await Promise.all([unlink(executablePath), unlink(comPath)]);
220 });221 const options = {222 preferLocal: true,223 localDir: FIXTURES_DIRECTORY,224 extendEnv: false,225 env: {226 Path: path.dirname(process.execPath),227 PathExt: '.EXE;.COM',228 },229 };230 const nodeExpression = 'JSON.stringify({executablePath: process.execPath, argv0: process.argv0})';231 const {stdout} = await execa(command, ['--print', nodeExpression], options);232 const {executablePath: actualExecutablePath, argv0} = JSON.parse(stdout);

TweaksSindre Sorhusbefore adoption698a1b8

lines 233–234why, one step back
233 assert.equal(actualExecutablePath.toLowerCase(), executablePath.toLowerCase());
234 assert.equal(argv0, command);

Fix `preferLocal` argument escaping edge-case on Windows (#1259)Sindre Sorhusbefore adoptione771733

lines 235–237why, one step back
235
236 const {stdout: stdoutSync} = execaSync(command, ['--print', nodeExpression], options);237 const {executablePath: actualExecutablePathSync, argv0: argv0Sync} = JSON.parse(stdoutSync);

TweaksSindre Sorhusbefore adoption698a1b8

lines 238–239why, one step back
238 assert.equal(actualExecutablePathSync.toLowerCase(), executablePath.toLowerCase());
239 assert.equal(argv0Sync, command);

Fix `preferLocal` argument escaping edge-case on Windows (#1259)Sindre Sorhusbefore adoptione771733

lines 240–241why, one step back
240 });
241

TweaksSindre Sorhusbefore adoption698a1b8

line 242why, one step back
242 test('Respects NoDefaultCurrentDirectoryInExePath', async t => {

Fix `preferLocal` argument escaping edge-case on Windows (#1259)Sindre Sorhusbefore adoptione771733

lines 243–255why, one step back
243 const binaryDirectory = path.join(FIXTURES_DIRECTORY, 'node_modules', '.bin');
244 await mkdir(binaryDirectory, {recursive: true});245 const command = 'node-current-directory';246 const currentDirectoryExecutable = path.join(FIXTURES_DIRECTORY, `${command}.exe`);247 const pathExecutable = path.join(binaryDirectory, `${command}.exe`);248 await Promise.all([249 cp(process.execPath, currentDirectoryExecutable),250 cp(process.execPath, pathExecutable),251 ]);252253 const environmentName = 'NoDefaultCurrentDirectoryInExePath';254 const originalValue = process.env[environmentName];255 delete process.env[environmentName];

TweaksSindre Sorhusbefore adoption698a1b8

line 256why, one step back
256 t.after(async () => {

Fix `preferLocal` argument escaping edge-case on Windows (#1259)Sindre Sorhusbefore adoptione771733

lines 257–274why, one step back
257 if (originalValue === undefined) {
258 delete process.env[environmentName];259 } else {260 process.env[environmentName] = originalValue;261 }262263 await Promise.all([unlink(currentDirectoryExecutable), unlink(pathExecutable)]);264 });265266 const options = {267 cwd: FIXTURES_DIRECTORY,268 extendEnv: false,269 env: {270 Path: binaryDirectory,271 PathExt: '.EXE',272 },273 };274 const {stdout: currentDirectoryStdout} = await execa(command, ['--print', 'process.execPath'], options);

TweaksSindre Sorhusbefore adoption698a1b8

line 275why, one step back
275 assert.equal(currentDirectoryStdout.toLowerCase(), currentDirectoryExecutable.toLowerCase());

Fix `preferLocal` argument escaping edge-case on Windows (#1259)Sindre Sorhusbefore adoptione771733

lines 276–277why, one step back
276
277 const {stdout: currentDirectoryStdoutSync} = execaSync(command, ['--print', 'process.execPath'], options);

TweaksSindre Sorhusbefore adoption698a1b8

line 278why, one step back
278 assert.equal(currentDirectoryStdoutSync.toLowerCase(), currentDirectoryExecutable.toLowerCase());

Fix `preferLocal` argument escaping edge-case on Windows (#1259)Sindre Sorhusbefore adoptione771733

lines 279–281why, one step back
279
280 process.env[environmentName] = '1';281 const {stdout} = await execa(command, ['--print', 'process.execPath'], options);

TweaksSindre Sorhusbefore adoption698a1b8

line 282why, one step back
282 assert.equal(stdout.toLowerCase(), pathExecutable.toLowerCase());

Fix `preferLocal` argument escaping edge-case on Windows (#1259)Sindre Sorhusbefore adoptione771733

lines 283–284why, one step back
283
284 const {stdout: stdoutSync} = execaSync(command, ['--print', 'process.execPath'], options);

TweaksSindre Sorhusbefore adoption698a1b8

line 285why, one step back
285 assert.equal(stdoutSync.toLowerCase(), pathExecutable.toLowerCase());

Fix `preferLocal` argument escaping edge-case on Windows (#1259)Sindre Sorhusbefore adoptione771733

lines 286–287why, one step back
286 });
287

TweaksSindre Sorhusbefore adoption698a1b8

line 288why, one step back
288 test('Uses the resolved batch file when current-directory search is disabled', async t => {

Fix `preferLocal` argument escaping edge-case on Windows (#1259)Sindre Sorhusbefore adoptione771733

lines 289–301why, one step back
289 const binaryDirectory = path.join(FIXTURES_DIRECTORY, 'node_modules', '.bin');
290 await mkdir(binaryDirectory, {recursive: true});291 const command = 'batch-current-directory';292 const currentDirectoryBatchFile = path.join(FIXTURES_DIRECTORY, `${command}.cmd`);293 const pathBatchFile = path.join(binaryDirectory, `${command}.cmd`);294 await Promise.all([295 writeFile(currentDirectoryBatchFile, '@echo current directory'),296 writeFile(pathBatchFile, '@echo PATH'),297 ]);298299 const environmentName = 'NoDefaultCurrentDirectoryInExePath';300 const originalValue = process.env[environmentName];301 delete process.env[environmentName];

TweaksSindre Sorhusbefore adoption698a1b8

line 302why, one step back
302 t.after(async () => {

Fix `preferLocal` argument escaping edge-case on Windows (#1259)Sindre Sorhusbefore adoptione771733

lines 303–321why, one step back
303 if (originalValue === undefined) {
304 delete process.env[environmentName];305 } else {306 process.env[environmentName] = originalValue;307 }308309 await Promise.all([unlink(currentDirectoryBatchFile), unlink(pathBatchFile)]);310 });311312 const options = {313 cwd: FIXTURES_DIRECTORY,314 extendEnv: false,315 env: {316 Path: binaryDirectory,317 PathExt: '.CMD',318 [environmentName]: '',319 },320 };321 const {stdout} = await execa(command, options);

TweaksSindre Sorhusbefore adoption698a1b8

line 322why, one step back
322 assert.equal(stdout, 'PATH');

Fix `preferLocal` argument escaping edge-case on Windows (#1259)Sindre Sorhusbefore adoptione771733

lines 323–324why, one step back
323
324 const {stdout: stdoutSync} = execaSync(command, options);

TweaksSindre Sorhusbefore adoption698a1b8

line 325why, one step back
325 assert.equal(stdoutSync, 'PATH');

Fix `preferLocal` argument escaping edge-case on Windows (#1259)Sindre Sorhusbefore adoptione771733

lines 326–329why, one step back
326
327 delete options.env[environmentName];328 process.env[environmentName] = '';329 const {stdout: parentEnvironmentStdout} = await execa(command, options);

TweaksSindre Sorhusbefore adoption698a1b8

line 330why, one step back
330 assert.equal(parentEnvironmentStdout, 'PATH');

Fix `preferLocal` argument escaping edge-case on Windows (#1259)Sindre Sorhusbefore adoptione771733

lines 331–332why, one step back
331
332 const {stdout: parentEnvironmentStdoutSync} = execaSync(command, options);

TweaksSindre Sorhusbefore adoption698a1b8

line 333why, one step back
333 assert.equal(parentEnvironmentStdoutSync, 'PATH');

Fix `preferLocal` argument escaping edge-case on Windows (#1259)Sindre Sorhusbefore adoptione771733

lines 334–341why, one step back
334 });
335336 test('Does not search PATH for drive-relative commands', async t => {337 const binaryDirectory = path.join(FIXTURES_DIRECTORY, 'node_modules', '.bin');338 await mkdir(binaryDirectory, {recursive: true});339 const commandName = 'node-drive-relative';340 const pathExecutable = path.join(binaryDirectory, `${commandName}.exe`);341 await cp(process.execPath, pathExecutable);

TweaksSindre Sorhusbefore adoption698a1b8

line 342why, one step back
342 t.after(async () => {

Fix `preferLocal` argument escaping edge-case on Windows (#1259)Sindre Sorhusbefore adoptione771733

lines 343–354why, one step back
343 await unlink(pathExecutable);
344 });345 const drive = path.parse(FIXTURES_DIRECTORY).root.slice(0, 2);346 const command = `${drive}${commandName}`;347 const options = {348 cwd: FIXTURES_DIRECTORY,349 extendEnv: false,350 env: {351 Path: binaryDirectory,352 PathExt: '.EXE',353 },354 };

TweaksSindre Sorhusbefore adoption698a1b8

lines 355–356why, one step back
355 await assertRejects(execa(command, [], options));
356 assertThrows(() => execaSync(command, [], options));

Fix `preferLocal` argument escaping edge-case on Windows (#1259)Sindre Sorhusbefore adoptione771733

lines 357–358why, one step back
357 });
358

Move away from `cross-spawn` (#1251)Sindre Sorhusbefore adoption4cafc3f

lines 359–360why, one step back
359 // A `.cmd` file needs `cmd.exe`, so its forward-slash path must be normalized to
360 // backslashes, otherwise it fails with ENOENT.

TweaksSindre Sorhusbefore adoption698a1b8

line 361why, one step back
361 test('Runs a .cmd file given as a relative POSIX-style subpath', async () => {

Move away from `cross-spawn` (#1251)Sindre Sorhusbefore adoption4cafc3f

line 362why, one step back
362 const {stdout} = await execa('fixtures/hello.cmd', {cwd: path.join(FIXTURES_DIRECTORY, '..')});

TweaksSindre Sorhusbefore adoption698a1b8

line 363why, one step back
363 assert.equal(stdout, 'Hello World');

Move away from `cross-spawn` (#1251)Sindre Sorhusbefore adoption4cafc3f

lines 364–366why, one step back
364 });
365366 /*

Fix arguments of Windows batch files that read them with `%~1`Sindre Sorhusbefore adoption93bddc4

line 367why, one step back
367 Metacharacters must survive `cmd.exe` interpreting them twice for batch files: once when the batch file is invoked and once when it re-expands the arguments with `%*`.

Move away from `cross-spawn` (#1251)Sindre Sorhusbefore adoption4cafc3f

lines 368–377why, one step back
368 The `cmd`-shims npm generates in `node_modules/.bin/` are the canonical example.
369 */370 const setupCmdShim = async () => {371 const binaryDirectory = path.join(FIXTURES_DIRECTORY, 'node_modules', '.bin');372 await mkdir(binaryDirectory, {recursive: true});373 const shimPath = path.join(binaryDirectory, 'echo-cmd-shim.cmd');374 await cp(path.join(FIXTURES_DIRECTORY, 'echo-cmd-shim.cmd'), shimPath);375 return shimPath;376 };377

Fix arguments of Windows batch files that read them with `%~1`Sindre Sorhusbefore adoption93bddc4

line 378why, one step back
378 test('Escapes metacharacters for node_modules/.bin cmd-shims', async () => {

Move away from `cross-spawn` (#1251)Sindre Sorhusbefore adoption4cafc3f

lines 379–381why, one step back
379 const shimPath = await setupCmdShim();
380 const commandArgument = '"(foo|bar>baz|foz)"';381 const {stdout} = await execa(shimPath, [commandArgument]);

TweaksSindre Sorhusbefore adoption698a1b8

line 382why, one step back
382 assert.equal(stdout, commandArgument);

Move away from `cross-spawn` (#1251)Sindre Sorhusbefore adoption4cafc3f

lines 383–384why, one step back
383
384 const {stdout: stdoutSync} = execaSync(shimPath, [commandArgument]);

TweaksSindre Sorhusbefore adoption698a1b8

line 385why, one step back
385 assert.equal(stdoutSync, commandArgument);

Fix `preferLocal` argument escaping edge-case on Windows (#1259)Sindre Sorhusbefore adoptione771733

lines 386–387why, one step back
386 });
387

Fix arguments of Windows batch files that read them with `%~1`Sindre Sorhusbefore adoption93bddc4

line 388why, one step back
388 test('Escapes metacharacters for explicit batch files excluded from PATHEXT', async () => {

Fix `preferLocal` argument escaping edge-case on Windows (#1259)Sindre Sorhusbefore adoptione771733

lines 389–391why, one step back
389 const commandArgument = '"& whoami &"';
390 const command = path.join(FIXTURES_DIRECTORY, 'echo-shim.cmd');391 const {stdout} = await execa(command, [commandArgument], nodeOnlyOptions);

TweaksSindre Sorhusbefore adoption698a1b8

line 392why, one step back
392 assert.equal(stdout, commandArgument);

Fix `preferLocal` argument escaping edge-case on Windows (#1259)Sindre Sorhusbefore adoptione771733

lines 393–394why, one step back
393
394 const {stdout: stdoutSync} = execaSync(command, [commandArgument], nodeOnlyOptions);

TweaksSindre Sorhusbefore adoption698a1b8

line 395why, one step back
395 assert.equal(stdoutSync, commandArgument);

Fix `preferLocal` argument escaping edge-case on Windows (#1259)Sindre Sorhusbefore adoptione771733

lines 396–397why, one step back
396 });
397

TweaksSindre Sorhusbefore adoption698a1b8

line 398why, one step back
398 test('Runs an explicit shebang script excluded from PATHEXT', async () => {

Fix `preferLocal` argument escaping edge-case on Windows (#1259)Sindre Sorhusbefore adoptione771733

line 399why, one step back
399 const command = path.join(FIXTURES_DIRECTORY, 'echo.js');

TweaksSindre Sorhusbefore adoption698a1b8

line 400why, one step back
400 await testResolvesCommand(command, nodeOnlyOptions);

Fix `preferLocal` argument escaping edge-case on Windows (#1259)Sindre Sorhusbefore adoptione771733

lines 401–402why, one step back
401 });
402

Fix arguments of Windows batch files that read them with `%~1`Sindre Sorhusbefore adoption93bddc4

line 403why, one step back
403 test('Escapes metacharacters for preferLocal cmd-shims', async () => {

Fix `preferLocal` argument escaping edge-case on Windows (#1259)Sindre Sorhusbefore adoptione771733

lines 404–419why, one step back
404 await setupCmdShim();
405 const commandArgument = 'a&whoami';406 const originalPathExt = process.env.PATHEXT;407 process.env.PATHEXT = '.EXE';408 const options = {409 preferLocal: true,410 localDir: FIXTURES_DIRECTORY,411 extendEnv: false,412 env: {413 Path: path.dirname(process.execPath),414 PathExt: '.EXE;.CMD',415 },416 };417418 try {419 const {stdout} = await execa('echo-cmd-shim', [commandArgument], options);

TweaksSindre Sorhusbefore adoption698a1b8

line 420why, one step back
420 assert.equal(stdout, commandArgument);

Fix `preferLocal` argument escaping edge-case on Windows (#1259)Sindre Sorhusbefore adoptione771733

lines 421–422why, one step back
421
422 const {stdout: stdoutSync} = execaSync('echo-cmd-shim', [commandArgument], options);

TweaksSindre Sorhusbefore adoption698a1b8

line 423why, one step back
423 assert.equal(stdoutSync, commandArgument);

Fix `preferLocal` argument escaping edge-case on Windows (#1259)Sindre Sorhusbefore adoptione771733

lines 424–430why, one step back
424 } finally {
425 if (originalPathExt === undefined) {426 delete process.env.PATHEXT;427 } else {428 process.env.PATHEXT = originalPathExt;429 }430 }

Move away from `cross-spawn` (#1251)Sindre Sorhusbefore adoption4cafc3f

lines 431–439why, one step back
431 });
432}433434// On Windows, `.cmd`/`.bat` files are run through `cmd.exe`, which interprets several435// characters specially. Those must be escaped in arguments, otherwise they could expand436// variables or inject commands (the "BatBadBut" class of vulnerabilities). `echo-shim.cmd`437// forwards its arguments to `echo.js`, so the subprocess prints back the exact438// `process.argv` it received: each argument must arrive unchanged and inert.439if (isWindows) {

TweaksSindre Sorhusbefore adoption698a1b8

line 440why, one step back
440 const testEscaping = async commandArgument => {

Move away from `cross-spawn` (#1251)Sindre Sorhusbefore adoption4cafc3f

line 441why, one step back
441 const {stdout} = await execa('echo-shim.cmd', [commandArgument]);

TweaksSindre Sorhusbefore adoption698a1b8

line 442why, one step back
442 assert.equal(stdout, commandArgument);

Move away from `cross-spawn` (#1251)Sindre Sorhusbefore adoption4cafc3f

lines 443–444why, one step back
443
444 const {stdout: stdoutSync} = execaSync('echo-shim.cmd', [commandArgument]);

TweaksSindre Sorhusbefore adoption698a1b8

line 445why, one step back
445 assert.equal(stdoutSync, commandArgument);

Move away from `cross-spawn` (#1251)Sindre Sorhusbefore adoption4cafc3f

lines 446–448why, one step back
446 };
447448 // A `%VAR%` argument must be passed literally, not expanded by `cmd.exe`.

TweaksSindre Sorhusbefore adoption698a1b8

line 449why, one step back
449 test('Does not expand environment variables in arguments', () => testEscaping('%PATH%'));

Move away from `cross-spawn` (#1251)Sindre Sorhusbefore adoption4cafc3f

line 450why, one step back
450 // Delayed expansion (`!VAR!`) must not be interpreted either.

TweaksSindre Sorhusbefore adoption698a1b8

line 451why, one step back
451 test('Does not expand delayed environment variables in arguments', () => testEscaping('!PATH!'));

Move away from `cross-spawn` (#1251)Sindre Sorhusbefore adoption4cafc3f

line 452why, one step back
452 // An `&` must not be interpreted as a command separator.

TweaksSindre Sorhusbefore adoption698a1b8

line 453why, one step back
453 test('Does not allow command injection via `&` in arguments', () => testEscaping('a&b'));

Move away from `cross-spawn` (#1251)Sindre Sorhusbefore adoption4cafc3f

line 454why, one step back
454 // A `|` must not be interpreted as a pipe.

TweaksSindre Sorhusbefore adoption698a1b8

line 455why, one step back
455 test('Does not allow command injection via `|` in arguments', () => testEscaping('a|whoami'));

Move away from `cross-spawn` (#1251)Sindre Sorhusbefore adoption4cafc3f

line 456why, one step back
456 // Redirections (`<`, `>`) must not be interpreted.

TweaksSindre Sorhusbefore adoption698a1b8

line 457why, one step back
457 test('Does not allow command injection via redirections in arguments', () => testEscaping('a>b<c'));

Move away from `cross-spawn` (#1251)Sindre Sorhusbefore adoption4cafc3f

line 458why, one step back
458 // Parentheses must not be interpreted as command grouping.

TweaksSindre Sorhusbefore adoption698a1b8

line 459why, one step back
459 test('Does not allow command injection via parentheses in arguments', () => testEscaping('(whoami)'));

Move away from `cross-spawn` (#1251)Sindre Sorhusbefore adoption4cafc3f

line 460why, one step back
460 // A quote followed by an injected command must stay inert.

TweaksSindre Sorhusbefore adoption698a1b8

line 461why, one step back
461 test('Does not allow command injection via nested quotes in arguments', () => testEscaping('"& whoami &"'));

Move away from `cross-spawn` (#1251)Sindre Sorhusbefore adoption4cafc3f

line 462why, one step back
462 // Double quotes must be preserved.

TweaksSindre Sorhusbefore adoption698a1b8

line 463why, one step back
463 test('Preserves double quotes in arguments', () => testEscaping('a"b"c'));

Move away from `cross-spawn` (#1251)Sindre Sorhusbefore adoption4cafc3f

line 464why, one step back
464 // Carets are the escape character in `cmd.exe` and must be passed literally.

TweaksSindre Sorhusbefore adoption698a1b8

line 465why, one step back
465 test('Preserves carets in arguments', () => testEscaping('a^b^^c'));

Move away from `cross-spawn` (#1251)Sindre Sorhusbefore adoption4cafc3f

line 466why, one step back
466 // Backslashes, including trailing ones, must be preserved next to quotes.

TweaksSindre Sorhusbefore adoption698a1b8

line 467why, one step back
467 test('Preserves backslashes and double quotes in arguments', () => testEscaping('a\\"b\\\\"c\\'));

Move away from `cross-spawn` (#1251)Sindre Sorhusbefore adoption4cafc3f

lines 468–472why, one step back
468
469 // Every kind of tricky argument must also roundtrip through `cmd.exe` unchanged, not470 // just be inert. Unlike `echo.js` (which resolves to `node.exe` and bypasses `cmd.exe`),471 // `echo-shim.cmd` forces the `cmd.exe` escaping path. This mirrors the roundtrip tests472 // above, but validates the `cmd.exe`-specific escaping rather than Node's native escaping.

TweaksSindre Sorhusbefore adoption698a1b8

line 473why, one step back
473 const testCmdRoundtrip = async commandArguments => {

Move away from `cross-spawn` (#1251)Sindre Sorhusbefore adoption4cafc3f

lines 474–476why, one step back
474 const expectedStdout = commandArguments.join('\n');
475476 const {stdout} = await execa('echo-shim.cmd', commandArguments);

TweaksSindre Sorhusbefore adoption698a1b8

line 477why, one step back
477 assert.equal(stdout, expectedStdout);

Move away from `cross-spawn` (#1251)Sindre Sorhusbefore adoption4cafc3f

lines 478–479why, one step back
478
479 const {stdout: stdoutSync} = execaSync('echo-shim.cmd', commandArguments);

TweaksSindre Sorhusbefore adoption698a1b8

line 480why, one step back
480 assert.equal(stdoutSync, expectedStdout);

Move away from `cross-spawn` (#1251)Sindre Sorhusbefore adoption4cafc3f

lines 481–483why, one step back
481 };
482483 // eslint-disable-next-line no-template-curly-in-string

TweaksSindre Sorhusbefore adoption698a1b8

lines 484–485why, one step back
484 test('Roundtrips shell metacharacters through cmd.exe', () => testCmdRoundtrip(['()', '[]', '%!', '^', '<', '>', '|', ';', ',', '=', '`', '*', '?', '$HOME', '${x}', '~']));
485 test('Roundtrips backslashes and double quotes through cmd.exe', () => testCmdRoundtrip(['a\\b', 'a\\\\b', 'a\\', 'a\\"b', '\\"', 'a"\\', '\\\\"', '"\\\\"']));

Move away from `cross-spawn` (#1251)Sindre Sorhusbefore adoption4cafc3f

lines 486–488why, one step back
486 // Long backslash runs must also roundtrip through the `cmd.exe` escaping specifically,
487 // which is where the backslash-doubling actually runs. Kept modest to stay under the488 // `cmd.exe` command-line length limit once the backslashes are doubled.

TweaksSindre Sorhusbefore adoption698a1b8

line 489why, one step back
489 test('Roundtrips long backslash runs before quotes through cmd.exe', () => testCmdRoundtrip([`${'\\'.repeat(200)}"`, '\\'.repeat(200), `a${'\\'.repeat(100)}"b`]));

Move away from `cross-spawn` (#1251)Sindre Sorhusbefore adoption4cafc3f

lines 490–491why, one step back
490 // The per-length sweep must roundtrip through `cmd.exe` too, since the corrupted doubling
491 // is exactly what this escaping path performs.

TweaksSindre Sorhusbefore adoption698a1b8

line 492why, one step back
492 test('Roundtrips backslash runs of every length through cmd.exe', () => testCmdRoundtrip(backslashRunArguments));

Move away from `cross-spawn` (#1251)Sindre Sorhusbefore adoption4cafc3f

line 493why, one step back
493 // Consecutive double quotes must survive the `cmd.exe` escaping, each escaped on its own.

TweaksSindre Sorhusbefore adoption698a1b8

lines 494–495why, one step back
494 test('Roundtrips consecutive double quotes through cmd.exe', () => testCmdRoundtrip(['""', '"""', 'a""b', '""""']));
495 test('Roundtrips arguments with spaces through cmd.exe', () => testCmdRoundtrip(['a b', ' '.repeat(3), 'foo bar baz', 'André Cruz']));

Move away from `cross-spawn` (#1251)Sindre Sorhusbefore adoption4cafc3f

lines 496–497why, one step back
496
497 /*

Fix arguments of Windows batch files that read them with `%~1`Sindre Sorhusbefore adoption93bddc4

line 498why, one step back
498 `.bat` files re-expand their arguments through `cmd.exe` exactly like `.cmd` files, so they need the same escaping.

Move away from `cross-spawn` (#1251)Sindre Sorhusbefore adoption4cafc3f

lines 499–500why, one step back
499 `echo-shim.bat` is the `.bat` twin of `echo-shim.cmd`.
500 */

TweaksSindre Sorhusbefore adoption698a1b8

line 501why, one step back
501 const testBatEscaping = async commandArgument => {

Move away from `cross-spawn` (#1251)Sindre Sorhusbefore adoption4cafc3f

line 502why, one step back
502 const {stdout} = await execa('echo-shim.bat', [commandArgument]);

TweaksSindre Sorhusbefore adoption698a1b8

line 503why, one step back
503 assert.equal(stdout, commandArgument);

Move away from `cross-spawn` (#1251)Sindre Sorhusbefore adoption4cafc3f

lines 504–505why, one step back
504
505 const {stdout: stdoutSync} = execaSync('echo-shim.bat', [commandArgument]);

TweaksSindre Sorhusbefore adoption698a1b8

line 506why, one step back
506 assert.equal(stdoutSync, commandArgument);

Move away from `cross-spawn` (#1251)Sindre Sorhusbefore adoption4cafc3f

lines 507–509why, one step back
507 };
508509 // The same nested-quote injection must stay inert when the batch file is a `.bat`.

TweaksSindre Sorhusbefore adoption698a1b8

line 510why, one step back
510 test('Does not allow command injection via nested quotes in `.bat` arguments', () => testBatEscaping('"& whoami &"'));

Move away from `cross-spawn` (#1251)Sindre Sorhusbefore adoption4cafc3f

line 511why, one step back
511 // Metacharacters must survive the double `cmd.exe` expansion for `.bat` files too.

TweaksSindre Sorhusbefore adoption698a1b8

line 512why, one step back
512 test('Roundtrips shell metacharacters through a `.bat` file', () => testBatEscaping('(foo|bar>baz|foz)'));

Fix arguments of Windows batch files that read them with `%~1`Sindre Sorhusbefore adoption93bddc4

line 513why, one step back
513 // A `.bat` file not in `node_modules/.bin/` must still be escaped against the double expansion, since the location is irrelevant: any batch file can re-expand its arguments.

TweaksSindre Sorhusbefore adoption698a1b8

line 514why, one step back
514 test('Does not expand environment variables in `.bat` arguments', () => testBatEscaping('%PATH%'));

Move away from `cross-spawn` (#1251)Sindre Sorhusbefore adoption4cafc3f

line 515why, one step back
515 // Backslashes, double quotes and metacharacters combined are the trickiest to escape, and must survive the double `cmd.exe` expansion of a `.bat` file intact.

TweaksSindre Sorhusbefore adoption698a1b8

line 516why, one step back
516 test('Preserves backslashes, quotes and metacharacters in `.bat` arguments', () => testBatEscaping('a\\"&b\\\\"|c\\'));

Fix arguments of Windows batch files that read them with `%~1`Sindre Sorhusbefore adoption93bddc4

line 517why, one step back
517 // Several tricky arguments passed together must each be escaped independently and arrive in order, not merged nor reordered.

TweaksSindre Sorhusbefore adoption698a1b8

line 518why, one step back
518 test('Roundtrips multiple tricky arguments through a `.bat` file', async () => {

Move away from `cross-spawn` (#1251)Sindre Sorhusbefore adoption4cafc3f

lines 519–522why, one step back
519 const commandArguments = ['a b', 'c&d', '"e"', 'f\\"g', '%h%'];
520 const expectedStdout = commandArguments.join('\n');521522 const {stdout} = await execa('echo-shim.bat', commandArguments);

TweaksSindre Sorhusbefore adoption698a1b8

line 523why, one step back
523 assert.equal(stdout, expectedStdout);

Move away from `cross-spawn` (#1251)Sindre Sorhusbefore adoption4cafc3f

lines 524–525why, one step back
524
525 const {stdout: stdoutSync} = execaSync('echo-shim.bat', commandArguments);

TweaksSindre Sorhusbefore adoption698a1b8

line 526why, one step back
526 assert.equal(stdoutSync, expectedStdout);

Move away from `cross-spawn` (#1251)Sindre Sorhusbefore adoption4cafc3f

line 527why, one step back
527 });

Fix arguments of Windows batch files that read them with `%~1`Sindre Sorhusbefore adoption93bddc4

lines 528–545why, one step back
528
529 /*530 Unlike cmd-shims, some batch files read their own arguments with `%1` or `%~1` instead of forwarding them with `%*`, like Maven's `mvn.cmd`.531 `read-arguments.cmd` prints its first argument, then its second one when the first one is `-f`.532 `ECHO` ends lines with CRLF.533 */534 const testReadArguments = async (commandArguments, expectedLines) => {535 const {stdout} = await execa('read-arguments.cmd', commandArguments);536 assert.deepEqual(stdout.split('\r\n'), expectedLines);537538 const {stdout: stdoutSync} = execaSync('read-arguments.cmd', commandArguments);539 assert.deepEqual(stdoutSync.split('\r\n'), expectedLines);540 };541542 test('Passes arguments to batch files reading them with `%~1`', () => testReadArguments(['-f', 'C:\\repo\\pom.xml'], ['first is -f', 'file is "C:\\repo\\pom.xml"']));543 test('Passes metacharacters to batch files reading them with `%~1`', () => testReadArguments(['-f', 'C:\\my repo\\a&b|c%PATH%.xml'], ['first is -f', 'file is "C:\\my repo\\a&b|c%PATH%.xml"']));544 // Like Rust, only arguments that need quoting are quoted, so `%1` matches what the user passed, like when run from a terminal545 test('Only quotes arguments that need it for batch files reading them with `%1`', () => testReadArguments(['a b'], ['first is "a b"']));

Move away from `cross-spawn` (#1251)Sindre Sorhusbefore adoption4cafc3f

lines 546–548why, one step back
546
547 // `cmd.exe` interprets CR and LF as command separators and provides no way to escape548 // them, so those are rejected to prevent command injection.

TweaksSindre Sorhusbefore adoption698a1b8

lines 549–551why, one step back
549 test('Rejects arguments containing a line break', () => {
550 assertThrows(() => execa('echo-shim.cmd', ['a\r\nb']), {instanceOf: TypeError, message: /line break/});551 assertThrows(() => execaSync('echo-shim.cmd', ['a\r\nb']), {instanceOf: TypeError, message: /line break/});

Move away from `cross-spawn` (#1251)Sindre Sorhusbefore adoption4cafc3f

lines 552–554why, one step back
552 });
553554 // The command itself is validated too, not just its arguments.

TweaksSindre Sorhusbefore adoption698a1b8

lines 555–557why, one step back
555 test('Rejects a command containing a line break', () => {
556 assertThrows(() => execa('echo\r\nshim.cmd', []), {instanceOf: TypeError, message: /line break/});557 assertThrows(() => execaSync('echo\r\nshim.cmd', []), {instanceOf: TypeError, message: /line break/});

Move away from `cross-spawn` (#1251)Sindre Sorhusbefore adoption4cafc3f

lines 558–560why, one step back
558 });
559560 // A shell escapes the arguments itself, so the line break rejection does not apply.

TweaksSindre Sorhusbefore adoption698a1b8

lines 561–563why, one step back
561 test('Does not reject line breaks when using a shell', async () => {
562 await assert.doesNotReject(execa('echo-shim.cmd', ['a\r\nb'], {shell: true, reject: false}));563 assert.doesNotThrow(() => execaSync('echo-shim.cmd', ['a\r\nb'], {shell: true, reject: false}));

Move away from `cross-spawn` (#1251)Sindre Sorhusbefore adoption4cafc3f

lines 564–565why, one step back
564 });
565}