Skip to content

Commit 7d59b78

Browse files
committed
Fix handling of jar files with + chars in their path
Closes gh-17208
1 parent 4894aff commit 7d59b78

File tree

2 files changed

+30
-3
lines changed
  • spring-boot-project/spring-boot-tools/spring-boot-loader/src

2 files changed

+30
-3
lines changed

spring-boot-project/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/jar/Handler.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@
2020
import java.io.IOException;
2121
import java.lang.ref.SoftReference;
2222
import java.net.MalformedURLException;
23+
import java.net.URI;
2324
import java.net.URL;
2425
import java.net.URLConnection;
25-
import java.net.URLDecoder;
2626
import java.net.URLStreamHandler;
2727
import java.util.Map;
2828
import java.util.concurrent.ConcurrentHashMap;
@@ -302,8 +302,7 @@ private JarFile getRootJarFile(String name) throws IOException {
302302
if (!name.startsWith(FILE_PROTOCOL)) {
303303
throw new IllegalStateException("Not a file URL");
304304
}
305-
String path = name.substring(FILE_PROTOCOL.length());
306-
File file = new File(URLDecoder.decode(path, "UTF-8"));
305+
File file = new File(URI.create(name));
307306
Map<File, JarFile> cache = rootFileCache.get();
308307
JarFile result = (cache != null) ? cache.get(file) : null;
309308
if (result == null) {

spring-boot-project/spring-boot-tools/spring-boot-loader/src/test/java/org/springframework/boot/loader/jar/HandlerTests.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,34 @@ public void fallbackToJdksJarUrlStreamHandler() throws Exception {
167167
assertThat(jdkConnection).isNotInstanceOf(JarURLConnection.class);
168168
}
169169

170+
@Test
171+
public void whenJarHasAPlusInItsPathConnectionJarFileMatchesOriginalJarFile() throws Exception {
172+
File testJar = this.temporaryFolder.newFile("t+e+s+t.jar");
173+
TestJarCreator.createTestJar(testJar);
174+
URL url = new URL(null, "jar:" + testJar.toURI().toURL() + "!/nested.jar!/3.dat", this.handler);
175+
JarURLConnection connection = (JarURLConnection) url.openConnection();
176+
try {
177+
assertThat(connection.getJarFile().getRootJarFile().getFile()).isEqualTo(testJar);
178+
}
179+
finally {
180+
connection.getJarFile().close();
181+
}
182+
}
183+
184+
@Test
185+
public void whenJarHasASpaceInItsPathConnectionJarFileMatchesOriginalJarFile() throws Exception {
186+
File testJar = this.temporaryFolder.newFile("t e s t.jar");
187+
TestJarCreator.createTestJar(testJar);
188+
URL url = new URL(null, "jar:" + testJar.toURI().toURL() + "!/nested.jar!/3.dat", this.handler);
189+
JarURLConnection connection = (JarURLConnection) url.openConnection();
190+
try {
191+
assertThat(connection.getJarFile().getRootJarFile().getFile()).isEqualTo(testJar);
192+
}
193+
finally {
194+
connection.getJarFile().close();
195+
}
196+
}
197+
170198
private void assertStandardAndCustomHandlerUrlsAreEqual(String context, String spec) throws MalformedURLException {
171199
URL standardUrl = new URL(new URL("jar:" + context), spec);
172200
URL customHandlerUrl = new URL(new URL("jar", null, -1, context, this.handler), spec);

0 commit comments

Comments
 (0)