Skip to content

Commit 1d92605

Browse files
authored
Use Object.create(null) as merge target (#7920)
1 parent dff7140 commit 1d92605

File tree

4 files changed

+11
-6
lines changed

4 files changed

+11
-6
lines changed

src/core/core.controller.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ defaults._set('global', {
4141
* returns a deep copy of the result, thus doesn't alter inputs.
4242
*/
4343
function mergeScaleConfig(/* config objects ... */) {
44-
return helpers.merge({}, [].slice.call(arguments), {
44+
return helpers.merge(Object.create(null), [].slice.call(arguments), {
4545
merger: function(key, target, source, options) {
4646
if (key === 'xAxes' || key === 'yAxes') {
4747
var slen = source[key].length;
@@ -81,9 +81,9 @@ function mergeScaleConfig(/* config objects ... */) {
8181
* a deep copy of the result, thus doesn't alter inputs.
8282
*/
8383
function mergeConfig(/* config objects ... */) {
84-
return helpers.merge({}, [].slice.call(arguments), {
84+
return helpers.merge(Object.create(null), [].slice.call(arguments), {
8585
merger: function(key, target, source, options) {
86-
var tval = target[key] || {};
86+
var tval = target[key] || Object.create(null);
8787
var sval = source[key];
8888

8989
if (key === 'scales') {
@@ -100,7 +100,7 @@ function mergeConfig(/* config objects ... */) {
100100
}
101101

102102
function initConfig(config) {
103-
config = config || {};
103+
config = config || Object.create(null);
104104

105105
// Do NOT use mergeConfig for the data object because this method merges arrays
106106
// and so would change references to labels and datasets, preventing data updates.

src/core/core.datasetController.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ helpers.extend(DatasetController.prototype, {
275275
*/
276276
_configure: function() {
277277
var me = this;
278-
me._config = helpers.merge({}, [
278+
me._config = helpers.merge(Object.create(null), [
279279
me.chart.options.datasets[me._type],
280280
me.getDataset(),
281281
], {

src/core/core.scaleService.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ module.exports = {
2222
},
2323
getScaleDefaults: function(type) {
2424
// Return the scale defaults merged with the global settings so that we always use the latest ones
25-
return this.defaults.hasOwnProperty(type) ? helpers.merge({}, [defaults.scale, this.defaults[type]]) : {};
25+
return this.defaults.hasOwnProperty(type) ? helpers.merge(Object.create(null), [defaults.scale, this.defaults[type]]) : {};
2626
},
2727
updateScaleDefaults: function(type, additions) {
2828
var me = this;

test/specs/helpers.core.tests.js

+5
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,11 @@ describe('Chart.helpers.core', function() {
265265
});
266266

267267
describe('clone', function() {
268+
it('should not allow prototype pollution', function() {
269+
var test = helpers.clone(JSON.parse('{"__proto__":{"polluted": true}}'));
270+
expect(test.prototype).toBeUndefined();
271+
expect(Object.prototype.polluted).toBeUndefined();
272+
});
268273
it('should clone primitive values', function() {
269274
expect(helpers.clone()).toBe(undefined);
270275
expect(helpers.clone(null)).toBe(null);

0 commit comments

Comments
 (0)