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

coheigea pushed a commit to branch 4.1.x-fixes
in repository https://gitbox.apache.org/repos/asf/cxf.git


The following commit(s) were added to refs/heads/4.1.x-fixes by this push:
     new 20023f1c8d1 Check archive locations against the allowed URL schemes in 
URIResolver (#3794)
20023f1c8d1 is described below

commit 20023f1c8d13a1460e80b7a5a3f1990e2f5e6df2
Author: Colm O hEigeartaigh <[email protected]>
AuthorDate: Mon Nov 30 14:46:04 2026 +0000

    Check archive locations against the allowed URL schemes in URIResolver 
(#3794)
    
    * Check archive locations against the allowed URL schemes in URIResolver
    
    * PR review fixes
    
    * Adding war and tested to the allowed schemes
    
    * Review comments
    
    (cherry picked from commit 974dc955a3d1be0fee04a36374869fa1e228d3eb)
---
 .../java/org/apache/cxf/resource/URIResolver.java  |  71 ++++++++--
 .../org/apache/cxf/resource/URIResolverTest.java   | 148 +++++++++++++++++++++
 2 files changed, 210 insertions(+), 9 deletions(-)

diff --git a/core/src/main/java/org/apache/cxf/resource/URIResolver.java 
b/core/src/main/java/org/apache/cxf/resource/URIResolver.java
index 6ce9a927692..783e87c305e 100644
--- a/core/src/main/java/org/apache/cxf/resource/URIResolver.java
+++ b/core/src/main/java/org/apache/cxf/resource/URIResolver.java
@@ -43,6 +43,8 @@ import java.util.Map;
 import java.util.Set;
 import java.util.logging.Level;
 import java.util.logging.Logger;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
 
 import org.apache.cxf.common.CXFPermissions;
 import org.apache.cxf.common.classloader.ClassLoaderUtils;
@@ -65,14 +67,18 @@ public class URIResolver implements AutoCloseable {
     private static final Logger LOG = LogUtils.getLogger(URIResolver.class);
     private static final String ALLOWED_URL_SCHEMES_PROPERTY =
         "org.apache.cxf.resource.uriresolver.allowedSchemes";
+    // "nested" (Spring Boot 3.2+ fat jars, e.g. 
jar:nested:/app.jar/!BOOT-INF/lib/x.jar!/a.xsd) refers to a
+    // local file. "war" (Tomcat with unpackWARs="false") wraps the URL of the 
WAR, so it is also an archive scheme
     private static final Set<String> DEFAULT_ALLOWED_URL_SCHEMES =
         Collections.unmodifiableSet(
             new HashSet<>(Arrays.asList("file", "http", "https", "jar", "zip", 
"wsjar", "local", "classpath", "vfs",
-                    "resource", "bundleresource")));
+                    "resource", "bundleresource", "nested", "war")));
     private static final Set<String> NETWORK_URL_SCHEMES =
         Collections.unmodifiableSet(new HashSet<>(Arrays.asList("http", 
"https", "ftp")));
     private static final Set<String> ARCHIVE_URL_SCHEMES =
-        Collections.unmodifiableSet(new HashSet<>(Arrays.asList("jar", 
"wsjar", "zip")));
+        Collections.unmodifiableSet(new HashSet<>(Arrays.asList("jar", 
"wsjar", "zip", "war")));
+    // An RFC 3986 scheme of at least two characters, so that a Windows drive 
letter is not taken for one
+    private static final Pattern URL_SCHEME = 
Pattern.compile("^([a-zA-Z][a-zA-Z0-9+.-]+):");
     private static final Set<String> ADDITIONAL_LOCAL_URL_SCHEMES =
         Collections.unmodifiableSet(new HashSet<>(Arrays.asList("bundle", 
"bundleresource")));
 
