Skip to content

Commit d0ce4da

Browse files
committed
Use URI encoded values when creating NestedPath URIs
Update `NestedPath.toUri()` so that the URI is constructed using encoded strings. Fixes gh-40615
1 parent 75dac14 commit d0ce4da

File tree

4 files changed

+131
-5
lines changed

4 files changed

+131
-5
lines changed

spring-boot-project/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/nio/file/NestedPath.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2023 the original author or authors.
2+
* Copyright 2012-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -138,9 +138,9 @@ public Path relativize(Path other) {
138138
@Override
139139
public URI toUri() {
140140
try {
141-
String uri = "nested:" + this.fileSystem.getJarPath().toUri().getPath();
141+
String uri = "nested:" + this.fileSystem.getJarPath().toUri().getRawPath();
142142
if (this.nestedEntryName != null) {
143-
uri += "/!" + this.nestedEntryName;
143+
uri += "/!" + UriPathEncoder.encode(this.nestedEntryName);
144144
}
145145
return new URI(uri);
146146
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
* Copyright 2012-2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.loader.nio.file;
18+
19+
import java.io.ByteArrayOutputStream;
20+
import java.nio.charset.StandardCharsets;
21+
22+
/**
23+
* URL Path Encoder based.
24+
*
25+
* @author Phillip Webb
26+
*/
27+
final class UriPathEncoder {
28+
29+
// Based on org.springframework.web.util.UriUtils
30+
31+
private static char[] ALLOWED = "/:@-._~!$&\'()*+,;=".toCharArray();
32+
33+
private UriPathEncoder() {
34+
}
35+
36+
static String encode(String path) {
37+
byte[] bytes = path.getBytes(StandardCharsets.UTF_8);
38+
for (byte b : bytes) {
39+
if (isAllowed(b)) {
40+
return encode(bytes);
41+
}
42+
}
43+
return path;
44+
}
45+
46+
private static String encode(byte[] bytes) {
47+
ByteArrayOutputStream result = new ByteArrayOutputStream(bytes.length);
48+
for (byte b : bytes) {
49+
if (isAllowed(b)) {
50+
result.write(b);
51+
}
52+
else {
53+
result.write('%');
54+
result.write(Character.toUpperCase(Character.forDigit((b >> 4) & 0xF, 16)));
55+
result.write(Character.toUpperCase(Character.forDigit(b & 0xF, 16)));
56+
}
57+
}
58+
return result.toString(StandardCharsets.UTF_8);
59+
}
60+
61+
private static boolean isAllowed(int ch) {
62+
for (char allowed : ALLOWED) {
63+
if (ch == allowed) {
64+
return true;
65+
}
66+
}
67+
return isAlpha(ch) || isDigit(ch);
68+
}
69+
70+
private static boolean isAlpha(int ch) {
71+
return (ch >= 'a' && ch <= 'z' || ch >= 'A' && ch <= 'Z');
72+
}
73+
74+
private static boolean isDigit(int ch) {
75+
return (ch >= '0' && ch <= '9');
76+
}
77+
78+
}

spring-boot-project/spring-boot-tools/spring-boot-loader/src/test/java/org/springframework/boot/loader/nio/file/NestedPathTests.java

+13-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2023 the original author or authors.
2+
* Copyright 2012-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -162,7 +162,18 @@ void relativizeThrowsException() {
162162

163163
@Test
164164
void toUriReturnsUri() throws Exception {
165-
assertThat(this.path.toUri()).isEqualTo(new URI("nested:" + this.jarPath.toUri().getPath() + "/!nested.jar"));
165+
assertThat(this.path.toUri())
166+
.isEqualTo(new URI("nested:" + this.jarPath.toUri().getRawPath() + "/!nested.jar"));
167+
}
168+
169+
@Test
170+
void toUriWhenHasSpecialCharsReturnsEncodedUri() throws Exception {
171+
this.jarPath = new File(this.temp, "te st.jar").toPath();
172+
this.provider = new NestedFileSystemProvider();
173+
this.fileSystem = new NestedFileSystem(this.provider, this.jarPath);
174+
this.path = new NestedPath(this.fileSystem, "ne sted.jar");
175+
assertThat(this.path.toUri())
176+
.isEqualTo(new URI("nested:" + this.jarPath.toUri().getRawPath() + "/!ne%20sted.jar"));
166177
}
167178

168179
@Test
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright 2012-2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.loader.nio.file;
18+
19+
import org.junit.jupiter.api.Test;
20+
21+
import static org.assertj.core.api.Assertions.assertThat;
22+
23+
/**
24+
* Tests for {@link UriPathEncoder}.
25+
*
26+
* @author Phillip Webb
27+
*/
28+
class UriPathEncoderTests {
29+
30+
@Test
31+
void encodePath() {
32+
assertThat(UriPathEncoder.encode("/foo/bar")).isEqualTo("/foo/bar");
33+
assertThat(UriPathEncoder.encode("/foo bar")).isEqualTo("/foo%20bar");
34+
assertThat(UriPathEncoder.encode("/Z\u00fcrich")).isEqualTo("/Z%C3%BCrich");
35+
}
36+
37+
}

0 commit comments

Comments
 (0)