Skip to content

Commit 624a5f8

Browse files
committed
Apply URL decoding for nested jar entry names
Update `JarURLConnection` to apply URL decoding to nested jar entry names. Fixes gh-12765
1 parent f08c496 commit 624a5f8

File tree

7 files changed

+24
-5
lines changed

7 files changed

+24
-5
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -258,10 +258,10 @@ static JarURLConnection get(URL url, JarFile jarFile) throws IOException {
258258
int separator;
259259
int index = 0;
260260
while ((separator = spec.indexOf(SEPARATOR, index)) > 0) {
261-
String entryName = spec.substring(index, separator);
262-
JarEntry jarEntry = jarFile.getJarEntry(entryName);
261+
JarEntryName entryName = JarEntryName.get(spec.substring(index, separator));
262+
JarEntry jarEntry = jarFile.getJarEntry(entryName.toString());
263263
if (jarEntry == null) {
264-
return JarURLConnection.notFound(jarFile, JarEntryName.get(entryName));
264+
return JarURLConnection.notFound(jarFile, entryName);
265265
}
266266
jarFile = jarFile.getNestedJarFile(jarEntry);
267267
index += separator + SEPARATOR.length();

spring-boot-tools/spring-boot-loader/src/test/java/org/springframework/boot/loader/TestJarCreator.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ public static void createTestJar(File file, boolean unpackNested) throws Excepti
5252

5353
writeNestedEntry("nested.jar", unpackNested, jarOutputStream);
5454
writeNestedEntry("another-nested.jar", unpackNested, jarOutputStream);
55+
writeNestedEntry("space nested.jar", unpackNested, jarOutputStream);
5556
}
5657
finally {
5758
jarOutputStream.close();

spring-boot-tools/spring-boot-loader/src/test/java/org/springframework/boot/loader/archive/ExplodedArchiveTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ public void getManifest() throws Exception {
108108
@Test
109109
public void getEntries() throws Exception {
110110
Map<String, Archive.Entry> entries = getEntriesMap(this.archive);
111-
assertThat(entries.size()).isEqualTo(10);
111+
assertThat(entries.size()).isEqualTo(11);
112112
}
113113

114114
@Test

spring-boot-tools/spring-boot-loader/src/test/java/org/springframework/boot/loader/archive/JarFileArchiveTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public void getManifest() throws Exception {
8282
@Test
8383
public void getEntries() throws Exception {
8484
Map<String, Archive.Entry> entries = getEntriesMap(this.archive);
85-
assertThat(entries.size()).isEqualTo(10);
85+
assertThat(entries.size()).isEqualTo(11);
8686
}
8787

8888
@Test

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ public void visitRecords() throws Exception {
9090
assertThat(headers.next().getName().toString()).isEqualTo("special/\u00EB.dat");
9191
assertThat(headers.next().getName().toString()).isEqualTo("nested.jar");
9292
assertThat(headers.next().getName().toString()).isEqualTo("another-nested.jar");
93+
assertThat(headers.next().getName().toString()).isEqualTo("space nested.jar");
9394
assertThat(headers.hasNext()).isFalse();
9495
}
9596

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ public void jdkJarFile() throws Exception {
9393
assertThat(entries.nextElement().getName()).isEqualTo("special/\u00EB.dat");
9494
assertThat(entries.nextElement().getName()).isEqualTo("nested.jar");
9595
assertThat(entries.nextElement().getName()).isEqualTo("another-nested.jar");
96+
assertThat(entries.nextElement().getName()).isEqualTo("space nested.jar");
9697
assertThat(entries.hasMoreElements()).isFalse();
9798
URL jarUrl = new URL("jar:" + this.rootJarFile.toURI() + "!/");
9899
URLClassLoader urlClassLoader = new URLClassLoader(new URL[] { jarUrl });
@@ -135,6 +136,7 @@ public void getEntries() throws Exception {
135136
assertThat(entries.nextElement().getName()).isEqualTo("special/\u00EB.dat");
136137
assertThat(entries.nextElement().getName()).isEqualTo("nested.jar");
137138
assertThat(entries.nextElement().getName()).isEqualTo("another-nested.jar");
139+
assertThat(entries.nextElement().getName()).isEqualTo("space nested.jar");
138140
assertThat(entries.hasMoreElements()).isFalse();
139141
}
140142

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,21 @@ public void connectionToEntryInNestedJarFromUrlThatUsesExistingUrlAsContext()
137137
.hasSameContentAs(new ByteArrayInputStream(new byte[] { 3 }));
138138
}
139139

140+
@Test
141+
public void connectionToEntryWithSpaceNestedEntry() throws Exception {
142+
URL url = new URL("jar:file:" + getRelativePath() + "!/space nested.jar!/3.dat");
143+
assertThat(JarURLConnection.get(url, this.jarFile).getInputStream())
144+
.hasSameContentAs(new ByteArrayInputStream(new byte[] { 3 }));
145+
}
146+
147+
@Test
148+
public void connectionToEntryWithEncodedSpaceNestedEntry() throws Exception {
149+
URL url = new URL(
150+
"jar:file:" + getRelativePath() + "!/space%20nested.jar!/3.dat");
151+
assertThat(JarURLConnection.get(url, this.jarFile).getInputStream())
152+
.hasSameContentAs(new ByteArrayInputStream(new byte[] { 3 }));
153+
}
154+
140155
@Test
141156
public void getContentLengthReturnsLengthOfUnderlyingEntry() throws Exception {
142157
URL url = new URL(new URL("jar", null, -1,

0 commit comments

Comments
 (0)