mirror of
https://github.com/chartjs/Chart.js.git
synced 2026-03-11 18:56:49 +01:00
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:
@@ -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];
|
||||
|
||||
|
||||
@@ -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'});
|
||||
|
||||
Reference in New Issue
Block a user