Skip to content

Commit dff7140

Browse files
authored
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
1 parent d919188 commit dff7140

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

Diff for: src/helpers/helpers.core.js

+16
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
'use strict';
22

3+
function isValidKey(key) {
4+
return ['__proto__', 'prototype', 'constructor'].indexOf(key) === -1;
5+
}
6+
37
/**
48
* @namespace Chart.helpers
59
*/
@@ -196,6 +200,12 @@ var helpers = {
196200
* @private
197201
*/
198202
_merger: function(key, target, source, options) {
203+
if (!isValidKey(key)) {
204+
// We want to ensure we do not copy prototypes over
205+
// as this can pollute global namespaces
206+
return;
207+
}
208+
199209
var tval = target[key];
200210
var sval = source[key];
201211

@@ -211,6 +221,12 @@ var helpers = {
211221
* @private
212222
*/
213223
_mergerIf: function(key, target, source) {
224+
if (!isValidKey(key)) {
225+
// We want to ensure we do not copy prototypes over
226+
// as this can pollute global namespaces
227+
return;
228+
}
229+
214230
var tval = target[key];
215231
var sval = source[key];
216232

Diff for: test/specs/helpers.core.tests.js

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

325325
describe('merge', function() {
326+
it('should not allow prototype pollution', function() {
327+
var test = helpers.merge({}, JSON.parse('{"__proto__":{"polluted": true}}'));
328+
expect(test.prototype).toBeUndefined();
329+
expect(Object.prototype.polluted).toBeUndefined();
330+
});
326331
it('should update target and return it', function() {
327332
var target = {a: 1};
328333
var result = helpers.merge(target, {a: 2, b: 'foo'});

0 commit comments

Comments
 (0)