@@ -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:\t ftp://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