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

coheigea pushed a commit to branch coheigea/URIResolver
in repository https://gitbox.apache.org/repos/asf/cxf.git

commit df311c663b316bf84fe3cb5c3f42ccb75d09ca21
Author: Colm O hEigeartaigh <[email protected]>
AuthorDate: Fri Nov 27 09:53:43 2026 +0000

    Check archive locations against the allowed URL schemes in URIResolver
---
 .../java/org/apache/cxf/resource/URIResolver.java  | 60 ++++++++++++++++++---
 .../org/apache/cxf/resource/URIResolverTest.java   | 62 ++++++++++++++++++++++
 2 files changed, 115 insertions(+), 7 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..500f82da352 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;
@@ -73,6 +75,8 @@ public class URIResolver implements AutoCloseable {
         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")));
+    // 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 +461,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 +509,51 @@ 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.
+     */
+    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;
+            }
+            target = target.substring(scheme.length() + 1);
+            scheme = getScheme(target);
+        }
+        return null;
+    }
+
+    private static String getScheme(String reference) {
+        Matcher matcher = URL_SCHEME.matcher(reference);
+        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..d36b44d2edc 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,29 @@ 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 testBundleResourceProtocolAllowedByDefault() throws Exception {
         assertTrue(URIResolver.getAllowedSchemes().contains("bundleresource"));
@@ -373,4 +396,43 @@ 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));
+
+        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