-
Notifications
You must be signed in to change notification settings - Fork 349
/
test.js
1272 lines (1252 loc) · 51.6 KB
/
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
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
const assert = require('assert');
const sinon = require('sinon');
describe('sanitizeHtml', function() {
let sanitizeHtml;
it('should be successfully initialized', function() {
sanitizeHtml = require('../index.js');
});
it('should escape self closing tags', () => {
assert.equal(sanitizeHtml('before <img src="test.png" /> after', {
disallowedTagsMode: 'escape',
allowedTags: [],
allowedAttributes: false
}), 'before <img src="test.png" /> after');
});
it('should pass through simple well-formed whitelisted markup', function() {
assert.equal(sanitizeHtml('<div><p>Hello <b>there</b></p></div>'), '<div><p>Hello <b>there</b></p></div>');
});
it('should not pass through any text outside html tag boundary since html tag is found and option is ON', function() {
assert.equal(sanitizeHtml('Text before html tag<html><div><p>Hello <b>there</b></p></div></html>Text after html tag!P�X��[<p>paragraph after closing html</p>', {
enforceHtmlBoundary: true
}
), '<div><p>Hello <b>there</b></p></div>');
});
it('should pass through text outside html tag boundary since option is OFF', function() {
assert.equal(sanitizeHtml('Text before html tag<html><div><p>Hello <b>there</b></p></div></html>Text after html tag!P�X��[<p>paragraph after closing html</p>', {
enforceHtmlBoundary: false
}
), 'Text before html tag<div><p>Hello <b>there</b></p></div>Text after html tag!P�X��[<p>paragraph after closing html</p>');
});
it('should pass through text outside html tag boundary since option is ON but html tag is not found', function() {
assert.equal(sanitizeHtml('Text before div tag<div><p>Hello <b>there</b></p></div>Text after div tag!P�X��[<p>paragraph after closing div</p>', {
enforceHtmlBoundary: true
}
), 'Text before div tag<div><p>Hello <b>there</b></p></div>Text after div tag!P�X��[<p>paragraph after closing div</p>');
});
it('should pass through all markup if allowedTags and allowedAttributes are set to false', function() {
assert.equal(sanitizeHtml('<div><wiggly worms="ewww">hello</wiggly></div>', {
allowedTags: false,
allowedAttributes: false
}), '<div><wiggly worms="ewww">hello</wiggly></div>');
});
it('should respect text nodes at top level', function() {
assert.equal(sanitizeHtml('Blah blah blah<p>Whee!</p>'), 'Blah blah blah<p>Whee!</p>');
});
it('should reject markup not whitelisted without destroying its text', function() {
assert.equal(sanitizeHtml('<div><wiggly>Hello</wiggly></div>'), '<div>Hello</div>');
});
it('should escape markup not whitelisted', function() {
assert.equal(sanitizeHtml('<div><wiggly>Hello</wiggly></div>', { disallowedTagsMode: 'escape' }), '<div><wiggly>Hello</wiggly></div>');
});
it('should accept a custom list of allowed tags', function() {
assert.equal(sanitizeHtml('<blue><red><green>Cheese</green></red></blue>', { allowedTags: [ 'blue', 'green' ] }), '<blue><green>Cheese</green></blue>');
});
it('should reject attributes not whitelisted', function() {
assert.equal(sanitizeHtml('<a href="foo.html" whizbang="whangle">foo</a>'), '<a href="foo.html">foo</a>');
});
it('should accept a custom list of allowed attributes per element', function() {
assert.equal(sanitizeHtml('<a href="foo.html" whizbang="whangle">foo</a>', { allowedAttributes: { a: [ 'href', 'whizbang' ] } }), '<a href="foo.html" whizbang="whangle">foo</a>');
});
it('should clean up unclosed img tags and p tags', function() {
assert.equal(sanitizeHtml('<img src="foo.jpg"><p>Whee<p>Again<p>Wow<b>cool</b>', {
allowedTags: sanitizeHtml.defaults.allowedTags.concat([ 'img' ])
}), '<img src="foo.jpg" /><p>Whee</p><p>Again</p><p>Wow<b>cool</b></p>');
});
it('should reject hrefs that are not relative, ftp, http, https or mailto', function() {
assert.equal(sanitizeHtml('<a href="http://google.com">google</a><a href="https://google.com">https google</a><a href="ftp://example.com">ftp</a><a href="mailto:test@test.com">mailto</a><a href="/relative.html">relative</a><a href="javascript:alert(0)">javascript</a>'), '<a href="http://google.com">google</a><a href="https://google.com">https google</a><a href="ftp://example.com">ftp</a><a href="mailto:test@test.com">mailto</a><a href="/relative.html">relative</a><a>javascript</a>');
});
it('should cope identically with capitalized attributes and tags and should tolerate capitalized schemes', function() {
assert.equal(sanitizeHtml('<A HREF="http://google.com">google</a><a href="HTTPS://google.com">https google</a><a href="ftp://example.com">ftp</a><a href="mailto:test@test.com">mailto</a><a href="/relative.html">relative</a><a href="javascript:alert(0)">javascript</a>'), '<a href="http://google.com">google</a><a href="HTTPS://google.com">https google</a><a href="ftp://example.com">ftp</a><a href="mailto:test@test.com">mailto</a><a href="/relative.html">relative</a><a>javascript</a>');
});
it('should drop the content of script elements', function() {
assert.equal(sanitizeHtml('<script>alert("ruhroh!");</script><p>Paragraph</p>'), '<p>Paragraph</p>');
});
it('should drop the content of style elements', function() {
assert.equal(sanitizeHtml('<style>.foo { color: blue; }</style><p>Paragraph</p>'), '<p>Paragraph</p>');
});
it('should drop the content of textarea elements', function() {
assert.equal(sanitizeHtml('<textarea>Nifty</textarea><p>Paragraph</p>'), '<p>Paragraph</p>');
});
it('should drop the content of option elements', function() {
assert.equal(sanitizeHtml('<select><option>one</option><option>two</option></select><p>Paragraph</p>'), '<p>Paragraph</p>');
});
it('should drop the content of textarea elements but keep the closing parent tag, when nested', function() {
assert.equal(sanitizeHtml('<p>Paragraph<textarea>Nifty</textarea></p>'), '<p>Paragraph</p>');
});
it('should retain the content of fibble elements by default', function() {
assert.equal(sanitizeHtml('<fibble>Nifty</fibble><p>Paragraph</p>'), 'Nifty<p>Paragraph</p>');
});
it('should discard the content of fibble elements if specified for nonTextTags', function() {
assert.equal(sanitizeHtml('<fibble>Nifty</fibble><p>Paragraph</p>', { nonTextTags: [ 'fibble' ] }), '<p>Paragraph</p>');
});
it('should retain allowed tags within a fibble element if fibble is not specified for nonTextTags', function() {
assert.equal(sanitizeHtml('<fibble>Ni<em>f</em>ty</fibble><p>Paragraph</p>', {}), 'Ni<em>f</em>ty<p>Paragraph</p>');
});
it('should discard allowed tags within a fibble element if fibble is specified for nonTextTags', function() {
assert.equal(sanitizeHtml('<fibble>Ni<em>f</em>ty</fibble><p>Paragraph</p>', { nonTextTags: [ 'fibble' ] }), '<p>Paragraph</p>');
});
it('should preserve textarea content if textareas are allowed', function() {
assert.equal(sanitizeHtml('<textarea>Nifty</textarea><p>Paragraph</p>', {
allowedTags: [ 'textarea', 'p' ]
}), '<textarea>Nifty</textarea><p>Paragraph</p>');
});
it('should preserve entities as such', function() {
assert.equal(sanitizeHtml('<a name="<silly>"><Kapow!></a>'), '<a name="<silly>"><Kapow!></a>');
});
it('should dump comments', function() {
assert.equal(sanitizeHtml('<p><!-- Blah blah -->Whee</p>'), '<p>Whee</p>');
});
it('should dump a sneaky encoded javascript url', function() {
assert.equal(sanitizeHtml('<a href="javascript:alert('XSS')">Hax</a>'), '<a>Hax</a>');
});
it('should dump an uppercase javascript url', function() {
assert.equal(sanitizeHtml('<a href="JAVASCRIPT:alert(\'foo\')">Hax</a>'), '<a>Hax</a>');
});
it('should dump a javascript URL with a comment in the middle (probably only respected by browsers in XML data islands, but just in case someone enables those)', function() {
assert.equal(sanitizeHtml('<a href="java<!-- -->script:alert(\'foo\')">Hax</a>'), '<a>Hax</a>');
});
it('should not mess up a hashcode with a : in it', function() {
assert.equal(sanitizeHtml('<a href="awesome.html#this:stuff">Hi</a>'), '<a href="awesome.html#this:stuff">Hi</a>');
});
it('should dump character codes 1-32 before testing scheme', function() {
assert.equal(sanitizeHtml('<a href="java\0\t\r\n script:alert(\'foo\')">Hax</a>'), '<a>Hax</a>');
});
it('should dump character codes 1-32 even when escaped with padding rather than trailing ;', function() {
assert.equal(sanitizeHtml('<a href="javascript:alert(\'foo\')">Hax</a>'), '<a>Hax</a>');
// This one is weird, but the browser does not interpret it
// as a scheme, so we're OK. That character is 65535, not null. I
// think it's a limitation of the entities module
assert.equal(sanitizeHtml('<a href="java�script:alert(\'foo\')">Hax</a>'), '<a href="java�script:alert(\'foo\')">Hax</a>');
});
it('should still like nice schemes', function() {
assert.equal(sanitizeHtml('<a href="http://google.com/">Hi</a>'), '<a href="http://google.com/">Hi</a>');
});
it('should still like nice relative URLs', function() {
assert.equal(sanitizeHtml('<a href="hello.html">Hi</a>'), '<a href="hello.html">Hi</a>');
});
it('should replace ol to ul', function() {
assert.equal(sanitizeHtml('<ol><li>Hello world</li></ol>', { transformTags: { ol: 'ul' } }), '<ul><li>Hello world</li></ul>');
});
it('should replace ol to ul and add class attribute with foo value', function() {
assert.equal(sanitizeHtml('<ol><li>Hello world</li></ol>', {
transformTags: { ol: sanitizeHtml.simpleTransform('ul', { class: 'foo' }) },
allowedAttributes: { ul: [ 'class' ] }
}), '<ul class="foo"><li>Hello world</li></ul>');
});
it('should replace ol to ul, left attributes foo and bar untouched, remove baz attribute and add class attributte with foo value', function() {
assert.equal(sanitizeHtml('<ol foo="foo" bar="bar" baz="baz"><li>Hello world</li></ol>', {
transformTags: { ol: sanitizeHtml.simpleTransform('ul', { class: 'foo' }) },
allowedAttributes: { ul: [ 'foo', 'bar', 'class' ] }
}), '<ul foo="foo" bar="bar" class="foo"><li>Hello world</li></ul>');
});
it('should replace ol to ul and replace all attributes to class attribute with foo value', function() {
assert.equal(sanitizeHtml('<ol foo="foo" bar="bar" baz="baz"><li>Hello world</li></ol>', {
transformTags: { ol: sanitizeHtml.simpleTransform('ul', { class: 'foo' }, false) },
allowedAttributes: { ul: [ 'foo', 'bar', 'class' ] }
}), '<ul class="foo"><li>Hello world</li></ul>');
});
it('should replace ol to ul and add attribute class with foo value and attribute bar with bar value', function() {
assert.equal(sanitizeHtml('<ol><li>Hello world</li></ol>', {
transformTags: {
ol: function(tagName, attribs) {
attribs.class = 'foo';
attribs.bar = 'bar';
return {
tagName: 'ul',
attribs: attribs
};
}
},
allowedAttributes: { ul: [ 'bar', 'class' ] }
}), '<ul class="foo" bar="bar"><li>Hello world</li></ul>');
});
it('should replace text and attributes when they are changed by transforming function', function () {
assert.equal(sanitizeHtml('<a href="http://somelink">some text</a>', {
transformTags: {
a: function (tagName, attribs) {
return {
tagName: tagName,
attribs: attribs,
text: ''
};
}
}
}), '<a href="http://somelink"></a>');
});
it('should replace text and attributes when they are changed by transforming function and textFilter is set', function () {
assert.equal(sanitizeHtml('<a href="http://somelink">some text</a>', {
transformTags: {
a: function (tagName, attribs) {
return {
tagName: tagName,
attribs: attribs,
text: 'some text need"to<be>filtered'
};
}
},
textFilter: function (text, tagName) {
return text.replace(/\s/g, '_');
}
}), '<a href="http://somelink">some_text_need"to<be>filtered</a>');
});
it('should replace text and attributes when they are changed by transforming function and textFilter is not set', function () {
assert.equal(sanitizeHtml('<a href="http://somelink">some text</a>', {
transformTags: {
a: function (tagName, attribs) {
return {
tagName: tagName,
attribs: attribs,
text: 'some good text'
};
}
}
}), '<a href="http://somelink">some good text</a>');
});
it('should add new text when not initially set and replace attributes when they are changed by transforming function', function () {
assert.equal(sanitizeHtml('<a href="http://somelink"></a>', {
transformTags: {
a: function (tagName, attribs) {
return {
tagName: tagName,
attribs: attribs,
text: 'some new text'
};
}
}
}), '<a href="http://somelink">some new text</a>');
});
it('should preserve text when initially set and replace attributes when they are changed by transforming function', function () {
assert.equal(sanitizeHtml('<a href="http://somelink">some initial text</a>', {
transformTags: {
a: function (tagName, attribs) {
return {
tagName: tagName,
attribs: attribs
};
}
}
}), '<a href="http://somelink">some initial text</a>');
});
it('should skip an empty link', function() {
assert.strictEqual(
sanitizeHtml('<p>This is <a href="http://www.linux.org"></a><br/>Linux</p>', {
exclusiveFilter: function (frame) {
return frame.tag === 'a' && !frame.text.trim();
}
}),
'<p>This is <br />Linux</p>'
);
});
it('Should expose a node\'s inner text and inner HTML to the filter', function() {
assert.strictEqual(
sanitizeHtml('<p>12<a href="http://www.linux.org"><br/>3<br></a><audio>4</audio></p>', {
exclusiveFilter: function(frame) {
if (frame.tag === 'p') {
assert.strictEqual(frame.text, '124');
} else if (frame.tag === 'a') {
assert.strictEqual(frame.text, '3');
return true;
} else if (frame.tag === 'br') {
assert.strictEqual(frame.text, '');
} else {
assert.fail('p, a, br', frame.tag);
}
return false;
}
}),
'<p>124</p>'
);
});
it('Should collapse nested empty elements', function() {
assert.strictEqual(
sanitizeHtml('<p><a href="http://www.linux.org"><br/></a></p>', {
exclusiveFilter: function(frame) {
return (frame.tag === 'a' || frame.tag === 'p') && !frame.text.trim();
}
}),
''
);
});
it('Should find child media elements that are in allowedTags', function() {
const markup = '<a href="http://www.linux.org"><img /><video></video></a>';
const sansVideo = '<a href="http://www.linux.org"><img /></a>';
const sanitizedMarkup = sanitizeHtml(markup, {
allowedTags: sanitizeHtml.defaults.allowedTags.concat([ 'img' ]),
exclusiveFilter: function(frame) {
if (frame.tag === 'a') {
// eslint-disable-next-line no-console
assert(frame.mediaChildren.length === 1);
}
return (frame.tag === 'a') && !frame.text.trim() && !frame.mediaChildren.length;
}
});
assert.strictEqual(sanitizedMarkup, sansVideo);
});
it('Exclusive filter should not affect elements which do not match the filter condition', function () {
assert.strictEqual(
sanitizeHtml('I love <a href="www.linux.org" target="_hplink">Linux</a> OS',
{
exclusiveFilter: function (frame) {
return (frame.tag === 'a') && !frame.text.trim();
}
}),
'I love <a href="www.linux.org" target="_hplink">Linux</a> OS'
);
});
it('should disallow data URLs with default allowedSchemes', function() {
assert.equal(
sanitizeHtml(
// teeny-tiny valid transparent GIF in a data URL
'<img src="data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==" />',
{
allowedTags: [ 'img' ]
}
),
'<img />'
);
});
it('should allow data URLs with custom allowedSchemes', function() {
assert.equal(
sanitizeHtml(
// teeny-tiny valid transparent GIF in a data URL
'<img src="data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==" />',
{
allowedTags: [ 'img', 'p' ],
allowedSchemes: [ 'data', 'http' ]
}
),
'<img src="data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==" />'
);
});
it('should allow specific classes when whitelisted with allowedClasses for a single tag', function() {
assert.equal(
sanitizeHtml(
'<p class="nifty simple dippy">whee</p>',
{
allowedTags: [ 'p' ],
allowedClasses: {
p: [ 'nifty' ]
}
}
),
'<p class="nifty">whee</p>'
);
});
it('should allow specific classes when whitelisted with allowedClasses for all tags', function() {
assert.equal(
sanitizeHtml(
'<p class="nifty simple dippy">whee</p><div class="dippy nifty simple"></div>',
{
allowedTags: [ 'p', 'div' ],
allowedClasses: {
'*': [ 'nifty' ]
}
}
),
'<p class="nifty">whee</p><div class="nifty"></div>'
);
});
it('should allow all classes that are whitelisted for a single tag or all tags', function() {
assert.equal(
sanitizeHtml(
'<p class="nifty simple dippy">whee</p><div class="simple dippy nifty"></div>',
{
allowedTags: [ 'p', 'div' ],
allowedClasses: {
'*': [ 'simple' ],
p: [ 'nifty' ],
div: [ 'dippy' ]
}
}
),
'<p class="nifty simple">whee</p><div class="simple dippy"></div>'
);
});
it('should allow defining schemes on a per-tag basis', function() {
assert.equal(
sanitizeHtml(
// teeny-tiny valid transparent GIF in a data URL
'<img src="data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==" /><a href="https://www.example.com"></a>',
{
allowedTags: [ 'img', 'a' ],
allowedSchemes: [ 'http' ],
allowedSchemesByTag: {
img: [ 'data' ],
a: [ 'https' ]
}
}
),
'<img src="data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==" /><a href="https://www.example.com"></a>'
);
assert.equal(
sanitizeHtml(
// teeny-tiny valid transparent GIF in a data URL
'<img src="data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==" /><a href="https://www.example.com"></a>',
{
allowedTags: [ 'img', 'a' ],
allowedSchemes: [ 'http' ],
allowedSchemesByTag: {
img: [],
a: [ 'https' ]
}
}
),
'<img /><a href="https://www.example.com"></a>'
);
});
it('should not act weird when the class attribute is empty', function() {
assert.equal(
sanitizeHtml(
'<p class="">whee</p>',
{
allowedTags: [ 'p' ],
allowedClasses: {
p: [ 'nifty' ]
}
}
),
'<p>whee</p>'
);
});
it('should not crash on bad markup', function() {
assert.equal(
sanitizeHtml(
'<p a'
),
''
);
});
it('should not allow a naked = sign followed by an unrelated attribute to result in one merged attribute with unescaped double quote marks', function() {
assert.equal(
sanitizeHtml(
'<IMG SRC= onmouseover="alert(\'XSS\');">',
{
allowedTags: [ 'img' ],
allowedAttributes: {
img: [ 'src' ]
}
}
),
// This is weird but not dangerous. Without the " there
// would probably be some way to make it come out as a
// separate attribute
'<img src="onmouseover="alert(\'XSS\');"" />'
);
});
it('should deliver a warning if using vulnerable tags', function() {
const spy = sinon.spy(console, 'warn');
const message = '\n\n⚠️ Your `allowedTags` option includes, `style`, which is inherently\nvulnerable to XSS attacks. Please remove it from `allowedTags`.\nOr, to disable this warning, add the `allowVulnerableTags` option\nand ensure you are accounting for this risk.\n\n';
sanitizeHtml(
'<style></style>',
{
allowedTags: [ 'style' ]
}
);
assert(spy.calledWith(message));
// Restore the spied-upon method
console.warn.restore();
});
it('should not deliver a warning if using the allowVulnerableTags option', function() {
const spy = sinon.spy(console, 'warn');
sanitizeHtml(
'<style></style>',
{
allowVulnerableTags: true,
allowedTags: [ 'style' ]
}
);
assert(spy.notCalled);
// Restore the spied-upon method
console.warn.restore();
});
it('should allow only whitelisted attributes, but to any tags, if tag is declared as "*"', function() {
assert.equal(
sanitizeHtml(
'<table bgcolor="1" align="left" notlisted="0"><img src="1.gif" align="center" alt="not listed too"/></table>',
{
allowedTags: [ 'table', 'img' ],
allowedAttributes: {
'*': [ 'bgcolor', 'align', 'src' ]
}
}
),
'<table bgcolor="1" align="left"><img src="1.gif" align="center" /></table>'
);
});
it('should not filter if exclusive filter does not match after transforming tags', function() {
assert.equal(
sanitizeHtml(
'<a href="test.html">test</a>',
{
allowedTags: [ 'a' ],
allowedAttributes: { a: [ 'href', 'target' ] },
transformTags: {
a: function (tagName, attribs) {
if (!attribs.href) {
return false;
}
return {
tagName: tagName,
attribs: {
target: '_blank',
href: attribs.href
}
};
}
},
exclusiveFilter: function(frame) {
return frame.tag === 'a' && frame.text.trim() === 'blah';
}
}
),
'<a target="_blank" href="test.html">test</a>'
);
});
it('should filter if exclusive filter does match after transforming tags', function() {
assert.equal(
sanitizeHtml(
'<a href="test.html">blah</a>',
{
allowedTags: [ 'a' ],
allowedAttributes: { a: [ 'href', 'target' ] },
transformTags: {
a: function (tagName, attribs) {
if (!attribs.href) {
return false;
}
return {
tagName: tagName,
attribs: {
target: '_blank',
href: attribs.href
}
};
}
},
exclusiveFilter: function(frame) {
return frame.tag === 'a' && frame.text.trim() === 'blah';
}
}
),
''
);
});
it('should allow transform on all tags using \'*\'', function () {
assert.equal(
sanitizeHtml(
'<p>Text</p>',
{
allowedTags: [ 'p' ],
allowedAttributes: { p: [ 'style' ] },
transformTags: {
'*': function (tagName, attribs) {
return {
tagName: tagName,
attribs: {
style: 'text-align: center'
}
};
}
}
}
),
'<p style="text-align:center">Text</p>'
);
});
it('should not be faked out by double <', function() {
assert.equal(
sanitizeHtml('<<img src="javascript:evil"/>img src="javascript:evil"/>'
),
'<img src="javascript:evil"/>'
);
assert.equal(
sanitizeHtml('<<a href="javascript:evil"/>a href="javascript:evil"/>'
),
'<<a>a href="javascript:evil"/></a>'
);
});
it('should allow attributes to be specified as globs', function() {
assert.equal(
sanitizeHtml('<a data-target="#test" data-foo="hello">click me</a>', {
allowedTags: [ 'a' ],
allowedAttributes: { a: [ 'data-*' ] }
}), '<a data-target="#test" data-foo="hello">click me</a>'
);
assert.equal(
sanitizeHtml('<a data-target="#test" data-my-foo="hello">click me</a>', {
allowedTags: [ 'a' ],
allowedAttributes: { a: [ 'data-*-foo' ] }
}), '<a data-my-foo="hello">click me</a>'
);
});
it('should quote regex chars in attributes specified as globs', function() {
assert.equal(
sanitizeHtml('<a data-b.c="#test" data-bcc="remove this">click me</a>', {
allowedTags: [ 'a' ],
allowedAttributes: { a: [ 'data-b.*' ] }
}), '<a data-b.c="#test">click me</a>'
);
});
it('should not escape inner content of script and style tags (when allowed)', function() {
assert.equal(
sanitizeHtml('<div>"normal text"</div><script>"this is code"</script>', {
allowedTags: [ 'script' ]
}), '"normal text"<script>"this is code"</script>'
);
assert.equal(
sanitizeHtml('<div>"normal text"</div><style>body { background-image: url("image.test"); }</style>', {
allowedTags: [ 'style' ]
}), '"normal text"<style>body { background-image: url("image.test"); }</style>'
);
});
it('should not unescape escapes found inside script tags', function() {
assert.equal(
sanitizeHtml('<script>alert(""This is cool but just ironically so I quoted it"")</script>',
{
allowedTags: [ 'script' ]
}
),
'<script>alert(""This is cool but just ironically so I quoted it"")</script>'
);
});
it('should process text nodes with provided function', function() {
assert.equal(
sanitizeHtml('"normal text this should be removed"', {
textFilter: function(text, tagName) {
return text.replace(' this should be removed', '');
}
}), '"normal text"'
);
});
it('should skip text nodes based on tagName', function() {
assert.equal(
sanitizeHtml('<a>normal text this should be removed</a><b>normal text this should be removed</b>', {
textFilter: function(text, tagName) {
if (tagName === 'a') {
return text;
};
return text.replace(' this should be removed', '');
}
}), '<a>normal text this should be removed</a><b>normal text</b>'
);
});
it('should respect htmlparser2 options when passed in', function() {
assert.equal(
sanitizeHtml('<Archer><Sterling>I am</Sterling></Archer>', {
allowedTags: false,
allowedAttributes: false
}),
'<archer><sterling>I am</sterling></archer>'
);
assert.equal(
sanitizeHtml('<Archer><Sterling>I am</Sterling></Archer>', {
allowedTags: false,
allowedAttributes: false,
parser: {
lowerCaseTags: false
}
}),
'<Archer><Sterling>I am</Sterling></Archer>'
);
});
it('should not crash due to tag names that are properties of the universal Object prototype', function() {
assert.equal(
sanitizeHtml('!<__proto__>!'),
'!<__proto__>!');
});
it('should correctly maintain escaping when allowing a nonTextTags tag other than script or style', function() {
assert.equal(
sanitizeHtml('!<textarea></textarea><svg/onload=prompt`xs`></textarea>!',
{ allowedTags: [ 'textarea' ] }
), '!<textarea></textarea><svg/onload=prompt`xs`></textarea>!'
);
});
it('should allow protocol relative links by default', function() {
assert.equal(
sanitizeHtml('<a href="//cnn.com/example">test</a>'),
'<a href="//cnn.com/example">test</a>'
);
});
it('should not allow protocol relative links when allowProtocolRelative is false', function() {
assert.equal(
sanitizeHtml('<a href="//cnn.com/example">test</a>', { allowProtocolRelative: false }),
'<a>test</a>'
);
assert.equal(
sanitizeHtml('<a href="/\\cnn.com/example">test</a>', { allowProtocolRelative: false }),
'<a>test</a>'
);
assert.equal(
sanitizeHtml('<a href="\\\\cnn.com/example">test</a>', { allowProtocolRelative: false }),
'<a>test</a>'
);
assert.equal(
sanitizeHtml('<a href="\\/cnn.com/example">test</a>', { allowProtocolRelative: false }),
'<a>test</a>'
);
});
it('should still allow regular relative URLs when allowProtocolRelative is false', function() {
assert.equal(
sanitizeHtml('<a href="/welcome">test</a>', { allowProtocolRelative: false }),
'<a href="/welcome">test</a>'
);
});
it('should discard srcset by default', function() {
assert.equal(
sanitizeHtml('<img src="fallback.jpg" srcset="foo.jpg 100w 2x, bar.jpg 200w 1x" />', {
allowedTags: sanitizeHtml.defaults.allowedTags.concat([ 'img' ])
}),
'<img src="fallback.jpg" />'
);
});
it('should accept srcset if allowed', function() {
assert.equal(
sanitizeHtml('<img src="fallback.jpg" srcset="foo.jpg 100w, bar.jpg 200w" />', {
allowedTags: sanitizeHtml.defaults.allowedTags.concat([ 'img' ]),
allowedAttributes: { img: [ 'src', 'srcset' ] }
}),
'<img src="fallback.jpg" srcset="foo.jpg 100w, bar.jpg 200w" />'
);
assert.equal(
sanitizeHtml('<img src="fallback.jpg" srcset="foo.jpg 2x, bar.jpg 1x" />', {
allowedTags: sanitizeHtml.defaults.allowedTags.concat([ 'img' ]),
allowedAttributes: { img: [ 'src', 'srcset' ] }
}),
'<img src="fallback.jpg" srcset="foo.jpg 2x, bar.jpg 1x" />'
);
});
it('should drop bogus srcset', function() {
assert.equal(
sanitizeHtml('<img src="fallback.jpg" srcset="foo.jpg 100w, bar.jpg 200w, javascript:alert(1) 100w" />', {
allowedTags: sanitizeHtml.defaults.allowedTags.concat([ 'img' ]),
allowedAttributes: { img: [ 'src', 'srcset' ] }
}),
'<img src="fallback.jpg" srcset="foo.jpg 100w, bar.jpg 200w" />'
);
});
it('should accept srcset with urls containing commas', function() {
assert.equal(
sanitizeHtml('<img src="fallback.jpg" srcset="/upload/f_auto,q_auto:eco,c_fit,w_1460,h_2191/abc.jpg 1460w, /upload/f_auto,q_auto:eco,c_fit,w_1360,h_2041/abc.jpg" />', {
allowedTags: sanitizeHtml.defaults.allowedTags.concat([ 'img' ]),
allowedAttributes: { img: [ 'src', 'srcset' ] }
}),
'<img src="fallback.jpg" srcset="/upload/f_auto,q_auto:eco,c_fit,w_1460,h_2191/abc.jpg 1460w, /upload/f_auto,q_auto:eco,c_fit,w_1360,h_2041/abc.jpg" />'
);
});
it('text from transformTags should not specify tags', function() {
const input = '<input value="<script>alert(1)</script>">';
const want = '<u class="inlined-input"><script>alert(1)</script></u>';
// Runs the sanitizer with a policy that turns an attribute into
// text. A policy like this might be used to turn inputs into
// inline elements that look like the original but which do not
// affect form submissions.
const got = sanitizeHtml(
input,
{
allowedTags: [ 'u' ],
allowedAttributes: { '*': [ 'class' ] },
transformTags: {
input: function (tagName, attribs) {
return {
tagName: 'u',
attribs: { class: 'inlined-input' },
text: attribs.value
};
}
}
});
assert.equal(got, want);
});
it('drop attribute names with meta-characters', function() {
assert.equal(
sanitizeHtml('<span data-<script>alert(1)//>', {
allowedTags: [ 'span' ],
allowedAttributes: { span: [ 'data-*' ] }
}),
'<span>alert(1)//></span>'
);
});
it('should sanitize styles correctly', function() {
const sanitizeString = '<p dir="ltr"><strong>beste</strong><em>testestes</em><s>testestset</s><u>testestest</u></p><ul dir="ltr"> <li><u>test</u></li></ul><blockquote dir="ltr"> <ol> <li><u>test</u></li><li><u>test</u></li><li style="text-align: right"><u>test</u></li><li style="text-align: justify"><u>test</u></li></ol> <p><u><span style="color:#00FF00">test</span></u></p><p><span style="color:#00FF00"><span style="font-size:36px">TESTETESTESTES</span></span></p></blockquote>';
const expected = '<p dir="ltr"><strong>beste</strong><em>testestes</em><s>testestset</s><u>testestest</u></p><ul dir="ltr"> <li><u>test</u></li></ul><blockquote dir="ltr"> <ol> <li><u>test</u></li><li><u>test</u></li><li style="text-align: right"><u>test</u></li><li style="text-align: justify"><u>test</u></li></ol> <p><u><span style="color:#00FF00">test</span></u></p><p><span style="color:#00FF00"><span style="font-size:36px">TESTETESTESTES</span></span></p></blockquote>';
assert.equal(
sanitizeHtml(sanitizeString, {
allowedTags: false,
allowedAttributes: {
'*': [ 'dir' ],
p: [ 'dir', 'style' ],
li: [ 'style' ],
span: [ 'style' ]
},
allowedStyles: {
'*': {
// Matches hex
color: [ /#(0x)?[0-9a-f]+/i ],
'text-align': [ /left/, /right/, /center/, /justify/, /initial/, /inherit/ ],
'font-size': [ /36px/ ]
}
}
}).replace(/ /g, ''), expected.replace(/ /g, '')
);
});
it('Should remove empty style tags', function() {
assert.equal(
sanitizeHtml('<span style=\'\'></span>', {
allowedTags: false,
allowedAttributes: false
}),
'<span></span>'
);
});
it('Should remove invalid styles', function() {
assert.equal(
sanitizeHtml('<span style=\'color: blue; text-align: justify\'></span>', {
allowedTags: false,
allowedAttributes: {
span: [ 'style' ]
},
allowedStyles: {
span: {
color: [ /blue/ ],
'text-align': [ /left/ ]
}
}
}), '<span style="color:blue"></span>'
);
});
it('Should allow a specific style from global', function() {
assert.equal(
sanitizeHtml('<span style=\'color: yellow; text-align: center; font-family: helvetica\'></span>', {
allowedTags: false,
allowedAttributes: {
span: [ 'style' ]
},
allowedStyles: {
'*': {
color: [ /yellow/ ],
'text-align': [ /center/ ]
},
span: {
color: [ /green/ ],
'font-family': [ /helvetica/ ]
}
}
}), '<span style="color:yellow;text-align:center;font-family:helvetica"></span>'
);
});
it('Should allow hostnames in an iframe that are whitelisted', function() {
assert.equal(
sanitizeHtml('<iframe src="https://www.youtube.com/embed/c2IlcS7AHxM"></iframe>', {
allowedTags: [ 'p', 'iframe', 'a', 'img', 'i' ],
allowedAttributes: {
iframe: [ 'src', 'href' ],
a: [ 'src', 'href' ],
img: [ 'src' ]
},
allowedIframeHostnames: [ 'www.youtube.com', 'player.vimeo.com' ]
}), '<iframe src="https://www.youtube.com/embed/c2IlcS7AHxM"></iframe>'
);
});
it('Should remove iframe src urls that are not included in whitelisted hostnames', function() {
assert.equal(
sanitizeHtml('<iframe src="https://www.embed.vevo.com/USUV71704255"></iframe>', {
allowedTags: [ 'p', 'iframe', 'a', 'img', 'i' ],
allowedAttributes: {
iframe: [ 'src', 'href' ],
a: [ 'src', 'href' ],
img: [ 'src' ]
},
allowedIframeHostnames: [ 'www.youtube.com', 'player.vimeo.com' ]
}), '<iframe></iframe>'
);
});
it('Should not allow iframe urls that do not have proper hostname', function() {
assert.equal(
sanitizeHtml('<iframe src="https://www.vimeo.com/embed/c2IlcS7AHxM"></iframe>', {
allowedTags: [ 'p', 'iframe', 'a', 'img', 'i' ],
allowedAttributes: {
iframe: [ 'src', 'href' ],
a: [ 'src', 'href' ],
img: [ 'src' ]
},
allowedIframeHostnames: [ 'www.youtube.com', 'player.vimeo.com' ]
}), '<iframe></iframe>'
);
});
it('Should allow iframe through if no hostname option is set', function() {
assert.equal(
sanitizeHtml('<iframe src="https://www.vimeo.com/embed/c2IlcS7AHxM"></iframe>', {
allowedTags: [ 'p', 'iframe', 'a', 'img', 'i' ],
allowedAttributes: {
iframe: [ 'src', 'href' ],
a: [ 'src', 'href' ],
img: [ 'src' ]
}
}), '<iframe src="https://www.vimeo.com/embed/c2IlcS7AHxM"></iframe>'
);
});
it('Should allow domains in an iframe that are whitelisted', function() {
assert.equal(
sanitizeHtml('<iframe src="https://www.foo.us02web.zoom.us/embed/c2IlcS7AHxM"></iframe>', {
allowedTags: [ 'p', 'iframe', 'a', 'img', 'i' ],
allowedAttributes: {
iframe: [ 'src', 'href' ],
a: [ 'src', 'href' ],
img: [ 'src' ]
},
allowedIframeDomains: [ 'zoom.us' ]
}), '<iframe src="https://www.foo.us02web.zoom.us/embed/c2IlcS7AHxM"></iframe>'
);
});
it('Should allow second-level domains in an iframe that are whitelisted', function() {
assert.equal(
sanitizeHtml('<iframe src="https://zoom.us/embed/c2IlcS7AHxM"></iframe>', {
allowedTags: [ 'p', 'iframe', 'a', 'img', 'i' ],
allowedAttributes: {
iframe: [ 'src', 'href' ],
a: [ 'src', 'href' ],
img: [ 'src' ]
},
allowedIframeDomains: [ 'zoom.us' ]
}), '<iframe src="https://zoom.us/embed/c2IlcS7AHxM"></iframe>'
);
});
it('Should remove iframe src urls that are not included in whitelisted domains', function() {
assert.equal(
sanitizeHtml('<iframe src="https://www.prefix.us02web.zoom.us/embed/c2IlcS7AHxM"></iframe>', {
allowedTags: [ 'p', 'iframe', 'a', 'img', 'i' ],
allowedAttributes: {
iframe: [ 'src', 'href' ],
a: [ 'src', 'href' ],
img: [ 'src' ]
},
allowedIframeDomains: [ 'vimeo.com' ]
}), '<iframe></iframe>'
);
});
it('Should remove iframe src urls with host that ends as whitelisted domains ' +
' but not preceeded with a dot', function() {
assert.equal(
sanitizeHtml('<iframe src="https://www.zoomzoom.us/embed/c2IlcS7AHxM"></iframe>', {
allowedTags: [ 'p', 'iframe', 'a', 'img', 'i' ],
allowedAttributes: {
iframe: [ 'src', 'href' ],
a: [ 'src', 'href' ],
img: [ 'src' ]
},
allowedIframeDomains: [ 'zoom.us' ]
}), '<iframe></iframe>'
);
});
it('Should allow hostnames in an iframe that are whitelisted in allowedIframeHostnames ' +
'and are not whitelisted in allowedIframeDomains', function() {
assert.equal(
sanitizeHtml('<iframe src="https://www.youtube.com/embed/c2IlcS7AHxM"></iframe>', {
allowedTags: [ 'p', 'iframe', 'a', 'img', 'i' ],
allowedAttributes: {
iframe: [ 'src', 'href' ],
a: [ 'src', 'href' ],
img: [ 'src' ]
},
allowedIframeHostnames: [ 'www.youtube.com', 'player.vimeo.com' ],
allowedIframeDomains: [ 'zoom.us' ]
}), '<iframe src="https://www.youtube.com/embed/c2IlcS7AHxM"></iframe>'
);
});
it('Should allow hostnames in an iframe that are not whitelisted in allowedIframeHostnames ' +
'and are whitelisted in allowedIframeDomains', function() {
assert.equal(
sanitizeHtml('<iframe src="https://www.us02web.zoom.us/embed/c2IlcS7AHxM"></iframe>', {
allowedTags: [ 'p', 'iframe', 'a', 'img', 'i' ],
allowedAttributes: {
iframe: [ 'src', 'href' ],
a: [ 'src', 'href' ],
img: [ 'src' ]
},
allowedIframeHostnames: [ 'www.youtube.com', 'player.vimeo.com' ],
allowedIframeDomains: [ 'zoom.us' ]
}), '<iframe src="https://www.us02web.zoom.us/embed/c2IlcS7AHxM"></iframe>'
);
});