Skip to content
This repository was archived by the owner on Dec 21, 2023. It is now read-only.

Commit d34e05a

Browse files
petebacondarwinCarmen Wick
authored and
Carmen Wick
committed
fix(angular.merge): do not merge __proto__ property
By blocking `__proto__` on deep merging, this commit prevents the `Object` prototype from being polluted.
1 parent 685596a commit d34e05a

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

src/Angular.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -350,8 +350,10 @@ function baseExtend(dst, objs, deep) {
350350
} else if (isElement(src)) {
351351
dst[key] = src.clone();
352352
} else {
353-
if (!isObject(dst[key])) dst[key] = isArray(src) ? [] : {};
354-
baseExtend(dst[key], [src], true);
353+
if (key !== '__proto__') {
354+
if (!isObject(dst[key])) dst[key] = isArray(src) ? [] : {};
355+
baseExtend(dst[key], [src], true);
356+
}
355357
}
356358
} else {
357359
dst[key] = src;

test/AngularSpec.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -784,6 +784,19 @@ describe('angular', function() {
784784
expect(isElement(dst.jqObject)).toBeTruthy();
785785
expect(dst.jqObject.nodeName).toBeUndefined(); // i.e it is a jqLite/jQuery object
786786
});
787+
788+
it('should not merge the __proto__ property', function() {
789+
var src = JSON.parse('{ "__proto__": { "xxx": "polluted" } }');
790+
var dst = {};
791+
792+
merge(dst, src);
793+
794+
if (typeof dst.__proto__ !== 'undefined') { // eslint-disable-line
795+
// Should not overwrite the __proto__ property or pollute the Object prototype
796+
expect(dst.__proto__).toBe(Object.prototype); // eslint-disable-line
797+
}
798+
expect(({}).xxx).toBeUndefined();
799+
});
787800
});
788801

789802

0 commit comments

Comments
 (0)