Skip to content

Commit 974dc95

Browse files
authored
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
1 parent 4ba009f commit 974dc95

2 files changed

Lines changed: 210 additions & 9 deletions

File tree

‎core/src/main/java/org/apache/cxf/resource/URIResolver.java‎

Lines changed: 62 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@
4343
import java.util.Set;
4444
import java.util.logging.Level;
4545
import java.util.logging.Logger;
46+
import java.util.regex.Matcher;
47+
import java.util.regex.Pattern;
4648

4749
import org.apache.cxf.common.CXFPermissions;
4850
import org.apache.cxf.common.classloader.ClassLoaderUtils;
@@ -65,14 +67,18 @@ public class URIResolver implements AutoCloseable {
6567
private static final Logger LOG = LogUtils.getLogger(URIResolver.class);
6668
private static final String ALLOWED_URL_SCHEMES_PROPERTY =
6769
"org.apache.cxf.resource.uriresolver.allowedSchemes";
70+
// "nested" (Spring Boot 3.2+ fat jars, e.g. jar:nested:/app.jar/!BOOT-INF/lib/x.jar!/a.xsd) refers to a
71+
// local file. "war" (Tomcat with unpackWARs="false") wraps the URL of the WAR, so it is also an archive scheme
6872
private static final Set<String> DEFAULT_ALLOWED_URL_SCHEMES =
6973
Collections.unmodifiableSet(
7074
new HashSet<>(Arrays.asList("file", "http", "https", "jar", "zip", "wsjar", "local", "classpath", "vfs",
71-
"resource", "bundleresource")));
75+
"resource", "bundleresource", "nested", "war")));
7276
private static final Set<String> NETWORK_URL_SCHEMES =
7377
Collections.unmodifiableSet(new HashSet<>(Arrays.asList("http", "https", "ftp")));
7478
private static final Set<String> ARCHIVE_URL_SCHEMES =
75-
Collections.unmodifiableSet(new HashSet<>(Arrays.asList("jar", "wsjar", "zip")));
79+
Collections.unmodifiableSet(new HashSet<>(Arrays.asList("jar", "wsjar", "zip", "war")));
80+
// An RFC 3986 scheme of at least two characters, so that a Windows drive letter is not taken for one
81+
private static final Pattern URL_SCHEME = Pattern.compile("^([a-zA-Z][a-zA-Z0-9+.-]+):");
7682
private static final Set<String> ADDITIONAL_LOCAL_URL_SCHEMES =
7783
Collections.unmodifiableSet(new HashSet<>(Arrays.asList("bundle", "bundleresource")));
7884

@@ -457,15 +463,12 @@ private void tryRemote(String uriStr) throws IOException {
457463
}
458464

