Skip to content

Commit 534f123

Browse files
committed
Add explicit FileSystemResource path check for trailing slash
Closes gh-34509
1 parent e421104 commit 534f123

File tree

4 files changed

+45
-12
lines changed

4 files changed

+45
-12
lines changed

Diff for: spring-webflux/src/main/java/org/springframework/web/reactive/resource/ResourceHandlerUtils.java

+7-4
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
import org.apache.commons.logging.LogFactory;
2525

2626
import org.springframework.core.io.ClassPathResource;
27-
import org.springframework.core.io.PathResource;
27+
import org.springframework.core.io.FileSystemResource;
2828
import org.springframework.core.io.Resource;
2929
import org.springframework.core.io.UrlResource;
3030
import org.springframework.core.log.LogFormatUtils;
@@ -57,15 +57,18 @@ public static void assertResourceLocation(@Nullable Resource location) {
5757
Assert.notNull(location, "Resource location must not be null");
5858
try {
5959
String path;
60-
if (location instanceof PathResource) {
60+
if (location instanceof org.springframework.core.io.PathResource) {
6161
return;
6262
}
63-
else if (location instanceof UrlResource) {
64-
path = location.getURL().toExternalForm();
63+
else if (location instanceof FileSystemResource fileSystemResource) {
64+
path = fileSystemResource.getPath();
6565
}
6666
else if (location instanceof ClassPathResource classPathResource) {
6767
path = classPathResource.getPath();
6868
}
69+
else if (location instanceof UrlResource) {
70+
path = location.getURL().toExternalForm();
71+
}
6972
else {
7073
path = location.getURL().getPath();
7174
}

Diff for: spring-webflux/src/test/java/org/springframework/web/reactive/function/server/PathResourceLookupFunctionTests.java

+16-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2024 the original author or authors.
2+
* Copyright 2002-2025 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.
@@ -26,6 +26,7 @@
2626
import reactor.test.StepVerifier;
2727

2828
import org.springframework.core.io.ClassPathResource;
29+
import org.springframework.core.io.FileSystemResource;
2930
import org.springframework.core.io.Resource;
3031
import org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest;
3132
import org.springframework.web.testfixture.server.MockServerWebExchange;
@@ -39,8 +40,7 @@ class PathResourceLookupFunctionTests {
3940
void normal() throws Exception {
4041
ClassPathResource location = new ClassPathResource("org/springframework/web/reactive/function/server/");
4142

42-
PathResourceLookupFunction
43-
function = new PathResourceLookupFunction("/resources/**", location);
43+
PathResourceLookupFunction function = new PathResourceLookupFunction("/resources/**", location);
4444
MockServerHttpRequest mockRequest = MockServerHttpRequest.get("https://localhost/resources/response.txt").build();
4545
ServerRequest request = new DefaultServerRequest(MockServerWebExchange.from(mockRequest), Collections.emptyList());
4646
Mono<Resource> result = function.apply(request);
@@ -124,4 +124,17 @@ void composeResourceLookupFunction() {
124124
.verify();
125125
}
126126

127+
@Test
128+
@SuppressWarnings("removal")
129+
void withPathResource() {
130+
org.springframework.core.io.PathResource location = new org.springframework.core.io.PathResource("/static/");
131+
PathResourceLookupFunction function = new PathResourceLookupFunction("/resources/**", location);
132+
}
133+
134+
@Test
135+
void withFileSystemResource() {
136+
FileSystemResource location = new FileSystemResource("/static/");
137+
PathResourceLookupFunction function = new PathResourceLookupFunction("/resources/**", location);
138+
}
139+
127140
}

Diff for: spring-webmvc/src/main/java/org/springframework/web/servlet/resource/ResourceHandlerUtils.java

+7-4
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
import org.apache.commons.logging.LogFactory;
2525

2626
import org.springframework.core.io.ClassPathResource;
27-
import org.springframework.core.io.PathResource;
27+
import org.springframework.core.io.FileSystemResource;
2828
import org.springframework.core.io.Resource;
2929
import org.springframework.core.io.UrlResource;
3030
import org.springframework.core.log.LogFormatUtils;
@@ -58,15 +58,18 @@ public static void assertResourceLocation(@Nullable Resource location) {
5858
Assert.notNull(location, "Resource location must not be null");
5959
try {
6060
String path;
61-
if (location instanceof PathResource) {
61+
if (location instanceof org.springframework.core.io.PathResource) {
6262
return;
6363
}
64-
else if (location instanceof UrlResource) {
65-
path = location.getURL().toExternalForm();
64+
else if (location instanceof FileSystemResource fileSystemResource) {
65+
path = fileSystemResource.getPath();
6666
}
6767
else if (location instanceof ClassPathResource classPathResource) {
6868
path = classPathResource.getPath();
6969
}
70+
else if (location instanceof UrlResource) {
71+
path = location.getURL().toExternalForm();
72+
}
7073
else {
7174
path = location.getURL().getPath();
7275
}

Diff for: spring-webmvc/src/test/java/org/springframework/web/servlet/function/PathResourceLookupFunctionTests.java

+15-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2023 the original author or authors.
2+
* Copyright 2002-2025 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.
@@ -24,6 +24,7 @@
2424
import org.junit.jupiter.api.Test;
2525

2626
import org.springframework.core.io.ClassPathResource;
27+
import org.springframework.core.io.FileSystemResource;
2728
import org.springframework.core.io.Resource;
2829
import org.springframework.web.servlet.handler.PathPatternsTestUtils;
2930

@@ -102,4 +103,17 @@ private ServerRequest initRequest(String httpMethod, String requestUri) {
102103
Collections.emptyList());
103104
}
104105

106+
@Test
107+
@SuppressWarnings("removal")
108+
void withPathResource() {
109+
org.springframework.core.io.PathResource location = new org.springframework.core.io.PathResource("/static/");
110+
PathResourceLookupFunction function = new PathResourceLookupFunction("/resources/**", location);
111+
}
112+
113+
@Test
114+
void withFileSystemResource() {
115+
FileSystemResource location = new FileSystemResource("/static/");
116+
PathResourceLookupFunction function = new PathResourceLookupFunction("/resources/**", location);
117+
}
118+
105119
}

0 commit comments

Comments
 (0)