Skip to content

Commit 2072474

Browse files
fix: skip unsupported values in chart SVG export (#10284) (CP: 24.9) (#10290)
Co-authored-by: Diego Cardoso <diego@vaadin.com>
1 parent 7e3951a commit 2072474

3 files changed

Lines changed: 124 additions & 0 deletions

File tree

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/**
2+
* Applies fixes to the bundled Highcharts version, same as in @vaadin/charts.
3+
* TODO: Remove when upgrading Highcharts, see vaadin/web-components#12213
4+
*/
5+
6+
/* eslint-env node, es6 */
7+
8+
const RESERVED_KEYS = ['__proto__', 'constructor'];
9+
10+
const UNSUPPORTED_SCHEMES = ['javascript', 'vbscript', 'data'];
11+
12+
// Relative links are resolved against a placeholder, as the exporter has no page URL
13+
const BASE_URL = 'https://localhost/';
14+
15+
const hasOwn = (object, key) => Object.prototype.hasOwnProperty.call(object, key);
16+
17+
const isReservedKey = (key) => RESERVED_KEYS.includes(key);
18+
19+
const isUnsupportedLink = (value) => {
20+
if (typeof value !== 'string') {
21+
return true;
22+
}
23+
try {
24+
// The URL parser normalizes case and whitespace the same way as navigation does
25+
return UNSUPPORTED_SCHEMES.includes(new URL(value, BASE_URL).protocol.slice(0, -1));
26+
} catch (_) {
27+
return true;
28+
}
29+
};
30+
31+
function applyHighchartsPatches(Highcharts) {
32+
const { Chart, Point } = Highcharts;
33+
34+
// Skip reserved option names in point objects
35+
Highcharts.wrap(Point.prototype, 'optionsToObject', function (proceed, options) {
36+
if (
37+
options &&
38+
typeof options === 'object' &&
39+
!Array.isArray(options) &&
40+
RESERVED_KEYS.some((key) => hasOwn(options, key))
41+
) {
42+
options = Object.fromEntries(Object.entries(options).filter(([key]) => !isReservedKey(key)));
43+
}
44+
return proceed.call(this, options);
45+
});
46+
47+
// Skip reserved path segments in point keys
48+
Highcharts.wrap(Point.prototype, 'setNestedProperty', function (proceed, object, value, key) {
49+
if (String(key).split('.').some(isReservedKey)) {
50+
return object;
51+
}
52+
return proceed.call(this, object, value, key);
53+
});
54+
55+
// Ignore credits links that use an unsupported URL scheme
56+
Highcharts.wrap(Chart.prototype, 'addCredits', function (proceed, credits) {
57+
const options = Highcharts.merge(true, this.options.credits, credits);
58+
if (options && options.href && isUnsupportedLink(options.href)) {
59+
Highcharts.error(33, false, this, { 'Invalid attribute in config': 'credits.href' });
60+
delete options.href;
61+
}
62+
return proceed.call(this, options);
63+
});
64+
}
65+
66+
module.exports = applyHighchartsPatches;

‎vaadin-charts-flow-parent/vaadin-charts-flow-svg-generator/jsdom-exporter.js‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ require("highcharts/modules/organization")(Highcharts);
4949
require("highcharts/modules/xrange")(Highcharts);
5050
require("highcharts/modules/bullet")(Highcharts);
5151
require("highcharts/modules/annotations")(Highcharts);
52+
require('./highcharts-patches.js')(Highcharts);
5253

5354
win.Date = Date;
5455

‎vaadin-charts-flow-parent/vaadin-charts-flow-svg-generator/test/index.test.js‎

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,63 @@ describe('jsdom-exporter', () => {
150150
});
151151
});
152152

153+
describe('option values', () => {
154+
// eslint-disable-next-line no-script-url
155+
const UNSUPPORTED_URL = 'javascript:void(0)';
156+
157+
const Highcharts = jsdomExporter.__get__('Highcharts');
158+
159+
beforeEach(() => mock());
160+
161+
afterEach(() => {
162+
mock.restore();
163+
delete Object.prototype.custom;
164+
});
165+
166+
function createChart(options) {
167+
const container = exporterDom.window.document.createElement('div');
168+
return Highcharts.chart(container, options);
169+
}
170+
171+
it('should set nested point keys', async () => {
172+
const chart = createChart({ series: [{ keys: ['y', 'custom.value'], data: [[1, 'a']] }] });
173+
expect(chart.series[0].points[0].custom.value).to.equal('a');
174+
});
175+
176+
it('should ignore reserved segments in point keys', async () => {
177+
const result = await jsdomExporter({
178+
chartConfiguration: {
179+
series: [{ keys: ['y', '__proto__.custom', 'constructor.prototype.custom'], data: [[1, 'a', 'b']] }]
180+
}
181+
});
182+
expect({}.custom).to.be.undefined;
183+
expect(parseSVG(result.svgString).querySelector('.highcharts-series')).to.be.not.null;
184+
});
185+
186+
it('should ignore reserved option names in point objects', async () => {
187+
const result = await jsdomExporter({
188+
chartConfiguration: {
189+
series: [{ data: JSON.parse('[{ "y": 1, "__proto__": { "custom": "a" }, "constructor": { "custom": "b" } }]') }]
190+
}
191+
});
192+
expect(parseSVG(result.svgString).querySelectorAll('.highcharts-series-group .highcharts-point')).to.have.lengthOf(1);
193+
});
194+
195+
['https://vaadin.com', 'about.html', 'tel:+123'].forEach((href) => {
196+
it(`should keep supported credits links: ${href}`, () => {
197+
const chart = createChart({ credits: { enabled: true, href } });
198+
expect(chart.options.credits.href).to.equal(href);
199+
});
200+
});
201+
202+
[UNSUPPORTED_URL, ` JAVASCRIPT:void(0)`, 'data:text/html,Text'].forEach((href) => {
203+
it(`should ignore credits links that use an unsupported URL scheme: ${JSON.stringify(href)}`, () => {
204+
const chart = createChart({ credits: { enabled: true, href } });
205+
expect(chart.options.credits.href).to.be.undefined;
206+
});
207+
});
208+
});
209+
153210
describe('timeline', () => {
154211
beforeEach(() => mock());
155212

0 commit comments

Comments
 (0)