@@ -232,7 +232,28 @@ public static final void testStyleFiltering() {
232
232
}
233
233
234
234
@ Test
235
- public void testCustomPropertyStyleFilterung () {
235
+ public void testSpecificStyleFilterung () {
236
+ assertEquals (
237
+ Arrays .stream (new String [] {
238
+ "<h1>Header</h1>" ,
239
+ "<p>Paragraph 1</p>" ,
240
+ "<p>Click me out</p>" ,
241
+ "<p></p>" ,
242
+ "<p><b>Fancy</b> with <i><b>soupy</b></i><b> tags</b>." ,
243
+ "</p><p style=\" text-align:center\" >Stylish Para 1</p>" ,
244
+ "<p style=\" color:red\" >Stylish Para 2</p>" ,
245
+ "" }).collect (Collectors .joining ("\n " )),
246
+ apply (new HtmlPolicyBuilder ()
247
+ .allowCommonInlineFormattingElements ()
248
+ .allowCommonBlockElements ()
249
+ .allowStyling (CssSchema .withProperties (
250
+ List .of ("color" , "text-align" , "font-size" )))
251
+ .allowStandardUrlProtocols ()));
252
+ }
253
+
254
+
255
+ @ Test
256
+ public void testCustomPropertyStyleFiltering () {
236
257
assertEquals (
237
258
Arrays .stream (new String [] {
238
259
"<h1>Header</h1>" ,
@@ -256,7 +277,29 @@ public void testCustomPropertyStyleFilterung() {
256
277
}
257
278
258
279
@ Test
259
- public void testCustomPropertyStyleFilterungDisallowed () {
280
+ public void testUnionStyleFiltering () {
281
+ assertEquals (
282
+ Arrays .stream (new String [] {
283
+ "<h1>Header</h1>" ,
284
+ "<p>Paragraph 1</p>" ,
285
+ "<p>Click me out</p>" ,
286
+ "<p></p>" ,
287
+ "<p><b>Fancy</b> with <i><b>soupy</b></i><b> tags</b>." ,
288
+ "</p><p style=\" text-align:center\" >Stylish Para 1</p>" ,
289
+ "<p style=\" color:red\" >Stylish Para 2</p>" ,
290
+ "" }).collect (Collectors .joining ("\n " )),
291
+ apply (new HtmlPolicyBuilder ()
292
+ .allowCommonInlineFormattingElements ()
293
+ .allowCommonBlockElements ()
294
+ .allowStyling (CssSchema .withProperties (
295
+ List .of ("color" , "text-align" )))
296
+ .allowStyling ( // union allowed style properties
297
+ CssSchema .withProperties (List .of ("font-size" )))
298
+ .allowStandardUrlProtocols ()));
299
+ }
300
+
301
+ @ Test
302
+ public void testCustomPropertyStyleFilteringDisallowed () {
260
303
assertEquals (
261
304
Arrays .stream (new String [] {
262
305
"<h1>Header</h1>" ,
@@ -339,6 +382,25 @@ public static final void testAllowUrlProtocols() {
339
382
.allowUrlProtocols ("http" )));
340
383
}
341
384
385
+ @ Test
386
+ public static final void testDisallowUrlProtocols () {
387
+ assertEquals (
388
+ Arrays .stream (new String [] {
389
+ "Header" ,
390
+ "Paragraph 1" ,
391
+ "Click me out" ,
392
+ "<img src=\" canary.png\" alt=\" local-canary\" />" ,
393
+ "Fancy with soupy tags." ,
394
+ "Stylish Para 1" ,
395
+ "Stylish Para 2" ,
396
+ "" }).collect (Collectors .joining ("\n " )),
397
+ apply (new HtmlPolicyBuilder ()
398
+ .allowElements ("img" )
399
+ .allowAttributes ("src" , "alt" ).onElements ("img" )
400
+ .allowUrlProtocols ("http" , "https" )
401
+ .disallowUrlProtocols ("http" )));
402
+ }
403
+
342
404
@ Test
343
405
public static final void testPossibleFalloutFromIssue5 () {
344
406
assertEquals (
@@ -897,6 +959,52 @@ public static final void testEmptyDefaultLinkRelsSet() {
897
959
pf .sanitize ("<a href=\" http://example.com\" target=\" _blank\" >eg</a>" ));
898
960
}
899
961
962
+ @ Test
963
+ public static final void testRequireAndSkipRels () {
964
+ PolicyFactory pf = new HtmlPolicyBuilder ()
965
+ .allowElements ("a" )
966
+ .allowAttributes ("href" , "target" ).onElements ("a" )
967
+ .allowStandardUrlProtocols ()
968
+ .requireRelsOnLinks ("noreferrer" )
969
+ .skipRelsOnLinks ("noopener" , "noreferrer" )
970
+ .toFactory ();
971
+
972
+ assertEquals (
973
+ "<a href=\" http://example.com\" target=\" _blank\" >eg</a>" ,
974
+ pf .sanitize ("<a href=\" http://example.com\" target=\" _blank\" >eg</a>" ));
975
+
976
+ assertEquals (
977
+ "<a href=\" http://example.com\" target=\" _blank\" >eg</a>" ,
978
+ pf .sanitize ("<a href=\" http://example.com\" rel=noreferrer target=\" _blank\" >eg</a>" ));
979
+
980
+ assertEquals (
981
+ "<a href=\" http://example.com\" target=\" _blank\" >eg</a>" ,
982
+ pf .sanitize ("<a href=\" http://example.com\" rel=noopener target=\" _blank\" >eg</a>" ));
983
+ }
984
+
985
+ @ Test
986
+ public static final void testSkipAndRequireRels () {
987
+ PolicyFactory pf = new HtmlPolicyBuilder ()
988
+ .allowElements ("a" )
989
+ .allowAttributes ("href" , "target" ).onElements ("a" )
990
+ .allowStandardUrlProtocols ()
991
+ .skipRelsOnLinks ("noopener" , "noreferrer" )
992
+ .requireRelsOnLinks ("noreferrer" )
993
+ .toFactory ();
994
+
995
+ assertEquals (
996
+ "<a href=\" http://example.com\" target=\" _blank\" rel=\" noreferrer\" >eg</a>" ,
997
+ pf .sanitize ("<a href=\" http://example.com\" target=\" _blank\" >eg</a>" ));
998
+
999
+ assertEquals (
1000
+ "<a href=\" http://example.com\" target=\" _blank\" rel=\" noreferrer\" >eg</a>" ,
1001
+ pf .sanitize ("<a href=\" http://example.com\" rel=noreferrer target=\" _blank\" >eg</a>" ));
1002
+
1003
+ assertEquals (
1004
+ "<a href=\" http://example.com\" target=\" _blank\" rel=\" noreferrer\" >eg</a>" ,
1005
+ pf .sanitize ("<a href=\" http://example.com\" rel=noopener target=\" _blank\" >eg</a>" ));
1006
+ }
1007
+
900
1008
@ Test
901
1009
public static final void testExplicitRelsSkip () {
902
1010
PolicyFactory pf = new HtmlPolicyBuilder ()
@@ -963,6 +1071,64 @@ public static final void testDirLi() {
963
1071
"<dir compact=\" compact\" ><li>something</li></dir>" ));
964
1072
}
965
1073
1074
+ @ Test
1075
+ public void testDisallowTextIn () {
1076
+ HtmlPolicyBuilder sharedPolicyBuilder = new HtmlPolicyBuilder ()
1077
+ .allowElements ("div" )
1078
+ .allowAttributes ("style" ).onElements ("div" );
1079
+
1080
+ PolicyFactory allowPolicy = sharedPolicyBuilder .toFactory ();
1081
+ assertEquals ("<div style=\" display:node\" >Some Text</div>" ,
1082
+ allowPolicy .sanitize ("<div style=\" display:node\" >Some Text</div>" ));
1083
+
1084
+ PolicyFactory disallowTextPolicy =
1085
+ sharedPolicyBuilder .disallowTextIn ("div" ).toFactory ();
1086
+ assertEquals ("<div style=\" display:node\" ></div>" ,
1087
+ disallowTextPolicy .sanitize (
1088
+ "<div style=\" display:node\" >Some Text</div>" ));
1089
+ }
1090
+
1091
+ @ Test
1092
+ public void testDisallowAttribute () {
1093
+ HtmlPolicyBuilder sharedPolicyBuilder = new HtmlPolicyBuilder ()
1094
+ .allowElements ("div" , "p" )
1095
+ .allowAttributes ("style" ).onElements ("div" , "p" );
1096
+
1097
+ PolicyFactory allowPolicy = sharedPolicyBuilder .toFactory ();
1098
+ assertEquals (
1099
+ "<p style=\" display:node\" >Some</p><div style=\" display:node\" >Text</div>" ,
1100
+ allowPolicy .sanitize (
1101
+ "<p style=\" display:node\" >Some</p><div style=\" display:node\" >Text</div>" ));
1102
+
1103
+ PolicyFactory disallowTextPolicy =
1104
+ sharedPolicyBuilder .disallowAttributes ("style" ).onElements ("p" ).toFactory ();
1105
+ assertEquals ("<p>Some</p><div style=\" display:node\" >Text</div>" ,
1106
+ disallowTextPolicy .sanitize (
1107
+ "<p style=\" display:node\" >Some</p><div style=\" display:node\" >Text</div>" ));
1108
+ }
1109
+
1110
+ @ Test
1111
+ public void testCreativeCSSStyling () {
1112
+ PolicyFactory policy = new HtmlPolicyBuilder ()
1113
+ .allowElements ("p" )
1114
+ .allowAttributes ("style" ).onElements ("p" ).allowStyling ().toFactory ();
1115
+
1116
+ assertEquals ("<p>Some</p>" ,
1117
+ policy .sanitize ("<p style=\" {display:none\" >Some</p>" ));
1118
+
1119
+ assertEquals ("<p style=\" color:red\" >Some</p>" ,
1120
+ policy .sanitize ("<p style=\" {display:none;};color:red\" >Some</p>" ));
1121
+
1122
+ assertEquals ("<p style=\" color:red\" >Some</p>" ,
1123
+ policy .sanitize ("<p style=\" {display:none;}color:red\" >Some</p>" ));
1124
+
1125
+ assertEquals ("<p style=\" color:red\" >Some</p>" ,
1126
+ policy .sanitize ("<p style=\" display:none }; color:red\" >Some</p>" ));
1127
+
1128
+ assertEquals ("<p style=\" color:red\" >Some</p>" ,
1129
+ policy .sanitize ("<p style=\" {display:none;}}color:red\" >Some</p>" ));
1130
+ }
1131
+
966
1132
@ Test
967
1133
public static void testScriptTagWithCommentBlockContainingHtmlCommentEnd () {
968
1134
PolicyFactory scriptSanitizer = new HtmlPolicyBuilder ()
@@ -1057,6 +1223,12 @@ public static final void testTextareaIsNotTextArea() {
1057
1223
assertEquals ("x<textArea>y</textArea>" , textAreaPolicy .sanitize (input ));
1058
1224
}
1059
1225
1226
+ @ Test
1227
+ public static final void testHtmlPolicyBuilderDefinitionWithNoAttributesDefinedGlobally () {
1228
+ // Does not crash with a runtime exception
1229
+ new HtmlPolicyBuilder ().allowElements ().allowAttributes ().globally ().toFactory ();
1230
+ }
1231
+
1060
1232
@ Test
1061
1233
public static final void testCSSFontSize () {
1062
1234
HtmlPolicyBuilder builder = new HtmlPolicyBuilder ();
0 commit comments