Skip to content

Commit 6b3cebd

Browse files
subbudvkmikesamuel
andauthored
Fix : CSS Child Combinator Parsing Bug (#297)
* Fix : Bug in HTMLStreamRendering for Child Combinator CSS * Fix : ChildCombinator CSS Parsing Issue --------- Co-authored-by: Mike Samuel <mikesamuel@gmail.com>
1 parent 5b420f9 commit 6b3cebd

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

src/main/java/org/owasp/html/HtmlStreamRenderer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ private static int checkHtmlCdataCloseable(
341341
}
342342
break;
343343
case '>':
344-
if (i >= 2 && sb.charAt(i - 2) == '-' && sb.charAt(i - 2) == '-') {
344+
if (i >= 2 && sb.charAt(i - 2) == '-' && sb.charAt(i - 1) == '-') {
345345
if (innerStart < 0) { return i - 2; }
346346
// Merged start and end like <!--->
347347
if (innerStart + 6 > i) { return innerStart; }

src/test/java/org/owasp/html/HtmlPolicyBuilderTest.java

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -994,7 +994,7 @@ public static final void testTextareaIsNotTextArea() {
994994
assertEquals("x<textArea>y</textArea>", textAreaPolicy.sanitize(input));
995995
}
996996

997-
@Test
997+
@Test
998998
public static final void testCSSFontSize() {
999999
HtmlPolicyBuilder builder = new HtmlPolicyBuilder();
10001000
PolicyFactory factory = builder.allowElements("span")
@@ -1007,6 +1007,41 @@ public static final void testCSSFontSize() {
10071007
assertEquals(toSanitizeMedium, factory.sanitize(toSanitizeMedium));
10081008
}
10091009

1010+
@Test
1011+
public static final void testCSSChildCombinator() {
1012+
HtmlPolicyBuilder builder = new HtmlPolicyBuilder();
1013+
1014+
PolicyFactory factory = builder.allowElements("span","style","h1").allowTextIn("style","h1")
1015+
.allowAttributes("type").onElements("style").allowStyling()
1016+
.toFactory();
1017+
1018+
1019+
String toSanitize = "<style type=\"text/css\">\n"
1020+
+ "<!--\n"
1021+
+ ".hdg-1 {\n"
1022+
+ "width:100%;\n"
1023+
+ "}\n"
1024+
+ "\n"
1025+
+ ".hdg-1>._inner {\n"
1026+
+ "background-color: #999;\n"
1027+
+ "}\n"
1028+
+ "-->\n"
1029+
+ "</style>\n"
1030+
+ "<h1>Test</h1>\n"
1031+
+ "\n"
1032+
+ "<style>\n"
1033+
+ "<!--\n"
1034+
+ ".hdg-1 {\n"
1035+
+ "width:100%;\n"
1036+
+ "}\n"
1037+
+ "\n"
1038+
+ ".hdg-1>._inner {\n"
1039+
+ "background-color: #666;\n"
1040+
+ "}\n"
1041+
+ "-->\n"
1042+
+ "</style>";
1043+
assertEquals(toSanitize, factory.sanitize(toSanitize));
1044+
}
10101045

10111046
private static String apply(HtmlPolicyBuilder b) {
10121047
return apply(b, EXAMPLE);

0 commit comments

Comments
 (0)