This is an automated email from the ASF dual-hosted git repository.

robertlazarski pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/axis-axis2-java-core.git


The following commit(s) were added to refs/heads/master by this push:
     new 5a7dec7653 Test the hardened transform and DOM call sites
5a7dec7653 is described below

commit 5a7dec7653093aef79561e9e5e040cfc10c2e8bc
Author: Robert Lazarski <[email protected]>
AuthorDate: Fri Nov 13 05:47:38 2026 -1000

    Test the hardened transform and DOM call sites
    
    Cover SAAJ setContent with a SAXSource, ConvertUtils and SourceDataSource
    copying a Reader- or SAX-backed Source, and the codegen type-mapping
    loader. Each test fails against the previous unhardened call site.
    
    Co-Authored-By: Claude Opus 5.5 <[email protected]>
---
 .../codegen/extension/TypeMapperExtensionTest.java | 79 ++++++++++++++++++++++
 .../axis2/datasource/SourceDataSourceTests.java    | 74 ++++++++++++++++++++
 .../axis2/jaxws/utility/ConvertUtilsTests.java     | 65 ++++++++++++++++++
 .../test/org/apache/axis2/saaj/SOAPPartTest.java   | 34 ++++++++++
 4 files changed, 252 insertions(+)

diff --git 
a/modules/codegen/test/org/apache/axis2/wsdl/codegen/extension/TypeMapperExtensionTest.java
 