@@ -457,15 +463,12 @@ public class URIResolver implements AutoCloseable {
     }
 
     /**
-     * Verifies that the URL scheme is permitted by URIResolver allowlist 
policy.
+     * Verifies that the URL scheme is permitted by URIResolver allowlist 
policy. For an archive URL such as
+     * jar:http://host/a.jar!/a.xsd, the archive location's scheme must also 
be permitted.
      */
     public static void checkAllowedScheme(URL targetUrl) throws IOException {
-        String scheme = targetUrl.getProtocol();
-        if (scheme == null) {
-            return;
-        }
-        scheme = scheme.toLowerCase(Locale.ROOT);
-        if (!getAllowedSchemes().contains(scheme)) {
+        String scheme = getDisallowedScheme(targetUrl.toString(), null);
+        if (scheme != null) {
             throw new IOException("URL scheme '" + scheme + "' is not 
permitted for URIResolver. "
                                   + "Allowed schemes: " + getAllowedSchemes());
         }
@@ -508,6 +511,56 @@ public class URIResolver implements AutoCloseable {
         }
     }
 
+    /**
+     * Returns whether a reference may be fetched under the allowed schemes 
(see {@link #getAllowedSchemes()}).
+     * A relative reference is checked against the base URI, or is allowed if 
there is no base URI. For an
+     * archive URL such as jar:http://host/a.jar!/a.xsd, the archive 
location's scheme must also be allowed.
+     * Unlike {@link #checkAllowedScheme(URL)}, this does not need the 
reference to parse as a URL or URI.
+     */
+    public static boolean isAllowedReference(String href, String base) {
+        return href != null && getDisallowedScheme(href, base) == null;
+    }
+
+    /**
+     * Returns the first scheme of a reference that is not allowed, as for
+     * {@link #isAllowedReference(String, String)}, or null if all of its 
schemes are allowed.
+     * The base URI is only used when the reference is relative, as an 
absolute reference resolves to itself
+     * whatever the base URI is (RFC 3986, section 5.2). A nested archive is 
written in the reference itself,
+     * e.g. jar:http://host/a.jar!/a.xsd, and then both "jar" and "http" are 
checked.
+     */
+    public static String getDisallowedScheme(String href, String base) {
+        if (href == null) {
+            return null;
+        }
+        String target = href.trim();
+        if (getScheme(target) == null) {
+            if (base == null) {
+                return null;
+            }
+            target = base.trim();
+        }
+        Set<String> allowed = getAllowedSchemes();
+        String scheme = getScheme(target);
+        while (scheme != null) {
+            if (!allowed.contains(scheme)) {
+                return scheme;
+            }
+            if (!ARCHIVE_URL_SCHEMES.contains(scheme)) {
+                break;
+            }
+            // java.net.URL ignores leading whitespace and control characters 
in the archive location,
+            // e.g. "jar: ftp://host/a.jar!/a.xsd";, so they must not hide its 
scheme
+            target = target.substring(scheme.length() + 1).trim();
+            scheme = getScheme(target);
+        }
+        return null;
+    }
+
+    private static String getScheme(String href) {
+        Matcher matcher = URL_SCHEME.matcher(href);
+        return matcher.find() ? matcher.group(1).toLowerCase(Locale.ROOT) : 
null;
+    }
+
     private static String getInnermostScheme(URI uri) throws 
URISyntaxException {
         String scheme = uri.getScheme();
         if (scheme == null) {
diff --git a/core/src/test/java/org/apache/cxf/resource/URIResolverTest.java 
b/core/src/test/java/org/apache/cxf/resource/URIResolverTest.java
index bf1b42bab53..7f9eac604ac 100644
--- a/core/src/test/java/org/apache/cxf/resource/URIResolverTest.java
+++ b/core/src/test/java/org/apache/cxf/resource/URIResolverTest.java
@@ -209,6 +209,109 @@ public class URIResolverTest {
         }
     }
 
+    @Test
+    public void testArchiveWithDisallowedInnerSchemeRejected() throws 
Exception {
+        try {
+            new 
URIResolver("jar:ftp://127.0.0.1:12345/example.jar!/example.wsdl";);
+            fail("Expected IOException for an archive at a disallowed ftp:// 
location");
+        } catch (java.io.IOException ex) {
+            assertTrue(ex.getMessage().contains("ftp"));
+            assertTrue(ex.getMessage().contains("not permitted"));
+        }
+    }
+
+    @Test
+    public void testCheckAllowedSchemeChecksArchiveLocation() throws Exception 
{
+        URIResolver.checkAllowedScheme(new URL("jar:file:/tmp/a.jar!/a.xsd"));
+        URIResolver.checkAllowedScheme(new 
URL("jar:http://localhost/a.jar!/a.xsd";));
+        try {
+            URIResolver.checkAllowedScheme(new 
URL("jar:ftp://localhost/a.jar!/a.xsd";));
+            fail("Expected IOException for an archive at a disallowed ftp:// 
location");
+        } catch (java.io.IOException ex) {
+            assertTrue(ex.getMessage().contains("'ftp'"));
+        }
+    }
+
+    @Test
+    public void testLocalArchiveContainerSchemesAllowedByDefault() throws 
Exception {
+        // Spring Boot 3.2+ fat jars
+        String nested = "jar:nested:/app.jar/!BOOT-INF/lib/x.jar!/wsdl/a.xsd";
+        // Tomcat with unpackWARs="false"
+        String war = "jar:war:file:/app.war*/WEB-INF/lib/x.jar!/wsdl/a.xsd";
+
+        // no nested: or war: handler is registered here, so supply one to 
build the URLs
+        URLStreamHandler handler = new URLStreamHandler() {
+            @Override
+            protected URLConnection openConnection(URL u) throws IOException {
+                throw new IOException("not supported");
+            }
+        };
+        URIResolver.checkAllowedScheme(new URL(null, nested, handler));
+        URIResolver.checkAllowedScheme(new URL(null, war, handler));
+
+        assertTrue(URIResolver.isAllowedReference(nested, null));
+        assertTrue(URIResolver.isAllowedReference(war, null));
+        assertTrue("Relative reference against a nested jar",
+            URIResolver.isAllowedReference("b.xsd", nested));
+
+        assertTrue(URIResolver.isLocalReference(nested, null));
+        assertTrue(URIResolver.isLocalReference(war, null));
+        assertTrue("Relative reference against a nested jar", 
URIResolver.isLocalReference("b.xsd", nested));
+    }
+
+    @Test
+    public void testWarLocationWithDisallowedSchemeRejected() throws Exception 
{
+        // war: wraps another URL, like jar:, so the scheme of the WAR's 
location must be checked too
+        String war = "war:ftp://localhost/x.war*/a.xsd";;
+        String jarInWar = 
"jar:war:ftp://localhost/x.war*/WEB-INF/lib/x.jar!/a.xsd";;
+
+        assertEquals("ftp", URIResolver.getDisallowedScheme(war, null));
+        assertEquals("ftp", URIResolver.getDisallowedScheme(jarInWar, null));
+        assertFalse(URIResolver.isLocalReference(war, null));
+        assertFalse(URIResolver.isLocalReference(jarInWar, null));
+
+        URLStreamHandler handler = new URLStreamHandler() {
+            @Override
+            protected URLConnection openConnection(URL u) throws IOException {
+                throw new IOException("not supported");
+            }
+        };
+        for (String url : new String[] {war, jarInWar}) {
+            try {
+                URIResolver.checkAllowedScheme(new URL(null, url, handler));
+                fail("Expected IOException for " + url);
+            } catch (IOException ex) {
+                assertTrue(ex.getMessage().contains("'ftp'"));
+            }
+        }
+    }
+
+    @Test
+    public void testWhitespaceBeforeArchiveLocationDoesNotHideItsScheme() 
throws Exception {
+        // java.net.URL ignores leading whitespace and control characters in 
the archive location
+        for (String url : new String[] {"jar: ftp://localhost/a.jar!/a.xsd";, 
"jar:\tftp://localhost/a.jar!/a.xsd";,
+                                        "jar:jar: 
ftp://localhost/a.jar!/b.jar!/a.xsd"}) {
+            assertEquals(url, "ftp", URIResolver.getDisallowedScheme(url, 
null));
+            assertFalse(url, URIResolver.isLocalReference(url, null));
+        }
+
+        try (ServerSocket serverSocket = new ServerSocket(0)) {
+            serverSocket.setSoTimeout(500);
+            String url = "jar: ftp://127.0.0.1:"; + serverSocket.getLocalPort() 
+ "/a.jar!/a.xsd";
+            try {
+                new URIResolver(url);
+                fail("Expected IOException for an archive at a disallowed 
ftp:// location");
+            } catch (IOException ex) {
+                assertTrue(ex.getMessage().contains("'ftp'"));
+            }
+            try (Socket socket = serverSocket.accept()) {
+                fail("The archive should not have been fetched");
+            } catch (java.net.SocketTimeoutException ex) {
+                // expected - no connection was made
+            }
+        }
+    }
+
     @Test
     public void testBundleResourceProtocolAllowedByDefault() throws Exception {
         assertTrue(URIResolver.getAllowedSchemes().contains("bundleresource"));
@@ -373,4 +476,49 @@ public class URIResolverTest {
         assertFalse("Invalid URI", URIResolver.isLocalReference("file:/tmp/a 
b.xsl", null));
         assertFalse(URIResolver.isLocalReference(null, null));
     }
+
+    @Test
+    public void testIsAllowedReference() {
+        // Default allowed schemes, including network schemes
+        assertTrue(URIResolver.isAllowedReference("http://localhost/a.xsd";, 
null));
+        assertTrue(URIResolver.isAllowedReference("HTTPS://localhost/a.xsd", 
null));
+        assertTrue(URIResolver.isAllowedReference("file:/tmp/a b.xsd", null));
+        
assertTrue(URIResolver.isAllowedReference("jar:http://localhost/a.jar!/a.xsd";, 
null));
+        assertTrue("Relative reference against an allowed base",
+            URIResolver.isAllowedReference("b.xsd", "http://localhost/a.xsd";));
+        assertTrue("Relative reference without a base", 
URIResolver.isAllowedReference("b.xsd", null));
+        assertTrue("Windows path", 
URIResolver.isAllowedReference("C:\\schemas\\a.xsd", null));
+
+        assertFalse(URIResolver.isAllowedReference("ftp://localhost/a.xsd";, 
null));
+        assertFalse(URIResolver.isAllowedReference("gopher://localhost/a.xsd";, 
null));
+        assertFalse("Relative reference against a disallowed base",
+            URIResolver.isAllowedReference("b.xsd", "ftp://localhost/a.xsd";));
+        assertFalse("Archive at a disallowed location",
+            URIResolver.isAllowedReference("jar:ftp://localhost/a.jar!/a.xsd";, 
null));
+        assertFalse("Nested archive at a disallowed location",
+            
URIResolver.isAllowedReference("jar:jar:ftp://localhost/a.jar!/b.jar!/a.xsd";, 
null));
+        assertFalse("Disallowed scheme in a reference that is not a valid URI",
+            URIResolver.isAllowedReference("ftp://localhost/a b.xsd", null));
+        assertFalse(URIResolver.isAllowedReference(null, null));
+
+        // The base URI is only used for a relative reference
+        assertNull("Absolute reference is not checked against its base",
+            URIResolver.getDisallowedScheme("http://localhost/a.jar!/a.xsd";, 
"jar:ftp://localhost/b.jar!/b.xsd";));
+        assertEquals("Relative reference is checked against its base", "ftp",
+            URIResolver.getDisallowedScheme("a.xsd", 
"jar:ftp://localhost/b.jar!/b.xsd";));
+
+        String property = "org.apache.cxf.resource.uriresolver.allowedSchemes";
+        String oldValue = System.getProperty(property);
+        System.setProperty(property, "ftp");
+        try {
+            assertTrue("Configured allowed scheme", 
URIResolver.isAllowedReference("ftp://localhost/a.xsd";, null));
+            
assertTrue(URIResolver.isAllowedReference("jar:ftp://localhost/a.jar!/a.xsd";, 
null));
+        } finally {
+            if (oldValue == null) {
+                System.clearProperty(property);
+            } else {
+                System.setProperty(property, oldValue);
+            }
+        }
+    }
 }

Reply via email to