Skip to content

Commit 124b7dd

Browse files
DavideDgsmet
authored andcommitted
HV-1739 Fix CVE-2019-10219 Security issue with @SafeHtml
1 parent 2687d33 commit 124b7dd

File tree

2 files changed

+43
-5
lines changed

2 files changed

+43
-5
lines changed

engine/src/main/java/org/hibernate/validator/internal/constraintvalidators/hv/SafeHtmlValidator.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@
66
*/
77
package org.hibernate.validator.internal.constraintvalidators.hv;
88

9-
import java.util.Iterator;
9+
import java.util.List;
1010

1111
import javax.validation.ConstraintValidator;
1212
import javax.validation.ConstraintValidatorContext;
1313

1414
import org.hibernate.validator.constraints.SafeHtml;
1515
import org.jsoup.Jsoup;
1616
import org.jsoup.nodes.Document;
17-
import org.jsoup.nodes.Element;
17+
import org.jsoup.nodes.Node;
1818
import org.jsoup.parser.Parser;
1919
import org.jsoup.safety.Cleaner;
2020
import org.jsoup.safety.Whitelist;
@@ -91,9 +91,9 @@ private Document getFragmentAsDocument(CharSequence value) {
9191
Document document = Document.createShell( baseURI );
9292

9393
// add the fragment's nodes to the body of resulting document
94-
Iterator<Element> nodes = fragment.children().iterator();
95-
while ( nodes.hasNext() ) {
96-
document.body().appendChild( nodes.next() );
94+
List<Node> childNodes = fragment.childNodes();
95+
for ( Node node : childNodes ) {
96+
document.body().appendChild( node.clone() );
9797
}
9898

9999
return document;

engine/src/test/java/org/hibernate/validator/test/internal/constraintvalidators/hv/SafeHtmlValidatorTest.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,44 @@ public void testInvalidScriptTagIncluded() throws Exception {
5757
assertFalse( getSafeHtmlValidator().isValid( "Hello<script>alert('Doh')</script>World !", null ) );
5858
}
5959

60+
@Test
61+
// A "downlevel revealed" conditional 'comment' is not an (X)HTML comment at all,
62+
// despite the misleading name, it is default Microsoft syntax.
63+
// The tag is unrecognized by therefore executed
64+
public void testDownlevelRevealedConditionalComment() throws Exception {
65+
descriptorBuilder.setAttribute( "whitelistType", WhiteListType.BASIC );
66+
67+
assertFalse( getSafeHtmlValidator().isValid( "<![if !IE]>\n<SCRIPT>alert{'XSS'};</SCRIPT>\n<![endif]>", null ) );
68+
}
69+
70+
@Test
71+
public void testDownlevelHiddenConditionalComment() throws Exception {
72+
descriptorBuilder.setAttribute( "whitelistType", WhiteListType.BASIC );
73+
74+
assertFalse( getSafeHtmlValidator().isValid( "<!--[if gte IE 4]>\n<SCRIPT>alert{'XSS'};</SCRIPT>\n<![endif]-->", null ) );
75+
}
76+
77+
@Test
78+
public void testSimpleComment() throws Exception {
79+
descriptorBuilder.setAttribute( "whitelistType", WhiteListType.BASIC );
80+
81+
assertFalse( getSafeHtmlValidator().isValid( "<!-- Just a comment -->", null ) );
82+
}
83+
84+
@Test
85+
public void testServerSideIncludesSSI() throws Exception {
86+
descriptorBuilder.setAttribute( "whitelistType", WhiteListType.BASIC );
87+
88+
assertFalse( getSafeHtmlValidator().isValid( "<? echo{'<SCR}'; echo{'IPT>alert{\"XSS\"}</SCRIPT>'}; ?>", null ) );
89+
}
90+
91+
@Test
92+
public void testPHPScript() throws Exception {
93+
descriptorBuilder.setAttribute( "whitelistType", WhiteListType.BASIC );
94+
95+
assertFalse( getSafeHtmlValidator().isValid( "<? echo{'<SCR}'; echo{'IPT>alert{\"XSS\"}</SCRIPT>'}; ?>", null ) );
96+
}
97+
6098
@Test
6199
public void testInvalidIncompleteImgTagWithScriptIncluded() {
62100
descriptorBuilder.setAttribute( "whitelistType", WhiteListType.BASIC );

0 commit comments

Comments
 (0)