File tree Expand file tree Collapse file tree 3 files changed +43
-1
lines changed
main/java/io/logz/sawmill/utilities
java/io/logz/sawmill/utilities Expand file tree Collapse file tree 3 files changed +43
-1
lines changed Original file line number Diff line number Diff line change 77import javax .xml .parsers .ParserConfigurationException ;
88
99public class DocumentBuilderProvider {
10+
11+ private static final String DISALLOW_DOCTYPE = "http://apache.org/xml/features/disallow-doctype-decl" ;
12+ private static final String EXTERNAL_GENERAL_ENTITIES = "http://xml.org/sax/features/external-general-entities" ;
13+ private static final String EXTERNAL_PARAMETER_ENTITIES = "http://xml.org/sax/features/external-parameter-entities" ;
14+ private static final String LOAD_EXTERNAL_DTD = "http://apache.org/xml/features/nonvalidating/load-external-dtd" ;
15+
1016 private final ThreadLocal <DocumentBuilder > localDocumentBuilder ;
1117
1218 public DocumentBuilderProvider () {
1319 localDocumentBuilder = ThreadLocal .withInitial (() -> {
1420 try {
15- return DocumentBuilderFactory .newInstance ().newDocumentBuilder ();
21+ DocumentBuilderFactory factory = DocumentBuilderFactory .newInstance ();
22+ factory .setFeature (DISALLOW_DOCTYPE , true );
23+ factory .setFeature (EXTERNAL_GENERAL_ENTITIES , false );
24+ factory .setFeature (EXTERNAL_PARAMETER_ENTITIES , false );
25+ factory .setFeature (LOAD_EXTERNAL_DTD , false );
26+ factory .setXIncludeAware (false );
27+ factory .setExpandEntityReferences (false );
28+ return factory .newDocumentBuilder ();
1629 } catch (ParserConfigurationException e ) {
1730 throw new ProcessorConfigurationException ("failed to create document builder" , e );
1831 }
Original file line number Diff line number Diff line change 1+ package io .logz .sawmill .utilities ;
2+
3+ import java .io .InputStream ;
4+ import javax .xml .parsers .DocumentBuilder ;
5+ import org .junit .Test ;
6+ import static org .assertj .core .api .Assertions .assertThat ;
7+ import static org .assertj .core .api .Assertions .assertThatThrownBy ;
8+
9+ public class DocumentBuilderProviderTest {
10+
11+ private static final String XML_FILE = "/test_xml_injection.xml" ;
12+
13+ @ Test
14+ public void testDocumentBuilderProviderReturnsNonNullEntity () {
15+ DocumentBuilderProvider documentBuilderProvider = new DocumentBuilderProvider ();
16+ DocumentBuilder documentBuilder = documentBuilderProvider .provide ();
17+ assertThat (documentBuilder ).isNotNull ();
18+ }
19+
20+ @ Test
21+ public void testParseXml () {
22+ InputStream xmlFile = DocumentBuilderProviderTest .class .getResourceAsStream (XML_FILE );
23+ assertThatThrownBy (() -> new DocumentBuilderProvider ().provide ().parse (xmlFile ))
24+ .hasMessageStartingWith ("DOCTYPE is disallowed" );
25+ }
26+ }
Original file line number Diff line number Diff line change 1+ <?xml version =" 1.0" encoding =" UTF-8" ?>
2+ <!DOCTYPE foo [ <!ENTITY xxe SYSTEM " file:///" > ]>
3+ <stockCheck ><productId >&xxe; </productId ></stockCheck >
You can’t perform that action at this time.
0 commit comments