From 2920e895b5c8f528dc406b39cdd0b0c89c824f05 Mon Sep 17 00:00:00 2001 From: Evert Timberg Date: Sun, 18 Jul 2021 09:11:45 -0400 Subject: [PATCH 1/2] Ensure getPrototypeOf will not crash on plugin options with no scopes --- src/helpers/helpers.config.js | 2 +- test/specs/core.controller.tests.js | 17 +++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/src/helpers/helpers.config.js b/src/helpers/helpers.config.js index ce78ec68fa9..62f47949554 100644 --- a/src/helpers/helpers.config.js +++ b/src/helpers/helpers.config.js @@ -54,7 +54,7 @@ export function _createResolver(scopes, prefixes = [''], rootScopes = scopes, fa * A trap for Object.getPrototypeOf. */ getPrototypeOf() { - return Reflect.getPrototypeOf(scopes[0]); + return Reflect.getPrototypeOf(scopes[0] || Object.create(null)); }, /** diff --git a/test/specs/core.controller.tests.js b/test/specs/core.controller.tests.js index 393983852d2..0058bb3872f 100644 --- a/test/specs/core.controller.tests.js +++ b/test/specs/core.controller.tests.js @@ -1707,6 +1707,23 @@ describe('Chart', function() { 'before-0', 'after-0' ]); }); + + it('should not crash when accessing options of a blank inline plugin', function() { + var chart = window.acquireChart({ + type: 'line', + data: {datasets: [{}]}, + plugins: [{}], + }); + + function iterateOptions() { + for (const plugin of chart._plugins._init) { + // triggering bug https://github.com/chartjs/Chart.js/issues/9368 + expect(Object.getPrototypeOf(plugin.options)).toBeNull(); + } + } + + expect(iterateOptions).not.toThrow(); + }); }); describe('metasets', function() { From 7914ab10426c304fc6d3ca0dc16fe06fee3bee0d Mon Sep 17 00:00:00 2001 From: Evert Timberg Date: Fri, 23 Jul 2021 20:05:26 -0400 Subject: [PATCH 2/2] Code review improvements --- src/core/core.config.js | 3 +++ src/helpers/helpers.config.js | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/src/core/core.config.js b/src/core/core.config.js index ff97d5f7572..927437d4703 100644 --- a/src/core/core.config.js +++ b/src/core/core.config.js @@ -277,6 +277,9 @@ export default class Config { }); const array = Array.from(scopes); + if (array.length === 0) { + array.push(Object.create(null)); + } if (keysCached.has(keyLists)) { cache.set(keyLists, array); } diff --git a/src/helpers/helpers.config.js b/src/helpers/helpers.config.js index 62f47949554..ce78ec68fa9 100644 --- a/src/helpers/helpers.config.js +++ b/src/helpers/helpers.config.js @@ -54,7 +54,7 @@ export function _createResolver(scopes, prefixes = [''], rootScopes = scopes, fa * A trap for Object.getPrototypeOf. */ getPrototypeOf() { - return Reflect.getPrototypeOf(scopes[0] || Object.create(null)); + return Reflect.getPrototypeOf(scopes[0]); }, /**