parameters.js
This file
Path
lib/methods/parameters.js
40 lines · 1.8 KiB · JavaScript
Last changed by
Fix wrong-typed options and arguments being silently ignored or coercedSindre Sorhusbefore adoptionb57d643
History of this fileTake it
The walk stopped after 200 commits: lines older than c6ce870 point at it.
Remove `StdioOption` type (#1008)ehmickybefore adoptionc6ce870
lines 1–5why, one step back1import isPlainObject from 'is-plain-obj';2import {safeNormalizeFileUrl} from '../arguments/file-url.js';34// The command `arguments` and `options` are both optional.5// This also does basic validation on them and on the command file.Fix wrong-typed options and arguments being silently ignored or coercedSindre Sorhusbefore adoptionb57d643
lines 6–14why, one step back6export const normalizeParameters = (rawFile, rawArguments = [], rawOptions, ...rawExtraArguments) => {7 if (rawExtraArguments.length > 0) {8 throw new TypeError('Execa accepts at most three arguments: file, arguments, options.');9 }1011 if (rawOptions !== undefined && isPlainObject(rawArguments)) {12 throw new TypeError('Execa accepts at most one options object. Please merge the second and third arguments.');13 }14Remove `StdioOption` type (#1008)ehmickybefore adoptionc6ce870
line 15why, one step back15 const filePath = safeNormalizeFileUrl(rawFile, 'First argument');Fix wrong-typed options and arguments being silently ignored or coercedSindre Sorhusbefore adoptionb57d643
line 16why, one step back16 const [commandArguments, options = {}] = isPlainObject(rawArguments)Remove `StdioOption` type (#1008)ehmickybefore adoptionc6ce870
lines 17–37why, one step back17 ? [[], rawArguments]18 : [rawArguments, rawOptions];1920 if (!Array.isArray(commandArguments)) {21 throw new TypeError(`Second argument must be either an array of arguments or an options object: ${commandArguments}`);22 }2324 if (commandArguments.some(commandArgument => typeof commandArgument === 'object' && commandArgument !== null)) {25 throw new TypeError(`Second argument must be an array of strings: ${commandArguments}`);26 }2728 const normalizedArguments = commandArguments.map(String);29 const nullByteArgument = normalizedArguments.find(normalizedArgument => normalizedArgument.includes('\0'));30 if (nullByteArgument !== undefined) {31 throw new TypeError(`Arguments cannot contain null bytes ("\\0"): ${nullByteArgument}`);32 }3334 if (!isPlainObject(options)) {35 throw new TypeError(`Last argument must be an options object: ${options}`);36 }37Harden against prototype pollution (#1223)Sindre Sorhusbefore adoptionf3a2e84
lines 38–39why, one step back38 // Prevent prototype pollution by copying only own properties to a null-prototype object39 return [filePath, normalizedArguments, {__proto__: null, ...options}];Remove `StdioOption` type (#1008)ehmickybefore adoptionc6ce870
line 40why, one step back40};