Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit add78e6

Browse files
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 060bcde commit add78e6

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

src/Angular.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -342,8 +342,10 @@ function baseExtend(dst, objs, deep) {
342342
} else if (isElement(src)) {
343343
dst[key] = src.clone();
344344
} else {
345-
if (!isObject(dst[key])) dst[key] = isArray(src) ? [] : {};
346-
baseExtend(dst[key], [src], true);
345+
if (key !== '__proto__') {
346+
if (!isObject(dst[key])) dst[key] = isArray(src) ? [] : {};
347+
baseExtend(dst[key], [src], true);
348+
}
347349
}
348350
} else {
349351
dst[key] = src;

test/AngularSpec.js

+13
Original file line numberDiff line numberDiff line change
@@ -814,6 +814,19 @@ describe('angular', function() {
814814
expect(isElement(dst.jqObject)).toBeTruthy();
815815
expect(dst.jqObject.nodeName).toBeUndefined(); // i.e it is a jqLite/jQuery object
816816
});
817+
818+
it('should not merge the __proto__ property', function() {
819+
var src = JSON.parse('{ "__proto__": { "xxx": "polluted" } }');
820+
var dst = {};
821+
822+
merge(dst, src);
823+
824+
if (typeof dst.__proto__ !== 'undefined') { // eslint-disable-line
825+
// Should not overwrite the __proto__ property or pollute the Object prototype
826+
expect(dst.__proto__).toBe(Object.prototype); // eslint-disable-line
827+
}
828+
expect(({}).xxx).toBeUndefined();
829+
});
817830
});
818831

819832

0 commit comments

Comments
 (0)