]> git.ipfire.org Git - thirdparty/Chart.js.git/commitdiff
Prevent proxying CanvasGradient in Node platform (#9861)
authorJukka Kurkela <jukka.kurkela@gmail.com>
Tue, 16 Nov 2021 01:09:43 +0000 (03:09 +0200)
committerGitHub <noreply@github.com>
Tue, 16 Nov 2021 01:09:43 +0000 (20:09 -0500)
src/helpers/helpers.config.js
test/specs/helpers.config.tests.js

index f59ed31f6eb16a74fb21c2ccdacbea953d8f46e1..4d27f9e5b025376f3c46625f75d9cd20e9726795 100644 (file)
@@ -178,7 +178,8 @@ export function _descriptors(proxy, defaults = {scriptable: true, indexable: tru
 }
 
 const readKey = (prefix, name) => prefix ? prefix + _capitalize(name) : name;
-const needsSubResolver = (prop, value) => isObject(value) && prop !== 'adapters';
+const needsSubResolver = (prop, value) => isObject(value) && prop !== 'adapters' &&
+  (Object.getPrototypeOf(value) === null || value.constructor === Object);
 
 function _cached(target, prop, resolve) {
   if (Object.prototype.hasOwnProperty.call(target, prop)) {
@@ -218,7 +219,7 @@ function _resolveScriptable(prop, value, target, receiver) {
   _stack.add(prop);
   value = value(_context, _subProxy || receiver);
   _stack.delete(prop);
-  if (isObject(value)) {
+  if (needsSubResolver(prop, value)) {
     // When scriptable option returns an object, create a resolver on that.
     value = createSubResolver(_proxy._scopes, _proxy, prop, value);
   }
index 811d386d303c09cbd8e4cde0dece8a0ccc3256ec..58e295b580b9f7999daec4b0fb62de83d4b5e61a 100644 (file)
@@ -752,6 +752,29 @@ describe('Chart.helpers.config', function() {
       expect(fn()).toEqual('ok');
     });
 
+    it('should not create proxy for objects with custom constructor', function() {
+      class MyClass {
+        constructor() {
+          this.string = 'test string';
+        }
+        method(arg) {
+          return arg === undefined ? 'ok' : 'fail';
+        }
+      }
+
+      const defaults = {
+        test: new MyClass()
+      };
+
+      const resolver = _createResolver([{}, defaults]);
+      const opts = _attachContext(resolver, {index: 1});
+      const fn = opts.test.method;
+      expect(typeof fn).toBe('function');
+      expect(fn()).toEqual('ok');
+      expect(opts.test.string).toEqual('test string');
+      expect(opts.test.constructor).toEqual(MyClass);
+    });
+
     it('should properly set value to object in array of objects', function() {
       const defaults = {};
       const options = {