Skip to content
Myr

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 file

Take it

The walk stopped after 200 commits: lines older than 9f65b86 point at it.

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 1–5why, one step back
1import 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 back
6export 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 }14

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 15why, one step back
15 const filePath = safeNormalizeFileUrl(rawFile, 'First argument');

Fix wrong-typed options and arguments being silently ignored or coercedSindre Sorhusbefore adoptionb57d643

line 16why, one step back
16 const [commandArguments, options = {}] = isPlainObject(rawArguments)

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 17–37why, one step back
17 ? [[], 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 }37

Harden against prototype pollution (#1223)Sindre Sorhusbefore adoptionf3a2e84

lines 38–39why, one step back
38 // Prevent prototype pollution by copying only own properties to a null-prototype object
39 return [filePath, normalizedArguments, {__proto__: null, ...options}];

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 40why, one step back
40};