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
Rename files (#977)ehmickybefore adoption4b2e316
lines 1–3why, one step back1import isPlainObject from 'is-plain-obj';2import {safeNormalizeFileUrl} from '../arguments/file-url.js';3Add more code comments (#984)ehmickybefore adoptionfdd8a9f
lines 4–5why, one step back4// The command `arguments` and `options` are both optional.5// This also does basic validation on them and on the command file.Upgrade xo (#980)ehmickybefore adoptionccfa412
line 6why, one step back6export const normalizeParameters = (rawFile, rawArguments = [], rawOptions = {}) => {Rename files (#977)ehmickybefore adoption4b2e316
line 7why, one step back7 const filePath = safeNormalizeFileUrl(rawFile, 'First argument');Upgrade xo (#980)ehmickybefore adoptionccfa412
lines 8–10why, one step back8 const [commandArguments, options] = isPlainObject(rawArguments)9 ? [[], rawArguments]10 : [rawArguments, rawOptions];Rename files (#977)ehmickybefore adoption4b2e316
line 11why, one step back11Upgrade xo (#980)ehmickybefore adoptionccfa412
lines 12–13why, one step back12 if (!Array.isArray(commandArguments)) {13 throw new TypeError(`Second argument must be either an array of arguments or an options object: ${commandArguments}`);Rename files (#977)ehmickybefore adoption4b2e316
lines 14–15why, one step back14 }15Upgrade xo (#980)ehmickybefore adoptionccfa412
lines 16–17why, one step back16 if (commandArguments.some(commandArgument => typeof commandArgument === 'object' && commandArgument !== null)) {17 throw new TypeError(`Second argument must be an array of strings: ${commandArguments}`);Rename files (#977)ehmickybefore adoption4b2e316
lines 18–19why, one step back18 }19Upgrade xo (#980)ehmickybefore adoptionccfa412
lines 20–23why, one step back20 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}`);Rename files (#977)ehmickybefore adoption4b2e316
lines 24–29why, one step back24 }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}];Rename files (#977)ehmickybefore adoption4b2e316
line 32why, one step back32};