Skip to content
Myr

Improve input validation (#838)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by ehmicky · committed by GitHub · parent 672c2d7 · Browse files · 10 files+88 added−13 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

10 files+88 added−13 removed

  • tests:+48 added−0 removed
  • other:+40 added−13 removed

10 files+88 added−13 removed

modified:lib/arguments/node.js+2 added-2 removed
modified:lib/arguments/options.js+18 added-5 removed
modified:lib/async.js+2 added-2 removed
1import {setMaxListeners} from 'node:events';2import childProcess from 'node:child_process';3import {handleOptionalArguments, handleArguments} from './arguments/options.js';3import {normalizeArguments, handleArguments} from './arguments/options.js';4import {makeError, makeEarlyError, makeSuccessResult} from './return/error.js';5import {handleOutput} from './return/output.js';6import {handleInputAsync, pipeOutputAsync, cleanupStdioStreams} from './stdio/async.js';7import {spawnedKill} from './exit/kill.js';
36 unmodified lines
44 return spawned;45};4647const handleAsyncArguments = (rawFile, rawArgs, rawOptions) => {48 [rawArgs, rawOptions] = handleOptionalArguments(rawArgs, rawOptions);48 [rawFile, rawArgs, rawOptions] = normalizeArguments(rawFile, rawArgs, rawOptions);49 const {file, args, command, escapedCommand, options: normalizedOptions} = handleArguments(rawFile, rawArgs, rawOptions);50 const options = handleAsyncOptions(normalizedOptions);51 return {file, args, command, escapedCommand, options};52};
42 unmodified lines
modified:lib/command.js+4 added
modified:lib/script.js+11 added-2 removed
1import {ChildProcess} from 'node:child_process';2import isPlainObject from 'is-plain-obj';3import {isBinary, binaryToString} from './utils.js';4import {execa} from './async.js';5import {execaSync} from './sync.js';67const create$ = options => {8 function $(templatesOrOptions, ...expressions) {8 if (!Array.isArray(templatesOrOptions)) {9 if (isPlainObject(templatesOrOptions)) {10 return create$({...options, ...templatesOrOptions});11 }1213 if (!Array.isArray(templatesOrOptions)) {14 throw new TypeError('Please use either $(option) or $`command`.');15 }1617 const [file, ...args] = parseTemplates(templatesOrOptions, expressions);18 return execa(file, args, normalizeScriptOptions(options));19 }2021 $.sync = (templates, ...expressions) => {17 if (!Array.isArray(templates)) {22 if (isPlainObject(templates)) {23 throw new TypeError('Please use $(options).sync`command` instead of $.sync(options)`command`.');24 }2526 if (!Array.isArray(templates)) {27 throw new TypeError('A template string must be used: $.sync`command`.');28 }2930 const [file, ...args] = parseTemplates(templates, expressions);31 return execaSync(file, args, normalizeScriptOptions(options));32 };33
93 unmodified lines
modified:lib/sync.js+2 added-2 removed
1import childProcess from 'node:child_process';2import {handleOptionalArguments, handleArguments} from './arguments/options.js';2import {normalizeArguments, handleArguments} from './arguments/options.js';3import {makeError, makeEarlyError, makeSuccessResult} from './return/error.js';4import {handleOutput} from './return/output.js';5import {handleInputSync, pipeOutputSync} from './stdio/sync.js';6import {isFailedExit} from './exit/code.js';
43 unmodified lines
50 return makeSuccessResult({command, escapedCommand, stdio, options});51};5253const handleSyncArguments = (rawFile, rawArgs, rawOptions) => {54 [rawArgs, rawOptions] = handleOptionalArguments(rawArgs, rawOptions);54 [rawFile, rawArgs, rawOptions] = normalizeArguments(rawFile, rawArgs, rawOptions);55 const syncOptions = normalizeSyncOptions(rawOptions);56 const {file, args, command, escapedCommand, options} = handleArguments(rawFile, rawArgs, syncOptions);57 validateSyncOptions(options);58 return {file, args, command, escapedCommand, options};
9 unmodified lines
modified:test/arguments/options.js+20 added
121 unmodified lines
122test('execa()\'s command argument must be a string or file URL', testInvalidCommand, execa);123test('execaSync()\'s command argument must be a string or file URL', testInvalidCommand, execaSync);124test('execaNode()\'s command argument must be a string or file URL', testInvalidCommand, execaNode);125126const testInvalidArgs = async (t, execaMethod) => {127 t.throws(() => {128 execaMethod('echo', true);129 }, {message: /Second argument must be either/});130};131132test('execa()\'s second argument must be an array', testInvalidArgs, execa);133test('execaSync()\'s second argument must be an array', testInvalidArgs, execaSync);134test('execaNode()\'s second argument must be an array', testInvalidArgs, execaNode);135136const testInvalidOptions = async (t, execaMethod) => {137 t.throws(() => {138 execaMethod('echo', [], new Map());139 }, {message: /Last argument must be an options object/});140};141142test('execa()\'s third argument must be a plain object', testInvalidOptions, execa);143test('execaSync()\'s third argument must be a plain object', testInvalidOptions, execaSync);144test('execaNode()\'s third argument must be a plain object', testInvalidOptions, execaNode);145146test('use relative path with \'..\' chars', async t => {147 const pathViaParentDir = join('..', basename(fileURLToPath(new URL('../..', import.meta.url))), 'test', 'fixtures', 'noop.js');148 const {stdout} = await execa(pathViaParentDir, ['foo']);149 t.is(stdout, 'foo');
1 unmodified line
modified:test/command.js+20 added
36 unmodified lines
37test('execaCommandSync()', t => {38 const {stdout} = execaCommandSync('echo.js foo bar');39 t.is(stdout, 'foo\nbar');40});4142const testInvalidCommand = (t, execaCommand, invalidArgument) => {43 t.throws(() => {44 execaCommand(invalidArgument);45 }, {message: /First argument must be a string/});46};4748test('execaCommand() must use a string', testInvalidCommand, execaCommand, true);49test('execaCommandSync() must use a string', testInvalidCommand, execaCommandSync, true);50test('execaCommand() must have an argument', testInvalidCommand, execaCommand, undefined);51test('execaCommandSync() must have an argument', testInvalidCommand, execaCommandSync, undefined);5253const testInvalidArgs = (t, execaCommand) => {54 t.throws(() => {55 execaCommand('echo', ['']);56 }, {message: /Last argument must be an options object/});57};5859test('execaCommand() must not pass an array of arguments', testInvalidArgs, execaCommand);60test('execaCommandSync() must not pass an array of arguments', testInvalidArgs, execaCommandSync);
modified:test/script.js+8 added
148 unmodified lines
149 const {stdout} = await $` echo.js foo bar `;150 t.is(stdout, 'foo\nbar');151});152153test('$ must only use options or templates', t => {154 t.throws(() => $(true)`noop.js`, {message: /Please use either/});155});156157test('$.sync', t => {158 const {stdout} = $.sync`echo.js foo bar`;159 t.is(stdout, 'foo\nbar');160});
35 unmodified lines
196 const {stdout} = $.sync`echo.js ${[foo, 'bar']}`;197 t.is(stdout, 'foo\nbar');198});199200test('$.sync must only templates', t => {201 t.throws(() => $.sync(true)`noop.js`, {message: /A template string must be used/});202});203204const invalidExpression = test.macro({205 async exec(t, input, expected) {206 await t.throwsAsync(207 async () => $`echo.js ${input}`,
59 unmodified lines
modified:package.json+1 added
47 unmodified lines
48 "@sindresorhus/merge-streams": "^3.0.0",49 "cross-spawn": "^7.0.3",50 "get-stream": "^8.0.1",51 "human-signals": "^6.0.0",52 "is-plain-obj": "^4.1.0",53 "is-stream": "^4.0.1",54 "npm-run-path": "^5.2.0",55 "signal-exit": "^4.1.0",56 "strip-final-newline": "^4.0.0"
37 unmodified lines