-
-
Notifications
You must be signed in to change notification settings - Fork 860
Expand file tree
/
Copy pathtest-suite.js
More file actions
6530 lines (6013 loc) · 231 KB
/
Copy pathtest-suite.js
File metadata and controls
6530 lines (6013 loc) · 231 KB
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
/*
* DOMPurify test suite.
*
* Organised into QUnit modules. Modules group related tests so a failure
* narrows the search space; within a module, tests run top-to-bottom in
* declaration order. Cross-module ordering is not guaranteed, so anything
* that needs to run before/after siblings goes inside a single module
* (e.g. the hook-pollution module uses beforeEach/afterEach for isolation).
*
* Conventions:
* - `var` is reserved for the UMD wrapper and the per-test scratch where
* hoisting actually matters; everything else uses const/let.
* - Arrow callbacks for QUnit.test bodies; `function () {}` is used only
* where a hook needs its own `this` binding.
* - assert.contains([...]) is used when serialiser output varies across
* engines (jsdom vs Chromium vs Firefox vs WebKit). The list is the
* acceptable set, not "any of these is good enough".
*
* Security-regression tests carry a CVE / GHSA identifier in the test title
* where one is published. The accompanying comment block explains the
* primitive being guarded against; do not strip those comments — they are
* the only documentation that exists for several of these issues.
*/
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined'
? (module.exports = factory())
: typeof define === 'function' && define.amd
? define(factory)
: ((global =
typeof globalThis !== 'undefined' ? globalThis : global || self),
(global.testSuite = factory()));
})(this, function () {
return function testSuite(
DOMPurify,
window,
sanitizationTestCases,
xssTestCases
) {
const document = window.document;
const jQuery = window.jQuery;
// =======================================================================
// Data-driven sanitization tests.
// The fixtures live alongside the runner (`tests/fixtures/...`) so the
// suite can be reused against multiple builds (dist, src, minified)
// without duplicating the corpus.
// =======================================================================
QUnit.module('Sanitization (data-driven)');
sanitizationTestCases.forEach((testCase) => {
QUnit.test(`sanitization[${testCase.title}]`, (assert) => {
assert.contains(
DOMPurify.sanitize(testCase.payload),
testCase.expected,
`Payload: ${testCase.payload}`
);
});
});
// =======================================================================
// XSS tests — assert that alert() is never called by sanitized output.
// Three runners cover the three insertion sinks DOMPurify users hit
// in practice: native innerHTML, jQuery.html(), and document.write().
// The latter exists because Firefox historically behaves differently
// from Chromium when content is injected via the parser entry point.
// =======================================================================
QUnit.module('XSS — native innerHTML');
xssTestCases.forEach((testCase) => {
QUnit.test(`xss[${testCase.title}]`, (assert) => {
document.getElementById('qunit-fixture').innerHTML = DOMPurify.sanitize(
testCase.payload
);
const done = assert.async();
setTimeout(() => {
assert.notEqual(window.xssed, true, 'alert() was called');
document.getElementById('qunit-fixture').innerHTML = '';
window.xssed = false;
done();
}, 100);
});
});
QUnit.module('XSS — jQuery.html()');
xssTestCases.forEach((testCase) => {
QUnit.test(`xss[${testCase.title}]`, (assert) => {
jQuery('#qunit-fixture').html(DOMPurify.sanitize(testCase.payload));
const done = assert.async();
setTimeout(() => {
assert.notEqual(window.xssed, true, 'alert() was called');
jQuery('#qunit-fixture').empty();
window.xssed = false;
done();
}, 100);
});
});
QUnit.module('XSS — iframe document.write()');
xssTestCases.forEach((testCase) => {
QUnit.test(`xss[${testCase.title}]`, (assert) => {
const done = assert.async();
const iframe = document.createElement('iframe');
iframe.src = 'about:blank';
iframe.onload = function () {
iframe.contentDocument.write(
'<script>window.alert=function(){top.xssed=true;}</script>' +
DOMPurify.sanitize(testCase.payload)
);
assert.notEqual(
window.xssed,
true,
'alert() was called from document.write()'
);
window.xssed = false;
iframe.parentNode.removeChild(iframe);
done();
};
document.body.appendChild(iframe);
});
});
// Sanity check: confirm the iframe-write detector itself works when
// given a genuinely malicious payload. If this test ever stops firing
// alert(), the XSS — iframe document.write() module above is no longer
// testing what we think it is.
QUnit.module('XSS — detector self-test');
QUnit.test(
'iframe document.write() with raw payload triggers detector',
(assert) => {
const done = assert.async();
window.xssed = false;
const iframe = document.createElement('iframe');
iframe.src = 'about:blank';
iframe.onload = function () {
iframe.contentDocument.write(
'<script>window.alert=function(){parent.xssed=true;}</script>' +
'<script>alert(1);</script>'
);
assert.equal(
window.xssed,
true,
'alert() was called but the detector missed it'
);
window.xssed = false;
iframe.parentNode.removeChild(iframe);
done();
};
document.body.appendChild(iframe);
}
);
// =======================================================================
// Config: KEEP_CONTENT, ALLOWED_TAGS, ALLOWED_ATTR
// =======================================================================
QUnit.module('Config — KEEP_CONTENT, ALLOWED_TAGS, ALLOWED_ATTR');
QUnit.test(
'KEEP_CONTENT=false drops content of stripped tags',
(assert) => {
assert.equal(
DOMPurify.sanitize('<iframe>Hello</iframe>', { KEEP_CONTENT: false }),
''
);
}
);
QUnit.test(
'KEEP_CONTENT=true preserves content inside ALLOWED_TAGS / ALLOWED_ATTR',
(assert) => {
assert.contains(
DOMPurify.sanitize(
'<a href="#">abc<b style="color:red">123</b><q class="cite">123</b></a>',
{
ALLOWED_TAGS: ['b', 'q'],
ALLOWED_ATTR: ['style'],
KEEP_CONTENT: true,
}
),
[
'abc<b style="color:red">123</b><q>123</q>',
'abc<b style="color: red;">123</b><q>123</q>',
]
);
}
);
QUnit.test(
'KEEP_CONTENT=false drops content even when surrounding tags are allowed',
(assert) => {
assert.equal(
DOMPurify.sanitize(
'<a href="#">abc<b style="color:red">123</b><q class="cite">123</b></a>',
{
ALLOWED_TAGS: ['b', 'q'],
ALLOWED_ATTR: ['style'],
KEEP_CONTENT: false,
}
),
''
);
assert.equal(
DOMPurify.sanitize('<a href="#">abc</a>', {
ALLOWED_TAGS: ['b', 'q'],
KEEP_CONTENT: false,
}),
''
);
}
);
QUnit.test(
'KEEP_CONTENT preserves descendants when only the parent is allowed',
(assert) => {
assert.equal(
DOMPurify.sanitize('<form><input name="parentNode"></form>', {
ALLOWED_TAGS: ['input'],
KEEP_CONTENT: true,
}),
'<input>'
);
}
);
// =======================================================================
// Config: ALLOW_SELF_CLOSE_IN_ATTR
// =======================================================================
QUnit.module('Config — ALLOW_SELF_CLOSE_IN_ATTR');
QUnit.test('ALLOW_SELF_CLOSE_IN_ATTR=false strips attribute', (assert) => {
assert.equal(
DOMPurify.sanitize('<a href="#" class="foo <br/>">abc</a>', {
ALLOW_SELF_CLOSE_IN_ATTR: false,
}),
'<a href="#">abc</a>'
);
});
QUnit.test('ALLOW_SELF_CLOSE_IN_ATTR=true keeps attribute', (assert) => {
assert.contains(
DOMPurify.sanitize('<a href="#" class="foo <br/>">abc</a>', {
ALLOW_SELF_CLOSE_IN_ATTR: true,
}),
[
'<a href="#" class="foo <br/>">abc</a>',
'<a href="#" class="foo <br/>">abc</a>',
]
);
});
// =======================================================================
// Declarative Partial Updates — patch-directive attributes
// https://github.com/WICG/declarative-partial-updates/blob/main/patching-explainer.md
//
// <template for="..."> teleports / range-replaces content into a
// pre-existing element (by id or marker name); patchsrc fetches remote
// markup (script-loading equivalent for CSP). Patches apply on
// connection/stream — AFTER a parse-time sanitizer has run over a detached
// fragment — so these must never survive sanitization. PI range markers are
// already dropped by _isUnsafeNode; removal is gated on SAFE_FOR_XML. Each
// test uses a fresh instance so a persistent setConfig left by an earlier
// test cannot suppress _parseConfig and swallow the per-call config.
// =======================================================================
QUnit.module('Declarative partial updates');
QUnit.test(
'for teleport directive is stripped on non-label/output',
(assert) => {
const purify = DOMPurify(window);
assert.equal(
purify.sanitize('<div for="account-panel">x</div>'),
'<div>x</div>'
);
assert.equal(
purify.sanitize('<section for="cart#total">$0.00</section>'),
'<section>$0.00</section>'
);
}
);
QUnit.test('for is preserved on label and output', (assert) => {
const purify = DOMPurify(window);
assert.equal(
purify.sanitize('<label for="email">Email</label>'),
'<label for="email">Email</label>'
);
assert.equal(
purify.sanitize('<output for="a b">42</output>', {
ADD_TAGS: ['output'],
}),
'<output for="a b">42</output>'
);
});
QUnit.test(
'template[for] loses its patch directive when template is allowed',
(assert) => {
const purify = DOMPurify(window);
assert.contains(
purify.sanitize('<template for="account-panel"><b>x</b></template>', {
ADD_TAGS: ['template'],
}),
['<template><b>x</b></template>', '<template></template>', ''],
'template survives but the `for` patch directive does not'
);
}
);
QUnit.test('patchsrc is stripped even when explicitly added', (assert) => {
const purify = DOMPurify(window);
assert.equal(
purify.sanitize('<div patchsrc="//evil.example/p">x</div>', {
ADD_ATTR: ['patchsrc'],
}),
'<div>x</div>'
);
});
QUnit.test('processing-instruction range markers are removed', (assert) => {
const purify = DOMPurify(window);
assert.equal(
purify.sanitize('<section><?start name="g">hi<?end></section>'),
'<section>hi</section>'
);
assert.equal(
purify.sanitize('<ul><li>a</li><?marker name="m"><li>b</li></ul>'),
'<ul><li>a</li><li>b</li></ul>'
);
});
QUnit.test(
'SAFE_FOR_XML=false is the escape hatch for patch attributes',
(assert) => {
const purify = DOMPurify(window);
assert.equal(
purify.sanitize('<div for="target">x</div>', { SAFE_FOR_XML: false }),
'<div for="target">x</div>'
);
assert.equal(
purify.sanitize('<div patchsrc="//e">x</div>', {
ADD_ATTR: ['patchsrc'],
SAFE_FOR_XML: false,
}),
'<div patchsrc="//e">x</div>'
);
}
);
// =======================================================================
// Config: ALLOW_DATA_ATTR
// =======================================================================
QUnit.module('Config — ALLOW_DATA_ATTR');
QUnit.test('malformed data- attribute is stripped regardless', (assert) => {
assert.equal(
DOMPurify.sanitize('<a href="#" data-abc"="foo">abc</a>', {
ALLOW_DATA_ATTR: true,
}),
'<a href="#">abc</a>'
);
});
QUnit.test('ALLOW_DATA_ATTR=false strips data-* attributes', (assert) => {
assert.equal(
DOMPurify.sanitize('<a href="#" data-abc="foo">abc</a>', {
ALLOW_DATA_ATTR: false,
}),
'<a href="#">abc</a>'
);
});
QUnit.test(
'ALLOW_DATA_ATTR=true keeps well-formed data-* attributes',
(assert) => {
assert.contains(
DOMPurify.sanitize('<a href="#" data-abc="foo">abc</a>', {
ALLOW_DATA_ATTR: true,
}),
[
'<a data-abc="foo" href="#">abc</a>',
'<a href="#" data-abc="foo">abc</a>',
]
);
assert.contains(
DOMPurify.sanitize('<a href="#" data-abc-1-2-3="foo">abc</a>', {
ALLOW_DATA_ATTR: true,
}),
[
'<a data-abc-1-2-3="foo" href="#">abc</a>',
'<a href="#" data-abc-1-2-3="foo">abc</a>',
]
);
}
);
QUnit.test(
'ALLOW_DATA_ATTR with empty / non-ASCII names — name-validity rules apply',
(assert) => {
// Empty post-prefix name: rejected.
assert.equal(
DOMPurify.sanitize('<a href="#" data-""="foo">abc</a>', {
ALLOW_DATA_ATTR: true,
}),
'<a href="#">abc</a>'
);
// Latin-1 letters: accepted in engines that follow the HTML spec.
assert.contains(
DOMPurify.sanitize('<a href="#" data-äöü="foo">abc</a>', {
ALLOW_DATA_ATTR: true,
}),
[
'<a href="#" data-äöü="foo">abc</a>',
'<a data-äöü="foo" href="#">abc</a>',
]
);
// Middle-dot / combining marks — accepted-or-rejected depending on
// engine; IE11 and Edge throw InvalidCharacterError, modern engines
// accept.
assert.contains(
DOMPurify.sanitize('<a href="#" data-\u00B7._="foo">abc</a>', {
ALLOW_DATA_ATTR: true,
}),
[
'<a data-\u00B7._="foo" href="#">abc</a>',
'<a href="#">abc</a>',
'<a href="#" data-·._="foo">abc</a>',
]
);
// µ (micro sign, U+00B5) is not in the spec-defined name char set.
assert.equal(
DOMPurify.sanitize('<a href="#" data-\u00B5="foo">abc</a>', {
ALLOW_DATA_ATTR: true,
}),
'<a href="#">abc</a>'
);
}
);
QUnit.test(
'FORBID_ATTR overrides ALLOW_DATA_ATTR for the named attribute',
(assert) => {
assert.equal(
DOMPurify.sanitize('<a href="#" data-evil="foo">abc</a>', {
FORBID_ATTR: ['data-evil'],
}),
'<a href="#">abc</a>'
);
assert.equal(
DOMPurify.sanitize('<a href="#" data-evil="foo">abc</a>', {
ALLOW_DATA_ATTR: true,
FORBID_ATTR: ['data-evil'],
}),
'<a href="#">abc</a>'
);
}
);
// =======================================================================
// Config: ADD_TAGS & ADD_ATTR
// ADD_TAGS / ADD_ATTR can be either string arrays or predicate functions.
// The function form has its own subtleties (no leakage across calls,
// URI validation still runs after the function returns true, etc.).
// =======================================================================
QUnit.module('Config — ADD_TAGS & ADD_ATTR');
QUnit.test('ADD_TAGS as string array', (assert) => {
assert.equal(
DOMPurify.sanitize('<my-component>abc</my-component>', {
ADD_TAGS: ['my-component'],
}),
'<my-component>abc</my-component>'
);
});
QUnit.test(
'ADD_TAGS without matching ADD_ATTR strips the custom attr',
(assert) => {
assert.equal(
DOMPurify.sanitize('<my-component my-attr="foo">abc</my-component>', {
ADD_TAGS: ['my-component'],
}),
'<my-component>abc</my-component>'
);
}
);
QUnit.test('ADD_TAGS + ADD_ATTR keeps both', (assert) => {
assert.equal(
DOMPurify.sanitize('<my-component my-attr="foo">abc</my-component>', {
ADD_TAGS: ['my-component'],
ADD_ATTR: ['my-attr'],
}),
'<my-component my-attr="foo">abc</my-component>'
);
});
QUnit.test('ADD_TAGS as function selectively allows tags', (assert) => {
assert.equal(
DOMPurify.sanitize(
'<apple>content</apple><banana>content</banana><cherry>content</cherry>',
{
ADD_TAGS: (tagName) => ['apple', 'banana'].includes(tagName),
KEEP_CONTENT: false,
}
),
'<apple>content</apple><banana>content</banana>'
);
assert.equal(
DOMPurify.sanitize('<allowed>yes</allowed><forbidden>no</forbidden>', {
ADD_TAGS: (tagName) => tagName === 'allowed',
KEEP_CONTENT: false,
}),
'<allowed>yes</allowed>'
);
});
QUnit.test('ADD_TAGS as function supports pattern matching', (assert) => {
assert.equal(
DOMPurify.sanitize(
'<item1>one</item1><item2>two</item2><other>three</other>',
{
ADD_TAGS: (tagName) => tagName.startsWith('item'),
KEEP_CONTENT: false,
}
),
'<item1>one</item1><item2>two</item2>'
);
});
QUnit.test(
'ADD_ATTR as function receives (attrName, tagName) for tag-specific validation',
(assert) => {
assert.equal(
DOMPurify.sanitize(
'<one attribute-one="1" attribute-two="2"></one><two attribute-one="1" attribute-two="2"></two>',
{
ADD_TAGS: ['one', 'two'],
ADD_ATTR: (attributeName, tagName) => {
const allowed = {
one: ['attribute-one'],
two: ['attribute-two'],
};
return allowed[tagName]?.includes(attributeName) || false;
},
}
),
'<one attribute-one="1"></one><two attribute-two="2"></two>'
);
}
);
QUnit.test('ADD_ATTR as function works on built-in tags', (assert) => {
assert.equal(
DOMPurify.sanitize('<div custom-attr="test">content</div>', {
ADD_ATTR: (attr, tag) => tag === 'div' && attr === 'custom-attr',
}),
'<div custom-attr="test">content</div>'
);
});
QUnit.test(
'ADD_ATTR as function rejecting returns the default',
(assert) => {
assert.equal(
DOMPurify.sanitize('<one attribute-one="1" forbidden="bad"></one>', {
ADD_TAGS: ['one'],
ADD_ATTR: (attr, tag) => tag === 'one' && attr === 'attribute-one',
}),
'<one attribute-one="1"></one>'
);
}
);
QUnit.test('ADD_ATTR function must not bypass URI validation', (assert) => {
// Returning true from ADD_ATTR allows the *attribute name* but the
// attribute *value* still has to pass IS_ALLOWED_URI for URI-bearing
// attributes like href. Otherwise the function form would be a
// permanent javascript: bypass.
assert.ok(
DOMPurify.sanitize('<a href="javascript:alert(1)">x</a>', {
ADD_ATTR: (attr) => attr === 'href',
}).indexOf('javascript:') === -1,
'javascript: URI must be stripped from href'
);
assert.equal(
DOMPurify.sanitize('<a href="https://example.com">x</a>', {
ADD_ATTR: (attr) => attr === 'href',
}),
'<a href="https://example.com">x</a>',
'safe URI must be preserved in href'
);
});
QUnit.test(
'ADD_TAGS function does not leak permissiveness into subsequent array-based calls',
(assert) => {
// Permissive function call — must not stash permissiveness on the
// instance.
DOMPurify.sanitize('<b>x</b>', { ADD_TAGS: () => true });
const out = DOMPurify.sanitize(
'<iframe src="https://evil.com"></iframe>' +
'<object data="https://evil.com"></object>' +
'<embed src="https://evil.com">',
{ ADD_TAGS: ['custom-tag'] }
);
assert.ok(
!/<(iframe|object|embed)/i.test(out),
'array-based call must not inherit permissiveness: ' + out
);
const out2 = DOMPurify.sanitize(
'<iframe src="https://evil.com"></iframe>'
);
assert.ok(
!/<iframe/i.test(out2),
'default call after function-based ADD_TAGS must block iframe: ' +
out2
);
}
);
QUnit.test(
'ADD_ATTR function does not leak permissiveness into subsequent array-based calls',
(assert) => {
DOMPurify.sanitize('<b>x</b>', { ADD_ATTR: () => true });
const out = DOMPurify.sanitize(
'<a href="javascript:alert(1)">click</a>',
{ ADD_ATTR: ['class'] }
);
assert.ok(
out.indexOf('javascript:') === -1,
'array-based call must not inherit permissiveness: ' + out
);
const out2 = DOMPurify.sanitize(
'<a href="javascript:alert(1)">click</a>'
);
assert.ok(
out2.indexOf('javascript:') === -1,
'default call after function-based ADD_ATTR must block javascript: URIs: ' +
out2
);
}
);
// =======================================================================
// Config: FORBID_CONTENTS / ADD_FORBID_CONTENTS
// =======================================================================
QUnit.module('Config — FORBID_CONTENTS / ADD_FORBID_CONTENTS');
QUnit.test(
'FORBID_CONTENTS + FORBID_TAGS drops content of named ancestors',
(assert) => {
assert.equal(
DOMPurify.sanitize(
'<div><b>preserve me</b></div><p><b>no not preserve me</b></p>',
{ FORBID_CONTENTS: ['p'], FORBID_TAGS: ['div', 'p'] }
),
'<b>preserve me</b>'
);
}
);
QUnit.test(
'ADD_FORBID_CONTENTS extends the FORBID_CONTENTS set',
(assert) => {
assert.equal(
DOMPurify.sanitize(
'<div><b>preserve me</b></div><p><b>no not preserve me</b></p><a><i>also no preserve me</i></a>',
{
FORBID_CONTENTS: ['p'],
ADD_FORBID_CONTENTS: ['a'],
FORBID_TAGS: ['div', 'p', 'a'],
}
),
'<b>preserve me</b>'
);
}
);
QUnit.test('ADD_FORBID_CONTENTS preserves the default set', (assert) => {
assert.equal(
DOMPurify.sanitize(
'<script>var a = 1;</script><p><b>no not preserve me</b></p><a><i>preserve me</i></a>',
{ ADD_FORBID_CONTENTS: ['p'], FORBID_TAGS: ['script', 'p', 'a'] }
),
'<i>preserve me</i>'
);
});
// =======================================================================
// Config: SAFE_FOR_JQUERY (now a no-op; secure by default)
// The flag was removed but the behaviour it enabled is now unconditional.
// The cases below pin that behaviour so removing the legacy default would
// be immediately visible.
// =======================================================================
QUnit.module('Config — SAFE_FOR_JQUERY (legacy / secure by default)');
QUnit.test('style inside option is stripped', (assert) => {
assert.equal(
DOMPurify.sanitize(
'<a>123</a><option><style><img src=x onerror=alert(1)>'
),
'<a>123</a><option></option>'
);
});
QUnit.test('cross-boundary style + img inside option', (assert) => {
assert.contains(
DOMPurify.sanitize(
'<option><style></option></select><b><img src=xx: onerror=alert(1)></style></option>'
),
['<option></option>', '']
);
});
QUnit.test('iframe + script inside option', (assert) => {
assert.contains(
DOMPurify.sanitize(
'<option><iframe></select><b><script>alert(1)</script>'
),
['<option></option>', '']
);
});
QUnit.test('nested style tags with self-closing form', (assert) => {
assert.equal(
DOMPurify.sanitize('<b><style><style/><img src=xx: onerror=alert(1)>'),
'<b></b>'
);
});
QUnit.test('template element preservation varies by engine', (assert) => {
assert.contains(DOMPurify.sanitize('1<template><s>000</s></template>2'), [
'1<template><s>000</s></template>2',
'1<template></template>2',
'12',
]);
assert.contains(DOMPurify.sanitize('<template><s>000</s></template>'), [
'',
'<template><s>000</s></template>',
]);
});
QUnit.test(
'entity round-trip — github.com/cure53/DOMPurify/issues/283',
(assert) => {
assert.equal(
DOMPurify.sanitize('<i>&amp; <</i>'),
'<i>&amp; <</i>'
);
}
);
// =======================================================================
// Config: SAFE_FOR_TEMPLATES
// Scrubs Mustache-style {{...}} and ASP-style <% ... %> markers from
// text and attribute values so template engines cannot interpolate
// attacker-controlled expressions after sanitization.
// =======================================================================
QUnit.module('Config — SAFE_FOR_TEMPLATES');
QUnit.test(
'strips template markers from mixed text and style content',
(assert) => {
assert.equal(
DOMPurify.sanitize(
'<a>123{{456}}<b><style><% alert(1) %></style>456</b></a>',
{ SAFE_FOR_TEMPLATES: true }
),
'<a> <b><style> </style>456</b></a>'
);
}
);
QUnit.test(
'strips data-bind expressions even when not data-attributes',
(assert) => {
assert.equal(
DOMPurify.sanitize('<a data-bind="style: alert(1)"></a>', {
SAFE_FOR_TEMPLATES: true,
}),
'<a></a>'
);
}
);
QUnit.test(
'does not bring back stripped attributes via ALLOW_DATA_ATTR',
(assert) => {
assert.equal(
DOMPurify.sanitize('<a data-harmless=""></a>', {
SAFE_FOR_TEMPLATES: true,
ALLOW_DATA_ATTR: true,
}),
'<a></a>'
);
assert.equal(
DOMPurify.sanitize('<a data-harmless=""></a>', {
SAFE_FOR_TEMPLATES: false,
ALLOW_DATA_ATTR: false,
}),
'<a></a>'
);
}
);
QUnit.test(
'strips multiple template markers in the same node',
(assert) => {
assert.equal(
DOMPurify.sanitize(
'<a>{{123}}{{456}}<b><style><% alert(1) %><% 123 %></style>456</b></a>',
{ SAFE_FOR_TEMPLATES: true }
),
'<a> <b><style> </style>456</b></a>'
);
assert.equal(
DOMPurify.sanitize(
'<a>{{123}}abc{{456}}<b><style><% alert(1) %>def<% 123 %></style>456</b></a>',
{ SAFE_FOR_TEMPLATES: true }
),
'<a> <b><style> </style>456</b></a>'
);
}
);
QUnit.test(
'greedy matching: nested and unbalanced markers scrub fully',
(assert) => {
assert.equal(
DOMPurify.sanitize(
'<a>123{{45{{6}}<b><style><% alert(1)%> %></style>456</b></a>',
{ SAFE_FOR_TEMPLATES: true }
),
'<a> <b><style> </style>456</b></a>'
);
assert.equal(
DOMPurify.sanitize(
'<a>123{{45}}6}}<b><style><% <%alert(1) %></style>456</b></a>',
{ SAFE_FOR_TEMPLATES: true }
),
'<a> <b><style> </style>456</b></a>'
);
assert.equal(
DOMPurify.sanitize(
'<a>123{{<b>456}}</b><style><% alert(1) %></style>456</a>',
{ SAFE_FOR_TEMPLATES: true }
),
'<a>123 <b> </b><style> </style>456</a>'
);
}
);
QUnit.test(
'scrubs expressions that contain stripped subtrees',
(assert) => {
assert.contains(
DOMPurify.sanitize(
'<b>{{evil<script>alert(1)</script><form><img src=x name=textContent></form>}}</b>',
{ SAFE_FOR_TEMPLATES: true }
),
['<b> </b>', '<b> </b>', '<b> <form><img src="x"></form> </b>']
);
assert.contains(
DOMPurify.sanitize(
'<b>he{{evil<script>alert(1)</script><form><img src=x name=textContent></form>}}ya</b>',
{ SAFE_FOR_TEMPLATES: true }
),
[
'<b>he ya</b>',
'<b>he </b>',
'<b>he <form><img src="x"></form> ya</b>',
]
);
}
);
QUnit.test(
'handles cross-boundary <% ... %> spanning subtrees',
(assert) => {
assert.equal(
DOMPurify.sanitize(
'<a>123<% <b>456}}</b><style>{{ alert(1) }}</style>456 %></a>',
{ SAFE_FOR_TEMPLATES: true }
),
'<a>123 <b> </b><style> </style> </a>'
);
}
);
QUnit.test(
'scrubbed attribute values cannot reintroduce javascript:',
(assert) => {
assert.equal(
DOMPurify.sanitize('<a href="}}javascript:alert(1)"></a>', {
SAFE_FOR_TEMPLATES: true,
}),
'<a></a>'
);
}
);
QUnit.test(
'attribute and bare-string markers are also scrubbed',
(assert) => {
assert.equal(
DOMPurify.sanitize('<a class="{{999-333}}"></a>', {
SAFE_FOR_TEMPLATES: true,
}),
'<a class=" "></a>'
);
assert.equal(
DOMPurify.sanitize('{{999-333}}', { SAFE_FOR_TEMPLATES: true }),
' '
);
assert.equal(
DOMPurify.sanitize('{<x>{333+333}<x>}', { SAFE_FOR_TEMPLATES: true }),
' '
);
}
);
QUnit.test(
'CVE-2026-41239: IN_PLACE strips boundary-spanning Mustache expressions',
(assert) => {
// Per-text-node scrubbing during the sanitizer walk misses {{...}}
// whose halves sit on either side of a stripped foreign element: at
// walk time each surrounding text node holds only a single '{' or
// '}', so MUSTACHE_EXPR (which requires '{{' or '}}') matches
// nothing. Once the foreign element is removed, the surrounding
// text nodes are adjacent; a normalize() call merges them into one
// node containing '{{...}}', which a template-evaluating framework
// would interpolate on mount. The final scrub pass runs normalize()
// and then walks the merged character data so the joined
// expression is caught.
const dirty = document.createElement('div');
dirty.innerHTML =
'{<foo></foo>{constructor.constructor("alert(1)")()}<foo></foo>}';
DOMPurify.sanitize(dirty, {
SAFE_FOR_TEMPLATES: true,
IN_PLACE: true,
});
assert.notOk(
/\{\{[\s\S]*\}\}/.test(dirty.innerHTML),
'merged Mustache expression should be scrubbed'
);
assert.notOk(
/constructor/.test(dirty.innerHTML),
'expression body should not survive scrubbing'
);
}
);
QUnit.test(
'CVE-2026-41239: IN_PLACE strips boundary-spanning template-literal expressions',
(assert) => {
// Same bug class as the Mustache test above, for ES template
// literals: '$' and '{' land in separate text nodes, so TMPLIT_EXPR
// (which requires the paired '${' sigil) does not match per node.
// After normalize() merges the surrounding text fragments the final
// scrub walks the joined node and strips '${...}'.
const dirty = document.createElement('div');
dirty.innerHTML = '$<foo></foo>{<foo></foo>danger}';
DOMPurify.sanitize(dirty, {
SAFE_FOR_TEMPLATES: true,
IN_PLACE: true,
});
assert.notOk(
/\$\{[\s\S]*\}/.test(dirty.innerHTML),
'merged template-literal expression should be scrubbed'
);
}
);
QUnit.test(
'CVE-2026-41239: RETURN_DOM_FRAGMENT path also scrubs joined expressions',
(assert) => {
// RETURN_DOM_FRAGMENT was fixed alongside RETURN_DOM and now shares
// the same scrub helper as IN_PLACE. Pinning here so a future
// refactor of the post-walk return path cannot silently regress it.
const result = DOMPurify.sanitize(
'<div>{<foo></foo>{constructor.constructor("alert(1)")()}<foo></foo>}</div>',
{
SAFE_FOR_TEMPLATES: true,