Skip to content

Commit

Permalink
When objects are merged together, the target prototype can be pollute…
Browse files Browse the repository at this point in the history
…d. (#7918)

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

This change blocks updates to the `__proto__` key during config merge
  • Loading branch information
etimberg authored Oct 18, 2020
1 parent d919188 commit dff7140
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/helpers/helpers.core.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
'use strict';

function isValidKey(key) {
return ['__proto__', 'prototype', 'constructor'].indexOf(key) === -1;
}

/**
* @namespace Chart.helpers
*/
Expand Down Expand Up @@ -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];

Expand All @@ -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];

Expand Down
5 changes: 5 additions & 0 deletions test/specs/helpers.core.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'});
Expand Down

0 comments on commit dff7140

Please sign in to comment.