Skip to content

Commit d4e9f45

Browse files
committed
Fix path handling and wrapping in LoaderHidingResource
Closes gh-39472
1 parent 2ceb7b4 commit d4e9f45

File tree

2 files changed

+118
-4
lines changed

2 files changed

+118
-4
lines changed

Diff for: spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/jetty/LoaderHidingResource.java

+14-4
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,16 @@ final class LoaderHidingResource extends Resource {
4141

4242
private static final String LOADER_RESOURCE_PATH_PREFIX = "/org/springframework/boot/";
4343

44+
private final Path loaderBasePath;
45+
4446
private final Resource base;
4547

4648
private final Resource delegate;
4749

4850
LoaderHidingResource(Resource base, Resource delegate) {
4951
this.base = base;
5052
this.delegate = delegate;
53+
this.loaderBasePath = base.getPath().getFileSystem().getPath("/", "org", "springframework", "boot");
5154
}
5255

5356
@Override
@@ -141,12 +144,19 @@ public ReadableByteChannel newReadableByteChannel() throws IOException {
141144

142145
@Override
143146
public List<Resource> list() {
144-
return this.delegate.list().stream().filter(this::nonLoaderResource).toList();
147+
return asLoaderHidingResources(this.delegate.list());
145148
}
146149

147150
private boolean nonLoaderResource(Resource resource) {
148-
Path prefix = this.base.getPath().resolve(Path.of("org", "springframework", "boot"));
149-
return !resource.getPath().startsWith(prefix);
151+
return !resource.getPath().startsWith(this.loaderBasePath);
152+
}
153+
154+
private List<Resource> asLoaderHidingResources(Collection<Resource> resources) {
155+
return resources.stream().filter(this::nonLoaderResource).map(this::asLoaderHidingResource).toList();
156+
}
157+
158+
private Resource asLoaderHidingResource(Resource resource) {
159+
return (resource instanceof LoaderHidingResource) ? resource : new LoaderHidingResource(this.base, resource);
150160
}
151161

152162
@Override
@@ -175,7 +185,7 @@ public void copyTo(Path destination) throws IOException {
175185

176186
@Override
177187
public Collection<Resource> getAllResources() {
178-
return this.delegate.getAllResources().stream().filter(this::nonLoaderResource).toList();
188+
return asLoaderHidingResources(this.delegate.getAllResources());
179189
}
180190

181191
@Override
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
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.web.embedded.jetty;
18+
19+
import java.io.File;
20+
import java.io.FileOutputStream;
21+
import java.io.IOException;
22+
import java.net.URI;
23+
import java.nio.file.FileSystems;
24+
import java.util.ArrayList;
25+
import java.util.Collection;
26+
import java.util.Collections;
27+
import java.util.List;
28+
import java.util.jar.JarOutputStream;
29+
import java.util.zip.ZipEntry;
30+
31+
import org.eclipse.jetty.util.resource.PathResourceFactory;
32+
import org.eclipse.jetty.util.resource.Resource;
33+
import org.junit.jupiter.api.Test;
34+
import org.junit.jupiter.api.io.TempDir;
35+
36+
import static org.assertj.core.api.Assertions.assertThat;
37+
38+
/**
39+
* Tests for {@link LoaderHidingResource}.
40+
*
41+
* @author Andy Wilkinson
42+
*/
43+
class LoaderHidingResourceTests {
44+
45+
@Test
46+
void listHidesLoaderResources(@TempDir File temp) throws IOException {
47+
URI warUri = createExampleWar(temp);
48+
Resource resource = new PathResourceFactory().newResource(warUri);
49+
LoaderHidingResource loaderHidingResource = new LoaderHidingResource(resource, resource);
50+
assertThat(deepList(loaderHidingResource)).hasOnlyElementsOfType(LoaderHidingResource.class)
51+
.extracting(Resource::getName)
52+
.contains("/assets/image.jpg")
53+
.doesNotContain("/org/springframework/boot/Loader.class");
54+
}
55+
56+
@Test
57+
void getAllResourcesHidesLoaderResources(@TempDir File temp) throws IOException {
58+
URI warUri = createExampleWar(temp);
59+
Resource resource = new PathResourceFactory().newResource(warUri);
60+
LoaderHidingResource loaderHidingResource = new LoaderHidingResource(resource, resource);
61+
Collection<Resource> allResources = loaderHidingResource.getAllResources();
62+
assertThat(allResources).hasOnlyElementsOfType(LoaderHidingResource.class)
63+
.extracting(Resource::getName)
64+
.contains("/assets/image.jpg")
65+
.doesNotContain("/org/springframework/boot/Loader.class");
66+
}
67+
68+
@Test
69+
void resolveHidesLoaderResources(@TempDir File temp) throws IOException {
70+
URI warUri = createExampleWar(temp);
71+
Resource resource = new PathResourceFactory().newResource(warUri);
72+
LoaderHidingResource loaderHidingResource = new LoaderHidingResource(resource, resource);
73+
assertThat(loaderHidingResource.resolve("/assets/image.jpg").exists()).isTrue();
74+
assertThat(loaderHidingResource.resolve("/assets/image.jpg")).isInstanceOf(LoaderHidingResource.class);
75+
assertThat(loaderHidingResource.resolve("/assets/non-existent.jpg").exists()).isFalse();
76+
assertThat(loaderHidingResource.resolve("/assets/non-existent.jpg")).isInstanceOf(LoaderHidingResource.class);
77+
assertThat(loaderHidingResource.resolve("/org/springframework/boot/Loader.class")).isNull();
78+
}
79+
80+
private URI createExampleWar(File temp) throws IOException {
81+
File exampleWarFile = new File(temp, "example.war");
82+
try (JarOutputStream out = new JarOutputStream(new FileOutputStream(exampleWarFile))) {
83+
out.putNextEntry(new ZipEntry("org/"));
84+
out.putNextEntry(new ZipEntry("org/springframework/"));
85+
out.putNextEntry(new ZipEntry("org/springframework/boot/"));
86+
out.putNextEntry(new ZipEntry("org/springframework/boot/Loader.class"));
87+
out.putNextEntry(new ZipEntry("assets/"));
88+
out.putNextEntry(new ZipEntry("assets/image.jpg"));
89+
}
90+
URI warUri = URI.create("jar:" + exampleWarFile.toURI() + "!/");
91+
FileSystems.newFileSystem(warUri, Collections.emptyMap());
92+
return warUri;
93+
}
94+
95+
private List<Resource> deepList(Resource resource) {
96+
List<Resource> all = new ArrayList<>();
97+
for (Resource listed : resource.list()) {
98+
all.add(listed);
99+
all.addAll(deepList(listed));
100+
}
101+
return all;
102+
}
103+
104+
}

0 commit comments

Comments
 (0)