When objects are merged together, the target prototype can be polluted. (#7918)

* When objects are merged together, the target prototype can be polluted.

This change blocks updates to the `__proto__` key during config merge
This commit is contained in:
Evert Timberg
2020-10-18 13:47:08 -04:00
committed by GitHub
parent d919188925
commit dff7140070
2 changed files with 21 additions and 0 deletions

View File

@@ -1,5 +1,9 @@
'use strict';
function isValidKey(key) {
return ['__proto__', 'prototype', 'constructor'].indexOf(key) === -1;
}
/**
* @namespace Chart.helpers
*/
@@ -196,6 +200,12 @@ var helpers = {
* @private
*/
_merger: function(key, target, source, options) {
if (!isValidKey(key)) {
// We want to ensure we do not copy prototypes over
// as this can pollute global namespaces
return;
}
var tval = target[key];
var sval = source[key];
@@ -211,6 +221,12 @@ var helpers = {
* @private
*/
_mergerIf: function(key, target, source) {
if (!isValidKey(key)) {
// We want to ensure we do not copy prototypes over
// as this can pollute global namespaces
return;
}
var tval = target[key];
var sval = source[key];

View File

@@ -323,6 +323,11 @@ describe('Chart.helpers.core', function() {
});
describe('merge', function() {
it('should not allow prototype pollution', function() {
var test = helpers.merge({}, JSON.parse('{"__proto__":{"polluted": true}}'));
expect(test.prototype).toBeUndefined();
expect(Object.prototype.polluted).toBeUndefined();
});
it('should update target and return it', function() {
var target = {a: 1};
var result = helpers.merge(target, {a: 2, b: 'foo'});