Skip to content

Commit 55d0611

Browse files
committed
Guard against JarURLConnection with wrong JAR
Update `JarURLConnection.get()` to guard against the wrong nested JAR being used as context. Closes gh-11367
1 parent 5243adc commit 55d0611

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,10 @@ static void setUseFastExceptions(boolean useFastExceptions) {
255255

256256
static JarURLConnection get(URL url, JarFile jarFile) throws IOException {
257257
String spec = extractFullSpec(url, jarFile.getPathFromRoot());
258+
if (spec == null) {
259+
return (Boolean.TRUE.equals(useFastExceptions.get()) ? NOT_FOUND_CONNECTION
260+
: new JarURLConnection(url, null, EMPTY_JAR_ENTRY_NAME));
261+
}
258262
int separator;
259263
int index = 0;
260264
while ((separator = spec.indexOf(SEPARATOR, index)) > 0) {
@@ -264,7 +268,7 @@ static JarURLConnection get(URL url, JarFile jarFile) throws IOException {
264268
return JarURLConnection.notFound(jarFile, entryName);
265269
}
266270
jarFile = jarFile.getNestedJarFile(jarEntry);
267-
index += separator + SEPARATOR.length();
271+
index = separator + SEPARATOR.length();
268272
}
269273
JarEntryName jarEntryName = JarEntryName.get(spec, index);
270274
if (Boolean.TRUE.equals(useFastExceptions.get())) {
@@ -279,8 +283,8 @@ static JarURLConnection get(URL url, JarFile jarFile) throws IOException {
279283
private static String extractFullSpec(URL url, String pathFromRoot) {
280284
String file = url.getFile();
281285
int separatorIndex = file.indexOf(SEPARATOR);
282-
if (separatorIndex < 0) {
283-
return "";
286+
if (separatorIndex < 0 || !file.startsWith(pathFromRoot, separatorIndex)) {
287+
return null;
284288
}
285289
int specIndex = separatorIndex + SEPARATOR.length() + pathFromRoot.length();
286290
return file.substring(specIndex);

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import java.io.ByteArrayInputStream;
2020
import java.io.File;
21+
import java.io.FileNotFoundException;
2122
import java.net.URL;
2223

2324
import org.junit.Before;
@@ -152,6 +153,15 @@ public void connectionToEntryWithEncodedSpaceNestedEntry() throws Exception {
152153
.hasSameContentAs(new ByteArrayInputStream(new byte[] { 3 }));
153154
}
154155

156+
@Test(expected = FileNotFoundException.class)
157+
public void connectionToEntryUsingWrongAbsoluteUrlForEntryFromNestedJarFile()
158+
throws Exception {
159+
URL url = new URL("jar:file:" + getAbsolutePath() + "!/w.jar!/3.dat");
160+
JarFile nested = this.jarFile
161+
.getNestedJarFile(this.jarFile.getEntry("nested.jar"));
162+
JarURLConnection.get(url, nested).getInputStream();
163+
}
164+
155165
@Test
156166
public void getContentLengthReturnsLengthOfUnderlyingEntry() throws Exception {
157167
URL url = new URL(new URL("jar", null, -1,

0 commit comments

Comments
 (0)