b/modules/codegen/test/org/apache/axis2/wsdl/codegen/extension/TypeMapperExtensionTest.java
new file mode 100644
index 0000000000..3971f6eea1
--- /dev/null
+++ 
b/modules/codegen/test/org/apache/axis2/wsdl/codegen/extension/TypeMapperExtensionTest.java
@@ -0,0 +1,79 @@
+/*
+ * 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.wsdl.codegen.extension;
+
+import org.apache.axis2.wsdl.codegen.CodeGenConfiguration;
+import org.apache.axis2.wsdl.codegen.CodeGenerationException;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.io.TempDir;
+
+import javax.xml.namespace.QName;
+import java.io.File;
+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.assertThrows;
+
+/**
+ * The type-mapping file is read with the secure DOM factory, so a DOCTYPE is
+ * refused rather than its external entities resolved.
+ */
+public class TypeMapperExtensionTest {
+
+    private static final String MAPPING =
+            "<mappings dbf='adb'><mapping>"
+            + "<qname namespace='urn:test'>Thing</qname>"
+            + "<value>%s</value>"
+            + "</mapping></mappings>";
+
+    @TempDir
+    Path dir;
+
+    private CodeGenConfiguration configure(String mappingFile) throws 
Exception {
+        File file = dir.resolve("mapping.xml").toFile();
+        Files.write(file.toPath(), 
mappingFile.getBytes(StandardCharsets.UTF_8));
+        CodeGenConfiguration configuration = new CodeGenConfiguration();
+        configuration.setDatabindingType("adb");
+        configuration.setTypeMappingFile(file);
+        return configuration;
+    }
+
+    @Test
+    public void testMappingFileIsRead() throws Exception {
+        CodeGenConfiguration configuration =
+                configure(String.format(MAPPING, "com.example.Thing"));
+        new TypeMapperExtension().engage(configuration);
+        assertEquals("com.example.Thing", configuration.getTypeMapper()
+                .getAllMappedNames().get(new QName("urn:test", "Thing")));
+    }
+
+    @Test
+    public void testMappingFileWithDoctypeIsRefused() throws Exception {
+        Path secret = dir.resolve("secret.txt");
+        Files.write(secret, 
"com.example.Secret".getBytes(StandardCharsets.UTF_8));
+        String mapping = "<!DOCTYPE mappings [<!ENTITY xxe SYSTEM '" + 
secret.toUri() + "'>]>"
+                + String.format(MAPPING, "&xxe;");
+        CodeGenConfiguration configuration = configure(mapping);
+        assertThrows(CodeGenerationException.class,
+                () -> new TypeMapperExtension().engage(configuration));
+    }
+}
diff --git 
a/modules/jaxws/test/org/apache/axis2/datasource/SourceDataSourceTests.java 
b/modules/jaxws/test/org/apache/axis2/datasource/SourceDataSourceTests.java
new file mode 100644
index 0000000000..79fa97c997
--- /dev/null
+++ b/modules/jaxws/test/org/apache/axis2/datasource/SourceDataSourceTests.java
@@ -0,0 +1,74 @@
+/*
+ * 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.datasource;
+
+import java.io.File;
+import java.io.FileWriter;
+import java.io.StringReader;
+
+import javax.xml.stream.XMLStreamReader;
+import javax.xml.transform.sax.SAXSource;
+
+import junit.framework.TestCase;
+
+import org.apache.axiom.om.OMException;
+import org.xml.sax.InputSource;
+
+/**
+ * A SAXSource without its own XMLReader is copied through a transformer, which
+ * parses it with its own parser.
+ */
+public class SourceDataSourceTests extends TestCase {
+
+    private static final String MARKER = "JAXWS_XXE_SECRET_MARKER";
+
+    private static String text(SAXSource source) throws Exception {
+        XMLStreamReader reader = new SourceDataSource(source).getReader();
+        StringBuilder text = new StringBuilder();
+        while (reader.hasNext()) {
+            if (reader.next() == XMLStreamReader.CHARACTERS) {
+                text.append(reader.getText());
+            }
+        }
+        return text.toString();
+    }
+
+    public void testExternalEntityIsNotResolved() throws Exception {
+        File secret = File.createTempFile("jaxws-xxe", ".txt");
+        secret.deleteOnExit();
+        try (FileWriter w = new FileWriter(secret)) {
+            w.write(MARKER);
+        }
+        String xml = "<?xml version='1.0'?>"
+                + "<!DOCTYPE a [<!ENTITY xxe SYSTEM '" + secret.toURI() + 
"'>]>"
+                + "<a>&xxe;</a>";
+        try {
+            String out = text(new SAXSource(new InputSource(new 
StringReader(xml))));
+            assertFalse("external entity was resolved: " + out, 
out.contains(MARKER));
+        } catch (OMException expected) {
+            // Refusing the document outright is equally acceptable.
+        }
+    }
+
+    public void testPlainDocumentIsRead() throws Exception {
+        String out = text(new SAXSource(new InputSource(new 
StringReader("<a><b>text</b></a>"))));
+        assertEquals("text", out);
+    }
+}
diff --git 
a/modules/jaxws/test/org/apache/axis2/jaxws/utility/ConvertUtilsTests.java 
b/modules/jaxws/test/org/apache/axis2/jaxws/utility/ConvertUtilsTests.java
new file mode 100644
index 0000000000..ad03d09fd2
--- /dev/null
+++ b/modules/jaxws/test/org/apache/axis2/jaxws/utility/ConvertUtilsTests.java
@@ -0,0 +1,65 @@
+/*
+ * 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.utility;
+
+import java.io.File;
+import java.io.FileWriter;
+import java.io.StringReader;
+import java.nio.charset.StandardCharsets;
+
+import javax.xml.transform.stream.StreamSource;
+
+import jakarta.xml.ws.WebServiceException;
+import junit.framework.TestCase;
+
+/**
+ * A StreamSource backed by a Reader has no InputStream to copy, so convert()
+ * copies it through a transformer, which parses it with its own parser.
+ */
+public class ConvertUtilsTests extends TestCase {
+
+    private static final String MARKER = "JAXWS_XXE_SECRET_MARKER";
+
+    public void testSourceToBytesDoesNotResolveExternalEntity() throws 
Exception {
+        File secret = File.createTempFile("jaxws-xxe", ".txt");
+        secret.deleteOnExit();
+        try (FileWriter w = new FileWriter(secret)) {
+            w.write(MARKER);
+        }
+        String xml = "<?xml version='1.0'?>"
+                + "<!DOCTYPE a [<!ENTITY xxe SYSTEM '" + secret.toURI() + 
"'>]>"
+                + "<a>&xxe;</a>";
+        try {
+            byte[] bytes = (byte[]) ConvertUtils.convert(
+                    new StreamSource(new StringReader(xml)), byte[].class);
+            String out = new String(bytes, StandardCharsets.UTF_8);
+            assertFalse("external entity was resolved: " + out, 
out.contains(MARKER));
+        } catch (WebServiceException expected) {
+            // Refusing the document outright is equally acceptable.
+        }
+    }
+
+    public void testSourceToBytesCopiesPlainDocument() throws Exception {
+        byte[] bytes = (byte[]) ConvertUtils.convert(
+                new StreamSource(new StringReader("<a><b>text</b></a>")), 
byte[].class);
+        String out = new String(bytes, StandardCharsets.UTF_8);
+        assertTrue(out, out.contains("<b>text</b>"));
+    }
+}
diff --git a/modules/saaj/test/org/apache/axis2/saaj/SOAPPartTest.java 
b/modules/saaj/test/org/apache/axis2/saaj/SOAPPartTest.java
index b24b87faa4..f016aa2ec5 100644
--- a/modules/saaj/test/org/apache/axis2/saaj/SOAPPartTest.java
+++ b/modules/saaj/test/org/apache/axis2/saaj/SOAPPartTest.java
@@ -25,6 +25,7 @@ import org.junit.runner.RunWith;
 import org.w3c.dom.Document;
 import org.w3c.dom.Node;
 import org.w3c.dom.NodeList;
