parameters.js
This file
Path
lib/methods/parameters.js
32 lines · 1.4 KiB · JavaScript
Last changed by
Harden against prototype pollution (#1223)Sindre Sorhusbefore adoptionf3a2e84
History of this fileTake it
The walk stopped after 200 commits: lines older than c8a6ed7 point at it.
Improve documentation (#1003)ehmickybefore adoptionc8a6ed7
lines 1–29why, 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.6export const normalizeParameters = (rawFile, rawArguments = [], rawOptions = {}) => {7 const filePath = safeNormalizeFileUrl(rawFile, 'First argument');8 const [commandArguments, options] = isPlainObject(rawArguments)9 ? [[], rawArguments]10 : [rawArguments, rawOptions];1112 if (!Array.isArray(commandArguments)) {13 throw new TypeError(`Second argument must be either an array of arguments or an options object: ${commandArguments}`);14 }1516 if (commandArguments.some(commandArgument => typeof commandArgument === 'object' && commandArgument !== null)) {17 throw new TypeError(`Second argument must be an array of strings: ${commandArguments}`);18 }1920 const normalizedArguments = commandArguments.map(String);21 const nullByteArgument = normalizedArguments.find(normalizedArgument => normalizedArgument.includes('\0'));22 if (nullByteArgument !== undefined) {23 throw new TypeError(`Arguments cannot contain null bytes ("\\0"): ${nullByteArgument}`);24 }2526 if (!isPlainObject(options)) {27 throw new TypeError(`Last argument must be an options object: ${options}`);28 }29Harden against prototype pollution (#1223)Sindre Sorhusbefore adoptionf3a2e84
lines 30–31why, one step back30 // Prevent prototype pollution by copying only own properties to a null-prototype object31 return [filePath, normalizedArguments, {__proto__: null, ...options}];Improve documentation (#1003)ehmickybefore adoptionc8a6ed7
line 32why, one step back32};