diff --git a/lib/charts.js b/lib/charts.js index 97eaa37..3a4e6c1 100644 --- a/lib/charts.js +++ b/lib/charts.js @@ -5,6 +5,8 @@ const winston = require('winston'); const { CanvasRenderService } = require('chartjs-node-canvas'); const { NodeVM } = require('vm2'); +const { fixNodeVmObject } = require('./util'); + const logger = new (winston.Logger)({ level: process.env.LOG_LEVEL || 'info', transports: [ @@ -64,6 +66,8 @@ function renderChart(width, height, backgroundColor, untrustedChart) { chart = untrustedChart; } + fixNodeVmObject(chart); + chart.options = chart.options || {}; if (chart.type === 'donut') { diff --git a/lib/util.js b/lib/util.js new file mode 100644 index 0000000..32feef2 --- /dev/null +++ b/lib/util.js @@ -0,0 +1,20 @@ +function fixNodeVmObject(obj) { + // Fix for https://github.com/patriksimek/vm2/issues/198 + for (const key in obj) { + if (obj.hasOwnProperty(key)) { + const val = obj[key]; + if (Array.isArray(val)) { + obj[key] = Array.from(val); + obj[key].forEach((arrObj) => { + fixNodeVmObject(arrObj); + }); + } else if (typeof val === 'object' && val !== null) { + fixNodeVmObject(val); + } + } + } +} + +module.exports = { + fixNodeVmObject, +};