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
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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 = "<!DOCTYPE r [<!ENTITY e SYSTEM \"" + secretFile.toURI() + "\">]>"
+ "<r xmlns=\"urn:test\">&e;</r>";
}

@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 = "<!DOCTYPE r [<!ENTITY e \"internal-value\">]><r xmlns=\"urn:test\">&e;</r>";
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 "";
}
}
}