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
This commit is contained in:
Phillip Webb 2018-03-16 11:56:34 -07:00
parent e40acf2447
commit e975dbe3f0
2 changed files with 22 additions and 1 deletions

View File

@ -92,7 +92,8 @@ public class Handler extends URLStreamHandler {
@Override
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);
}
try {

View File

@ -485,4 +485,24 @@ public class JarFileTests {
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);
}
}
}