Skip to content

Fix : CSS Child Combinator Parsing Bug #297

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/main/java/org/owasp/html/HtmlStreamRenderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ private static int checkHtmlCdataCloseable(
}
break;
case '>':
if (i >= 2 && sb.charAt(i - 2) == '-' && sb.charAt(i - 2) == '-') {
if (i >= 2 && sb.charAt(i - 2) == '-' && sb.charAt(i - 1) == '-') {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks

if (innerStart < 0) { return i - 2; }
// Merged start and end like <!--->
if (innerStart + 6 > i) { return innerStart; }
Expand Down
37 changes: 36 additions & 1 deletion src/test/java/org/owasp/html/HtmlPolicyBuilderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -994,7 +994,7 @@ public static final void testTextareaIsNotTextArea() {
assertEquals("x<textArea>y</textArea>", textAreaPolicy.sanitize(input));
}

@Test
@Test
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is just an opportunistic fix while merging.

public static final void testCSSFontSize() {
HtmlPolicyBuilder builder = new HtmlPolicyBuilder();
PolicyFactory factory = builder.allowElements("span")
Expand All @@ -1007,6 +1007,41 @@ public static final void testCSSFontSize() {
assertEquals(toSanitizeMedium, factory.sanitize(toSanitizeMedium));
}

@Test
public static final void testCSSChildCombinator() {
HtmlPolicyBuilder builder = new HtmlPolicyBuilder();

PolicyFactory factory = builder.allowElements("span","style","h1").allowTextIn("style","h1")
.allowAttributes("type").onElements("style").allowStyling()
.toFactory();


String toSanitize = "<style type=\"text/css\">\n"
+ "<!--\n"
+ ".hdg-1 {\n"
+ "width:100%;\n"
+ "}\n"
+ "\n"
+ ".hdg-1>._inner {\n"
+ "background-color: #999;\n"
+ "}\n"
+ "-->\n"
+ "</style>\n"
+ "<h1>Test</h1>\n"
+ "\n"
+ "<style>\n"
+ "<!--\n"
+ ".hdg-1 {\n"
+ "width:100%;\n"
+ "}\n"
+ "\n"
+ ".hdg-1>._inner {\n"
+ "background-color: #666;\n"
+ "}\n"
+ "-->\n"
+ "</style>";
assertEquals(toSanitize, factory.sanitize(toSanitize));
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the unit test.


private static String apply(HtmlPolicyBuilder b) {
return apply(b, EXAMPLE);
Expand Down