Commit e975dbe3 authored by Phillip Webb's avatar Phillip Webb

Only use jar shortcut for matching URLs

Update JAR `Handler` logic so that the existing `jarFile` is only used
if the requested URL starts with the same path. Prior to this commit it
was possible to construct a URL with another URL as context. This could
mean that the `handler` was shared and the already resolved `jarFile`
contained in the handler wasn't necessarily suitable.

Fixes gh-12483
parent e40acf24
...@@ -92,7 +92,8 @@ public class Handler extends URLStreamHandler { ...@@ -92,7 +92,8 @@ public class Handler extends URLStreamHandler {
@Override @Override
protected URLConnection openConnection(URL url) throws IOException { protected URLConnection openConnection(URL url) throws IOException {
if (this.jarFile != null) { if (this.jarFile != null
&& url.toString().startsWith(this.jarFile.getUrl().toString())) {
return JarURLConnection.get(url, this.jarFile); return JarURLConnection.get(url, this.jarFile);
} }
try { try {
......
...@@ -485,4 +485,24 @@ public class JarFileTests { ...@@ -485,4 +485,24 @@ public class JarFileTests {
assertThat(temp.delete()).isTrue(); assertThat(temp.delete()).isTrue();
} }
@Test
public void createUrlFromStringWithContextWhenNotFound() throws Exception {
// gh-12483
JarURLConnection.setUseFastExceptions(true);
try {
JarFile.registerUrlProtocolHandler();
JarFile nested = this.jarFile
.getNestedJarFile(this.jarFile.getEntry("nested.jar"));
URL context = nested.getUrl();
new URL(context, "jar:" + this.rootJarFile.toURI() + "!/nested.jar!/3.dat")
.openConnection().getInputStream().close();
this.thrown.expect(FileNotFoundException.class);
new URL(context, "jar:" + this.rootJarFile.toURI() + "!/no.dat")
.openConnection().getInputStream().close();
}
finally {
JarURLConnection.setUseFastExceptions(false);
}
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment