-
Notifications
You must be signed in to change notification settings - Fork 222
/
Copy pathAntiSamyTest.java
737 lines (574 loc) · 41 KB
/
AntiSamyTest.java
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
/*
* Copyright (c) 2007-2010, Arshan Dabirsiaghi, Jason Li
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
* Neither the name of OWASP nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.owasp.html;
import java.io.IOException;
import java.util.regex.Pattern;
import org.apache.commons.codec.binary.Base64;
import junit.framework.AssertionFailedError;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
/**
* This class tests AntiSamy functionality and the basic policy file which
* should be immune to XSS and CSS phishing attacks.
*
* @author Arshan Dabirsiaghi
*
*/
@SuppressWarnings("javadoc")
public class AntiSamyTest extends TestCase {
static final boolean RUN_KNOWN_FAILURES = false;
private static HtmlSanitizer.Policy makePolicy(Appendable buffer) {
final HtmlStreamRenderer renderer = HtmlStreamRenderer.create(
buffer,
new Handler<IOException>() {
public void handle(IOException ex) {
AssertionFailedError failure = new AssertionFailedError();
failure.initCause(ex);
throw failure;
}
},
new Handler<String>() {
public void handle(String errorMessage) {
fail(errorMessage);
}
});
return new HtmlPolicyBuilder()
.allowElements(
"a", "b", "br", "div", "font", "i", "img", "input", "li",
"ol", "p", "span", "td", "ul")
.allowAttributes("checked", "type").onElements("input")
.allowAttributes("color").onElements("font")
.allowAttributes("href").onElements("a")
.allowAttributes("src").onElements("img")
.allowAttributes("class", "id", "title").globally()
.allowAttributes("char").matching(
new AttributePolicy() {
public String apply(
String elementName, String attributeName, String value) {
return value.length() == 1 ? value : null;
}
}).onElements("td")
.allowStandardUrlProtocols()
.requireRelNofollowOnLinks()
.allowStyling()
.build(renderer);
}
static String sanitize(String html) {
StringBuilder sb = new StringBuilder();
HtmlSanitizer.sanitize(html, makePolicy(sb));
return sb.toString();
}
private static final String[] BASE64_BAD_XML_STRINGS = new String[] {
// first string is
// "<a - href=\"http://www.owasp.org\">click here</a>"
"PGEgLSBocmVmPSJodHRwOi8vd3d3Lm93YXNwLm9yZyI+Y2xpY2sgaGVyZTwvYT4=",
// the rest are randomly generated 300 byte sequences which generate
// parser errors, turned into Strings
"uz0sEy5aDiok6oufQRaYPyYOxbtlACRnfrOnUVIbOstiaoB95iw+dJYuO5sI9nudhRtSYLANlcdgO0pRb+65qKDwZ5o6GJRMWv4YajZk+7Q3W/GN295XmyWUpxuyPGVi7d5fhmtYaYNW6vxyKK1Wjn9IEhIrfvNNjtEF90vlERnz3wde4WMaKMeciqgDXuZHEApYmUcu6Wbx4Q6WcNDqohAN/qCli74tvC+Umy0ZsQGU7E+BvJJ1tLfMcSzYiz7Q15ByZOYrA2aa0wDu0no3gSatjGt6aB4h30D9xUP31LuPGZ2GdWwMfZbFcfRgDSh42JPwa1bODmt5cw0Y8ACeyrIbfk9IkX1bPpYfIgtO7TwuXjBbhh2EEixOZ2YkcsvmcOSVTvraChbxv6kP",
"PIWjMV4y+MpuNLtcY3vBRG4ZcNaCkB9wXJr3pghmFA6rVXAik+d5lei48TtnHvfvb5rQZVceWKv9cR/9IIsLokMyN0omkd8j3TV0DOh3JyBjPHFCu1Gp4Weo96h5C6RBoB0xsE4QdS2Y1sq/yiha9IebyHThAfnGU8AMC4AvZ7DDBccD2leZy2Q617ekz5grvxEG6tEcZ3fCbJn4leQVVo9MNoerim8KFHGloT+LxdgQR6YN5y1ii3bVGreM51S4TeANujdqJXp8B7B1Gk3PKCRS2T1SNFZedut45y+/w7wp5AUQCBUpIPUj6RLp+y3byWhcbZbJ70KOzTSZuYYIKLLo8047Fej43bIaghJm0F9yIKk3C5gtBcw8T5pciJoVXrTdBAK/8fMVo29P",
"uCk7HocubT6KzJw2eXpSUItZFGkr7U+D89mJw70rxdqXP2JaG04SNjx3dd84G4bz+UVPPhPO2gBAx2vHI0xhgJG9T4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Z/XcHxv58w6hNrXiWqFNjxlu5RZHvj1oQQXnS2n8qw8e/c+8ea2TiDIVr4OmgZz1G9uSPBeOZJvySqdgNPMpgfjZwkL2ez9/x31sLuQxi/FW3DFXU6kGSUjaq8g/iGXlaaAcQ0t9Gy+y005Z9wpr2JWWzishL+1JZp9D4SY/r3NHDphN4MNdLHMNBRPSIgfsaSqfLraIt+zWIycsd+nksVxtPv9wcyXy51E1qlHr6Uygz2VZYD9q9zyxEX4wRP2VEewHYUomL9d1F6gGG5fN3z82bQ4hI9uDirWhneWazUOQBRud5otPOm9",
"C3c+d5Q9lyTafPLdelG1TKaLFinw1TOjyI6KkrQyHKkttfnO58WFvScl1TiRcB/iHxKahskoE2+VRLUIhctuDU4sUvQh/g9Arw0LAA4QTxuLFt01XYdigurz4FT15ox2oDGGGrRb3VGjDTXK1OWVJoLMW95EVqyMc9F+Fdej85LHE+8WesIfacjUQtTG1tzYVQTfubZq0+qxXws8QrxMLFtVE38tbeXo+Ok1/U5TUa6FjWflEfvKY3XVcl8RKkXua7fVz/Blj8Gh+dWe2cOxa0lpM75ZHyz9adQrB2Pb4571E4u2xI5un0R0MFJZBQuPDc1G5rPhyk+Hb4LRG3dS0m8IASQUOskv93z978L1+Abu9CLP6d6s5p+BzWxhMUqwQXC/CCpTywrkJ0RG",
};
@Override
protected void setUp() throws Exception {
super.setUp();
}
@Override
protected void tearDown() throws Exception {
super.tearDown();
}
public static Test suite() {
TestSuite suite = new TestSuite(AntiSamyTest.class);
return suite;
}
/*
* Test basic XSS cases.
*/
public static void testScriptAttacks() {
assertSanitizedDoesNotContain("test<script>alert(document.cookie)</script>", "script");
assertSanitizedDoesNotContain("test<script>alert(document.cookie)</script>", "script");
assertSanitizedDoesNotContain("<<<><<script src=http://fake-evil.ru/test.js>", "<script");
assertSanitizedDoesNotContain("<<<><<script src=http://fake-evil.ru/test.js>", "<script");
assertSanitizedDoesNotContain("<script<script src=http://fake-evil.ru/test.js>>", "<script");
assertSanitizedDoesNotContain("<script<script src=http://fake-evil.ru/test.js>>", "<script");
assertSanitizedDoesNotContain("<SCRIPT/XSS SRC=\"http://ha.ckers.org/xss.js\"></SCRIPT>", "<script");
assertSanitizedDoesNotContain("<SCRIPT/XSS SRC=\"http://ha.ckers.org/xss.js\"></SCRIPT>", "<script");
assertSanitizedDoesNotContain("<BODY onload!#$%&()*~+-_.,:;?@[/|\\]^`=alert(\"XSS\")>", "onload");
assertSanitizedDoesNotContain("<BODY onload!#$%&()*~+-_.,:;?@[/|\\]^`=alert(\"XSS\")>", "onload");
assertSanitizedDoesNotContain("<BODY ONLOAD=alert('XSS')>", "alert");
assertSanitizedDoesNotContain("<BODY ONLOAD=alert('XSS')>", "alert");
assertSanitizedDoesNotContain("<iframe src=http://ha.ckers.org/scriptlet.html <", "<iframe");
assertSanitizedDoesNotContain("<iframe src=http://ha.ckers.org/scriptlet.html <", "<iframe");
assertSanitizedDoesNotContain("<INPUT TYPE=\"IMAGE\" SRC=\"javascript:alert('XSS');\">", "src");
assertSanitizedDoesNotContain("<INPUT TYPE=\"IMAGE\" SRC=\"javascript:alert('XSS');\">", "src");
assertSanitizedDoesNotContain("<a onblur=\"alert(secret)\" href=\"http://www.google.com\">Google</a>", "alert");
assertSanitizedDoesNotContain("<a onblur=\"alert(secret)\" href=\"http://www.google.com\">Google</a>", "alert");
}
public static void testImgAttacks() {
assertSanitizedDoesContain("<img src=\"http://www.myspace.com/img.gif\"/>", "<img");
assertSanitizedDoesContain("<img src=\"http://www.myspace.com/img.gif\"/>", "<img");
assertSanitizedDoesNotContain("<img src=javascript:alert(document.cookie)>", "<img");
assertSanitizedDoesNotContain("<IMG SRC=javascript:alert('XSS')>", "<img");
assertSanitizedDoesNotContain("<IMG SRC=javascript:alert('XSS')>", "<img");
assertSanitizedDoesNotContain("<IMG SRC='javascript:alert('XSS')'>", "src");
assertSanitizedDoesNotContain("<IMG SRC='javascript:alert('XSS')'>", "src");
assertSanitizedDoesNotContain("<IMG SRC=\"jav
ascript:alert('XSS');\">", "alert");
assertSanitizedDoesNotContain("<IMG SRC=\"jav
ascript:alert('XSS');\">", "alert");
String s = "<IMG SRC=javascript:alert('XSS')>";
if (sanitize(s).length() != 0) {
assertSanitizedDoesContain(s, "&");
}
s = "<IMG SRC=javascript:alert('XSS')>";
if (sanitize(s).length() != 0) {
assertSanitizedDoesContain(s, "&");
}
sanitize("<IMG SRC=javascript:alert('XSS')>");
sanitize("<IMG SRC=javascript:alert('XSS')>");
assertSanitizedDoesNotContain("<IMG SRC=\"javascript:alert('XSS')\"", "javascript");
assertSanitizedDoesNotContain("<IMG SRC=\"javascript:alert('XSS')\"", "javascript");
assertSanitizedDoesNotContain("<IMG LOWSRC=\"javascript:alert('XSS')\">", "javascript");
assertSanitizedDoesNotContain("<IMG LOWSRC=\"javascript:alert('XSS')\">", "javascript");
assertSanitizedDoesNotContain("<BGSOUND SRC=\"javascript:alert('XSS');\">", "javascript");
assertSanitizedDoesNotContain("<BGSOUND SRC=\"javascript:alert('XSS');\">", "javascript");
}
public static void testHrefAttacks() {
assertSanitizedDoesNotContain("<LINK REL=\"stylesheet\" HREF=\"javascript:alert('XSS');\">", "href");
assertSanitizedDoesNotContain("<LINK REL=\"stylesheet\" HREF=\"javascript:alert('XSS');\">", "href");
assertSanitizedDoesNotContain("<LINK REL=\"stylesheet\" HREF=\"http://ha.ckers.org/xss.css\">", "href");
assertSanitizedDoesNotContain("<LINK REL=\"stylesheet\" HREF=\"http://ha.ckers.org/xss.css\">", "href");
assertSanitizedDoesNotContain("<STYLE>@import'http://ha.ckers.org/xss.css';</STYLE>", "ha.ckers.org");
assertSanitizedDoesNotContain("<STYLE>@import'http://ha.ckers.org/xss.css';</STYLE>", "ha.ckers.org");
assertSanitizedDoesNotContain("<STYLE>BODY{-moz-binding:url(\"http://ha.ckers.org/xssmoz.xml#xss\")}</STYLE>", "ha.ckers.org");
assertSanitizedDoesNotContain("<STYLE>BODY{-moz-binding:url(\"http://ha.ckers.org/xssmoz.xml#xss\")}</STYLE>", "ha.ckers.org");
assertSanitizedDoesNotContain("<STYLE>li {list-style-image: url(\"javascript:alert('XSS')\");}</STYLE><UL><LI>XSS", "javascript");
assertSanitizedDoesNotContain("<STYLE>li {list-style-image: url(\"javascript:alert('XSS')\");}</STYLE><UL><LI>XSS", "javascript");
assertSanitizedDoesNotContain("<IMG SRC='vbscript:msgbox(\"XSS\")'>", "vbscript");
assertSanitizedDoesNotContain("<IMG SRC='vbscript:msgbox(\"XSS\")'>", "vbscript");
assertSanitizedDoesNotContain("<META HTTP-EQUIV=\"refresh\" CONTENT=\"0; URL=http://;URL=javascript:alert('XSS');\">", "<meta");
assertSanitizedDoesNotContain("<META HTTP-EQUIV=\"refresh\" CONTENT=\"0; URL=http://;URL=javascript:alert('XSS');\">", "<meta");
assertSanitizedDoesNotContain("<META HTTP-EQUIV=\"refresh\" CONTENT=\"0;url=javascript:alert('XSS');\">", "<meta");
assertSanitizedDoesNotContain("<META HTTP-EQUIV=\"refresh\" CONTENT=\"0;url=javascript:alert('XSS');\">", "<meta");
assertSanitizedDoesNotContain("<META HTTP-EQUIV=\"refresh\" CONTENT=\"0;url=data:text/html;base64,PHNjcmlwdD5hbGVydCgnWFNTJyk8L3NjcmlwdD4K\">", "<meta");
assertSanitizedDoesNotContain("<META HTTP-EQUIV=\"refresh\" CONTENT=\"0;url=data:text/html;base64,PHNjcmlwdD5hbGVydCgnWFNTJyk8L3NjcmlwdD4K\">", "<meta");
assertSanitizedDoesNotContain("<IFRAME SRC=\"javascript:alert('XSS');\"></IFRAME>", "iframe");
assertSanitizedDoesNotContain("<IFRAME SRC=\"javascript:alert('XSS');\"></IFRAME>", "iframe");
assertSanitizedDoesNotContain("<FRAMESET><FRAME SRC=\"javascript:alert('XSS');\"></FRAMESET>", "javascript");
assertSanitizedDoesNotContain("<FRAMESET><FRAME SRC=\"javascript:alert('XSS');\"></FRAMESET>", "javascript");
assertSanitizedDoesNotContain("<TABLE BACKGROUND=\"javascript:alert('XSS')\">", "background");
assertSanitizedDoesNotContain("<TABLE BACKGROUND=\"javascript:alert('XSS')\">", "background");
assertSanitizedDoesNotContain("<TABLE><TD BACKGROUND=\"javascript:alert('XSS')\">", "background");
assertSanitizedDoesNotContain("<TABLE><TD BACKGROUND=\"javascript:alert('XSS')\">", "background");
assertSanitizedDoesNotContain("<DIV STYLE=\"background-image: url(javascript:alert('XSS'))\">", "javascript");
assertSanitizedDoesNotContain("<DIV STYLE=\"background-image: url(javascript:alert('XSS'))\">", "javascript");
assertSanitizedDoesNotContain("<DIV STYLE=\"width: expression(alert('XSS'));\">", "alert");
assertSanitizedDoesNotContain("<DIV STYLE=\"width: expression(alert('XSS'));\">", "alert");
assertSanitizedDoesNotContain("<IMG STYLE=\"xss:expr/*XSS*/ession(alert('XSS'))\">", "alert");
assertSanitizedDoesNotContain("<IMG STYLE=\"xss:expr/*XSS*/ession(alert('XSS'))\">", "alert");
assertSanitizedDoesNotContain("<STYLE>@im\\port'\\ja\\vasc\\ript:alert(\"XSS\")';</STYLE>", "ript:alert");
assertSanitizedDoesNotContain("<STYLE>@im\\port'\\ja\\vasc\\ript:alert(\"XSS\")';</STYLE>", "ript:alert");
assertSanitizedDoesNotContain("<BASE HREF=\"javascript:alert('XSS');//\">", "javascript");
assertSanitizedDoesNotContain("<BASE HREF=\"javascript:alert('XSS');//\">", "javascript");
assertSanitizedDoesNotContain("<BaSe hReF=\"http://arbitrary.com/\">", "<base");
assertSanitizedDoesNotContain("<BaSe hReF=\"http://arbitrary.com/\">", "<base");
assertSanitizedDoesNotContain("<OBJECT TYPE=\"text/x-scriptlet\" DATA=\"http://ha.ckers.org/scriptlet.html\"></OBJECT>", "<object");
assertSanitizedDoesNotContain("<OBJECT TYPE=\"text/x-scriptlet\" DATA=\"http://ha.ckers.org/scriptlet.html\"></OBJECT>", "<object");
assertSanitizedDoesNotContain("<OBJECT classid=clsid:ae24fdae-03c6-11d1-8b76-0080c744f389><param name=url value=javascript:alert('XSS')></OBJECT>", "javascript");
assertSanitizedDoesNotContain("<OBJECT classid=clsid:ae24fdae-03c6-11d1-8b76-0080c744f389><param name=url value=javascript:alert('XSS')></OBJECT>", "javascript");
assertSanitizedDoesNotContain("<EMBED SRC=\"http://ha.ckers.org/xss.swf\" AllowScriptAccess=\"always\"></EMBED>", "<embed");
assertSanitizedDoesNotContain("<EMBED SRC=\"http://ha.ckers.org/xss.swf\" AllowScriptAccess=\"always\"></EMBED>", "<embed");
assertSanitizedDoesNotContain("<EMBED SRC=\"data:image/svg+xml;base64,PHN2ZyB4bWxuczpzdmc9Imh0dH A6Ly93d3cudzMub3JnLzIwMDAvc3ZnIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcv MjAwMC9zdmciIHhtbG5zOnhsaW5rPSJodHRwOi8vd3d3LnczLm9yZy8xOTk5L3hs aW5rIiB2ZXJzaW9uPSIxLjAiIHg9IjAiIHk9IjAiIHdpZHRoPSIxOTQiIGhlaWdodD0iMjAw IiBpZD0ieHNzIj48c2NyaXB0IHR5cGU9InRleHQvZWNtYXNjcmlwdCI+YWxlcnQoIlh TUyIpOzwvc2NyaXB0Pjwvc3ZnPg==\" type=\"image/svg+xml\" AllowScriptAccess=\"always\"></EMBED>", "<embed");
assertSanitizedDoesNotContain("<EMBED SRC=\"data:image/svg+xml;base64,PHN2ZyB4bWxuczpzdmc9Imh0dH A6Ly93d3cudzMub3JnLzIwMDAvc3ZnIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcv MjAwMC9zdmciIHhtbG5zOnhsaW5rPSJodHRwOi8vd3d3LnczLm9yZy8xOTk5L3hs aW5rIiB2ZXJzaW9uPSIxLjAiIHg9IjAiIHk9IjAiIHdpZHRoPSIxOTQiIGhlaWdodD0iMjAw IiBpZD0ieHNzIj48c2NyaXB0IHR5cGU9InRleHQvZWNtYXNjcmlwdCI+YWxlcnQoIlh TUyIpOzwvc2NyaXB0Pjwvc3ZnPg==\" type=\"image/svg+xml\" AllowScriptAccess=\"always\"></EMBED>", "<embed");
assertSanitizedDoesNotContain("<SCRIPT a=\">\" SRC=\"http://ha.ckers.org/xss.js\"></SCRIPT>", "<script");
assertSanitizedDoesNotContain("<SCRIPT a=\">\" SRC=\"http://ha.ckers.org/xss.js\"></SCRIPT>", "<script");
assertSanitizedDoesNotContain("<SCRIPT a=\">\" '' SRC=\"http://ha.ckers.org/xss.js\"></SCRIPT>", "<script");
assertSanitizedDoesNotContain("<SCRIPT a=\">\" '' SRC=\"http://ha.ckers.org/xss.js\"></SCRIPT>", "<script");
assertSanitizedDoesNotContain("<SCRIPT a=`>` SRC=\"http://ha.ckers.org/xss.js\"></SCRIPT>", "<script");
assertSanitizedDoesNotContain("<SCRIPT a=`>` SRC=\"http://ha.ckers.org/xss.js\"></SCRIPT>", "<script");
assertSanitizedDoesNotContain("<SCRIPT a=\">'>\" SRC=\"http://ha.ckers.org/xss.js\"></SCRIPT>", "<script");
assertSanitizedDoesNotContain("<SCRIPT a=\">'>\" SRC=\"http://ha.ckers.org/xss.js\"></SCRIPT>", "<script");
assertSanitizedDoesNotContain("<SCRIPT>document.write(\"<SCRI\");</SCRIPT>PT SRC=\"http://ha.ckers.org/xss.js\"></SCRIPT>", "script");
assertSanitizedDoesNotContain("<SCRIPT>document.write(\"<SCRI\");</SCRIPT>PT SRC=\"http://ha.ckers.org/xss.js\"></SCRIPT>", "script");
assertSanitizedDoesNotContain("<SCRIPT SRC=http://ha.ckers.org/xss.js", "<script");
assertSanitizedDoesNotContain("<SCRIPT SRC=http://ha.ckers.org/xss.js", "<script");
assertSanitizedDoesNotContain("<div/style=\-\mo\z\-b\i\nd\in\g:\url(//business\i\nfo.co.uk\/labs\/xbl\/xbl\.xml\#xss)&>", "style");
assertSanitizedDoesNotContain("<div/style=\-\mo\z\-b\i\nd\in\g:\url(//business\i\nfo.co.uk\/labs\/xbl\/xbl\.xml\#xss)&>", "style");
assertSanitizedDoesNotContain("<a href='aim: &c:\\windows\\system32\\calc.exe' ini='C:\\Documents and Settings\\All Users\\Start Menu\\Programs\\Startup\\pwnd.bat'>", "aim.exe");
assertSanitizedDoesNotContain("<a href='aim: &c:\\windows\\system32\\calc.exe' ini='C:\\Documents and Settings\\All Users\\Start Menu\\Programs\\Startup\\pwnd.bat'>", "aim.exe");
assertSanitizedDoesNotContain("<!--\n<A href=\n- --><a href=javascript:alert:document.domain>test-->", "javascript");
assertSanitizedDoesNotContain("<!--\n<A href=\n- --><a href=javascript:alert:document.domain>test-->", "javascript");
assertSanitizedDoesNotContain("<a></a style=\"\"xx:expr/**/ession(document.appendChild(document.createElement('script')).src='http://h4k.in/i.js')\">", "document");
assertSanitizedDoesNotContain("<a></a style=\"\"xx:expr/**/ession(document.appendChild(document.createElement('script')).src='http://h4k.in/i.js')\">", "document");
}
/*
* Test CSS protections.
*/
public static void testCssAttacks() {
assertSanitizedDoesNotContain("<div style=\"position:absolute\">", "position");
assertSanitizedDoesNotContain("<div style=\"position:absolute\">", "position");
assertSanitizedDoesNotContain("<style>b { position:absolute }</style>", "position");
assertSanitizedDoesNotContain("<style>b { position:absolute }</style>", "position");
assertSanitizedDoesNotContain("<div style=\"z-index:25\">test</div>", "z-index");
assertSanitizedDoesNotContain("<div style=\"z-index:25\">test</div>", "z-index");
assertSanitizedDoesNotContain("<style>z-index:25</style>", "z-index");
assertSanitizedDoesNotContain("<style>z-index:25</style>", "z-index");
}
/*
* Test a bunch of strings that have tweaked the XML parsing capabilities of
* NekoHTML.
*/
public static void testIllegalXML() throws Exception {
for (int i = 0; i < BASE64_BAD_XML_STRINGS.length; i++) {
String testStr = new String(
Base64.decodeBase64(BASE64_BAD_XML_STRINGS[i]),
"UTF-8");
sanitize(testStr);
sanitize(testStr);
}
// These fail in AntiSamy due to a bug in NekoHTML
assertEquals(
"<a href=\"http://www.test.com\" rel=\"nofollow\"></a>",
sanitize("<a . href=\"http://www.test.com\">"));
assertEquals(
"<a href=\"http://www.test.com\" rel=\"nofollow\"></a>",
sanitize("<a - href=\"http://www.test.com\">"));
assertTrue(sanitize("<style>") != null);
}
public static void testPreviousBugs() {
/*
* issues 12 (and 36, which was similar). empty tags cause display
* problems/"formjacking"
*/
{
Pattern p = Pattern.compile(".*<strong(\\s*)/>.*", Pattern.DOTALL);
String s1 = sanitize("<br ><strong></strong><a>hello world</a><b /><i/><hr>");
String s2 = sanitize("<br ><strong></strong><a>hello world</a><b /><i/><hr>");
assertFalse(p.matcher(s1).matches());
p = Pattern.compile(".*<b(\\s*)/>.*");
assertFalse(p.matcher(s1).matches());
assertFalse(p.matcher(s2).matches());
p = Pattern.compile(".*<i(\\s*)/>.*");
assertFalse(p.matcher(s1).matches());
assertFalse(p.matcher(s2).matches());
p = Pattern.compile(".*<hr(\\s*)/>.*");
assertFalse(p.matcher(s1).matches());
assertFalse(p.matcher(s2).matches());
}
/* issue #20 */
assertSanitizedDoesNotContain("<b><i>Some Text</b></i>", "<i />");
assertSanitizedDoesNotContain("<b><i>Some Text</b></i>", "<i />");
/* issue #25 */
assertEquals(
"<div>Test</div>", sanitize("<div style=\"margin: xxx\">Test</div>"));
/* issue #28 */
assertSanitizedDoesContain(
"<div style=\"font-family: Geneva, Arial, courier new, sans-serif\">Test</div>",
"font-family:'geneva' , 'arial' , 'courier new' , sans-serif");
/* issue #29 - missing quotes around properties with spaces */
if (RUN_KNOWN_FAILURES) {
String s = "<style type=\"text/css\"><![CDATA[P {\n font-family: \"Arial Unicode MS\";\n}\n]]></style>";
assertEquals(s, sanitize(s));
}
/* issue #30 */
if (RUN_KNOWN_FAILURES) {
String s = "<style type=\"text/css\"><![CDATA[P { margin-bottom: 0.08in; } ]]></style>";
s = sanitize(s);
// followup - does the patch fix multiline CSS?
String s2 = "<style type=\"text/css\"><![CDATA[\r\nP {\r\n margin-bottom: 0.08in;\r\n}\r\n]]></style>";
assertEquals("<style type=\"text/css\"><![CDATA[P {\n\tmargin-bottom: 0.08in;\n}\n]]></style>", sanitize(s2));
// next followup - does non-CDATA parsing still work?
String s3 = "<style>P {\n\tmargin-bottom: 0.08in;\n}\n";
assertEquals("<style>P {\n\tmargin-bottom: 0.08in;\n}\n</style>\n", sanitize(s3));
// for other
// tests
}
/* issue #32 - nekos problem */
{
String s = "<SCRIPT =\">\" SRC=\"\"></SCRIPT>";
sanitize(s);
sanitize(s);
}
/* issue #37 - OOM */
{
String dirty = "<a onblur=\"try {parent.deselectBloggerImageGracefully();}" + "catch(e) {}\""
+ "href=\"http://www.charityadvantage.com/ChildrensmuseumEaston/images/BookswithBill.jpg\"><img" + "style=\"FLOAT: right; MARGIN: 0px 0px 10px 10px; WIDTH: 150px; CURSOR:"
+ "hand; HEIGHT: 100px\" alt=\"\"" + "src=\"http://www.charityadvantage.com/ChildrensmuseumEaston/images/BookswithBill.jpg\""
+ "border=\"0\" /></a><br />Poor Bill, couldn't make it to the Museum's <span" + "class=\"blsp-spelling-corrected\" id=\"SPELLING_ERROR_0\">story time</span>"
+ "today, he was so busy shoveling! Well, we sure missed you Bill! So since" + "ou were busy moving snow we read books about snow. We found a clue in one"
+ "book which revealed a snowplow at the end of the story - we wish it had" + "driven to your driveway Bill. We also read a story which shared fourteen"
+ "<em>Names For Snow. </em>We'll catch up with you next week....wonder which" + "hat Bill will wear?<br />Jane";
String s = sanitize(dirty);
assertNotNull(s);
}
/* issue #38 - color problem/color combinations */
{
String s = "<font color=\"#fff\">Test</font>";
String expected = "<font color=\"#fff\">Test</font>";
assertEquals(expected, sanitize(s));
assertEquals(expected, sanitize(s));
s = "<div style=\"color: #fff\">Test 3 letter code</div>";
expected = "<div style=\"color:#fff\">Test 3 letter code</div>";
assertEquals(expected, sanitize(s));
assertEquals(expected, sanitize(s));
s = "<font color=\"red\">Test</font>";
expected = "<font color=\"red\">Test</font>";
assertEquals(expected, sanitize(s));
assertEquals(expected, sanitize(s));
s = "<font color=\"neonpink\">Test</font>";
expected = s;
assertEquals(expected, sanitize(s));
assertEquals(expected, sanitize(s));
if (RUN_KNOWN_FAILURES) {
s = "<font color=\"#0000\">Test</font>";
expected = "<font>Test</font>";
assertEquals(expected, sanitize(s));
assertEquals(expected, sanitize(s));
}
if (RUN_KNOWN_FAILURES) {
s = "<div style=\"color: #0000\">Test</div>";
expected = "<div>Test</div>";
assertEquals(expected, sanitize(s));
assertEquals(expected, sanitize(s));
}
s = "<font color=\"#000000\">Test</font>";
expected = "<font color=\"#000000\">Test</font>";
assertEquals(expected, sanitize(s));
assertEquals(expected, sanitize(s));
s = "<div style=\"color: #000000\">Test</div>";
expected = "<div style=\"color:#000000\">Test</div>";
assertEquals(expected, sanitize(s));
assertEquals(expected, sanitize(s));
s = "<b><u>foo<style><script>alert(1)</script></style>@import 'x';</u>bar";
sanitize(s);
}
/* issue #40 - handling <style> media attributes right */
if (RUN_KNOWN_FAILURES) {
assertSanitizedDoesContain("<style media=\"print, projection, screen\"> P { margin: 1em; }</style>", "print, projection, screen");
}
/* issue #41 - comment handling */
{
assertEquals("text ", sanitize("text <!-- comment -->"));
assertEquals("text ", sanitize("text <!-- comment -->"));
assertEquals("<div>text </div>", sanitize("<div>text <!-- comment --></div>"));
assertEquals("<div>text </div>", sanitize("<div>text <!-- comment --></div>"));
assertEquals("<div>text </div>", sanitize("<div>text <!--[if IE]> comment <[endif]--></div>"));
assertEquals("<div>text </div>", sanitize("<div>text <!--[if IE]> comment <[endif]--></div>"));
/*
* Check to see how nested conditional comments are handled. This is
* not very clean but the main goal is to avoid any tags. Not sure
* on encodings allowed in comments.
*/
String input = "<div>text <!--[if IE]> <!--[if gte 6]> comment <[endif]--><[endif]--></div>";
String expected = "<div>text <[endif]--></div>";
String output = sanitize(input);
assertEquals(expected, output);
input = "<div>text <!--[if IE]> <!--[if gte 6]> comment <[endif]--><[endif]--></div>";
expected = "<div>text <[endif]--></div>";
output = sanitize(input);
assertEquals(expected, output);
/*
* Regular comment nested inside conditional comment. Test makes
* sure
*/
assertEquals("<div>text comment <[endif]--></div>", sanitize("<div>text <!--[if IE]> <!-- IE specific --> comment <[endif]--></div>"));
/*
* These play with whitespace and have invalid comment syntax.
*/
assertEquals("<div>text </div>", sanitize("<div>text <!-- [ if lte 6 ]>\ncomment <[ endif\n]--></div>"));
assertEquals("<div>text comment </div>", sanitize("<div>text <![if !IE]> comment <![endif]></div>"));
assertEquals("<div>text comment </div>", sanitize("<div>text <![ if !IE]> comment <![endif]></div>"));
String attack = "[if lte 8]<script>";
String spacer = "<![if IE]>";
StringBuffer sb = new StringBuffer();
sb.append("<div>text<!");
for (int i = 0; i < attack.length(); i++) {
sb.append(attack.charAt(i));
sb.append(spacer);
}
sb.append("<![endif]>");
String s = sb.toString();
assertSanitizedDoesNotContain(s, "<script");
assertSanitizedDoesNotContain(s, "<script");
}
/*
* issue #44 - childless nodes of non-allowed elements won't cause an error
*/
{
String s = "<iframe src='http://foo.com/'></iframe>" + "<script src=''></script>" + "<link href='/foo.css'>";
assertEquals(s, "", sanitize(s));
}
/* issue #51 - offsite urls with () are found to be invalid */
assertSanitizedDoesNotContain(
"<a href='http://subdomain.domain/(S(ke0lpq54bw0fvp53a10e1a45))/MyPage.aspx'>test</a>", "(");
/* issue #56 - unnecessary spaces */
{
String s = "<SPAN style='font-weight: bold;'>Hello World!</SPAN>";
assertEquals(
"<span style=\"font-weight:bold\">Hello World!</span>",
sanitize(s));
}
/* issue #58 - input not in list of allowed-to-be-empty tags */
{
String s = "tgdan <input/> g h";
assertEquals("tgdan g h", sanitize(s));
}
/* issue #61 - input has newline appended if ends with an accepted tag */
{
String dirtyInput = "blah <b>blah</b>.";
String s = sanitize(dirtyInput);
assertEquals(dirtyInput, s);
}
/* issue #69 - char attribute should allow single char or entity ref */
{
String s = "<td char='.'>test</td>";
assertSanitizedDoesContain(s, "char");
assertSanitizedDoesContain(s, "char");
s = "<td char='..'>test</td>";
assertSanitizedDoesNotContain(s, "char");
assertSanitizedDoesNotContain(s, "char");
s = "<td char='"'>test</td>";
assertSanitizedDoesContain(s, "char");
assertSanitizedDoesContain(s, "char");
s = "<td char='"a'>test</td>";
assertSanitizedDoesNotContain(s, "char");
assertSanitizedDoesNotContain(s, "char");
s = "<td char='"&'>test</td>";
assertSanitizedDoesNotContain(s, "char");
assertSanitizedDoesNotContain(s, "char");
}
/* privately disclosed issue - cdata bypass */
{
String malInput = "<![CDATA[]><script>alert(1)</script>]]>";
assertSanitizedDoesNotContain(malInput, "<script");
}
/* this test is for confirming literal-lists work as
* advertised. it turned out to be an invalid / non-
* reproducible bug report but the test seemed useful
* enough to keep.
*/
{
String malInput = "hello<p align='invalid'>world</p>";
assertSanitizedDoesNotContain(malInput, "invalid");
String goodInput = "hello<p align='left'>world</p>";
if (RUN_KNOWN_FAILURES) {
assertSanitizedDoesContain(goodInput, "left");
}
}
}
/*
* Tests cases dealing with nofollowAnchors directive. Assumes anchor tags
* have an action set to "validate" (may be implicit) in the policy file.
*/
public static void testNofollowAnchors() {
// adds when not present
assertSanitized("<a href=\"blah\">link</a>", "<a href=\"blah\" rel=\"nofollow\">link</a>");
// adds properly even with bad attr
assertSanitized("<a href=\"blah\" bad=\"true\">link</a>", "<a href=\"blah\" rel=\"nofollow\">link</a>");
// rel with bad value gets corrected
assertSanitized("<a href=\"blah\" rel=\"blh\">link</a>", "<a href=\"blah\" rel=\"nofollow\">link</a>");
// correct attribute doesnt get messed with
assertSanitized("<a href=\"blah\" rel=\"nofollow\">link</a>", "<a href=\"blah\" rel=\"nofollow\">link</a>");
// if two correct attributes, only one remaining after scan
assertSanitized("<a href=\"blah\" rel=\"nofollow\" rel=\"nofollow\">link</a>", "<a href=\"blah\" rel=\"nofollow\">link</a>");
// test if value is off - does it add?
assertSanitizedDoesNotContain("a href=\"blah\">link</a>", "nofollow");
}
public static void testValidateParamAsEmbed() {
// let's start with a YouTube embed
String input = "<object width=\"560\" height=\"340\"><param name=\"movie\" value=\"http://www.youtube.com/v/IyAyd4WnvhU&hl=en&fs=1&\"></param><param name=\"allowFullScreen\" value=\"true\"></param><param name=\"allowscriptaccess\" value=\"always\"></param><embed src=\"http://www.youtube.com/v/IyAyd4WnvhU&hl=en&fs=1&\" type=\"application/x-shockwave-flash\" allowscriptaccess=\"always\" allowfullscreen=\"true\" width=\"560\" height=\"340\"></embed></object>";
String expectedOutput = "<object height=\"340\" width=\"560\"><param name=\"movie\" value=\"http://www.youtube.com/v/IyAyd4WnvhU&hl=en&fs=1&\" /><param name=\"allowFullScreen\" value=\"true\" /><param name=\"allowscriptaccess\" value=\"always\" /><embed allowfullscreen=\"true\" allowscriptaccess=\"always\" height=\"340\" src=\"http://www.youtube.com/v/IyAyd4WnvhU&hl=en&fs=1&\" type=\"application/x-shockwave-flash\" width=\"560\" /></object>";
if (RUN_KNOWN_FAILURES) {
assertSanitizedDoesContain(input, expectedOutput);
} else {
assertSanitized(input, "");
}
String saxExpectedOutput = "<object width=\"560\" height=\"340\"><param name=\"movie\" value=\"http://www.youtube.com/v/IyAyd4WnvhU&hl=en&fs=1&\"><param name=\"allowFullScreen\" value=\"true\"><param name=\"allowscriptaccess\" value=\"always\"><embed src=\"http://www.youtube.com/v/IyAyd4WnvhU&hl=en&fs=1&\" type=\"application/x-shockwave-flash\" allowscriptaccess=\"always\" allowfullscreen=\"true\" width=\"560\" height=\"340\"></embed></object>";
if (RUN_KNOWN_FAILURES) {
assertSanitizedDoesContain(input, saxExpectedOutput);
} else {
assertSanitized(input, "");
}
// now what if someone sticks malicious URL in the value of the
// value attribute in the param tag? remove that param tag
input = "<object width=\"560\" height=\"340\"><param name=\"movie\" value=\"http://supermaliciouscode.com/badstuff.swf\"></param><param name=\"allowFullScreen\" value=\"true\"></param><param name=\"allowscriptaccess\" value=\"always\"></param><embed src=\"http://www.youtube.com/v/IyAyd4WnvhU&hl=en&fs=1&\" type=\"application/x-shockwave-flash\" allowscriptaccess=\"always\" allowfullscreen=\"true\" width=\"560\" height=\"340\"></embed></object>";
expectedOutput = "<object height=\"340\" width=\"560\"><param name=\"allowFullScreen\" value=\"true\" /><param name=\"allowscriptaccess\" value=\"always\" /><embed allowfullscreen=\"true\" allowscriptaccess=\"always\" height=\"340\" src=\"http://www.youtube.com/v/IyAyd4WnvhU&hl=en&fs=1&\" type=\"application/x-shockwave-flash\" width=\"560\" /></object>";
saxExpectedOutput = "<object width=\"560\" height=\"340\"><param name=\"allowFullScreen\" value=\"true\"><param name=\"allowscriptaccess\" value=\"always\"><embed src=\"http://www.youtube.com/v/IyAyd4WnvhU&hl=en&fs=1&\" type=\"application/x-shockwave-flash\" allowscriptaccess=\"always\" allowfullscreen=\"true\" width=\"560\" height=\"340\"></embed></object>";
if (RUN_KNOWN_FAILURES) {
assertSanitizedDoesContain(input, expectedOutput);
} else {
assertSanitized(input, "");
}
if (RUN_KNOWN_FAILURES) {
assertTrue(sanitize(input).equals(saxExpectedOutput));
} else {
assertSanitized(input, "");
}
// now what if someone sticks malicious URL in the value of the src
// attribute in the embed tag? remove that embed tag
input = "<object width=\"560\" height=\"340\"><param name=\"movie\" value=\"http://www.youtube.com/v/IyAyd4WnvhU&hl=en&fs=1&\"></param><param name=\"allowFullScreen\" value=\"true\"></param><param name=\"allowscriptaccess\" value=\"always\"></param><embed src=\"http://hereswhereikeepbadcode.com/ohnoscary.swf\" type=\"application/x-shockwave-flash\" allowscriptaccess=\"always\" allowfullscreen=\"true\" width=\"560\" height=\"340\"></embed></object>";
expectedOutput = "<object height=\"340\" width=\"560\"><param name=\"movie\" value=\"http://www.youtube.com/v/IyAyd4WnvhU&hl=en&fs=1&\" /><param name=\"allowFullScreen\" value=\"true\" /><param name=\"allowscriptaccess\" value=\"always\" /></object>";
saxExpectedOutput = "<object width=\"560\" height=\"340\"><param name=\"movie\" value=\"http://www.youtube.com/v/IyAyd4WnvhU&hl=en&fs=1&\"><param name=\"allowFullScreen\" value=\"true\"><param name=\"allowscriptaccess\" value=\"always\"></object>";
if (RUN_KNOWN_FAILURES) {
assertSanitizedDoesContain(input, expectedOutput);
} else {
assertSanitized(input, "");
}
if (RUN_KNOWN_FAILURES) {
assertSanitizedDoesContain(input, saxExpectedOutput);
} else {
assertSanitized(input, "");
}
}
private static void assertSanitizedDoesNotContain(
String html, String dangerousContent) {
String sanitized = sanitize(html);
int index = Strings.toLowerCase(sanitized).indexOf(
Strings.toLowerCase(dangerousContent));
assertEquals(
"`" + sanitized + "` from `" + html + "` contains `" +
dangerousContent + "`",
-1, index);
}
private static void assertSanitizedDoesContain(
String html, String dangerousContent) {
String sanitized = sanitize(html);
int index = Strings.toLowerCase(sanitized).indexOf(
Strings.toLowerCase(dangerousContent));
assertTrue(
"`" + sanitized + "` from `" + html + "` does not contain `" +
dangerousContent + "`",
index >= 0);
}
private static void assertSanitized(String html, String sanitized) {
assertEquals(sanitized, sanitize(html));
}
}