-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathsvg_text_utils_test.js
508 lines (416 loc) · 19.5 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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
var d3 = require('d3');
var util = require('@src/lib/svg_text_utils');
describe('svg+text utils', function() {
'use strict';
describe('convertToTspans', function() {
var stringFromCodePoint;
beforeAll(function() {
stringFromCodePoint = String.fromCodePoint;
});
afterEach(function() {
String.fromCodePoint = stringFromCodePoint;
});
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'];
var 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('allows encoded URIs in href', function() {
var node = mockTextSVGElement(
'<a href="https://example.com/?q=date%20%3E=%202018-01-01">click</a>'
);
expect(node.text()).toEqual('click');
assertAnchorAttrs(node);
assertAnchorLink(node, 'https://example.com/?q=date%20%3E=%202018-01-01');
});
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(this.href.baseVal,this.target.baseVal,"width=500,height=400");return false;'});
});
it('drops XSS attacks via popup script', function() {
var textCases = [
[
'<a href=\'#\' target=\'b\' popup=\'1");alert(document.cookie);//\'>XSS</a>',
'#', 'b', null
],
[
'<a href=\'#\' target=\'b");alert(document.cookie);//\' popup=\'1\'>XSS</a>',
'#', 'b");alert(document.cookie);//', '1'
],
[
'<a href=\'#");alert(document.cookie);//\' target=\'b\' popup=\'1\'>XSS</a>',
'#%22);alert(document.cookie);//', 'b', '1'
]
];
textCases.forEach(function(textCase) {
var node = mockTextSVGElement(textCase[0]);
var attrs = {};
if(textCase[3]) {
attrs.onclick = 'window.open(this.href.baseVal,this.target.baseVal,"' +
textCase[3] + '");return false;';
}
expect(node.text()).toEqual('XSS');
assertAnchorAttrs(node, attrs, textCase[0]);
assertAnchorLink(node, textCase[1], textCase[2], 'new', textCase[0]);
});
});
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()).toBe('100μ & < 10 > 0 100 × 20 ± 0.5 °');
});
it('decodes some HTML entities in text (number case)', function() {
var node = mockTextSVGElement(
'100μ & < 10 > 0  ' +
'100 × 20 ± 0.5 °'
);
expect(node.text()).toBe('100μ & < 10 > 0 100 × 20 ± 0.5 °');
});
it('decodes arbitrary decimal and hex number entities', function() {
var i = 0;
for(var n = 33; n < 0x10FFFF; n = Math.round(n * 1.03)) {
var node = mockTextSVGElement(
'&#x' + n.toString(16) +
'; = &#' + n.toString() +
'; = &#x' + n.toString(16).toUpperCase() + ';'
);
var char = String.fromCodePoint(n);
expect(node.text()).toBe(char + ' = ' + char + ' = ' + char, n);
i++;
}
// not really necessary to assert this, but we tested 355 characters,
// weighted toward the low end but continuing all the way to the
// end of the unicode definition
expect(i).toBe(355);
});
it('decodes arbitrary decimal and hex number entities (IE case)', function() {
// IE does not have String.fromCodePoint
String.fromCodePoint = undefined;
expect(String.fromCodePoint).toBeUndefined();
var i = 0;
for(var n = 33; n < 0x10FFFF; n = Math.round(n * 1.03)) {
var node = mockTextSVGElement(
'&#x' + n.toString(16) +
'; = &#' + n.toString() +
'; = &#x' + n.toString(16).toUpperCase() + ';'
);
var char = stringFromCodePoint(n);
expect(node.text()).toBe(char + ' = ' + char + ' = ' + char, n);
i++;
}
// not really necessary to assert this, but we tested 355 characters,
// weighted toward the low end but continuing all the way to the
// end of the unicode definition
expect(i).toBe(355);
});
it('does not decode entities prematurely', function() {
var testCases = [
'<b>not bold</b>',
'<b>not bold</b>',
'<b>not bold</b>',
'<b>not bold</b>',
'<b>not bold</b>'
];
testCases.forEach(function(testCase) {
var node = mockTextSVGElement(testCase);
expect(node.html()).toBe(
'<b>not bold</b>', testCase
);
});
var controlNode = mockTextSVGElement('<b>bold</b>');
expect(controlNode.html()).toBe(
'<tspan style="font-weight:bold">bold</tspan>'
);
});
it('supports superscript by itself', function() {
var node = mockTextSVGElement('<sup>123</sup>');
expect(node.html()).toBe(
'\u200b<tspan style="font-size:70%" dy="-0.6em">123</tspan>' +
'<tspan dy="0.42em">\u200b</tspan>');
});
it('supports subscript by itself', function() {
var node = mockTextSVGElement('<sub>123</sub>');
expect(node.html()).toBe(
'\u200b<tspan style="font-size:70%" dy="0.3em">123</tspan>' +
'<tspan dy="-0.21em">\u200b</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\u200b<tspan style="font-size:70%" dy="0.3em">4</tspan>' +
'<tspan dy="-0.21em">\u200b</tspan>\u200b' +
'<tspan style="font-size:70%" dy="-0.6em">2-</tspan>' +
'<tspan dy="0.42em">\u200b</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" x="0" y="0">be ' +
'<tspan style="font-weight:bold">Bold</tspan></tspan>' +
'<tspan class="line" dy="1.3em" x="0" y="0">' +
'<tspan style="font-weight:bold">and</tspan></tspan>' +
'<tspan class="line" dy="2.6em" x="0" y="0">' +
'<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" x="0" y="0">SO\u200b' +
'<tspan style="font-size:70%" dy="0.3em">4</tspan>' +
'<tspan dy="-0.21em">\u200b</tspan></tspan>' +
'<tspan class="line" dy="1.3em" x="0" y="0">\u200b' +
'<tspan style="font-size:70%" dy="0.3em">44</tspan>' +
'<tspan dy="-0.21em">\u200b</tspan></tspan>');
});
it('allows nested tags to break at <br>, eventually closed or not', function() {
var textCases = [
'<b><i><sup>many<br>lines<br>modified',
'<b><i><sup>many<br>lines<br>modified</sup></i></b>',
'<b><i><sup>many</sup><br><sup>lines</sup></i><br><i><sup>modified',
];
textCases.forEach(function(textCase) {
var node = mockTextSVGElement(textCase);
function opener(dy) {
return '<tspan class="line" dy="' + dy + 'em" x="0" y="0">' +
'<tspan style="font-weight:bold">' +
'<tspan style="font-style:italic">' +
'\u200b<tspan style="font-size:70%" dy="-0.6em">';
}
var closer = '</tspan><tspan dy="0.42em">\u200b</tspan>' +
'</tspan></tspan></tspan>';
expect(node.html()).toBe(
opener(0) + 'many' + closer +
opener(1.3) + 'lines' + closer +
opener(2.6) + 'modified' + closer, textCase);
});
});
it('ignores bare closing tags', function() {
var node = mockTextSVGElement('</sub>');
// sub shows up as a zero-width space (u200B) on either side of the 5:
expect(node.text()).toEqual('');
});
it('ignores extra closing tags', function() {
var node = mockTextSVGElement('test<sub>5</sub></sub>more');
// sub shows up as a zero-width space (u200B) on either side of the 5:
expect(node.text()).toEqual('test\u200b5\u200bmore');
});
});
describe('plainText:', function() {
var fn = util.plainText;
it('should strip tags except <br> by default', function() {
expect(fn('a<b>b</b><br><sup>tm</sup>a')).toBe('ab<br>tma');
});
it('should work in various cases w/o <br>', function() {
var sIn = 'ThisIsDATA<sup>300</sup>';
expect(fn(sIn)).toBe('ThisIsDATA300');
expect(fn(sIn, {len: 3})).toBe('Thi');
expect(fn(sIn, {len: 4})).toBe('T...');
expect(fn(sIn, {len: 13})).toBe('ThisIsDATA...');
expect(fn(sIn, {len: 16})).toBe('ThisIsDATA300');
expect(fn(sIn, {allowedTags: ['sup']})).toBe('ThisIsDATA<sup>300</sup>');
expect(fn(sIn, {len: 13, allowedTags: ['sup']})).toBe('ThisIsDATA...');
expect(fn(sIn, {len: 16, allowedTags: ['sup']})).toBe('ThisIsDATA<sup>300</sup>');
});
it('should work in various cases w/ <br>', function() {
var sIn = 'ThisIs<br>DATA<sup>300</sup>';
expect(fn(sIn)).toBe('ThisIs<br>DATA300');
expect(fn(sIn, {len: 3})).toBe('Thi');
expect(fn(sIn, {len: 4})).toBe('T...');
expect(fn(sIn, {len: 7})).toBe('ThisIs...');
expect(fn(sIn, {len: 8})).toBe('ThisIs...');
expect(fn(sIn, {len: 9})).toBe('ThisIs...');
expect(fn(sIn, {len: 10})).toBe('ThisIs<br>D...');
expect(fn(sIn, {len: 13})).toBe('ThisIs<br>DATA...');
expect(fn(sIn, {len: 16})).toBe('ThisIs<br>DATA300');
expect(fn(sIn, {allowedTags: ['sup']})).toBe('ThisIsDATA<sup>300</sup>');
expect(fn(sIn, {allowedTags: ['br', 'sup']})).toBe('ThisIs<br>DATA<sup>300</sup>');
});
});
});