From 58cd5662f1e6876f6649735fab1381d2beafdb95 Mon Sep 17 00:00:00 2001 From: Zibran Khan Date: Sun, 15 Nov 2026 15:40:34 +0530 Subject: [PATCH] stop resolving external entities in JAX-WS payload blocks --- .../databinding/impl/DataSourceBlockImpl.java | 4 +- .../databinding/impl/SourceBlockImpl.java | 5 +- .../databinding/impl/XMLStringBlockImpl.java | 4 +- .../impl/PayloadBlockExternalEntityTest.java | 127 ++++++++++++++++++ 4 files changed, 137 insertions(+), 3 deletions(-) create mode 100644 modules/jaxws/test/org/apache/axis2/jaxws/message/databinding/impl/PayloadBlockExternalEntityTest.java diff --git a/modules/jaxws/src/org/apache/axis2/jaxws/message/databinding/impl/DataSourceBlockImpl.java b/modules/jaxws/src/org/apache/axis2/jaxws/message/databinding/impl/DataSourceBlockImpl.java index d8a5eb5eca..97b2339bc9 100644 --- a/modules/jaxws/src/org/apache/axis2/jaxws/message/databinding/impl/DataSourceBlockImpl.java +++ b/modules/jaxws/src/org/apache/axis2/jaxws/message/databinding/impl/DataSourceBlockImpl.java @@ -24,6 +24,7 @@ import org.apache.axiom.om.OMFactory; import org.apache.axiom.om.OMNamespace; import org.apache.axiom.om.OMSourcedElement; +import org.apache.axiom.om.util.StAXParserConfiguration; import org.apache.axiom.om.util.StAXUtils; import org.apache.axiom.soap.SOAP11Constants; import org.apache.axis2.builder.DataSourceBuilder; @@ -123,7 +124,8 @@ protected DataSource _getBOFromOM(OMElement omElement, Void busContext) protected XMLStreamReader _getReaderFromBO(DataSource busObj, Void busContext) throws XMLStreamException, WebServiceException { try { - return StAXUtils.createXMLStreamReader(busObj.getInputStream()); + return StAXUtils.createXMLStreamReader(StAXParserConfiguration.STANDALONE, + busObj.getInputStream()); } catch (Exception e) { String className = (busObj == null) ? "none" : busObj.getClass().getName(); throw ExceptionFactory diff --git a/modules/jaxws/src/org/apache/axis2/jaxws/message/databinding/impl/SourceBlockImpl.java b/modules/jaxws/src/org/apache/axis2/jaxws/message/databinding/impl/SourceBlockImpl.java index 4280d561a8..5b93b6994e 100644 --- a/modules/jaxws/src/org/apache/axis2/jaxws/message/databinding/impl/SourceBlockImpl.java +++ b/modules/jaxws/src/org/apache/axis2/jaxws/message/databinding/impl/SourceBlockImpl.java @@ -24,6 +24,7 @@ import org.apache.axiom.om.OMDataSource; import org.apache.axiom.om.OMElement; import org.apache.axiom.om.OMSourcedElement; +import org.apache.axiom.om.util.StAXParserConfiguration; import org.apache.axiom.om.util.StAXUtils; import org.apache.axiom.soap.SOAP11Constants; import org.apache.axis2.datasource.SourceDataSource; @@ -178,7 +179,9 @@ protected XMLStreamReader _getReaderFromBO(Source busObj, Void busContext) */ if (busObj instanceof StreamSource) { - XMLInputFactory f = StAXUtils.getXMLInputFactory(); + // STANDALONE: the payload comes from the application, so do not + // resolve external entities or load an external DTD while reading it. + XMLInputFactory f = StAXUtils.getXMLInputFactory(StAXParserConfiguration.STANDALONE); return f.createXMLStreamReader(busObj); } diff --git a/modules/jaxws/src/org/apache/axis2/jaxws/message/databinding/impl/XMLStringBlockImpl.java b/modules/jaxws/src/org/apache/axis2/jaxws/message/databinding/impl/XMLStringBlockImpl.java index bdd3bae09d..8bd9d5f85e 100644 --- a/modules/jaxws/src/org/apache/axis2/jaxws/message/databinding/impl/XMLStringBlockImpl.java +++ b/modules/jaxws/src/org/apache/axis2/jaxws/message/databinding/impl/XMLStringBlockImpl.java @@ -25,6 +25,7 @@ import org.apache.axiom.om.OMException; import org.apache.axiom.om.OMSourcedElement; import org.apache.axiom.om.ds.StringOMDataSource; +import org.apache.axiom.om.util.StAXParserConfiguration; import org.apache.axiom.om.util.StAXUtils; import org.apache.axis2.jaxws.ExceptionFactory; import org.apache.axis2.jaxws.message.databinding.XMLStringBlock; @@ -86,7 +87,8 @@ protected String _getBOFromOM(OMElement omElement, Void busContext) protected XMLStreamReader _getReaderFromBO(String busObj, Void busContext) throws XMLStreamException { // Create an XMLStreamReader from the inputFactory using the String as the sources - return StAXUtils.createXMLStreamReader(new StringReader(busObj)); + return StAXUtils.createXMLStreamReader(StAXParserConfiguration.STANDALONE, + new StringReader(busObj)); } @Override diff --git a/modules/jaxws/test/org/apache/axis2/jaxws/message/databinding/impl/PayloadBlockExternalEntityTest.java b/modules/jaxws/test/org/apache/axis2/jaxws/message/databinding/impl/PayloadBlockExternalEntityTest.java new file mode 100644 index 0000000000..0f858bf7c4 --- /dev/null +++ b/modules/jaxws/test/org/apache/axis2/jaxws/message/databinding/impl/PayloadBlockExternalEntityTest.java @@ -0,0 +1,127 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.axis2.jaxws.message.databinding.impl; + +import java.io.File; +import java.io.StringReader; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; + +import javax.xml.stream.XMLStreamConstants; +import javax.xml.stream.XMLStreamReader; +import javax.xml.transform.stream.StreamSource; + +import jakarta.mail.util.ByteArrayDataSource; + +import junit.framework.TestCase; + +import org.apache.axis2.jaxws.message.Block; +import org.apache.axis2.jaxws.message.Message; +import org.apache.axis2.jaxws.message.Protocol; +import org.apache.axis2.jaxws.message.factory.DataSourceBlockFactory; +import org.apache.axis2.jaxws.message.factory.MessageFactory; +import org.apache.axis2.jaxws.message.factory.SourceBlockFactory; +import org.apache.axis2.jaxws.message.factory.XMLStringBlockFactory; +import org.apache.axis2.jaxws.registry.FactoryRegistry; + +/** + * Source, String and DataSource payloads must not have external entities resolved + * when the block is read, since the content is whatever the application handed in. + */ +public class PayloadBlockExternalEntityTest extends TestCase { + + private static final String SECRET = "external-entity-content"; + + private File secretFile; + private String payload; + + @Override + protected void setUp() throws Exception { + secretFile = File.createTempFile("payload-xxe", ".txt"); + Files.write(secretFile.toPath(), SECRET.getBytes(StandardCharsets.UTF_8)); + payload = "]>" + + "&e;"; + } + + @Override + protected void tearDown() throws Exception { + secretFile.delete(); + } + + public void testStreamSourcePayload() throws Exception { + SourceBlockFactory factory = + (SourceBlockFactory) FactoryRegistry.getFactory(SourceBlockFactory.class); + Block block = factory.createFrom(new StreamSource(new StringReader(payload)), null, null); + assertFalse(serialize(block).contains(SECRET)); + } + + public void testStringPayload() throws Exception { + XMLStringBlockFactory factory = + (XMLStringBlockFactory) FactoryRegistry.getFactory(XMLStringBlockFactory.class); + Block block = factory.createFrom(payload, null, null); + assertFalse(serialize(block).contains(SECRET)); + } + + public void testDataSourcePayload() throws Exception { + DataSourceBlockFactory factory = + (DataSourceBlockFactory) FactoryRegistry.getFactory(DataSourceBlockFactory.class); + Block block = factory.createFrom( + new ByteArrayDataSource(payload.getBytes(StandardCharsets.UTF_8), "application/xml"), + null, null); + String text; + try { + XMLStreamReader reader = block.getXMLStreamReader(true); + StringBuilder sb = new StringBuilder(); + while (reader.hasNext()) { + if (reader.next() == XMLStreamConstants.CHARACTERS) { + sb.append(reader.getText()); + } + } + text = sb.toString(); + } catch (Exception e) { + // Refusing the entity is as good as not expanding it. + text = ""; + } + assertFalse(text.contains(SECRET)); + } + + public void testInternalEntityIsStillExpanded() throws Exception { + SourceBlockFactory factory = + (SourceBlockFactory) FactoryRegistry.getFactory(SourceBlockFactory.class); + String internal = "]>&e;"; + Block block = factory.createFrom(new StreamSource(new StringReader(internal)), null, null); + assertTrue(serialize(block).contains("internal-value")); + } + + /** + * Puts the block in a SOAP body, as a Dispatch or Provider would, and serializes the + * message. An exception counts as not expanding the entity. + */ + private static String serialize(Block block) { + MessageFactory mf = (MessageFactory) FactoryRegistry.getFactory(MessageFactory.class); + try { + Message message = mf.create(Protocol.soap11); + message.setBodyBlock(block); + return message.getAsOMElement().toString(); + } catch (Exception e) { + return ""; + } + } +}