diff --git a/marklogic-client-api/src/main/java/com/marklogic/client/impl/FailedRequest.java b/marklogic-client-api/src/main/java/com/marklogic/client/impl/FailedRequest.java
index 00ca7c3be..71b94eb29 100644
--- a/marklogic-client-api/src/main/java/com/marklogic/client/impl/FailedRequest.java
+++ b/marklogic-client-api/src/main/java/com/marklogic/client/impl/FailedRequest.java
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2010-2025 Progress Software Corporation and/or its subsidiaries or affiliates. All Rights Reserved.
+ * Copyright (c) 2010-2026 Progress Software Corporation and/or its subsidiaries or affiliates. All Rights Reserved.
*/
package com.marklogic.client.impl;
@@ -7,7 +7,6 @@
import java.io.InputStream;
import javax.xml.parsers.DocumentBuilder;
-import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import com.marklogic.client.io.Format;
@@ -49,13 +48,9 @@ private static class FailedRequestXMLParser implements FailedRequestParser {
@Override
public FailedRequest parseFailedRequest(int httpStatus, InputStream is) {
FailedRequest failure = new FailedRequest();
- DocumentBuilderFactory factory = DocumentBuilderFactory
- .newInstance();
- factory.setNamespaceAware(true);
- factory.setValidating(false);
DocumentBuilder builder;
try {
- builder = factory.newDocumentBuilder();
+ builder = XmlFactories.makeNewDocumentBuilder();
Document doc = builder.parse(is);
String statusCode = null;
NodeList statusCodes = doc.getElementsByTagNameNS(ERROR_NS, "status-code");
diff --git a/marklogic-client-api/src/main/java/com/marklogic/client/impl/XmlFactories.java b/marklogic-client-api/src/main/java/com/marklogic/client/impl/XmlFactories.java
index 5f931dfba..8015b27e7 100644
--- a/marklogic-client-api/src/main/java/com/marklogic/client/impl/XmlFactories.java
+++ b/marklogic-client-api/src/main/java/com/marklogic/client/impl/XmlFactories.java
@@ -5,10 +5,13 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+import org.xml.sax.SAXException;
import javax.xml.XMLConstants;
+import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
+import javax.xml.parsers.SAXParserFactory;
import javax.xml.stream.FactoryConfigurationError;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLOutputFactory;
@@ -64,6 +67,30 @@ public static XMLInputFactory makeNewInputFactory() {
return factory;
}
+ /**
+ * Creates a namespace-aware SAX factory configured with secure defaults.
+ * @return a new SAX parser factory
+ */
+ public static SAXParserFactory makeNewSAXParserFactory() {
+ SAXParserFactory factory = SAXParserFactory.newInstance();
+ factory.setNamespaceAware(true);
+ factory.setXIncludeAware(false);
+ setSaxFeature(factory, XMLConstants.FEATURE_SECURE_PROCESSING, true);
+ setSaxFeature(factory, "http://apache.org/xml/features/disallow-doctype-decl", true);
+ setSaxFeature(factory, "http://xml.org/sax/features/external-general-entities", false);
+ setSaxFeature(factory, "http://xml.org/sax/features/external-parameter-entities", false);
+ setSaxFeature(factory, "http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
+ return factory;
+ }
+
+ private static void setSaxFeature(SAXParserFactory factory, String feature, boolean value) {
+ try {
+ factory.setFeature(feature, value);
+ } catch (ParserConfigurationException | SAXException e) {
+ logger.warn("Unable to set {} on SAXParserFactory; cause: {}", feature, e.getMessage());
+ }
+ }
+
public static TransformerFactory makeNewTransformerFactory() {
TransformerFactory factory = TransformerFactory.newInstance();
// Avoids Polaris warning related to https://cwe.mitre.org/data/definitions/611.html .
@@ -127,6 +154,13 @@ private static DocumentBuilderFactory makeNewDocumentBuilderFactory() {
return factory;
}
+ /**
+ * Creates a {@link DocumentBuilder} from a new factory configured with secure defaults.
+ */
+ static DocumentBuilder makeNewDocumentBuilder() throws ParserConfigurationException {
+ return makeNewDocumentBuilderFactory().newDocumentBuilder();
+ }
+
/**
* Returns a shared {@link DocumentBuilderFactory} configured with secure defaults.
*
diff --git a/marklogic-client-api/src/main/java/com/marklogic/client/io/QueryOptionsListHandle.java b/marklogic-client-api/src/main/java/com/marklogic/client/io/QueryOptionsListHandle.java
index e00337bbd..4e14c0955 100644
--- a/marklogic-client-api/src/main/java/com/marklogic/client/io/QueryOptionsListHandle.java
+++ b/marklogic-client-api/src/main/java/com/marklogic/client/io/QueryOptionsListHandle.java
@@ -1,11 +1,10 @@
/*
- * Copyright (c) 2010-2025 Progress Software Corporation and/or its subsidiaries or affiliates. All Rights Reserved.
+ * Copyright (c) 2010-2026 Progress Software Corporation and/or its subsidiaries or affiliates. All Rights Reserved.
*/
package com.marklogic.client.io;
import java.io.InputStream;
import java.io.InputStreamReader;
-import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
@@ -13,12 +12,18 @@
import jakarta.xml.bind.JAXBException;
import jakarta.xml.bind.Marshaller;
import jakarta.xml.bind.Unmarshaller;
+import javax.xml.parsers.ParserConfigurationException;
+import javax.xml.transform.sax.SAXSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+import org.xml.sax.InputSource;
+import org.xml.sax.SAXException;
+import org.xml.sax.XMLReader;
import com.marklogic.client.MarkLogicBindingException;
import com.marklogic.client.MarkLogicIOException;
+import com.marklogic.client.impl.XmlFactories;
import com.marklogic.client.io.marker.OperationNotSupported;
import com.marklogic.client.io.marker.QueryOptionsListReadHandle;
import com.marklogic.client.query.QueryOptionsListBuilder;
@@ -89,9 +94,12 @@ protected Class receiveAs() {
@Override
protected void receiveContent(InputStream content) {
try {
+ XMLReader reader = XmlFactories.makeNewSAXParserFactory().newSAXParser().getXMLReader();
optionsHolder = (QueryOptionsListBuilder.OptionsList) unmarshaller.unmarshal(
- new InputStreamReader(content, StandardCharsets.UTF_8)
- );
+ new SAXSource(reader, new InputSource(new InputStreamReader(content, StandardCharsets.UTF_8))));
+ } catch (ParserConfigurationException | SAXException e) {
+ logger.error("Failed to create XML parser for query options list", e);
+ throw new MarkLogicIOException(e);
} catch (JAXBException e) {
logger.error("Failed to unmarshall query options list",e);
throw new MarkLogicIOException(e);
diff --git a/marklogic-client-api/src/main/java/com/marklogic/client/io/TuplesHandle.java b/marklogic-client-api/src/main/java/com/marklogic/client/io/TuplesHandle.java
index f39ed2716..600b1cd32 100644
--- a/marklogic-client-api/src/main/java/com/marklogic/client/io/TuplesHandle.java
+++ b/marklogic-client-api/src/main/java/com/marklogic/client/io/TuplesHandle.java
@@ -1,25 +1,30 @@
/*
- * Copyright (c) 2010-2025 Progress Software Corporation and/or its subsidiaries or affiliates. All Rights Reserved.
+ * Copyright (c) 2010-2026 Progress Software Corporation and/or its subsidiaries or affiliates. All Rights Reserved.
*/
package com.marklogic.client.io;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
-import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import jakarta.xml.bind.JAXBContext;
import jakarta.xml.bind.JAXBException;
import jakarta.xml.bind.Unmarshaller;
+import javax.xml.parsers.ParserConfigurationException;
+import javax.xml.transform.sax.SAXSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+import org.xml.sax.InputSource;
+import org.xml.sax.SAXException;
+import org.xml.sax.XMLReader;
import com.marklogic.client.MarkLogicBindingException;
import com.marklogic.client.MarkLogicIOException;
import com.marklogic.client.impl.TuplesBuilder;
+import com.marklogic.client.impl.XmlFactories;
import com.marklogic.client.io.marker.OperationNotSupported;
import com.marklogic.client.io.marker.TuplesReadHandle;
import com.marklogic.client.query.AggregateResult;
@@ -92,9 +97,12 @@ protected Class receiveAs() {
@Override
protected void receiveContent(InputStream content) {
try {
+ XMLReader reader = XmlFactories.makeNewSAXParserFactory().newSAXParser().getXMLReader();
tuplesHolder = (TuplesBuilder.Tuples) unmarshaller.unmarshal(
- new InputStreamReader(content, StandardCharsets.UTF_8)
- );
+ new SAXSource(reader, new InputSource(new InputStreamReader(content, StandardCharsets.UTF_8))));
+ } catch (ParserConfigurationException | SAXException e) {
+ logger.error("Failed to create XML parser for tuples", e);
+ throw new MarkLogicIOException(e);
} catch (JAXBException e) {
logger.error("Failed to unmarshall tuples",e);
throw new MarkLogicIOException(e);
diff --git a/marklogic-client-api/src/main/java/com/marklogic/client/io/ValuesHandle.java b/marklogic-client-api/src/main/java/com/marklogic/client/io/ValuesHandle.java
index 2cfdb9d5d..9530ebeb4 100644
--- a/marklogic-client-api/src/main/java/com/marklogic/client/io/ValuesHandle.java
+++ b/marklogic-client-api/src/main/java/com/marklogic/client/io/ValuesHandle.java
@@ -1,25 +1,30 @@
/*
- * Copyright (c) 2010-2025 Progress Software Corporation and/or its subsidiaries or affiliates. All Rights Reserved.
+ * Copyright (c) 2010-2026 Progress Software Corporation and/or its subsidiaries or affiliates. All Rights Reserved.
*/
package com.marklogic.client.io;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
-import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import jakarta.xml.bind.JAXBContext;
import jakarta.xml.bind.JAXBException;
import jakarta.xml.bind.Unmarshaller;
+import javax.xml.parsers.ParserConfigurationException;
+import javax.xml.transform.sax.SAXSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+import org.xml.sax.InputSource;
+import org.xml.sax.SAXException;
+import org.xml.sax.XMLReader;
import com.marklogic.client.MarkLogicBindingException;
import com.marklogic.client.MarkLogicIOException;
import com.marklogic.client.impl.ValuesBuilder;
+import com.marklogic.client.impl.XmlFactories;
import com.marklogic.client.io.marker.OperationNotSupported;
import com.marklogic.client.io.marker.ValuesReadHandle;
import com.marklogic.client.query.AggregateResult;
@@ -97,9 +102,12 @@ protected Class receiveAs() {
@Override
protected void receiveContent(InputStream content) {
try {
+ XMLReader reader = XmlFactories.makeNewSAXParserFactory().newSAXParser().getXMLReader();
valuesHolder = (ValuesBuilder.Values) unmarshaller.unmarshal(
- new InputStreamReader(content, StandardCharsets.UTF_8)
- );
+ new SAXSource(reader, new InputSource(new InputStreamReader(content, StandardCharsets.UTF_8))));
+ } catch (ParserConfigurationException | SAXException e) {
+ logger.error("Failed to create XML parser for values", e);
+ throw new MarkLogicIOException(e);
} catch (JAXBException e) {
logger.error("Failed to unmarshall values",e);
throw new MarkLogicIOException(e);
diff --git a/marklogic-client-api/src/main/java/com/marklogic/client/io/ValuesListHandle.java b/marklogic-client-api/src/main/java/com/marklogic/client/io/ValuesListHandle.java
index 9d3ca8ef7..2b3b11ed6 100644
--- a/marklogic-client-api/src/main/java/com/marklogic/client/io/ValuesListHandle.java
+++ b/marklogic-client-api/src/main/java/com/marklogic/client/io/ValuesListHandle.java
@@ -1,23 +1,28 @@
/*
- * Copyright (c) 2010-2025 Progress Software Corporation and/or its subsidiaries or affiliates. All Rights Reserved.
+ * Copyright (c) 2010-2026 Progress Software Corporation and/or its subsidiaries or affiliates. All Rights Reserved.
*/
package com.marklogic.client.io;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
-import java.io.UnsupportedEncodingException;
import jakarta.xml.bind.JAXBContext;
import jakarta.xml.bind.JAXBException;
import jakarta.xml.bind.Unmarshaller;
+import javax.xml.parsers.ParserConfigurationException;
+import javax.xml.transform.sax.SAXSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+import org.xml.sax.InputSource;
+import org.xml.sax.SAXException;
+import org.xml.sax.XMLReader;
import com.marklogic.client.MarkLogicBindingException;
import com.marklogic.client.MarkLogicIOException;
import com.marklogic.client.impl.ValuesListBuilder;
+import com.marklogic.client.impl.XmlFactories;
import com.marklogic.client.io.marker.OperationNotSupported;
import com.marklogic.client.io.marker.ValuesListReadHandle;
import com.marklogic.client.query.ValuesListResults;
@@ -110,9 +115,12 @@ protected Class receiveAs() {
@Override
protected void receiveContent(InputStream content) {
try {
+ XMLReader reader = XmlFactories.makeNewSAXParserFactory().newSAXParser().getXMLReader();
valuesHolder = (ValuesListBuilder.ValuesList) unmarshaller.unmarshal(
- new InputStreamReader(content, StandardCharsets.UTF_8)
- );
+ new SAXSource(reader, new InputSource(new InputStreamReader(content, StandardCharsets.UTF_8))));
+ } catch (ParserConfigurationException | SAXException e) {
+ logger.error("Failed to create XML parser for values list", e);
+ throw new MarkLogicIOException(e);
} catch (JAXBException e) {
logger.error("Failed to unmarshall values list",e);
throw new MarkLogicIOException(e);
diff --git a/marklogic-client-api/src/test/java/com/marklogic/client/test/FailedRequestTest.java b/marklogic-client-api/src/test/java/com/marklogic/client/test/FailedRequestTest.java
index 7c68a73be..c049b5124 100644
--- a/marklogic-client-api/src/test/java/com/marklogic/client/test/FailedRequestTest.java
+++ b/marklogic-client-api/src/test/java/com/marklogic/client/test/FailedRequestTest.java
@@ -7,16 +7,24 @@
import com.marklogic.client.admin.QueryOptionsManager;
import com.marklogic.client.document.XMLDocumentManager;
import com.marklogic.client.impl.FailedRequest;
+import com.marklogic.client.impl.XmlFactories;
import com.marklogic.client.io.Format;
import com.marklogic.client.io.StringHandle;
import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.io.TempDir;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamWriter;
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
import java.io.StringWriter;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Path;
import static org.junit.jupiter.api.Assertions.*;
@@ -112,4 +120,54 @@ public void testNullFailedRequestInFailedRequestException() {
assertNull(e.getServerMessage());
assertNull(e.getServerStackTrace());
}
+
+ @Test
+ public void testFailedRequestParsingIsIndependentOfSharedFactoryConfiguration() {
+ DocumentBuilderFactory sharedFactory = XmlFactories.getDocumentBuilderFactory();
+ boolean namespaceAware = sharedFactory.isNamespaceAware();
+ try {
+ sharedFactory.setNamespaceAware(false);
+ String xml = "" +
+ "400Bad Request" +
+ "RESTAPI-INVALIDCONTENTInvalid content";
+
+ FailedRequest failure = FailedRequest.getFailedRequest(400, "application/xml",
+ new ByteArrayInputStream(xml.getBytes(StandardCharsets.UTF_8)));
+
+ assertEquals(400, failure.getStatusCode());
+ assertEquals("Bad Request", failure.getStatus());
+ assertEquals("RESTAPI-INVALIDCONTENT", failure.getMessageCode());
+ assertEquals("Invalid content", failure.getMessage());
+ } finally {
+ sharedFactory.setNamespaceAware(namespaceAware);
+ }
+ }
+
+ @Test
+ public void testErrorResponseDoesNotExpandDoctypeEntities() {
+ String xml = "]>" +
+ "&x;";
+
+ FailedRequest failure = FailedRequest.getFailedRequest(400, "application/xml",
+ new ByteArrayInputStream(xml.getBytes(StandardCharsets.UTF_8)));
+
+ assertEquals(400, failure.getStatusCode());
+ assertNotNull(failure.getMessage());
+ assertFalse(failure.getMessage().contains("EXPANDED"));
+ }
+
+ @Test
+ public void testErrorResponseDoesNotExpandExternalEntities(@TempDir Path directory) throws IOException {
+ Path secret = directory.resolve("entity.txt");
+ Files.writeString(secret, "EXTERNAL_ENTITY_CONTENT", StandardCharsets.UTF_8);
+ String xml = "]>" +
+ "&x;";
+
+ FailedRequest failure = FailedRequest.getFailedRequest(400, "application/xml",
+ new ByteArrayInputStream(xml.getBytes(StandardCharsets.UTF_8)));
+
+ assertEquals(400, failure.getStatusCode());
+ assertNotNull(failure.getMessage());
+ assertFalse(failure.getMessage().contains("EXTERNAL_ENTITY_CONTENT"));
+ }
}
diff --git a/marklogic-client-api/src/test/java/com/marklogic/client/test/XmlResponseHandleSecurityTest.java b/marklogic-client-api/src/test/java/com/marklogic/client/test/XmlResponseHandleSecurityTest.java
new file mode 100644
index 000000000..cc3911e82
--- /dev/null
+++ b/marklogic-client-api/src/test/java/com/marklogic/client/test/XmlResponseHandleSecurityTest.java
@@ -0,0 +1,90 @@
+/*
+ * Copyright (c) 2010-2026 Progress Software Corporation and/or its subsidiaries or affiliates. All Rights Reserved.
+ */
+package com.marklogic.client.test;
+
+import com.marklogic.client.MarkLogicIOException;
+import com.marklogic.client.impl.HandleAccessor;
+import com.marklogic.client.io.QueryOptionsListHandle;
+import com.marklogic.client.io.TuplesHandle;
+import com.marklogic.client.io.ValuesHandle;
+import com.marklogic.client.io.ValuesListHandle;
+import com.marklogic.client.io.marker.AbstractReadHandle;
+import org.junit.jupiter.api.Test;
+
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.nio.charset.StandardCharsets;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+class XmlResponseHandleSecurityTest {
+ private static final String REST_NS = "http://marklogic.com/rest-api";
+ private static final String SEARCH_NS = "http://marklogic.com/appservices/search";
+
+ @Test
+ void queryOptionsList() {
+ QueryOptionsListHandle handle = new QueryOptionsListHandle();
+ String xml = "test" +
+ "/u";
+ verify(handle, "query-options", xml);
+ assertEquals("/u", handle.getValuesMap().get("test"));
+ }
+
+ @Test
+ void tuples() {
+ TuplesHandle handle = new TuplesHandle();
+ String xml = "";
+ verify(handle, "values-response", xml);
+ assertEquals("test", handle.getName());
+ }
+
+ @Test
+ void values() {
+ ValuesHandle handle = new ValuesHandle();
+ String xml = "";
+ verify(handle, "values-response", xml);
+ assertEquals("test", handle.getName());
+ assertEquals("xs:string", handle.getType());
+ }
+
+ @Test
+ void valuesList() {
+ ValuesListHandle handle = new ValuesListHandle();
+ String xml = "test" +
+ "/u";
+ verify(handle, "values-list", xml);
+ assertEquals("/u", handle.getValuesMap().get("test"));
+ }
+
+ private void verify(AbstractReadHandle handle, String root, String xml) {
+ CloseTrackingInputStream valid = new CloseTrackingInputStream(xml);
+ HandleAccessor.receiveContent(handle, valid);
+ assertTrue(valid.closed, "valid response stream must be closed");
+
+ String doctype = "]>" + xml.replace("test", "&x;");
+ CloseTrackingInputStream withDoctype = new CloseTrackingInputStream(doctype);
+ assertThrows(MarkLogicIOException.class, () -> HandleAccessor.receiveContent(handle, withDoctype));
+ assertTrue(withDoctype.closed, "DOCTYPE response stream must be closed");
+
+ CloseTrackingInputStream malformed = new CloseTrackingInputStream("<" + root + ">");
+ assertThrows(MarkLogicIOException.class, () -> HandleAccessor.receiveContent(handle, malformed));
+ assertTrue(malformed.closed, "malformed response stream must be closed");
+ }
+
+ private static class CloseTrackingInputStream extends ByteArrayInputStream {
+ private boolean closed;
+
+ CloseTrackingInputStream(String xml) {
+ super(xml.getBytes(StandardCharsets.UTF_8));
+ }
+
+ @Override
+ public void close() throws IOException {
+ closed = true;
+ super.close();
+ }
+ }
+}