Skip to content

Commit

Permalink
Use encoded version of path for jar URLs
Browse files Browse the repository at this point in the history
Update `JarUrl` so that the encoded version of the path is used.
This allows jars to placed in directories with `#` or `!` in the
name.

Fixes spring-projectsgh-38660
  • Loading branch information
philwebb committed Dec 7, 2023
1 parent 847daf4 commit 359a6cb
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public static URL create(File file, String nestedEntryName, String path) {
}

private static String getJarReference(File file, String nestedEntryName) {
String jarFilePath = file.toURI().getPath();
String jarFilePath = file.toURI().getRawPath().replace("!", "%21");
return (nestedEntryName != null) ? "nested:" + jarFilePath + "/!" + nestedEntryName : "file:" + jarFilePath;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,20 @@ void getLastModifiedHeaderReturnsFileModifiedTime() throws IOException {
}
}

@Test
void getJarFileWhenInFolderWithEncodedCharsReturnsJarFile() throws Exception {
this.temp = new File(this.temp, "te#st");
this.temp.mkdirs();
this.file = new File(this.temp, "test.jar");
this.url = JarUrl.create(this.file, "nested.jar");
assertThat(this.url.toString()).contains("te%23st");
TestJar.create(this.file);
JarUrlConnection connection = JarUrlConnection.open(this.url);
JarFile jarFile = connection.getJarFile();
assertThat(jarFile).isNotNull();
assertThat(jarFile.getEntry("3.dat")).isNotNull();
}

private long withoutNanos(long epochMilli) {
return Instant.ofEpochMilli(epochMilli).with(ChronoField.NANO_OF_SECOND, 0).toEpochMilli();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

import org.springframework.boot.loader.net.util.UrlDecoder;

import static org.assertj.core.api.Assertions.assertThat;

/**
Expand Down Expand Up @@ -84,4 +86,14 @@ void createWithFileNameAndPathReturnsUrl() {
assertThat(url).hasToString("jar:nested:%s/!lib.jar!/com/example/My.class".formatted(this.jarFileUrlPath));
}

@Test
void createWithReservedCharsInName() throws Exception {
String badFolderName = "foo#bar!/baz/!oof";
this.temp = new File(this.temp, badFolderName);
setup();
URL url = JarUrl.create(this.jarFile, "lib.jar", "com/example/My.class");
assertThat(url).hasToString("jar:nested:%s/!lib.jar!/com/example/My.class".formatted(this.jarFileUrlPath));
assertThat(UrlDecoder.decode(url.toString())).contains(badFolderName);
}

}

0 comments on commit 359a6cb

Please sign in to comment.