Skip to content

Commit 7a588b3

Browse files
authored
fix: skip unsupported values in chart options (#12887) (#12919)
1 parent f6b97b2 commit 7a588b3

3 files changed

Lines changed: 161 additions & 0 deletions

File tree

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/**
2+
* @license
3+
* Copyright (c) 2000 - 2024 Vaadin Ltd.
4+
*
5+
* This program is available under Vaadin Commercial License and Service Terms.
6+
*
7+
*
8+
* See https://vaadin.com/commercial-license-and-service-terms for the full
9+
* license.
10+
*/
11+
import Highcharts from 'highcharts/es-modules/masters/highstock.src.js';
12+
13+
// TODO: Remove when upgrading Highcharts, see vaadin/web-components#12213
14+
15+
const { Chart, Point } = Highcharts;
16+
17+
const RESERVED_KEYS = ['__proto__', 'constructor'];
18+
19+
const isReservedKey = (key) => RESERVED_KEYS.includes(key);
20+
21+
const UNSUPPORTED_SCHEMES = ['javascript', 'vbscript', 'data'];
22+
23+
const isUnsupportedLink = (value) => {
24+
if (typeof value !== 'string') {
25+
return true;
26+
}
27+
try {
28+
// The URL parser normalizes case and whitespace the same way as navigation does
29+
return UNSUPPORTED_SCHEMES.includes(new URL(value, document.baseURI).protocol.slice(0, -1));
30+
} catch (_) {
31+
return true;
32+
}
33+
};
34+
35+
/* eslint-disable @typescript-eslint/no-invalid-this, prefer-arrow-callback */
36+
37+
// Skip reserved option names in point objects
38+
Highcharts.wrap(Point.prototype, 'optionsToObject', function (proceed, options) {
39+
if (
40+
options &&
41+
typeof options === 'object' &&
42+
!Array.isArray(options) &&
43+
RESERVED_KEYS.some((key) => Object.prototype.hasOwnProperty.call(options, key))
44+
) {
45+
options = Object.fromEntries(Object.entries(options).filter(([key]) => !isReservedKey(key)));
46+
}
47+
return proceed.call(this, options);
48+
});
49+
50+
// Skip reserved path segments in point keys
51+
Highcharts.wrap(Point.prototype, 'setNestedProperty', function (proceed, object, value, key) {
52+
if (String(key).split('.').some(isReservedKey)) {
53+
return object;
54+
}
55+
return proceed.call(this, object, value, key);
56+
});
57+
58+
// Ignore credits links that use an unsupported URL scheme
59+
Highcharts.wrap(Chart.prototype, 'addCredits', function (proceed, credits) {
60+
const options = Highcharts.merge(true, this.options.credits, credits);
61+
if (options && options.href && isUnsupportedLink(options.href)) {
62+
Highcharts.error(33, false, this, { 'Invalid attribute in config': 'credits.href' });
63+
delete options.href;
64+
}
65+
return proceed.call(this, options);
66+
});
67+
68+
/* eslint-enable @typescript-eslint/no-invalid-this, prefer-arrow-callback */

‎packages/charts/src/vaadin-chart.js‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import 'highcharts/es-modules/masters/modules/timeline.src.js';
2626
import 'highcharts/es-modules/masters/modules/organization.src.js';
2727
import 'highcharts/es-modules/masters/modules/xrange.src.js';
2828
import 'highcharts/es-modules/masters/modules/bullet.src.js';
29+
import './highcharts-patches.js';
2930
import { FlattenedNodesObserver } from '@polymer/polymer/lib/utils/flattened-nodes-observer.js';
3031
import { beforeNextRender } from '@polymer/polymer/lib/utils/render-status.js';
3132
import { html, PolymerElement } from '@polymer/polymer/polymer-element.js';
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
import { expect } from '@esm-bundle/chai';
2+
import { fixtureSync, oneEvent } from '@vaadin/testing-helpers';
3+
import '../vaadin-chart.js';
4+
import Highcharts from 'highcharts/es-modules/masters/highstock.src.js';
5+
6+
// eslint-disable-next-line no-script-url
7+
const UNSUPPORTED_URL = 'javascript:void(0)';
8+
9+
describe('vaadin-chart option values', () => {
10+
let chart, chartContainer;
11+
12+
beforeEach(async () => {
13+
chart = fixtureSync(`
14+
<vaadin-chart>
15+
<vaadin-chart-series values="[10, 20, 30]"></vaadin-chart-series>
16+
</vaadin-chart>
17+
`);
18+
await oneEvent(chart, 'chart-load');
19+
chartContainer = chart.$.chart;
20+
});
21+
22+
describe('point keys', () => {
23+
afterEach(() => {
24+
delete Object.prototype.custom;
25+
});
26+
27+
it('should set nested point keys', () => {
28+
const series = chart.configuration.addSeries({ keys: ['y', 'custom.value'], data: [[1, 'a']] });
29+
expect(series.points[0].custom.value).to.equal('a');
30+
});
31+
32+
it('should ignore reserved segments in point keys', () => {
33+
const series = chart.configuration.addSeries({
34+
keys: ['y', '__proto__.custom', 'constructor.prototype.custom'],
35+
data: [[1, 'a', 'b']],
36+
});
37+
expect({}.custom).to.be.undefined;
38+
expect(series.points[0].y).to.equal(1);
39+
});
40+
});
41+
42+
describe('point options', () => {
43+
it('should ignore reserved option names in point objects', () => {
44+
const series = chart.configuration.series[0];
45+
series.setData(JSON.parse('[{ "y": 1, "__proto__": { "custom": "a" }, "constructor": { "custom": "b" } }]'));
46+
const point = series.points[0];
47+
expect(point).to.be.instanceOf(Highcharts.Point);
48+
expect(point.y).to.equal(1);
49+
expect(point.custom).to.be.undefined;
50+
expect(point.options).to.not.have.own.property('constructor');
51+
});
52+
});
53+
54+
describe('credits', () => {
55+
async function setCreditsHref(href) {
56+
chart.updateConfiguration({ credits: { enabled: true, href } });
57+
await oneEvent(chart, 'chart-redraw');
58+
}
59+
60+
[
61+
'https://vaadin.com',
62+
'HTTPS://VAADIN.COM',
63+
'about.html',
64+
'/about',
65+
'#about',
66+
'tel:+123',
67+
'ftp://vaadin.com',
68+
].forEach((href) => {
69+
it(`should keep supported credits links: ${href}`, async () => {
70+
await setCreditsHref(href);
71+
expect(chart.configuration.options.credits.href).to.equal(href);
72+
});
73+
});
74+
75+
[UNSUPPORTED_URL, ` JAVASCRIPT:void(0)`, `java\tscript:void(0)`, 'data:text/html,Text', 'vbscript:Text'].forEach(
76+
(href) => {
77+
it(`should ignore credits links that use an unsupported URL scheme: ${JSON.stringify(href)}`, async () => {
78+
await setCreditsHref(href);
79+
expect(chartContainer.querySelector('.highcharts-credits')).to.be.ok;
80+
expect(chart.configuration.options.credits.href).to.be.undefined;
81+
});
82+
},
83+
);
84+
85+
it('should ignore credits links that use an unsupported URL scheme on update', async () => {
86+
chart.updateConfiguration({ credits: { enabled: true } });
87+
await oneEvent(chart, 'chart-redraw');
88+
chart.configuration.credits.update({ href: UNSUPPORTED_URL });
89+
expect(chart.configuration.options.credits.href).to.be.undefined;
90+
});
91+
});
92+
});

0 commit comments

Comments
 (0)