459465
/**
460-
* Verifies that the URL scheme is permitted by URIResolver allowlist policy.
466+
* Verifies that the URL scheme is permitted by URIResolver allowlist policy. For an archive URL such as
467+
* jar:http://host/a.jar!/a.xsd, the archive location's scheme must also be permitted.
461468
*/
462469
public static void checkAllowedScheme(URL targetUrl) throws IOException {
463-
String scheme = targetUrl.getProtocol();
464-
if (scheme == null) {
465-
return;
466-
}
467-
scheme = scheme.toLowerCase(Locale.ROOT);
468-
if (!getAllowedSchemes().contains(scheme)) {
470+
String scheme = getDisallowedScheme(targetUrl.toString(), null);
471+
if (scheme != null) {
469472
throw new IOException("URL scheme '" + scheme + "' is not permitted for URIResolver. "
470473
+ "Allowed schemes: " + getAllowedSchemes());
471474
}
@@ -508,6 +511,56 @@ public static boolean isLocalReference(String href, String base) {
508511
}
509512
}
510513

514+
/**
515+
* Returns whether a reference may be fetched under the allowed schemes (see {@link #getAllowedSchemes()}).
516+
* A relative reference is checked against the base URI, or is allowed if there is no base URI. For an
517+
* archive URL such as jar:http://host/a.jar!/a.xsd, the archive location's scheme must also be allowed.
518+
* Unlike {@link #checkAllowedScheme(URL)}, this does not need the reference to parse as a URL or URI.
519+
*/
520+
public static boolean isAllowedReference(String href, String base) {
521+
return href != null && getDisallowedScheme(href, base) == null;
522+
}
523+
524+
/**
525+
* Returns the first scheme of a reference that is not allowed, as for
526+
* {@link #isAllowedReference(String, String)}, or null if all of its schemes are allowed.
527+
* The base URI is only used when the reference is relative, as an absolute reference resolves to itself
528+
* whatever the base URI is (RFC 3986, section 5.2). A nested archive is written in the reference itself,
529+
* e.g. jar:http://host/a.jar!/a.xsd, and then both "jar" and "http" are checked.
530+
*/
531+
public static String getDisallowedScheme(String href, String base) {
532+
if (href == null) {
533+
return null;
534+
}
535+
String target = href.trim();
536+
if (getScheme(target) == null) {
537+
if (base == null) {
538+
return null;
539+
}
540+
target = base.trim();
541+
}
542+
Set<String> allowed = getAllowedSchemes();
543+
String scheme = getScheme(target);
544+
while (scheme != null) {
545+
if (!allowed.contains(scheme)) {
546+
return scheme;
547+
}
548+
if (!ARCHIVE_URL_SCHEMES.contains(scheme)) {
549+
break;
550+
}
551+
// java.net.URL ignores leading whitespace and control characters in the archive location,
552+
// e.g. "jar: ftp://host/a.jar!/a.xsd", so they must not hide its scheme
553+
target = target.substring(scheme.length() + 1).trim();
554+
scheme = getScheme(target);
555+
}
556+
return null;
557+
}
558+
559+
private static String getScheme(String href) {
560+
Matcher matcher = URL_SCHEME.matcher(href);
561+
return matcher.find() ? matcher.group(1).toLowerCase(Locale.ROOT) : null;
562+
}
563+
511564
private static String getInnermostScheme(URI uri) throws URISyntaxException {
512565
String scheme = uri.getScheme();
513566
if (scheme == null) {

‎core/src/test/java/org/apache/cxf/resource/URIResolverTest.java‎

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,109 @@ public void testFtpProtocolRejectedByDefault() throws Exception {
209209
}
210210
}
211211

212+
@Test
213+
public void testArchiveWithDisallowedInnerSchemeRejected() throws Exception {
214+
try {
215+
new URIResolver("jar:ftp://127.0.0.1:12345/example.jar!/example.wsdl");
216+
fail("Expected IOException for an archive at a disallowed ftp:// location");
217+
} catch (java.io.IOException ex) {
218+
assertTrue(ex.getMessage().contains("ftp"));
219+
assertTrue(ex.getMessage().contains("not permitted"));
220+
}
221+
}
222+
223+
@Test
224+
public void testCheckAllowedSchemeChecksArchiveLocation() throws Exception {
225+
URIResolver.checkAllowedScheme(new URL("jar:file:/tmp/a.jar!/a.xsd"));
226+
URIResolver.checkAllowedScheme(new URL("jar:http://localhost/a.jar!/a.xsd"));
227+
try {
228+
URIResolver.checkAllowedScheme(new URL("jar:ftp://localhost/a.jar!/a.xsd"));
229+
fail("Expected IOException for an archive at a disallowed ftp:// location");
230+
} catch (java.io.IOException ex) {
231+
assertTrue(ex.getMessage().contains("'ftp'"));
232+
}
233+
}
234+
235+
@Test
236+
public void testLocalArchiveContainerSchemesAllowedByDefault() throws Exception {
237+
// Spring Boot 3.2+ fat jars
238+
String nested = "jar:nested:/app.jar/!BOOT-INF/lib/x.jar!/wsdl/a.xsd";
239+
// Tomcat with unpackWARs="false"
240+
String war = "jar:war:file:/app.war*/WEB-INF/lib/x.jar!/wsdl/a.xsd";
241+
242+
// no nested: or war: handler is registered here, so supply one to build the URLs
243+
URLStreamHandler handler = new URLStreamHandler() {
244+
@Override
245+
protected URLConnection openConnection(URL u) throws IOException {
246+
throw new IOException("not supported");
247+
}
248+
};
249+
URIResolver.checkAllowedScheme(new URL(null, nested, handler));
250+
URIResolver.checkAllowedScheme(new URL(null, war, handler));
251+
252+
assertTrue(URIResolver.isAllowedReference(nested, null));
253+
assertTrue(URIResolver.isAllowedReference(war, null));
254+
assertTrue("Relative reference against a nested jar",
255+
URIResolver.isAllowedReference("b.xsd", nested));
256+
257+
assertTrue(URIResolver.isLocalReference(nested, null));
258+
assertTrue(URIResolver.isLocalReference(war, null));
259+
assertTrue("Relative reference against a nested jar", URIResolver.isLocalReference("b.xsd", nested));
260+
}
261+
262+
@Test
263+
public void testWarLocationWithDisallowedSchemeRejected() throws Exception {
264+
// war: wraps another URL, like jar:, so the scheme of the WAR's location must be checked too
265+
String war = "war:ftp://localhost/x.war*/a.xsd";
266+
String jarInWar = "jar:war:ftp://localhost/x.war*/WEB-INF/lib/x.jar!/a.xsd";
267+
268+
assertEquals("ftp", URIResolver.getDisallowedScheme(war, null));
269+
assertEquals("ftp", URIResolver.getDisallowedScheme(jarInWar, null));
270+
assertFalse(URIResolver.isLocalReference(war, null));
271+
assertFalse(URIResolver.isLocalReference(jarInWar, null));
272+
273+
URLStreamHandler handler = new URLStreamHandler() {
274+
@Override
275+
protected URLConnection openConnection(URL u) throws IOException {
276+
throw new IOException("not supported");
277+
}
278+
};
279+
for (String url : new String[] {war, jarInWar}) {
280+
try {
281+
URIResolver.checkAllowedScheme(new URL(null, url, handler));
282+
fail("Expected IOException for " + url);
283+
} catch (IOException ex) {
284+
assertTrue(ex.getMessage().contains("'ftp'"));
285+
}
286+
}
287+
}
288+
289+
@Test
290+
public void testWhitespaceBeforeArchiveLocationDoesNotHideItsScheme() throws Exception {
291+
// java.net.URL ignores leading whitespace and control characters in the archive location
292+
for (String url : new String[] {"jar: ftp://localhost/a.jar!/a.xsd", "jar:\tftp://localhost/a.jar!/a.xsd",
293+
"jar:jar: ftp://localhost/a.jar!/b.jar!/a.xsd"}) {
294+
assertEquals(url, "ftp", URIResolver.getDisallowedScheme(url, null));
295+
assertFalse(url, URIResolver.isLocalReference(url, null));
296+
}
297+
298+
try (ServerSocket serverSocket = new ServerSocket(0)) {
299+
serverSocket.setSoTimeout(500);
300+
String url = "jar: ftp://127.0.0.1:" + serverSocket.getLocalPort() + "/a.jar!/a.xsd";
301+
try {
302+
new URIResolver(url);
303+
fail("Expected IOException for an archive at a disallowed ftp:// location");
304+
} catch (IOException ex) {
305+
assertTrue(ex.getMessage().contains("'ftp'"));
306+
}
307+
try (Socket socket = serverSocket.accept()) {
308+
fail("The archive should not have been fetched");
309+
} catch (java.net.SocketTimeoutException ex) {
310+
// expected - no connection was made
311+
}
312+
}
313+
}
314+
212315
@Test
213316
public void testBundleResourceProtocolAllowedByDefault() throws Exception {
214317
assertTrue(URIResolver.getAllowedSchemes().contains("bundleresource"));
@@ -373,4 +476,49 @@ public void testIsLocalReference() {
373476
assertFalse("Invalid URI", URIResolver.isLocalReference("file:/tmp/a b.xsl", null));
374477
assertFalse(URIResolver.isLocalReference(null, null));
375478
}
479+
480+
@Test
481+
public void testIsAllowedReference() {
482+
// Default allowed schemes, including network schemes
483+
assertTrue(URIResolver.isAllowedReference("http://localhost/a.xsd", null));
484+
assertTrue(URIResolver.isAllowedReference("HTTPS://localhost/a.xsd", null));
485+
assertTrue(URIResolver.isAllowedReference("file:/tmp/a b.xsd", null));
486+
assertTrue(URIResolver.isAllowedReference("jar:http://localhost/a.jar!/a.xsd", null));
487+
assertTrue("Relative reference against an allowed base",
488+
URIResolver.isAllowedReference("b.xsd", "http://localhost/a.xsd"));
489+
assertTrue("Relative reference without a base", URIResolver.isAllowedReference("b.xsd", null));
490+
assertTrue("Windows path", URIResolver.isAllowedReference("C:\\schemas\\a.xsd", null));
491+
492+
assertFalse(URIResolver.isAllowedReference("ftp://localhost/a.xsd", null));
493+
assertFalse(URIResolver.isAllowedReference("gopher://localhost/a.xsd", null));
494+
assertFalse("Relative reference against a disallowed base",
495+
URIResolver.isAllowedReference("b.xsd", "ftp://localhost/a.xsd"));
496+
assertFalse("Archive at a disallowed location",
497+
URIResolver.isAllowedReference("jar:ftp://localhost/a.jar!/a.xsd", null));
498+
assertFalse("Nested archive at a disallowed location",
499+
URIResolver.isAllowedReference("jar:jar:ftp://localhost/a.jar!/b.jar!/a.xsd", null));
500+
assertFalse("Disallowed scheme in a reference that is not a valid URI",
501+
URIResolver.isAllowedReference("ftp://localhost/a b.xsd", null));
502+
assertFalse(URIResolver.isAllowedReference(null, null));
503+
504+
// The base URI is only used for a relative reference
505+
assertNull("Absolute reference is not checked against its base",
506+
URIResolver.getDisallowedScheme("http://localhost/a.jar!/a.xsd", "jar:ftp://localhost/b.jar!/b.xsd"));
507+
assertEquals("Relative reference is checked against its base", "ftp",
508+
URIResolver.getDisallowedScheme("a.xsd", "jar:ftp://localhost/b.jar!/b.xsd"));
509+
510+
String property = "org.apache.cxf.resource.uriresolver.allowedSchemes";
511+
String oldValue = System.getProperty(property);
512+
System.setProperty(property, "ftp");
513+
try {
514+
assertTrue("Configured allowed scheme", URIResolver.isAllowedReference("ftp://localhost/a.xsd", null));
515+
assertTrue(URIResolver.isAllowedReference("jar:ftp://localhost/a.jar!/a.xsd", null));
516+
} finally {
517+
if (oldValue == null) {
518+
System.clearProperty(property);
519+
} else {
520+
System.setProperty(property, oldValue);
521+
}
522+
}
523+
}
376524
}

0 commit comments

Comments
 (0)