Skip to content

Commit 0f55cc3

Browse files
committed
MLE-30241: Apply XXE protections
Replace bare DocumentBuilderFactory.newInstance() in OpenCSVBatcher.write() with XmlFactories.getDocumentBuilderFactory()
1 parent fa3b530 commit 0f55cc3

2 files changed

Lines changed: 27 additions & 5 deletions

File tree

examples/src/main/java/com/marklogic/client/example/extension/OpenCSVBatcher.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@
88

99
import javax.xml.namespace.QName;
1010
import javax.xml.parsers.DocumentBuilder;
11-
import javax.xml.parsers.DocumentBuilderFactory;
1211
import javax.xml.parsers.ParserConfigurationException;
1312

13+
import com.marklogic.client.impl.XmlFactories;
14+
1415
import com.opencsv.exceptions.CsvValidationException;
1516
import org.w3c.dom.Document;
1617
import org.w3c.dom.Element;
@@ -88,10 +89,7 @@ else if (!directory.endsWith("/"))
8889
}
8990
}
9091

91-
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
92-
factory.setNamespaceAware(true);
93-
factory.setValidating(false);
94-
DocumentBuilder docBuilder = factory.newDocumentBuilder();
92+
DocumentBuilder docBuilder = XmlFactories.getDocumentBuilderFactory().newDocumentBuilder();
9593

9694
String path = directory + rowName;
9795

marklogic-client-api/src/test/java/com/marklogic/client/test/example/extension/OpenCSVBatcherTest.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,15 @@
44
package com.marklogic.client.test.example.extension;
55

66
import com.marklogic.client.example.extension.OpenCSVBatcherExample;
7+
import com.marklogic.client.impl.XmlFactories;
78
import org.junit.jupiter.api.Test;
9+
import org.xml.sax.SAXException;
10+
11+
import javax.xml.parsers.DocumentBuilder;
12+
import java.io.ByteArrayInputStream;
13+
import java.nio.charset.StandardCharsets;
14+
15+
import static org.junit.jupiter.api.Assertions.assertThrows;
816

917
class OpenCSVBatcherTest {
1018

@@ -13,4 +21,20 @@ void testMain() throws Exception {
1321
// This is a simple smoke test to ensure that the main method runs without exceptions.
1422
OpenCSVBatcherExample.main(new String[0]);
1523
}
24+
25+
@Test
26+
void documentBuilderShouldRejectDoctype() throws Exception {
27+
// Verifies that the DocumentBuilder produced by XmlFactories.getDocumentBuilderFactory()
28+
// — the same factory now used by OpenCSVBatcher.write() — rejects DOCTYPE declarations,
29+
// confirming that XXE / DTD processing is disabled (CWE-611).
30+
DocumentBuilder builder = XmlFactories.getDocumentBuilderFactory().newDocumentBuilder();
31+
String xmlWithDoctype =
32+
"<?xml version=\"1.0\"?>" +
33+
"<!DOCTYPE foo [<!ENTITY xxe SYSTEM \"file:///etc/passwd\">]>" +
34+
"<foo>&xxe;</foo>";
35+
36+
assertThrows(SAXException.class, () ->
37+
builder.parse(new ByteArrayInputStream(xmlWithDoctype.getBytes(StandardCharsets.UTF_8)))
38+
);
39+
}
1640
}

0 commit comments

Comments
 (0)