Paragraph 1
", + "Click me out
", + "", + "Fancy with soupy tags.", + "
Stylish Para 1
", + "Stylish Para 2
", + ""}).collect(Collectors.joining("\n")), + apply(new HtmlPolicyBuilder() + .allowCommonInlineFormattingElements() + .allowCommonBlockElements() + .allowStyling(CssSchema.withProperties( + List.of("color", "text-align", "font-size"))) + .allowStandardUrlProtocols())); + } + + @Test + public void testUnionStyleFilterung() { + assertEquals( + Arrays.stream(new String[] { + "Paragraph 1
", + "Click me out
", + "", + "Fancy with soupy tags.", + "
Stylish Para 1
", + "Stylish Para 2
", + ""}).collect(Collectors.joining("\n")), + apply(new HtmlPolicyBuilder() + .allowCommonInlineFormattingElements() + .allowCommonBlockElements() + .allowStyling(CssSchema.withProperties( + List.of("color", "text-align"))) + .allowStyling( // union allowed style properties + CssSchema.withProperties(List.of("font-size"))) + .allowStandardUrlProtocols())); + } + @Test public static final void testElementTransforming() { assertEquals( @@ -289,6 +331,25 @@ public static final void testAllowUrlProtocols() { .allowUrlProtocols("http"))); } + @Test + public static final void testDisallowUrlProtocols() { + assertEquals( + Arrays.stream(new String[] { + "Header", + "Paragraph 1", + "Click me out", + "", + "Fancy with soupy tags.", + "Stylish Para 1", + "Stylish Para 2", + ""}).collect(Collectors.joining("\n")), + apply(new HtmlPolicyBuilder() + .allowElements("img") + .allowAttributes("src", "alt").onElements("img") + .allowUrlProtocols("http", "https") + .disallowUrlProtocols("http"))); + } + @Test public static final void testPossibleFalloutFromIssue5() { assertEquals( @@ -801,7 +862,7 @@ public static final void testLinkRelsWhenRelPresent() { } @Test - public static final void testRelLinksWhenRelisPartOfData() { + public final void testRelLinksWhenRelIsPartOfData() { PolicyFactory pf = new HtmlPolicyBuilder() .allowElements("a") .allowAttributes("href").onElements("a") @@ -810,7 +871,7 @@ public static final void testRelLinksWhenRelisPartOfData() { .allowStandardUrlProtocols() .toFactory(); String toSanitize = "test"; - assertTrue("Failure in testRelLinksWhenRelisPartOfData", pf.sanitize(toSanitize).equals(toSanitize)); + assertEquals(toSanitize, pf.sanitize(toSanitize)); } @Test @@ -847,6 +908,52 @@ public static final void testEmptyDefaultLinkRelsSet() { pf.sanitize("eg")); } + @Test + public static final void testRequireAndSkipRels() { + PolicyFactory pf = new HtmlPolicyBuilder() + .allowElements("a") + .allowAttributes("href", "target").onElements("a") + .allowStandardUrlProtocols() + .requireRelsOnLinks("noreferrer") + .skipRelsOnLinks("noopener", "noreferrer") + .toFactory(); + + assertEquals( + "eg", + pf.sanitize("eg")); + + assertEquals( + "eg", + pf.sanitize("eg")); + + assertEquals( + "eg", + pf.sanitize("eg")); + } + + @Test + public static final void testSkipAndRequireRels() { + PolicyFactory pf = new HtmlPolicyBuilder() + .allowElements("a") + .allowAttributes("href", "target").onElements("a") + .allowStandardUrlProtocols() + .skipRelsOnLinks("noopener", "noreferrer") + .requireRelsOnLinks("noreferrer") + .toFactory(); + + assertEquals( + "eg", + pf.sanitize("eg")); + + assertEquals( + "eg", + pf.sanitize("eg")); + + assertEquals( + "eg", + pf.sanitize("eg")); + } + @Test public static final void testExplicitRelsSkip() { PolicyFactory pf = new HtmlPolicyBuilder() @@ -913,6 +1020,64 @@ public static final void testDirLi() { "Some
Some
Some
Some
Some
", + policy.sanitize(" ")); + + assertEquals("Some
", + policy.sanitize(" ")); + + assertEquals("Some
", + policy.sanitize(" ")); + + assertEquals("Some
", + policy.sanitize(" ")); + + assertEquals("Some
", + policy.sanitize(" ")); + } + @Test public static void testScriptTagWithCommentBlockContainingHtmlCommentEnd() { PolicyFactory scriptSanitizer = new HtmlPolicyBuilder() @@ -1007,6 +1172,12 @@ public static final void testTextareaIsNotTextArea() { assertEquals("x", textAreaPolicy.sanitize(input)); } + @Test + public static final void testHtmlPolicyBuilderDefinitionWithNoAttributesDefinedGlobally() { + // Does not crash with a runtime exception + new HtmlPolicyBuilder().allowElements().allowAttributes().globally().toFactory(); + } + @Test public static final void testCSSFontSize() { HtmlPolicyBuilder builder = new HtmlPolicyBuilder(); diff --git a/src/test/java/org/owasp/html/SanitizersTest.java b/src/test/java/org/owasp/html/SanitizersTest.java index 25ede957..d14d1156 100644 --- a/src/test/java/org/owasp/html/SanitizersTest.java +++ b/src/test/java/org/owasp/html/SanitizersTest.java @@ -158,6 +158,58 @@ public static final void testImages() { ); } + @Test + public static final void testIntegerAttributePolicy() { + PolicyFactory s = Sanitizers.IMAGES; + assertEquals( + "", + s.sanitize( + "") + ); + + assertEquals( + "", + s.sanitize( + "") + ); + + assertEquals( + "", + s.sanitize( + "") + ); + + assertEquals( + "", + s.sanitize( + "") + ); + + assertEquals( + "", + s.sanitize( + "") + ); + + assertEquals( + "", + s.sanitize( + "") + ); + + assertEquals( + "", + s.sanitize( + "") + ); + + assertEquals( + "", + s.sanitize( + "") + ); + } + @Test public static final void testLinks() { PolicyFactory s = Sanitizers.LINKS;