@@ -229,6 +229,48 @@ public static final void testStyleFiltering() {
229
229
.allowStandardUrlProtocols ()));
230
230
}
231
231
232
+ @ Test
233
+ public void testSpecificStyleFilterung () {
234
+ assertEquals (
235
+ Arrays .stream (new String [] {
236
+ "<h1>Header</h1>" ,
237
+ "<p>Paragraph 1</p>" ,
238
+ "<p>Click me out</p>" ,
239
+ "<p></p>" ,
240
+ "<p><b>Fancy</b> with <i><b>soupy</b></i><b> tags</b>." ,
241
+ "</p><p style=\" text-align:center\" >Stylish Para 1</p>" ,
242
+ "<p style=\" color:red\" >Stylish Para 2</p>" ,
243
+ "" }).collect (Collectors .joining ("\n " )),
244
+ apply (new HtmlPolicyBuilder ()
245
+ .allowCommonInlineFormattingElements ()
246
+ .allowCommonBlockElements ()
247
+ .allowStyling (CssSchema .withProperties (
248
+ List .of ("color" , "text-align" , "font-size" )))
249
+ .allowStandardUrlProtocols ()));
250
+ }
251
+
252
+ @ Test
253
+ public void testUnionStyleFilterung () {
254
+ assertEquals (
255
+ Arrays .stream (new String [] {
256
+ "<h1>Header</h1>" ,
257
+ "<p>Paragraph 1</p>" ,
258
+ "<p>Click me out</p>" ,
259
+ "<p></p>" ,
260
+ "<p><b>Fancy</b> with <i><b>soupy</b></i><b> tags</b>." ,
261
+ "</p><p style=\" text-align:center\" >Stylish Para 1</p>" ,
262
+ "<p style=\" color:red\" >Stylish Para 2</p>" ,
263
+ "" }).collect (Collectors .joining ("\n " )),
264
+ apply (new HtmlPolicyBuilder ()
265
+ .allowCommonInlineFormattingElements ()
266
+ .allowCommonBlockElements ()
267
+ .allowStyling (CssSchema .withProperties (
268
+ List .of ("color" , "text-align" )))
269
+ .allowStyling ( // union allowed style properties
270
+ CssSchema .withProperties (List .of ("font-size" )))
271
+ .allowStandardUrlProtocols ()));
272
+ }
273
+
232
274
@ Test
233
275
public static final void testElementTransforming () {
234
276
assertEquals (
@@ -289,6 +331,25 @@ public static final void testAllowUrlProtocols() {
289
331
.allowUrlProtocols ("http" )));
290
332
}
291
333
334
+ @ Test
335
+ public static final void testDisallowUrlProtocols () {
336
+ assertEquals (
337
+ Arrays .stream (new String [] {
338
+ "Header" ,
339
+ "Paragraph 1" ,
340
+ "Click me out" ,
341
+ "<img src=\" canary.png\" alt=\" local-canary\" />" ,
342
+ "Fancy with soupy tags." ,
343
+ "Stylish Para 1" ,
344
+ "Stylish Para 2" ,
345
+ "" }).collect (Collectors .joining ("\n " )),
346
+ apply (new HtmlPolicyBuilder ()
347
+ .allowElements ("img" )
348
+ .allowAttributes ("src" , "alt" ).onElements ("img" )
349
+ .allowUrlProtocols ("http" , "https" )
350
+ .disallowUrlProtocols ("http" )));
351
+ }
352
+
292
353
@ Test
293
354
public static final void testPossibleFalloutFromIssue5 () {
294
355
assertEquals (
@@ -847,6 +908,52 @@ public static final void testEmptyDefaultLinkRelsSet() {
847
908
pf .sanitize ("<a href=\" http://example.com\" target=\" _blank\" >eg</a>" ));
848
909
}
849
910
911
+ @ Test
912
+ public static final void testRequireAndSkipRels () {
913
+ PolicyFactory pf = new HtmlPolicyBuilder ()
914
+ .allowElements ("a" )
915
+ .allowAttributes ("href" , "target" ).onElements ("a" )
916
+ .allowStandardUrlProtocols ()
917
+ .requireRelsOnLinks ("noreferrer" )
918
+ .skipRelsOnLinks ("noopener" , "noreferrer" )
919
+ .toFactory ();
920
+
921
+ assertEquals (
922
+ "<a href=\" http://example.com\" target=\" _blank\" >eg</a>" ,
923
+ pf .sanitize ("<a href=\" http://example.com\" target=\" _blank\" >eg</a>" ));
924
+
925
+ assertEquals (
926
+ "<a href=\" http://example.com\" target=\" _blank\" >eg</a>" ,
927
+ pf .sanitize ("<a href=\" http://example.com\" rel=noreferrer target=\" _blank\" >eg</a>" ));
928
+
929
+ assertEquals (
930
+ "<a href=\" http://example.com\" target=\" _blank\" >eg</a>" ,
931
+ pf .sanitize ("<a href=\" http://example.com\" rel=noopener target=\" _blank\" >eg</a>" ));
932
+ }
933
+
934
+ @ Test
935
+ public static final void testSkipAndRequireRels () {
936
+ PolicyFactory pf = new HtmlPolicyBuilder ()
937
+ .allowElements ("a" )
938
+ .allowAttributes ("href" , "target" ).onElements ("a" )
939
+ .allowStandardUrlProtocols ()
940
+ .skipRelsOnLinks ("noopener" , "noreferrer" )
941
+ .requireRelsOnLinks ("noreferrer" )
942
+ .toFactory ();
943
+
944
+ assertEquals (
945
+ "<a href=\" http://example.com\" target=\" _blank\" rel=\" noreferrer\" >eg</a>" ,
946
+ pf .sanitize ("<a href=\" http://example.com\" target=\" _blank\" >eg</a>" ));
947
+
948
+ assertEquals (
949
+ "<a href=\" http://example.com\" target=\" _blank\" rel=\" noreferrer\" >eg</a>" ,
950
+ pf .sanitize ("<a href=\" http://example.com\" rel=noreferrer target=\" _blank\" >eg</a>" ));
951
+
952
+ assertEquals (
953
+ "<a href=\" http://example.com\" target=\" _blank\" rel=\" noreferrer\" >eg</a>" ,
954
+ pf .sanitize ("<a href=\" http://example.com\" rel=noopener target=\" _blank\" >eg</a>" ));
955
+ }
956
+
850
957
@ Test
851
958
public static final void testExplicitRelsSkip () {
852
959
PolicyFactory pf = new HtmlPolicyBuilder ()
@@ -913,6 +1020,64 @@ public static final void testDirLi() {
913
1020
"<dir compact=\" compact\" ><li>something</li></dir>" ));
914
1021
}
915
1022
1023
+ @ Test
1024
+ public void testDisallowTextIn () {
1025
+ HtmlPolicyBuilder sharedPolicyBuilder = new HtmlPolicyBuilder ()
1026
+ .allowElements ("div" )
1027
+ .allowAttributes ("style" ).onElements ("div" );
1028
+
1029
+ PolicyFactory allowPolicy = sharedPolicyBuilder .toFactory ();
1030
+ assertEquals ("<div style=\" display:node\" >Some Text</div>" ,
1031
+ allowPolicy .sanitize ("<div style=\" display:node\" >Some Text</div>" ));
1032
+
1033
+ PolicyFactory disallowTextPolicy =
1034
+ sharedPolicyBuilder .disallowTextIn ("div" ).toFactory ();
1035
+ assertEquals ("<div style=\" display:node\" ></div>" ,
1036
+ disallowTextPolicy .sanitize (
1037
+ "<div style=\" display:node\" >Some Text</div>" ));
1038
+ }
1039
+
1040
+ @ Test
1041
+ public void testDisallowAttribute () {
1042
+ HtmlPolicyBuilder sharedPolicyBuilder = new HtmlPolicyBuilder ()
1043
+ .allowElements ("div" , "p" )
1044
+ .allowAttributes ("style" ).onElements ("div" , "p" );
1045
+
1046
+ PolicyFactory allowPolicy = sharedPolicyBuilder .toFactory ();
1047
+ assertEquals (
1048
+ "<p style=\" display:node\" >Some</p><div style=\" display:node\" >Text</div>" ,
1049
+ allowPolicy .sanitize (
1050
+ "<p style=\" display:node\" >Some</p><div style=\" display:node\" >Text</div>" ));
1051
+
1052
+ PolicyFactory disallowTextPolicy =
1053
+ sharedPolicyBuilder .disallowAttributes ("style" ).onElements ("p" ).toFactory ();
1054
+ assertEquals ("<p>Some</p><div style=\" display:node\" >Text</div>" ,
1055
+ disallowTextPolicy .sanitize (
1056
+ "<p style=\" display:node\" >Some</p><div style=\" display:node\" >Text</div>" ));
1057
+ }
1058
+
1059
+ @ Test
1060
+ public void testCreativeCSSStyling () {
1061
+ PolicyFactory policy = new HtmlPolicyBuilder ()
1062
+ .allowElements ("p" )
1063
+ .allowAttributes ("style" ).onElements ("p" ).allowStyling ().toFactory ();
1064
+
1065
+ assertEquals ("<p>Some</p>" ,
1066
+ policy .sanitize ("<p style=\" {display:none\" >Some</p>" ));
1067
+
1068
+ assertEquals ("<p style=\" color:red\" >Some</p>" ,
1069
+ policy .sanitize ("<p style=\" {display:none;};color:red\" >Some</p>" ));
1070
+
1071
+ assertEquals ("<p style=\" color:red\" >Some</p>" ,
1072
+ policy .sanitize ("<p style=\" {display:none;}color:red\" >Some</p>" ));
1073
+
1074
+ assertEquals ("<p style=\" color:red\" >Some</p>" ,
1075
+ policy .sanitize ("<p style=\" display:none }; color:red\" >Some</p>" ));
1076
+
1077
+ assertEquals ("<p style=\" color:red\" >Some</p>" ,
1078
+ policy .sanitize ("<p style=\" {display:none;}}color:red\" >Some</p>" ));
1079
+ }
1080
+
916
1081
@ Test
917
1082
public static void testScriptTagWithCommentBlockContainingHtmlCommentEnd () {
918
1083
PolicyFactory scriptSanitizer = new HtmlPolicyBuilder ()
0 commit comments