This is an automated email from the ASF dual-hosted git repository. coheigea pushed a commit to branch coheigea/schema-imports in repository https://gitbox.apache.org/repos/asf/cxf.git
commit d363f3604a2f62c6ab0cdb27dc22ad8ab31b087b Author: Colm O hEigeartaigh <[email protected]> AuthorDate: Mon Nov 30 15:04:17 2026 +0000 Apply the allowed URL schemes to schema imports and WSDL import locations --- .../cxf/ws/addressing/EndpointReferenceUtils.java | 12 +- .../ws/addressing/EndpointReferenceUtilsTest.java | 81 ++++++++++++-- .../cxf/wsdl11/AbstractWrapperWSDLLocator.java | 16 +-- .../cxf/wsdl11/AbstractWrapperWSDLLocatorTest.java | 121 +++++++++++++++++++++ 4 files changed, 209 insertions(+), 21 deletions(-) diff --git a/core/src/main/java/org/apache/cxf/ws/addressing/EndpointReferenceUtils.java b/core/src/main/java/org/apache/cxf/ws/addressing/EndpointReferenceUtils.java index b6604527d4d..193b4f0e943 100644 --- a/core/src/main/java/org/apache/cxf/ws/addressing/EndpointReferenceUtils.java +++ b/core/src/main/java/org/apache/cxf/ws/addressing/EndpointReferenceUtils.java @@ -207,6 +207,12 @@ public final class EndpointReferenceUtils { if (systemId == null) { systemId = publicId; } + if (systemId != null && !URIResolver.isAllowedReference(systemId, baseURI)) { + // Return an empty input rather than null: for null, a SchemaFactory that does not + // support ACCESS_EXTERNAL_SCHEMA (e.g. Apache Xerces) would fetch the schema itself + LOG.warning("Not resolving Schema " + systemId + " as its URL scheme is not allowed"); + return createInput(systemId, new byte[0]); + } if (systemId != null) { // Run inside doPrivileged so that sm.checkPermission() calls // inside the resolver chain (SecurityActions.fileExists) stop @@ -600,8 +606,10 @@ public final class EndpointReferenceUtils { } catch (Exception ex) { - // Something not right with the schema from the wsdl. - LOG.log(Level.WARNING, "SAXException for newSchema()", ex); + // Something not right with the schema from the wsdl. Callers set this null schema on + // the readers and writers, so schema validation is not performed for the service. + LOG.log(Level.SEVERE, "Could not build the schema for service " + serviceInfo.getName() + + ", so schema validation will NOT be performed for it", ex); for (SchemaInfo schemaInfo : serviceInfo.getSchemas()) { String s = StaxUtils.toString(schemaInfo.getElement(), 4); LOG.log(Level.INFO, "Schema for: " + schemaInfo.getNamespaceURI() + "\n" + s); diff --git a/core/src/test/java/org/apache/cxf/ws/addressing/EndpointReferenceUtilsTest.java b/core/src/test/java/org/apache/cxf/ws/addressing/EndpointReferenceUtilsTest.java index 83435714a89..b919aa01b4e 100644 --- a/core/src/test/java/org/apache/cxf/ws/addressing/EndpointReferenceUtilsTest.java +++ b/core/src/test/java/org/apache/cxf/ws/addressing/EndpointReferenceUtilsTest.java @@ -49,6 +49,7 @@ import static org.junit.Assert.assertTrue; public class EndpointReferenceUtilsTest { private static final String NAMESPACE = "urn:test:endpoint:reference:utils"; private static final String MEMORY_SYSTEM_ID = "memory:/schema.xsd"; + private static final String ALLOWED_SCHEMES_PROPERTY = "org.apache.cxf.resource.uriresolver.allowedSchemes"; @Rule public TemporaryFolder tempFolder = new TemporaryFolder(); @@ -125,6 +126,62 @@ public class EndpointReferenceUtilsTest { } } + @Test + public void testGetSchemaDoesNotFetchArchiveImportWithDisallowedInnerScheme() throws Exception { + try (LocalHttpProbeServer probeServer = new LocalHttpProbeServer()) { + // "jar" is an allowed scheme, but "ftp" is not, so the archive must not be fetched + ServiceInfo serviceInfo = createServiceInfoWithImport( + "jar:ftp://127.0.0.1:" + probeServer.getPort() + "/imported.jar!/imported.xsd"); + + EndpointReferenceUtils.getSchema(serviceInfo, null); + + probeServer.awaitCompletion(); + assertFalse("Archive with a disallowed inner scheme should not be fetched", + probeServer.wasConnected()); + } + } + + @Test + public void testGetSchemaFetchesImportWithConfiguredAllowedScheme() throws Exception { + // The fallback honours the configured allowed schemes, not a fixed rule + String previous = System.getProperty(ALLOWED_SCHEMES_PROPERTY); + System.setProperty(ALLOWED_SCHEMES_PROPERTY, "ftp"); + try (LocalHttpProbeServer probeServer = new LocalHttpProbeServer()) { + ServiceInfo serviceInfo = createServiceInfoWithImport( + "ftp://127.0.0.1:" + probeServer.getPort() + "/imported.xsd"); + + EndpointReferenceUtils.getSchema(serviceInfo, null); + + probeServer.awaitCompletion(); + assertTrue("Import with a configured allowed scheme should be fetched", probeServer.wasConnected()); + } finally { + if (previous == null) { + System.clearProperty(ALLOWED_SCHEMES_PROPERTY); + } else { + System.setProperty(ALLOWED_SCHEMES_PROPERTY, previous); + } + } + } + + /** + * Only the SchemaInfo element carries the import, so it is not loaded into the XmlSchemaCollection + * and is resolved through the last-resort fallback of the SchemaLSResourceResolver. + */ + private ServiceInfo createServiceInfoWithImport(String schemaLocation) throws Exception { + String schemaText = + "<xsd:schema xmlns:xsd='http://www.w3.org/2001/XMLSchema' " + + "targetNamespace='" + NAMESPACE + "' elementFormDefault='qualified'>" + + "<xsd:import namespace='urn:test:endpoint:reference:utils:imported' " + + "schemaLocation='" + schemaLocation + "'/>" + + "<xsd:element name='value' type='xsd:string'/>" + + "</xsd:schema>"; + Document doc = StaxUtils.read(new ByteArrayInputStream(schemaText.getBytes(StandardCharsets.UTF_8))); + + ServiceInfo serviceInfo = createServiceInfo(MEMORY_SYSTEM_ID); + serviceInfo.getSchemas().iterator().next().setElement(doc.getDocumentElement()); + return serviceInfo; + } + private File writeSchemaWithExternalDtd(String name, String namespace, int port) throws Exception { String schemaText = "<?xml version='1.0'?>" @@ -167,19 +224,25 @@ public class EndpointReferenceUtilsTest { private LocalHttpProbeServer() throws Exception { this.serverSocket = new ServerSocket(0); this.serverSocket.setSoTimeout(750); - this.thread = new Thread(this::acceptOneConnection, "EndpointReferenceUtilsTest-HttpProbe"); + this.thread = new Thread(this::acceptConnections, "EndpointReferenceUtilsTest-HttpProbe"); this.thread.setDaemon(true); this.thread.start(); } - private void acceptOneConnection() { - try (Socket socket = serverSocket.accept()) { - connected.set(true); - OutputStream out = socket.getOutputStream(); - out.write("HTTP/1.1 404 Not Found\r\nContent-Length: 0\r\n\r\n".getBytes(StandardCharsets.US_ASCII)); - out.flush(); - } catch (Exception ex) { - // timeout/no connection is expected for this test + // Answer every connection until none arrives within the accept timeout, so that a second + // request cannot block the test + private void acceptConnections() { + while (true) { + try (Socket socket = serverSocket.accept()) { + connected.set(true); + OutputStream out = socket.getOutputStream(); + out.write("HTTP/1.1 404 Not Found\r\nContent-Length: 0\r\n\r\n" + .getBytes(StandardCharsets.US_ASCII)); + out.flush(); + } catch (Exception ex) { + // timeout/no connection is expected for this test + return; + } } } diff --git a/rt/wsdl/src/main/java/org/apache/cxf/wsdl11/AbstractWrapperWSDLLocator.java b/rt/wsdl/src/main/java/org/apache/cxf/wsdl11/AbstractWrapperWSDLLocator.java index 75ceb9507ba..bc1f000f41b 100644 --- a/rt/wsdl/src/main/java/org/apache/cxf/wsdl11/AbstractWrapperWSDLLocator.java +++ b/rt/wsdl/src/main/java/org/apache/cxf/wsdl11/AbstractWrapperWSDLLocator.java @@ -21,8 +21,6 @@ package org.apache.cxf.wsdl11; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; -import java.net.URI; -import java.net.URISyntaxException; import javax.wsdl.xml.WSDLLocator; import javax.xml.stream.XMLStreamException; @@ -95,14 +93,12 @@ public abstract class AbstractWrapperWSDLLocator implements WSDLLocator { } public InputSource getImportInputSource(String parentLocation, String importLocation) { - // Do a check on the scheme to see if it's anything that could be a security risk - try { - URI url = new URI(importLocation); - if (!(url.getScheme() == null || URIResolver.getAllowedSchemes().contains(url.getScheme()))) { - throw new IllegalArgumentException("The " + url.getScheme() + " URI scheme is not allowed"); - } - } catch (URISyntaxException e) { - // Just continue here as we might still be able to load it from the filesystem + // Do a check on the scheme to see if it's anything that could be a security risk. This also checks + // the location of an archive (e.g. jar:ftp://...), and does not need the import location to parse + // as a URI. A relative import is resolved against its parent, which has already been checked. + String scheme = URIResolver.getDisallowedScheme(importLocation, null); + if (scheme != null) { + throw new IllegalArgumentException("The " + scheme + " URI scheme is not allowed"); } InputSource src = parent.getImportInputSource(parentLocation, importLocation); diff --git a/rt/wsdl/src/test/java/org/apache/cxf/wsdl11/AbstractWrapperWSDLLocatorTest.java b/rt/wsdl/src/test/java/org/apache/cxf/wsdl11/AbstractWrapperWSDLLocatorTest.java new file mode 100644 index 00000000000..cb307ec46f4 --- /dev/null +++ b/rt/wsdl/src/test/java/org/apache/cxf/wsdl11/AbstractWrapperWSDLLocatorTest.java @@ -0,0 +1,121 @@ +/** + * 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.cxf.wsdl11; + +import java.io.ByteArrayInputStream; +import java.nio.charset.StandardCharsets; +import java.util.ArrayList; +import java.util.List; + +import javax.wsdl.xml.WSDLLocator; + +import org.xml.sax.InputSource; + +import org.junit.Test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +public class AbstractWrapperWSDLLocatorTest { + + @Test + public void testImportWithDisallowedSchemeRejected() { + assertRejected("ftp://localhost/a.xsd", "ftp"); + } + + @Test + public void testImportOfArchiveWithDisallowedLocationRejected() { + assertRejected("jar:ftp://localhost/a.jar!/a.xsd", "ftp"); + } + + @Test + public void testImportWithDisallowedSchemeThatIsNotAValidUriRejected() { + // Previously a location that did not parse as a URI skipped the check + assertRejected("ftp://localhost/a b.xsd", "ftp"); + } + + @Test + public void testAllowedImportsResolved() { + RecordingLocator parent = new RecordingLocator(); + TestLocator locator = new TestLocator(parent); + locator.getImportInputSource("http://localhost/a.wsdl", "http://localhost/b.xsd"); + locator.getImportInputSource("http://localhost/a.wsdl", "b.xsd"); + locator.getImportInputSource("bundle://1.0:1/a.wsdl", "b.xsd"); + locator.getImportInputSource("file:/tmp/a.wsdl", "jar:file:/tmp/a.jar!/b.xsd"); + assertEquals(4, parent.imports.size()); + } + + private static void assertRejected(String importLocation, String scheme) { + RecordingLocator parent = new RecordingLocator(); + TestLocator locator = new TestLocator(parent); + try { + locator.getImportInputSource("http://localhost/a.wsdl", importLocation); + fail("Expected the import of " + importLocation + " to be rejected"); + } catch (IllegalArgumentException ex) { + assertTrue(ex.getMessage(), ex.getMessage().contains("The " + scheme + " URI scheme is not allowed")); + } + assertTrue("Rejected import should not reach the parent locator", parent.imports.isEmpty()); + } + + private static final class TestLocator extends AbstractWrapperWSDLLocator { + TestLocator(WSDLLocator parent) { + super("http://localhost/a.wsdl", parent); + } + + @Override + public InputSource getInputSource() { + return null; + } + + @Override + public InputSource getInputSource(String parentLocation, String importLocation) { + return null; + } + } + + private static final class RecordingLocator implements WSDLLocator { + private final List<String> imports = new ArrayList<>(); + + public InputSource getBaseInputSource() { + return null; + } + + public InputSource getImportInputSource(String parentLocation, String importLocation) { + imports.add(importLocation); + InputSource source = new InputSource( + new ByteArrayInputStream("<a/>".getBytes(StandardCharsets.UTF_8))); + source.setSystemId(importLocation); + return source; + } + + public String getBaseURI() { + return null; + } + + public String getLatestImportURI() { + return null; + } + + public void close() { + // nothing to close + } + } +}
