From 30cdcc73e33286565cfe690ed5b6d3afca7dbaab Mon Sep 17 00:00:00 2001 From: ashokrprogress Date: Tue, 1 Dec 2026 12:13:38 +0530 Subject: [PATCH] MLE-33478: Fix DOCTYPE handling in DOMHandle Reject DOCTYPE declarations on the LSParser used to read XML, closing the gap where DocumentBuilderFactory protections did not apply. Add regression tests for buffer parsing and resolver-configured stream parsing, including stream closure on rejection. --- .../com/marklogic/client/io/DOMHandle.java | 6 +++ .../client/test/ClosingHandlesTest.java | 45 ++++++++++++++++++- 2 files changed, 50 insertions(+), 1 deletion(-) diff --git a/marklogic-client-api/src/main/java/com/marklogic/client/io/DOMHandle.java b/marklogic-client-api/src/main/java/com/marklogic/client/io/DOMHandle.java index a19dd138e4..b97b06bd49 100644 --- a/marklogic-client-api/src/main/java/com/marklogic/client/io/DOMHandle.java +++ b/marklogic-client-api/src/main/java/com/marklogic/client/io/DOMHandle.java @@ -349,6 +349,11 @@ public byte[] contentToBytes(Document content) { throw new MarkLogicIOException(e); } } + /** + * Parses the stream as XML, rejecting DOCTYPE declarations even when a resource resolver is set. + * @param serialization the input stream, which is closed after parsing + * @return the parsed document, or null if the stream is null + */ @Override public Document toContent(InputStream serialization) { if (serialization == null) return null; @@ -365,6 +370,7 @@ public Document toContent(InputStream serialization) { DOMImplementationLS domImpl = (DOMImplementationLS) factory.newDocumentBuilder().getDOMImplementation(); LSParser parser = domImpl.createLSParser(DOMImplementationLS.MODE_SYNCHRONOUS, null); + parser.getDomConfig().setParameter("disallow-doctype", Boolean.TRUE); if (resolver != null) { parser.getDomConfig().setParameter("resource-resolver", resolver); } diff --git a/marklogic-client-api/src/test/java/com/marklogic/client/test/ClosingHandlesTest.java b/marklogic-client-api/src/test/java/com/marklogic/client/test/ClosingHandlesTest.java index 10de2c2392..7deba707fc 100644 --- a/marklogic-client-api/src/test/java/com/marklogic/client/test/ClosingHandlesTest.java +++ b/marklogic-client-api/src/test/java/com/marklogic/client/test/ClosingHandlesTest.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.test; @@ -10,12 +10,21 @@ import com.marklogic.client.io.*; import com.marklogic.client.io.marker.AbstractReadHandle; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; +import org.w3c.dom.ls.LSException; import jakarta.xml.bind.JAXBContext; +import javax.xml.parsers.DocumentBuilderFactory; import java.io.ByteArrayInputStream; import java.io.Closeable; import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; public class ClosingHandlesTest { @@ -36,6 +45,40 @@ public boolean isClosed() { } } + @Test + public void domHandleRejectsDoctypeInBuffer() { + DOMHandle handle = new DOMHandle(); + byte[] xml = "".getBytes(StandardCharsets.UTF_8); + LSException error = assertThrows(LSException.class, () -> handle.fromBuffer(xml)); + assertEquals(LSException.PARSE_ERR, error.code); + } + + @Test + public void domHandleClosesStreamWhenRejectingDoctypeWithResolver() { + DOMHandle handle = new DOMHandle(); + handle.setResolver((type, namespace, publicId, systemId, baseUri) -> { + throw new AssertionError("The resolver must not be consulted for a rejected DOCTYPE; systemId: " + systemId); + }); + MockInputStream stream = new MockInputStream( + "".getBytes(StandardCharsets.UTF_8)); + LSException error = assertThrows(LSException.class, () -> HandleAccessor.receiveContent(handle, stream)); + assertEquals(LSException.PARSE_ERR, error.code); + assertTrue(stream.isClosed()); + } + + @Test + public void domHandleRejectsXxeWithJdkParser(@TempDir Path dir) throws Exception { + Path secret = Files.writeString(dir.resolve("secret.txt"), "TOP-SECRET"); + byte[] xml = ("]>&x;") + .getBytes(StandardCharsets.UTF_8); + DOMHandle handle = new DOMHandle(); + // The test classpath includes Apache Xerces; use the JDK's built-in parser, which applications get by default. + handle.setFactory(DocumentBuilderFactory.newDefaultInstance()); + LSException error = assertThrows(LSException.class, () -> handle.fromBuffer(xml)); + assertEquals(LSException.PARSE_ERR, error.code); + assertNull(handle.get()); + } + @Test public void closeHandles() throws Exception { JAXBContext context = JAXBContext.newInstance(City.class);