Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Object.create(null) as merge target, to prevent prototype pollution #7920

Merged
merged 1 commit into from Oct 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/core/core.controller.js
Expand Up @@ -41,7 +41,7 @@ defaults._set('global', {
* returns a deep copy of the result, thus doesn't alter inputs.
*/
function mergeScaleConfig(/* config objects ... */) {
return helpers.merge({}, [].slice.call(arguments), {
return helpers.merge(Object.create(null), [].slice.call(arguments), {
merger: function(key, target, source, options) {
if (key === 'xAxes' || key === 'yAxes') {
var slen = source[key].length;
Expand Down Expand Up @@ -81,9 +81,9 @@ function mergeScaleConfig(/* config objects ... */) {
* a deep copy of the result, thus doesn't alter inputs.
*/
function mergeConfig(/* config objects ... */) {
return helpers.merge({}, [].slice.call(arguments), {
return helpers.merge(Object.create(null), [].slice.call(arguments), {
merger: function(key, target, source, options) {
var tval = target[key] || {};
var tval = target[key] || Object.create(null);
var sval = source[key];

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

function initConfig(config) {
config = config || {};
config = config || Object.create(null);

// Do NOT use mergeConfig 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.datasetController.js
Expand Up @@ -275,7 +275,7 @@ helpers.extend(DatasetController.prototype, {
*/
_configure: function() {
var me = this;
me._config = helpers.merge({}, [
me._config = helpers.merge(Object.create(null), [
me.chart.options.datasets[me._type],
me.getDataset(),
], {
Expand Down
2 changes: 1 addition & 1 deletion src/core/core.scaleService.js
Expand Up @@ -22,7 +22,7 @@ module.exports = {
},
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 @@ -265,6 +265,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