-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathsvg_text_utils_test.js
311 lines (248 loc) · 11.4 KB
/
svg_text_utils_test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
var d3 = require('d3');
var util = require('@src/lib/svg_text_utils');
describe('svg+text utils', function() {
'use strict';
describe('convertToTspans', function() {
function mockTextSVGElement(txt) {
return d3.select('body')
.append('svg')
.classed('text-tester', true)
.append('text')
.text(txt)
.call(util.convertToTspans)
.attr('transform', 'translate(50,50)');
}
function assertAnchorLink(node, href, target, show, msg) {
var a = node.select('a');
if(target === undefined) target = href === null ? null : '_blank';
if(show === undefined) show = href === null ? null : 'new';
expect(a.attr('xlink:href')).toBe(href, msg);
expect(a.attr('target')).toBe(target, msg);
expect(a.attr('xlink:show')).toBe(show, msg);
}
function assertTspanStyle(node, style, msg) {
var tspan = node.select('tspan');
expect(tspan.attr('style')).toBe(style, msg);
}
function assertAnchorAttrs(node, expectedAttrs, msg) {
var a = node.select('a');
if(!expectedAttrs) expectedAttrs = {};
var WHITE_LIST = ['xlink:href', 'xlink:show', 'style', 'target', 'onclick'],
attrs = listAttributes(a.node());
// check that no other attribute are found in anchor,
// which can be lead to XSS attacks.
var wrongAttrs = [];
attrs.forEach(function(attr) {
if(WHITE_LIST.indexOf(attr) === -1) wrongAttrs.push(attr);
});
expect(wrongAttrs).toEqual([], msg);
var style = expectedAttrs.style || '';
var fullStyle = style || '';
if(style) fullStyle += ';';
fullStyle += 'cursor:pointer';
expect(a.attr('style')).toBe(fullStyle, msg);
expect(a.attr('onclick')).toBe(expectedAttrs.onclick || null, msg);
}
function listAttributes(node) {
var items = Array.prototype.slice.call(node.attributes);
var attrs = items.map(function(item) {
return item.name;
});
return attrs;
}
afterEach(function() {
d3.selectAll('.text-tester').remove();
});
it('checks for XSS attack in href', function() {
var node = mockTextSVGElement(
'<a href="javascript:alert(\'attack\')">XSS</a>'
);
expect(node.text()).toEqual('XSS');
assertAnchorAttrs(node);
assertAnchorLink(node, null);
});
it('checks for XSS attack in href (with plenty of white spaces)', function() {
var node = mockTextSVGElement(
'<a href = " javascript:alert(\'attack\')">XSS</a>'
);
expect(node.text()).toEqual('XSS');
assertAnchorAttrs(node);
assertAnchorLink(node, null);
});
it('whitelists relative hrefs (interpreted as http)', function() {
var node = mockTextSVGElement(
'<a href="/mylink">mylink</a>'
);
expect(node.text()).toEqual('mylink');
assertAnchorAttrs(node);
assertAnchorLink(node, '/mylink');
});
it('whitelists http hrefs', function() {
var node = mockTextSVGElement(
'<a href="http://bl.ocks.org/">bl.ocks.org</a>'
);
expect(node.text()).toEqual('bl.ocks.org');
assertAnchorAttrs(node);
assertAnchorLink(node, 'http://bl.ocks.org/');
});
it('whitelists https hrefs', function() {
var node = mockTextSVGElement(
'<a href="https://plot.ly">plot.ly</a>'
);
expect(node.text()).toEqual('plot.ly');
assertAnchorAttrs(node);
assertAnchorLink(node, 'https://plot.ly');
});
it('whitelists mailto hrefs', function() {
var node = mockTextSVGElement(
'<a href="mailto:[email protected]">support</a>'
);
expect(node.text()).toEqual('support');
assertAnchorAttrs(node);
assertAnchorLink(node, 'mailto:[email protected]');
});
it('drops XSS attacks in href', function() {
// "XSS" gets interpreted as a relative link (http)
var textCases = [
'<a href="XSS\" onmouseover="alert(1)\" style="font-size:300px">Subtitle</a>',
'<a href="XSS" onmouseover="alert(1)" style="font-size:300px">Subtitle</a>'
];
textCases.forEach(function(textCase) {
var node = mockTextSVGElement(textCase);
expect(node.text()).toEqual('Subtitle');
assertAnchorAttrs(node, {style: 'font-size:300px'});
assertAnchorLink(node, 'XSS');
});
});
it('accepts href and style in <a> in any order and tosses other stuff', function() {
var textCases = [
'<a href="x" style="y">z</a>',
'<a href=\'x\' style="y">z</a>',
'<A HREF="x"StYlE=\'y\'>z</a>',
'<a style=\'y\'href=\'x\'>z</A>',
'<a \t\r\n href="x" \n\r\t style="y" \n \t \r>z</a>',
'<a magic="true" href="x" weather="cloudy" style="y" speed="42">z</a>',
'<a href="x" style="y">z</a href="nope" style="for real?">',
];
textCases.forEach(function(textCase) {
var node = mockTextSVGElement(textCase);
expect(node.text()).toEqual('z');
assertAnchorAttrs(node, {style: 'y'});
assertAnchorLink(node, 'x');
});
});
it('accepts `target` with links and tries to translate it to `xlink:show`', function() {
var specs = [
{target: '_blank', show: 'new'},
{target: '_self', show: 'replace'},
{target: '_parent', show: 'replace'},
{target: '_top', show: 'replace'},
{target: 'some_frame_name', show: 'new'}
];
specs.forEach(function(spec) {
var node = mockTextSVGElement('<a href="x" target="' + spec.target + '">link</a>');
assertAnchorLink(node, 'x', spec.target, spec.show, spec.target);
});
});
it('attaches onclick if popup is specified', function() {
var node = mockTextSVGElement('<a href="x" target="fred" popup="width=500,height=400">link</a>');
assertAnchorLink(node, 'x', 'fred', 'new');
assertAnchorAttrs(node, {onclick: 'window.open("x","fred","width=500,height=400");return false;'});
});
it('keeps query parameters in href', function() {
var textCases = [
'<a href="https://abc.com/myFeature.jsp?name=abc&pwd=def">abc.com?shared-key</a>',
'<a href="https://abc.com/myFeature.jsp?name=abc&pwd=def">abc.com?shared-key</a>'
];
textCases.forEach(function(textCase) {
var node = mockTextSVGElement(textCase);
assertAnchorAttrs(node, {}, textCase);
expect(node.text()).toEqual('abc.com?shared-key', textCase);
assertAnchorLink(node, 'https://abc.com/myFeature.jsp?name=abc&pwd=def', undefined, undefined, textCase);
});
});
it('allows basic spans', function() {
var node = mockTextSVGElement(
'<span>text</span>'
);
expect(node.text()).toEqual('text');
assertTspanStyle(node, null);
});
it('ignores unquoted styles in spans', function() {
var node = mockTextSVGElement(
'<span style=unquoted>text</span>'
);
expect(node.text()).toEqual('text');
assertTspanStyle(node, null);
});
it('allows quoted styles in spans', function() {
var node = mockTextSVGElement(
'<span style="quoted: yeah;">text</span>'
);
expect(node.text()).toEqual('text');
assertTspanStyle(node, 'quoted: yeah;');
});
it('ignores extra stuff after span styles', function() {
var node = mockTextSVGElement(
'<span style="quoted: yeah;"disallowed: indeed;">text</span>'
);
expect(node.text()).toEqual('text');
assertTspanStyle(node, 'quoted: yeah;');
});
it('escapes HTML entities in span styles', function() {
var node = mockTextSVGElement(
'<span style="quoted: yeah&\';;">text</span>'
);
expect(node.text()).toEqual('text');
assertTspanStyle(node, 'quoted: yeah&\';;');
});
it('decodes some HTML entities in text', function() {
var node = mockTextSVGElement(
'100μ & < 10 > 0 ' +
'100 × 20 ± 0.5 °'
);
expect(node.text()).toEqual('100μ & < 10 > 0 100 × 20 ± 0.5 °');
});
it('supports superscript by itself', function() {
var node = mockTextSVGElement('<sup>123</sup>');
expect(node.html()).toBe(
'<tspan style="font-size:70%" dy="-0.6em">123</tspan>' +
'<tspan dy="0.42em"></tspan>');
});
it('supports subscript by itself', function() {
var node = mockTextSVGElement('<sub>123</sub>');
expect(node.html()).toBe(
'<tspan style="font-size:70%" dy="0.3em">123</tspan>' +
'<tspan dy="-0.21em"></tspan>');
});
it('supports superscript and subscript together with normal text', function() {
var node = mockTextSVGElement('SO<sub>4</sub><sup>2-</sup>');
expect(node.html()).toBe(
'SO<tspan style="font-size:70%" dy="0.3em">4</tspan>' +
'<tspan dy="-0.21em"></tspan>' +
'<tspan style="font-size:70%" dy="-0.6em">2-</tspan>' +
'<tspan dy="0.42em"></tspan>');
});
it('allows one <b> to span <br>s', function() {
var node = mockTextSVGElement('be <b>Bold<br>and<br><i>Strong</i></b>');
expect(node.html()).toBe(
'<tspan class="line" dy="0em">be ' +
'<tspan style="font-weight:bold">Bold</tspan></tspan>' +
'<tspan class="line" dy="1.3em">' +
'<tspan style="font-weight:bold">and</tspan></tspan>' +
'<tspan class="line" dy="2.6em">' +
'<tspan style="font-weight:bold">' +
'<tspan style="font-style:italic">Strong</tspan></tspan></tspan>');
});
it('allows one <sub> to span <br>s', function() {
var node = mockTextSVGElement('SO<sub>4<br>44</sub>');
expect(node.html()).toBe(
'<tspan class="line" dy="0em">SO' +
'<tspan style="font-size:70%" dy="0.3em">4</tspan>' +
'<tspan dy="-0.21em"></tspan></tspan>' +
'<tspan class="line" dy="1.3em">' +
'<tspan style="font-size:70%" dy="0.3em">44</tspan>' +
'<tspan dy="-0.21em"></tspan></tspan>');
});
});
});