Skip to content
Myr

bind.js

This file

Path

lib/methods/bind.js

25 lines · 973 bytes · JavaScript

Last changed by

Harden against prototype pollution (#1223)Sindre Sorhusbefore adoptionf3a2e84

History of this file

Take it

Refactor logic and rename some variables (#983)ehmickybefore adoptionbd3b478

lines 1–4why, one step back
1import isPlainObject from 'is-plain-obj';
2import {FD_SPECIFIC_OPTIONS} from '../arguments/specific.js';34// Deep merge specific options like `env`. Shallow merge the other ones.

Harden against prototype pollution (#1223)Sindre Sorhusbefore adoptionf3a2e84

line 5why, one step back
5// Use spread (which only copies own properties) to safely read from boundOptions without prototype pollution

Refactor logic and rename some variables (#983)ehmickybefore adoptionbd3b478

line 6why, one step back
6export const mergeOptions = (boundOptions, options) => {

Harden against prototype pollution (#1223)Sindre Sorhusbefore adoptionf3a2e84

lines 7–8why, one step back
7 const safeBoundOptions = {__proto__: null, ...boundOptions};
8 const mergedOptions = Object.fromEntries(

Refactor logic and rename some variables (#983)ehmickybefore adoptionbd3b478

lines 9–10why, one step back
9 Object.entries(options).map(([optionName, optionValue]) => [
10 optionName,

Harden against prototype pollution (#1223)Sindre Sorhusbefore adoptionf3a2e84

line 11why, one step back
11 mergeOption(optionName, safeBoundOptions[optionName], optionValue),

Refactor logic and rename some variables (#983)ehmickybefore adoptionbd3b478

lines 12–13why, one step back
12 ]),
13 );

Harden against prototype pollution (#1223)Sindre Sorhusbefore adoptionf3a2e84

line 14why, one step back
14 return {...safeBoundOptions, ...mergedOptions};

Refactor logic and rename some variables (#983)ehmickybefore adoptionbd3b478

lines 15–25why, one step back
15};
1617const mergeOption = (optionName, boundOptionValue, optionValue) => {18 if (DEEP_OPTIONS.has(optionName) && isPlainObject(boundOptionValue) && isPlainObject(optionValue)) {19 return {...boundOptionValue, ...optionValue};20 }2122 return optionValue;23};2425const DEEP_OPTIONS = new Set(['env', ...FD_SPECIFIC_OPTIONS]);