Skip to content

Commit

Permalink
Use Object.create(null) as merge target (chartjs#7920)
Browse files Browse the repository at this point in the history
  • Loading branch information
kurkle authored and dracos committed Sep 25, 2023
1 parent 0c35bd2 commit b2f38cb
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/core/core.controller.js
Expand Up @@ -22,7 +22,7 @@ module.exports = function(Chart) {
* Initializes the given config with global and chart default values.
*/
function initConfig(config) {
config = config || {};
config = config || Object.create(null);

// Do NOT use configMerge() for the data object because this method merges arrays
// and so would change references to labels and datasets, preventing data updates.
Expand Down
2 changes: 1 addition & 1 deletion src/core/core.scaleService.js
Expand Up @@ -23,7 +23,7 @@ module.exports = function(Chart) {
},
getScaleDefaults: function(type) {
// Return the scale defaults merged with the global settings so that we always use the latest ones
return this.defaults.hasOwnProperty(type) ? helpers.merge({}, [defaults.scale, this.defaults[type]]) : {};
return this.defaults.hasOwnProperty(type) ? helpers.merge(Object.create(null), [defaults.scale, this.defaults[type]]) : {};
},
updateScaleDefaults: function(type, additions) {
var me = this;
Expand Down
5 changes: 5 additions & 0 deletions test/specs/helpers.core.tests.js
Expand Up @@ -238,6 +238,11 @@ describe('Chart.helpers.core', function() {
});

describe('clone', function() {
it('should not allow prototype pollution', function() {
var test = helpers.clone(JSON.parse('{"__proto__":{"polluted": true}}'));
expect(test.prototype).toBeUndefined();
expect(Object.prototype.polluted).toBeUndefined();
});
it('should clone primitive values', function() {
expect(helpers.clone()).toBe(undefined);
expect(helpers.clone(null)).toBe(null);
Expand Down

0 comments on commit b2f38cb

Please sign in to comment.