2424
2525package org .owasp .html ;
2626
27- import java .io . IOException ;
27+ import java .nio . charset . StandardCharsets ;
2828import java .util .regex .Pattern ;
2929
3030import org .apache .commons .codec .binary .Base64 ;
3131
32- import junit .framework .AssertionFailedError ;
33- import junit .framework .Test ;
34- import junit .framework .TestCase ;
35- import junit .framework .TestSuite ;
32+ import org .junit .jupiter .api .Assertions ;
33+ import org .junit .jupiter .api .Test ;
34+ import org .opentest4j .AssertionFailedError ;
35+
36+ import static org .junit .jupiter .api .Assertions .assertEquals ;
37+ import static org .junit .jupiter .api .Assertions .assertFalse ;
38+ import static org .junit .jupiter .api .Assertions .assertNotNull ;
39+ import static org .junit .jupiter .api .Assertions .assertTrue ;
3640
3741
3842/**
4246 * @author Arshan Dabirsiaghi
4347 *
4448 */
45- @ SuppressWarnings ("javadoc" )
46- public class AntiSamyTest extends TestCase {
49+ class AntiSamyTest {
4750
48- static final boolean RUN_KNOWN_FAILURES = false ;
51+ private static final boolean RUN_KNOWN_FAILURES = false ;
4952
5053 private static HtmlSanitizer .Policy makePolicy (Appendable buffer ) {
5154 final HtmlStreamRenderer renderer = HtmlStreamRenderer .create (
5255 buffer ,
53- new Handler <IOException >() {
54- public void handle (IOException ex ) {
55- AssertionFailedError failure = new AssertionFailedError ();
56- failure .initCause (ex );
57- throw failure ;
58- }
59- },
60- new Handler <String >() {
61- public void handle (String errorMessage ) {
62- fail (errorMessage );
63- }
64- });
56+ ex -> {
57+ AssertionFailedError failure = new AssertionFailedError ();
58+ failure .initCause (ex );
59+ throw failure ;
60+ },
61+ Assertions ::fail );
6562
6663 return new HtmlPolicyBuilder ()
6764 .allowElements (
@@ -73,12 +70,7 @@ public void handle(String errorMessage) {
7370 .allowAttributes ("src" ).onElements ("img" )
7471 .allowAttributes ("class" , "id" , "title" ).globally ()
7572 .allowAttributes ("char" ).matching (
76- new AttributePolicy () {
77- public String apply (
78- String elementName , String attributeName , String value ) {
79- return value .length () == 1 ? value : null ;
80- }
81- }).onElements ("td" )
73+ (elementName , attributeName , value ) -> value .length () == 1 ? value : null ).onElements ("td" )
8274 .allowStandardUrlProtocols ()
8375 .requireRelNofollowOnLinks ()
8476 .allowStyling ()
@@ -113,26 +105,12 @@ static String sanitize(String html) {
113105 "C3c+d5Q9lyTafPLdelG1TKaLFinw1TOjyI6KkrQyHKkttfnO58WFvScl1TiRcB/iHxKahskoE2+VRLUIhctuDU4sUvQh/g9Arw0LAA4QTxuLFt01XYdigurz4FT15ox2oDGGGrRb3VGjDTXK1OWVJoLMW95EVqyMc9F+Fdej85LHE+8WesIfacjUQtTG1tzYVQTfubZq0+qxXws8QrxMLFtVE38tbeXo+Ok1/U5TUa6FjWflEfvKY3XVcl8RKkXua7fVz/Blj8Gh+dWe2cOxa0lpM75ZHyz9adQrB2Pb4571E4u2xI5un0R0MFJZBQuPDc1G5rPhyk+Hb4LRG3dS0m8IASQUOskv93z978L1+Abu9CLP6d6s5p+BzWxhMUqwQXC/CCpTywrkJ0RG" ,
114106 };
115107
116- @ Override
117- protected void setUp () throws Exception {
118- super .setUp ();
119- }
120-
121- @ Override
122- protected void tearDown () throws Exception {
123- super .tearDown ();
124- }
125-
126- public static Test suite () {
127- TestSuite suite = new TestSuite (AntiSamyTest .class );
128- return suite ;
129- }
130-
131108 /*
132109 * Test basic XSS cases.
133110 */
134111
135- public static void testScriptAttacks () {
112+ @ Test
113+ void testScriptAttacks () {
136114 assertSanitizedDoesNotContain ("test<script>alert(document.cookie)</script>" , "script" );
137115 assertSanitizedDoesNotContain ("test<script>alert(document.cookie)</script>" , "script" );
138116
@@ -161,7 +139,8 @@ public static void testScriptAttacks() {
161139 assertSanitizedDoesNotContain ("<a onblur=\" alert(secret)\" href=\" http://www.google.com\" >Google</a>" , "alert" );
162140 }
163141
164- public static void testImgAttacks () {
142+ @ Test
143+ void testImgAttacks () {
165144 assertSanitizedDoesContain ("<img src=\" http://www.myspace.com/img.gif\" />" , "<img" );
166145 assertSanitizedDoesContain ("<img src=\" http://www.myspace.com/img.gif\" />" , "<img" );
167146
@@ -177,11 +156,11 @@ public static void testImgAttacks() {
177156 assertSanitizedDoesNotContain ("<IMG SRC=\" jav
ascript:alert('XSS');\" >" , "alert" );
178157
179158 String s = "<IMG SRC=javascript:alert('XSS')>" ;
180- if (sanitize (s ).length () != 0 ) {
159+ if (! sanitize (s ).isEmpty () ) {
181160 assertSanitizedDoesContain (s , "&" );
182161 }
183162 s = "<IMG SRC=javascript:alert('XSS')>" ;
184- if (sanitize (s ).length () != 0 ) {
163+ if (! sanitize (s ).isEmpty () ) {
185164 assertSanitizedDoesContain (s , "&" );
186165 }
187166
@@ -198,7 +177,8 @@ public static void testImgAttacks() {
198177 assertSanitizedDoesNotContain ("<BGSOUND SRC=\" javascript:alert('XSS');\" >" , "javascript" );
199178 }
200179
201- public static void testHrefAttacks () {
180+ @ Test
181+ void testHrefAttacks () {
202182 assertSanitizedDoesNotContain ("<LINK REL=\" stylesheet\" HREF=\" javascript:alert('XSS');\" >" , "href" );
203183 assertSanitizedDoesNotContain ("<LINK REL=\" stylesheet\" HREF=\" javascript:alert('XSS');\" >" , "href" );
204184
@@ -304,7 +284,8 @@ public static void testHrefAttacks() {
304284 * Test CSS protections.
305285 */
306286
307- public static void testCssAttacks () {
287+ @ Test
288+ void testCssAttacks () {
308289
309290 assertSanitizedDoesNotContain ("<div style=\" position:absolute\" >" , "position" );
310291 assertSanitizedDoesNotContain ("<div style=\" position:absolute\" >" , "position" );
@@ -323,14 +304,15 @@ public static void testCssAttacks() {
323304 * Test a bunch of strings that have tweaked the XML parsing capabilities of
324305 * NekoHTML.
325306 */
326- public static void testIllegalXML () throws Exception {
327- for (int i = 0 ; i < BASE64_BAD_XML_STRINGS .length ; i ++) {
328- String testStr = new String (
329- Base64 .decodeBase64 (BASE64_BAD_XML_STRINGS [i ]),
330- "UTF-8" );
331- sanitize (testStr );
332- sanitize (testStr );
333- }
307+ @ Test
308+ void testIllegalXML () {
309+ for (String base64BadXmlString : BASE64_BAD_XML_STRINGS ) {
310+ String testStr = new String (
311+ Base64 .decodeBase64 (base64BadXmlString ),
312+ StandardCharsets .UTF_8 );
313+ sanitize (testStr );
314+ sanitize (testStr );
315+ }
334316
335317 // These fail in AntiSamy due to a bug in NekoHTML
336318 assertEquals (
@@ -340,10 +322,11 @@ public static void testIllegalXML() throws Exception {
340322 "<a href=\" http://www.test.com\" rel=\" nofollow\" ></a>" ,
341323 sanitize ("<a - href=\" http://www.test.com\" >" ));
342324
343- assertTrue (sanitize ("<style>" ) != null );
325+ assertNotNull (sanitize ("<style>" ));
344326 }
345327
346- public static void testPreviousBugs () {
328+ @ Test
329+ void testPreviousBugs () {
347330
348331 /*
349332 * issues 12 (and 36, which was similar). empty tags cause display
@@ -533,7 +516,7 @@ public static void testPreviousBugs() {
533516 String attack = "[if lte 8]<script>" ;
534517 String spacer = "<![if IE]>" ;
535518
536- StringBuffer sb = new StringBuffer ();
519+ StringBuilder sb = new StringBuilder ();
537520
538521 sb .append ("<div>text<!" );
539522
@@ -555,7 +538,7 @@ public static void testPreviousBugs() {
555538 */
556539 {
557540 String s = "<iframe src='http://foo.com/'></iframe>" + "<script src=''></script>" + "<link href='/foo.css'>" ;
558- assertEquals (s , "" , sanitize (s ));
541+ assertEquals ("" , sanitize (s ), s );
559542 }
560543
561544 /* issue #51 - offsite urls with () are found to be invalid */
@@ -635,7 +618,8 @@ public static void testPreviousBugs() {
635618 * Tests cases dealing with nofollowAnchors directive. Assumes anchor tags
636619 * have an action set to "validate" (may be implicit) in the policy file.
637620 */
638- public static void testNofollowAnchors () {
621+ @ Test
622+ void testNofollowAnchors () {
639623 // adds when not present
640624 assertSanitized ("<a href=\" blah\" >link</a>" , "<a href=\" blah\" rel=\" nofollow\" >link</a>" );
641625
@@ -655,7 +639,8 @@ public static void testNofollowAnchors() {
655639 assertSanitizedDoesNotContain ("a href=\" blah\" >link</a>" , "nofollow" );
656640 }
657641
658- public static void testValidateParamAsEmbed () {
642+ @ Test
643+ void testValidateParamAsEmbed () {
659644 // let's start with a YouTube embed
660645 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>" ;
661646 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>" ;
@@ -684,7 +669,7 @@ public static void testValidateParamAsEmbed() {
684669 }
685670
686671 if (RUN_KNOWN_FAILURES ) {
687- assertTrue ( sanitize (input ). equals ( saxExpectedOutput ));
672+ assertEquals ( saxExpectedOutput , sanitize (input ));
688673 } else {
689674 assertSanitized (input , "" );
690675 }
@@ -715,9 +700,8 @@ private static void assertSanitizedDoesNotContain(
715700 int index = Strings .toLowerCase (sanitized ).indexOf (
716701 Strings .toLowerCase (dangerousContent ));
717702 assertEquals (
718- "`" + sanitized + "` from `" + html + "` contains `" +
719- dangerousContent + "`" ,
720- -1 , index );
703+ -1 , index ,
704+ "`" + sanitized + "` from `" + html + "` contains `" + dangerousContent + "`" );
721705 }
722706
723707 private static void assertSanitizedDoesContain (
@@ -726,9 +710,9 @@ private static void assertSanitizedDoesContain(
726710 int index = Strings .toLowerCase (sanitized ).indexOf (
727711 Strings .toLowerCase (dangerousContent ));
728712 assertTrue (
729- "`" + sanitized + "` from `" + html + "` does not contain `" +
730- dangerousContent + "`" ,
731- index >= 0 );
713+ index >= 0 ,
714+ "`" + sanitized + "` from `" + html + "` does not contain `" + dangerousContent + "`"
715+ );
732716 }
733717
734718 private static void assertSanitized (String html , String sanitized ) {
0 commit comments