+import org.xml.sax.InputSource;
 
 import javax.xml.parsers.DocumentBuilder;
 import javax.xml.parsers.DocumentBuilderFactory;
@@ -40,6 +41,7 @@ import jakarta.xml.soap.SOAPException;
 import jakarta.xml.soap.SOAPPart;
 import jakarta.xml.soap.Text;
 import javax.xml.transform.dom.DOMSource;
+import javax.xml.transform.sax.SAXSource;
 import javax.xml.transform.stream.StreamSource;
 import java.io.ByteArrayOutputStream;
 import java.io.File;
@@ -116,6 +118,38 @@ public class SOAPPartTest extends Assert {
                     out.toString("UTF-8").contains(marker));
     }
 
+    /**
+     * A SAXSource without its own XMLReader is parsed by the transformer that
+     * setContent copies it through, not by the hardened StAX factory.
+     */
+    @Test
+    public void testSetContentSAXSourceRejectsExternalEntity() throws 
Exception {
+        File secret = File.createTempFile("saaj-xxe", ".txt");
+        secret.deleteOnExit();
+        String marker = "SAAJ_XXE_SECRET_MARKER";
+        try (FileWriter w = new FileWriter(secret)) {
+            w.write(marker);
+        }
+        String xml =
+                "<?xml version=\"1.0\"?>\n" +
+                "<!DOCTYPE x [ <!ENTITY xxe SYSTEM \"" + secret.toURI() + "\"> 
]>\n" +
+                "<soapenv:Envelope 
xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\";>" +
+                
"<soapenv:Body><item>&xxe;</item></soapenv:Body></soapenv:Envelope>";
+
+        SOAPMessage message = MessageFactory.newInstance().createMessage();
+        SOAPPart soapPart = message.getSOAPPart();
+        try {
+            soapPart.setContent(new SAXSource(new InputSource(new 
StringReader(xml))));
+            message.saveChanges();
+        } catch (SOAPException expected) {
+            return;
+        }
+        ByteArrayOutputStream out = new ByteArrayOutputStream();
+        message.writeTo(out);
+        assertFalse("external entity must not be expanded into the message",
+                    out.toString("UTF-8").contains(marker));
+    }
+
     public void getContents(Iterator iterator, String indent) {
         while (iterator.hasNext()) {
             Node node = (Node)iterator.next();

Reply via email to