Harden against prototype pollution (#1223)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Sindre Sorhus · committed by GitHub · parent 6ad1a6e · Browse files · 4 files·+27 added−8 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+27 added−8 removed
- other:+27 added−8 removed
Provenance
- Commit
f3a2e84- Tree
b521993- Author
Sindre Sorhus <sindresorhus@gmail.com>- Committer
GitHub <noreply@github.com>- Parent
6ad1a6e
4 files changed+27 added−8 removed
modified:lib/arguments/options.js+6 added-2 removed
15 unmodified lines161718192021202122232425262716 unmodified lines44454647484950515210 unmodified lines63646566676869707129 unmodified lines15 unmodified lines
// Normalize the options object, and sometimes also the file paths and arguments.// Applies default values, validate allowed options, normalize them.export const normalizeOptions = (filePath, rawArguments, rawOptions) => { // Prevent prototype pollution by copying only own properties to a null-prototype object const sanitizedOptions = {__proto__: null, ...rawOptions}; rawOptions.cwd = normalizeCwd(rawOptions.cwd); const [processedFile, processedArguments, processedOptions] = handleNodeOption(filePath, rawArguments, rawOptions); sanitizedOptions.cwd = normalizeCwd(sanitizedOptions.cwd); const [processedFile, processedArguments, processedOptions] = handleNodeOption(filePath, rawArguments, sanitizedOptions);
const {command: file, args: commandArguments, options: initialOptions} = crossSpawn._parse(processedFile, processedArguments, processedOptions);
const fdOptions = normalizeFdSpecificOptions(initialOptions);16 unmodified lines
return {file, commandArguments, options};};
// Use null prototype to prevent prototype pollution from leaking throughconst addDefaultOptions = ({ extendEnv = true, preferLocal = false, cwd,10 unmodified lines ipc = ipcInput !== undefined || gracefulCancel, serialization = 'advanced', ...options}) => ({ __proto__: null, ...options, extendEnv, preferLocal, cwd,29 unmodified linesmodified:lib/methods/bind.js+5 added-3 removed
12345676891091112131214151617187 unmodified linesimport isPlainObject from 'is-plain-obj';import {FD_SPECIFIC_OPTIONS} from '../arguments/specific.js';
// Deep merge specific options like `env`. Shallow merge the other ones.// Use spread (which only copies own properties) to safely read from boundOptions without prototype pollutionexport const mergeOptions = (boundOptions, options) => { const safeBoundOptions = {__proto__: null, ...boundOptions}; const newOptions = Object.fromEntries( const mergedOptions = Object.fromEntries( Object.entries(options).map(([optionName, optionValue]) => [ optionName, mergeOption(optionName, boundOptions[optionName], optionValue), mergeOption(optionName, safeBoundOptions[optionName], optionValue), ]), ); return {...boundOptions, ...newOptions}; return {...safeBoundOptions, ...mergedOptions};};
const mergeOption = (optionName, boundOptionValue, optionValue) => { if (DEEP_OPTIONS.has(optionName) && isPlainObject(boundOptionValue) && isPlainObject(optionValue)) {7 unmodified linesmodified:lib/methods/node.js+14 added-2 removed
26 unmodified lines2728293031323334353637388 unmodified lines4748495048495152535455565758596061626326 unmodified lines }
const normalizedNodePath = safeNormalizeFileUrl(nodePath, 'The "nodePath" option'); const resolvedNodePath = path.resolve(cwd, normalizedNodePath); // Use spread (which only copies own properties) to safely get shell without reading polluted prototype const newOptions = { __proto__: null, shell: false, ...options, nodePath: resolvedNodePath, node: shouldHandleNode, cwd,8 unmodified lines }
return [ resolvedNodePath, [...nodeOptions, file, ...commandArguments], {ipc: true, ...newOptions, shell: false}, [ ...nodeOptions, file, ...commandArguments, ], { __proto__: null, ipc: true, ...newOptions, shell: false, }, ];};modified:lib/methods/parameters.js+2 added-1 removed
25 unmodified lines26 if (!isPlainObject(options)) {27 throw new TypeError(`Last argument must be an options object: ${options}`);28 }29 30+ // Prevent prototype pollution by copying only own properties to a null-prototype object30- return [filePath, normalizedArguments, options];31+ return [filePath, normalizedArguments, {__proto__: null, ...options}];32 };