Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
/*
* 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;

import java.io.IOException;
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;
Expand Down Expand Up @@ -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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 .
Expand Down Expand Up @@ -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.
* <p>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,29 @@
/*
* 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;

import jakarta.xml.bind.JAXBContext;
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;
Expand Down Expand Up @@ -89,9 +94,12 @@ protected Class<InputStream> 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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -92,9 +97,12 @@ protected Class<InputStream> 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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -97,9 +102,12 @@ protected Class<InputStream> 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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -110,9 +115,12 @@ protected Class<InputStream> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;

Expand Down Expand Up @@ -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 = "<error xmlns=\"http://marklogic.com/xdmp/error\">" +
"<status-code>400</status-code><status>Bad Request</status>" +
"<message-code>RESTAPI-INVALIDCONTENT</message-code><message>Invalid content</message></error>";

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 = "<!DOCTYPE error [<!ENTITY x 'EXPANDED'>]>" +
"<error xmlns=\"http://marklogic.com/xdmp/error\"><message>&x;</message></error>";

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 = "<!DOCTYPE error [<!ENTITY x SYSTEM \"" + secret.toUri() + "\">]>" +
"<error xmlns=\"http://marklogic.com/xdmp/error\"><message>&x;</message></error>";

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"));
}
}
Loading
Loading