Improve process arguments validation (#873)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by ehmicky · committed by GitHub · parent 2cd41ef · Browse files · 4 files·+53 added−12 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+53 added−12 removed
- tests:+41 added−8 removed
- other:+12 added−4 removed
Provenance
- Commit
54b5ea7- Tree
d91de23- Author
ehmicky <ehmicky@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
2cd41ef
4 files changed+53 added−12 removed
modified:lib/arguments/escape.js+1 added-3 removed
5 unmodified lines6789101112101112131448 unmodified lines5 unmodified lines const escapedCommand = fileAndArgs.map(arg => quoteString(escapeControlCharacters(arg))).join(' '); return {command, escapedCommand};};
const escapeControlCharacters = arg => typeof arg === 'string' ? arg.replaceAll(SPECIAL_CHAR_REGEXP, character => escapeControlCharacter(character)) : String(arg);const escapeControlCharacters = arg => arg.replaceAll(SPECIAL_CHAR_REGEXP, character => escapeControlCharacter(character));
const escapeControlCharacter = character => { const commonEscape = COMMON_ESCAPES[character]; if (commonEscape !== undefined) {48 unmodified linesmodified:lib/arguments/options.js+11 added-1 removed
18 unmodified lines19202122232425262728293031323334353627373839404173 unmodified lines18 unmodified lines if (!Array.isArray(args)) { throw new TypeError(`Second argument must be either an array of arguments or an options object: ${args}`); }
if (args.some(arg => typeof arg === 'object' && arg !== null)) { throw new TypeError(`Second argument must be an array of strings: ${args}`); }
const normalizedArgs = args.map(String); const nullByteArg = normalizedArgs.find(arg => arg.includes('\0')); if (nullByteArg !== undefined) { throw new TypeError(`Arguments cannot contain null bytes ("\\0"): ${nullByteArg}`); }
if (!isPlainObject(options)) { throw new TypeError(`Last argument must be an options object: ${options}`); }
return [filePath, args, options]; return [filePath, normalizedArgs, options];};
export const handleArguments = (filePath, rawArgs, rawOptions) => { const {command, escapedCommand} = joinCommand(filePath, rawArgs);73 unmodified linesmodified:test/arguments/options.js+34 added
131 unmodified lines132 test('execa()\'s second argument must be an array', testInvalidArgs, execa);133 test('execaSync()\'s second argument must be an array', testInvalidArgs, execaSync);134 test('execaNode()\'s second argument must be an array', testInvalidArgs, execaNode);135 136+const testInvalidArgsItems = async (t, execaMethod) => {137+ t.throws(() => {138+ execaMethod('echo', [{}]);139+ }, {message: 'Second argument must be an array of strings: [object Object]'});140+};141+142+test('execa()\'s second argument must not be objects', testInvalidArgsItems, execa);143+test('execaSync()\'s second argument must not be objects', testInvalidArgsItems, execaSync);144+test('execaNode()\'s second argument must not be objects', testInvalidArgsItems, execaNode);145+146+const testNullByteArg = async (t, execaMethod) => {147+ t.throws(() => {148+ execaMethod('echo', ['a\0b']);149+ }, {message: /null bytes/});150+};151+152+test('execa()\'s second argument must not include \\0', testNullByteArg, execa);153+test('execaSync()\'s second argument must not include \\0', testNullByteArg, execaSync);154+test('execaNode()\'s second argument must not include \\0', testNullByteArg, execaNode);155+156+const testSerializeArg = async (t, arg) => {157+ const {stdout} = await execa('noop.js', [arg]);158+ t.is(stdout, String(arg));159+};160+161+test('execa()\'s arguments can be numbers', testSerializeArg, 1);162+test('execa()\'s arguments can be booleans', testSerializeArg, true);163+test('execa()\'s arguments can be NaN', testSerializeArg, Number.NaN);164+test('execa()\'s arguments can be Infinity', testSerializeArg, Number.POSITIVE_INFINITY);165+test('execa()\'s arguments can be null', testSerializeArg, null);166+test('execa()\'s arguments can be undefined', testSerializeArg, undefined);167+test('execa()\'s arguments can be bigints', testSerializeArg, 1n);168+test('execa()\'s arguments can be symbols', testSerializeArg, Symbol('test'));169+170 const testInvalidOptions = async (t, execaMethod) => {171 t.throws(() => {172 execaMethod('echo', [], new Map());173 }, {message: /Last argument must be an options object/});11 unmodified linesmodified:test/script.js+7 added-8 removed
236 unmodified lines237 test('$ splits expressions - \\u{0063}}', testScriptStdout, () => $`echo.js ${'a'}\u{0063}}${'b'}`, 'ac}b');238 test('$ concatenates tokens - \\u{0063}}', testScriptStdout, () => $`echo.js \u{0063}}a\u{0063}} b`, 'c}ac}\nb');239 test('$ concatenates expressions - \\u{0063}}', testScriptStdout, () => $`echo.js \u{0063}}${'a'}\u{0063}} b`, 'c}ac}\nb');240 241-const testScriptErrorStdout = async (t, getChildProcess, expectedStdout) => {242- const {command} = await t.throwsAsync(getChildProcess(), {code: 'ERR_INVALID_ARG_VALUE'});243- t.is(command, `echo.js ${expectedStdout}`);241+const testScriptErrorStdout = async (t, getChildProcess) => {242+ t.throws(getChildProcess, {message: /null bytes/});243 };244 246-test('$ handles tokens - \\0', testScriptErrorStdout, () => $`echo.js \0`, '\0');247-test('$ splits tokens - \\0', testScriptErrorStdout, () => $`echo.js a\0b`, 'a\0b');248-test('$ splits expressions - \\0', testScriptErrorStdout, () => $`echo.js ${'a'}\0${'b'}`, 'a\0b');249-test('$ concatenates tokens - \\0', testScriptErrorStdout, () => $`echo.js \0a\0 b`, '\0a\0 b');250-test('$ concatenates expressions - \\0', testScriptErrorStdout, () => $`echo.js \0${'a'}\0 b`, '\0a\0 b');245+test('$ handles tokens - \\0', testScriptErrorStdout, () => $`echo.js \0`);246+test('$ splits tokens - \\0', testScriptErrorStdout, () => $`echo.js a\0b`);247+test('$ splits expressions - \\0', testScriptErrorStdout, () => $`echo.js ${'a'}\0${'b'}`);248+test('$ concatenates tokens - \\0', testScriptErrorStdout, () => $`echo.js \0a\0 b`);249+test('$ concatenates expressions - \\0', testScriptErrorStdout, () => $`echo.js \0${'a'}\0 b`);250 251 const testScriptStdoutSync = (t, getChildProcess, expectedStdout) => {252 const {stdout} = getChildProcess();253 t.is(stdout, expectedStdout);114 unmodified lines