Skip to content

Commit 0d59269

Browse files
authored
DEV-31192: Fixed injection abilities (#299)
1 parent f1d208e commit 0d59269

File tree

3 files changed

+43
-1
lines changed

3 files changed

+43
-1
lines changed

sawmill-core/src/main/java/io/logz/sawmill/utilities/DocumentBuilderProvider.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,25 @@
77
import javax.xml.parsers.ParserConfigurationException;
88

99
public 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
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE foo [ <!ENTITY xxe SYSTEM "file:///"> ]>
3+
<stockCheck><productId>&xxe;</productId></stockCheck>

0 commit comments

Comments
 (0)