Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d8e069c

Browse files
committed
Avoid merging properties on to __proto__ objects.
1 parent 5a3ff73 commit d8e069c

File tree

2 files changed

+32
-3
lines changed

2 files changed

+32
-3
lines changed

lodash.js

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1245,6 +1245,20 @@
12451245
return result;
12461246
}
12471247

1248+
/**
1249+
* Gets the value at `key`, unless `key` is "__proto__".
1250+
*
1251+
* @private
1252+
* @param {Object} object The object to query.
1253+
* @param {string} key The key of the property to get.
1254+
* @returns {*} Returns the property value.
1255+
*/
1256+
function safeGet(object, key) {
1257+
return key == '__proto__'
1258+
? undefined
1259+
: object[key];
1260+
}
1261+
12481262
/**
12491263
* Converts `set` to an array of its values.
12501264
*
@@ -3615,7 +3629,7 @@
36153629
}
36163630
else {
36173631
var newValue = customizer
3618-
? customizer(object[key], srcValue, (key + ''), object, source, stack)
3632+
? customizer(safeGet(object, key), srcValue, (key + ''), object, source, stack)
36193633
: undefined;
36203634

36213635
if (newValue === undefined) {
@@ -3642,8 +3656,8 @@
36423656
* counterparts.
36433657
*/
36443658
function baseMergeDeep(object, source, key, srcIndex, mergeFunc, customizer, stack) {
3645-
var objValue = object[key],
3646-
srcValue = source[key],
3659+
var objValue = safeGet(object, key),
3660+
srcValue = safeGet(source, key),
36473661
stacked = stack.get(srcValue);
36483662

36493663
if (stacked) {

test/test.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7539,6 +7539,21 @@
75397539
actual = _.groupBy([{ 'a': '__proto__' }], 'a');
75407540
assert.notOk(actual instanceof Array);
75417541
});
7542+
7543+
QUnit.test('should not merge "__proto__" properties', function(assert) {
7544+
assert.expect(1);
7545+
7546+
if (JSON) {
7547+
_.merge({}, JSON.parse('{"__proto__":{"a":1}}'));
7548+
7549+
var actual = "a" in objectProto;
7550+
delete objectProto.a;
7551+
7552+
assert.notOk(actual);
7553+
} else {
7554+
skipAssert(assert);
7555+
}
7556+
});
75427557
}());
75437558

75447559
/*--------------------------------------------------------------------------*/

0 commit comments

Comments
 (0)