From e0c090b95418758ce300ee3f25f543b6bc8ba666 Mon Sep 17 00:00:00 2001 From: Hemanth Madala Date: Mon, 23 Nov 2026 08:46:15 +0530 Subject: [PATCH 1/4] Disable DTD processing when parsing server XML responses (CWE-827) --- .../java/com/marklogic/client/impl/FailedRequest.java | 7 +------ .../com/marklogic/client/io/QueryOptionsListHandle.java | 9 ++++++++- .../main/java/com/marklogic/client/io/TuplesHandle.java | 9 ++++++++- .../main/java/com/marklogic/client/io/ValuesHandle.java | 9 ++++++++- .../java/com/marklogic/client/io/ValuesListHandle.java | 9 ++++++++- 5 files changed, 33 insertions(+), 10 deletions(-) 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 00ca7c3be6..74a1536ad2 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 @@ -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.getDocumentBuilderFactory().newDocumentBuilder(); 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/io/QueryOptionsListHandle.java b/marklogic-client-api/src/main/java/com/marklogic/client/io/QueryOptionsListHandle.java index e00337bbd0..dea760a7f3 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 @@ -13,12 +13,15 @@ import jakarta.xml.bind.JAXBException; import jakarta.xml.bind.Marshaller; import jakarta.xml.bind.Unmarshaller; +import javax.xml.stream.XMLStreamException; +import javax.xml.stream.XMLStreamReader; import org.slf4j.Logger; import org.slf4j.LoggerFactory; 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 +92,13 @@ protected Class receiveAs() { @Override protected void receiveContent(InputStream content) { try { - optionsHolder = (QueryOptionsListBuilder.OptionsList) unmarshaller.unmarshal( + XMLStreamReader reader = XmlFactories.makeNewInputFactory().createXMLStreamReader( new InputStreamReader(content, StandardCharsets.UTF_8) ); + optionsHolder = (QueryOptionsListBuilder.OptionsList) unmarshaller.unmarshal(reader); + } catch (XMLStreamException e) { + logger.error("Failed to parse 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 f39ed27161..324589cad8 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 @@ -13,6 +13,8 @@ import jakarta.xml.bind.JAXBContext; import jakarta.xml.bind.JAXBException; import jakarta.xml.bind.Unmarshaller; +import javax.xml.stream.XMLStreamException; +import javax.xml.stream.XMLStreamReader; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -20,6 +22,7 @@ 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 +95,13 @@ protected Class receiveAs() { @Override protected void receiveContent(InputStream content) { try { - tuplesHolder = (TuplesBuilder.Tuples) unmarshaller.unmarshal( + XMLStreamReader reader = XmlFactories.makeNewInputFactory().createXMLStreamReader( new InputStreamReader(content, StandardCharsets.UTF_8) ); + tuplesHolder = (TuplesBuilder.Tuples) unmarshaller.unmarshal(reader); + } catch (XMLStreamException e) { + logger.error("Failed to parse 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 2cfdb9d5d7..6ced72c989 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 @@ -13,6 +13,8 @@ import jakarta.xml.bind.JAXBContext; import jakarta.xml.bind.JAXBException; import jakarta.xml.bind.Unmarshaller; +import javax.xml.stream.XMLStreamException; +import javax.xml.stream.XMLStreamReader; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -20,6 +22,7 @@ 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 +100,13 @@ protected Class receiveAs() { @Override protected void receiveContent(InputStream content) { try { - valuesHolder = (ValuesBuilder.Values) unmarshaller.unmarshal( + XMLStreamReader reader = XmlFactories.makeNewInputFactory().createXMLStreamReader( new InputStreamReader(content, StandardCharsets.UTF_8) ); + valuesHolder = (ValuesBuilder.Values) unmarshaller.unmarshal(reader); + } catch (XMLStreamException e) { + logger.error("Failed to parse 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 9d3ca8ef7d..393d56af2a 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 @@ -11,6 +11,8 @@ import jakarta.xml.bind.JAXBContext; import jakarta.xml.bind.JAXBException; import jakarta.xml.bind.Unmarshaller; +import javax.xml.stream.XMLStreamException; +import javax.xml.stream.XMLStreamReader; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -18,6 +20,7 @@ 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 +113,13 @@ protected Class receiveAs() { @Override protected void receiveContent(InputStream content) { try { - valuesHolder = (ValuesListBuilder.ValuesList) unmarshaller.unmarshal( + XMLStreamReader reader = XmlFactories.makeNewInputFactory().createXMLStreamReader( new InputStreamReader(content, StandardCharsets.UTF_8) ); + valuesHolder = (ValuesListBuilder.ValuesList) unmarshaller.unmarshal(reader); + } catch (XMLStreamException e) { + logger.error("Failed to parse values list", e); + throw new MarkLogicIOException(e); } catch (JAXBException e) { logger.error("Failed to unmarshall values list",e); throw new MarkLogicIOException(e); From 5198aef223fed3317968e6df44464bab5c88123d Mon Sep 17 00:00:00 2001 From: Hemanth Madala Date: Mon, 23 Nov 2026 10:53:30 +0530 Subject: [PATCH 2/4] Close input stream in QueryOptionsListHandle after parsing --- .../com/marklogic/client/io/QueryOptionsListHandle.java | 7 +++++++ 1 file changed, 7 insertions(+) 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 dea760a7f3..ff0b9dc508 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 @@ -3,6 +3,7 @@ */ package com.marklogic.client.io; +import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.UnsupportedEncodingException; @@ -102,6 +103,12 @@ protected void receiveContent(InputStream content) { } catch (JAXBException e) { logger.error("Failed to unmarshall query options list",e); throw new MarkLogicIOException(e); + } finally { + try { + content.close(); + } catch (IOException e) { + // ignore. + } } } From 35e8d338e10944c7287677519ca6963b303fcbfd Mon Sep 17 00:00:00 2001 From: Hemanth Madala Date: Mon, 23 Nov 2026 20:43:47 +0530 Subject: [PATCH 3/4] Harden XML error response parsing Use a fresh secure DOM builder for error parsing and avoid closing the query options input stream. Add a regression test for shared factory configuration. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- .../marklogic/client/impl/FailedRequest.java | 2 +- .../marklogic/client/impl/XmlFactories.java | 8 ++++++ .../client/io/QueryOptionsListHandle.java | 7 ----- .../client/test/FailedRequestTest.java | 26 +++++++++++++++++++ 4 files changed, 35 insertions(+), 8 deletions(-) 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 74a1536ad2..e54e797797 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 @@ -50,7 +50,7 @@ public FailedRequest parseFailedRequest(int httpStatus, InputStream is) { FailedRequest failure = new FailedRequest(); DocumentBuilder builder; try { - builder = XmlFactories.getDocumentBuilderFactory().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 5f931dfba2..907ac98200 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 @@ -7,6 +7,7 @@ import org.slf4j.LoggerFactory; import javax.xml.XMLConstants; +import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import javax.xml.stream.FactoryConfigurationError; @@ -127,6 +128,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 ff0b9dc508..dea760a7f3 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 @@ -3,7 +3,6 @@ */ package com.marklogic.client.io; -import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.UnsupportedEncodingException; @@ -103,12 +102,6 @@ protected void receiveContent(InputStream content) { } catch (JAXBException e) { logger.error("Failed to unmarshall query options list",e); throw new MarkLogicIOException(e); - } finally { - try { - content.close(); - } catch (IOException e) { - // ignore. - } } } 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 7c68a73be6..0a8802fa45 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,20 @@ 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.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.StringWriter; +import java.nio.charset.StandardCharsets; import static org.junit.jupiter.api.Assertions.*; @@ -112,4 +116,26 @@ 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); + } + } } From 4d4d2073261a3f362329cdcc188d44fe3ff73cba Mon Sep 17 00:00:00 2001 From: Hemanth Madala Date: Wed, 2 Dec 2026 08:53:21 +0530 Subject: [PATCH 4/4] Adressing review comments --- .../marklogic/client/impl/FailedRequest.java | 2 +- .../marklogic/client/impl/XmlFactories.java | 26 ++++++ .../client/io/QueryOptionsListHandle.java | 21 ++--- .../com/marklogic/client/io/TuplesHandle.java | 21 ++--- .../com/marklogic/client/io/ValuesHandle.java | 21 ++--- .../marklogic/client/io/ValuesListHandle.java | 21 ++--- .../client/test/FailedRequestTest.java | 32 +++++++ .../test/XmlResponseHandleSecurityTest.java | 90 +++++++++++++++++++ 8 files changed, 193 insertions(+), 41 deletions(-) create mode 100644 marklogic-client-api/src/test/java/com/marklogic/client/test/XmlResponseHandleSecurityTest.java 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 e54e797797..71b94eb29f 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; 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 907ac98200..8015b27e7b 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,11 +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; @@ -65,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 . 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 dea760a7f3..4e14c0955f 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,11 +12,14 @@ import jakarta.xml.bind.JAXBException; import jakarta.xml.bind.Marshaller; import jakarta.xml.bind.Unmarshaller; -import javax.xml.stream.XMLStreamException; -import javax.xml.stream.XMLStreamReader; +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; @@ -92,12 +94,11 @@ protected Class receiveAs() { @Override protected void receiveContent(InputStream content) { try { - XMLStreamReader reader = XmlFactories.makeNewInputFactory().createXMLStreamReader( - new InputStreamReader(content, StandardCharsets.UTF_8) - ); - optionsHolder = (QueryOptionsListBuilder.OptionsList) unmarshaller.unmarshal(reader); - } catch (XMLStreamException e) { - logger.error("Failed to parse query options list", e); + XMLReader reader = XmlFactories.makeNewSAXParserFactory().newSAXParser().getXMLReader(); + optionsHolder = (QueryOptionsListBuilder.OptionsList) unmarshaller.unmarshal( + 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); 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 324589cad8..600b1cd327 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,23 +1,25 @@ /* - * 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.stream.XMLStreamException; -import javax.xml.stream.XMLStreamReader; +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; @@ -95,12 +97,11 @@ protected Class receiveAs() { @Override protected void receiveContent(InputStream content) { try { - XMLStreamReader reader = XmlFactories.makeNewInputFactory().createXMLStreamReader( - new InputStreamReader(content, StandardCharsets.UTF_8) - ); - tuplesHolder = (TuplesBuilder.Tuples) unmarshaller.unmarshal(reader); - } catch (XMLStreamException e) { - logger.error("Failed to parse tuples", e); + XMLReader reader = XmlFactories.makeNewSAXParserFactory().newSAXParser().getXMLReader(); + tuplesHolder = (TuplesBuilder.Tuples) unmarshaller.unmarshal( + 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); 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 6ced72c989..9530ebeb4f 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,23 +1,25 @@ /* - * 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.stream.XMLStreamException; -import javax.xml.stream.XMLStreamReader; +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; @@ -100,12 +102,11 @@ protected Class receiveAs() { @Override protected void receiveContent(InputStream content) { try { - XMLStreamReader reader = XmlFactories.makeNewInputFactory().createXMLStreamReader( - new InputStreamReader(content, StandardCharsets.UTF_8) - ); - valuesHolder = (ValuesBuilder.Values) unmarshaller.unmarshal(reader); - } catch (XMLStreamException e) { - logger.error("Failed to parse values", e); + XMLReader reader = XmlFactories.makeNewSAXParserFactory().newSAXParser().getXMLReader(); + valuesHolder = (ValuesBuilder.Values) unmarshaller.unmarshal( + 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); 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 393d56af2a..2b3b11ed62 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,21 +1,23 @@ /* - * 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.stream.XMLStreamException; -import javax.xml.stream.XMLStreamReader; +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; @@ -113,12 +115,11 @@ protected Class receiveAs() { @Override protected void receiveContent(InputStream content) { try { - XMLStreamReader reader = XmlFactories.makeNewInputFactory().createXMLStreamReader( - new InputStreamReader(content, StandardCharsets.UTF_8) - ); - valuesHolder = (ValuesListBuilder.ValuesList) unmarshaller.unmarshal(reader); - } catch (XMLStreamException e) { - logger.error("Failed to parse values list", e); + XMLReader reader = XmlFactories.makeNewSAXParserFactory().newSAXParser().getXMLReader(); + valuesHolder = (ValuesListBuilder.ValuesList) unmarshaller.unmarshal( + 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); 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 0a8802fa45..c049b5124a 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 @@ -11,6 +11,7 @@ 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; @@ -19,8 +20,11 @@ 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.*; @@ -138,4 +142,32 @@ public void testFailedRequestParsingIsIndependentOfSharedFactoryConfiguration() 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 0000000000..cc3911e825 --- /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(); + } + } +}