Skip to content

Commit e78179b

Browse files
committed
Decode static resource path with UriUtils
Closes gh-33859
1 parent 49a63e2 commit e78179b

File tree

4 files changed

+34
-28
lines changed

4 files changed

+34
-28
lines changed

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

+8-7
Original file line numberDiff line numberDiff line change
@@ -149,21 +149,22 @@ else if (path.charAt(i) > ' ' && path.charAt(i) != 127) {
149149

150150
private static String normalizePath(String path) {
151151
String result = path;
152+
result = decode(result);
152153
if (result.contains("%")) {
153154
result = decode(result);
154-
if (result.contains("%")) {
155-
result = decode(result);
156-
}
157-
if (result.contains("../")) {
158-
return StringUtils.cleanPath(result);
159-
}
155+
}
156+
if (!StringUtils.hasText(result)) {
157+
return result;
158+
}
159+
if (result.contains("../")) {
160+
return StringUtils.cleanPath(result);
160161
}
161162
return path;
162163
}
163164

164165
private static String decode(String path) {
165166
try {
166-
return URLDecoder.decode(path, StandardCharsets.UTF_8);
167+
return UriUtils.decode(path, StandardCharsets.UTF_8);
167168
}
168169
catch (Exception ex) {
169170
return "";

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

+9-7
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
import org.springframework.web.server.MethodNotAllowedException;
5757
import org.springframework.web.server.ServerWebExchange;
5858
import org.springframework.web.server.WebHandler;
59+
import org.springframework.web.util.UriUtils;
5960
import org.springframework.web.util.pattern.PathPattern;
6061

6162
/**
@@ -568,21 +569,22 @@ else if (path.charAt(i) > ' ' && path.charAt(i) != 127) {
568569

569570
private static String normalizePath(String path) {
570571
String result = path;
572+
result = decode(result);
571573
if (result.contains("%")) {
572574
result = decode(result);
573-
if (result.contains("%")) {
574-
result = decode(result);
575-
}
576-
if (result.contains("../")) {
577-
return StringUtils.cleanPath(result);
578-
}
575+
}
576+
if (!StringUtils.hasText(result)) {
577+
return result;
578+
}
579+
if (result.contains("../")) {
580+
return StringUtils.cleanPath(result);
579581
}
580582
return path;
581583
}
582584

583585
private static String decode(String path) {
584586
try {
585-
return URLDecoder.decode(path, StandardCharsets.UTF_8);
587+
return UriUtils.decode(path, StandardCharsets.UTF_8);
586588
}
587589
catch (Exception ex) {
588590
return "";

Diff for: spring-webmvc/src/main/java/org/springframework/web/servlet/function/PathResourceLookupFunction.java

+8-7
Original file line numberDiff line numberDiff line change
@@ -150,21 +150,22 @@ else if (path.charAt(i) > ' ' && path.charAt(i) != 127) {
150150

151151
private static String normalizePath(String path) {
152152
String result = path;
153+
result = decode(result);
153154
if (result.contains("%")) {
154155
result = decode(result);
155-
if (result.contains("%")) {
156-
result = decode(result);
157-
}
158-
if (result.contains("../")) {
159-
return StringUtils.cleanPath(result);
160-
}
156+
}
157+
if (!StringUtils.hasText(result)) {
158+
return result;
159+
}
160+
if (result.contains("../")) {
161+
return StringUtils.cleanPath(result);
161162
}
162163
return path;
163164
}
164165

165166
private static String decode(String path) {
166167
try {
167-
return URLDecoder.decode(path, StandardCharsets.UTF_8);
168+
return UriUtils.decode(path, StandardCharsets.UTF_8);
168169
}
169170
catch (Exception ex) {
170171
return "";

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

+9-7
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
import org.springframework.web.cors.CorsConfigurationSource;
6464
import org.springframework.web.servlet.HandlerMapping;
6565
import org.springframework.web.servlet.support.WebContentGenerator;
66+
import org.springframework.web.util.UriUtils;
6667
import org.springframework.web.util.UrlPathHelper;
6768

6869
/**
@@ -727,21 +728,22 @@ else if (path.charAt(i) > ' ' && path.charAt(i) != 127) {
727728

728729
private static String normalizePath(String path) {
729730
String result = path;
731+
result = decode(result);
730732
if (result.contains("%")) {
731733
result = decode(result);
732-
if (result.contains("%")) {
733-
result = decode(result);
734-
}
735-
if (result.contains("../")) {
736-
return StringUtils.cleanPath(result);
737-
}
734+
}
735+
if (!StringUtils.hasText(result)) {
736+
return result;
737+
}
738+
if (result.contains("../")) {
739+
return StringUtils.cleanPath(result);
738740
}
739741
return path;
740742
}
741743

742744
private static String decode(String path) {
743745
try {
744-
return URLDecoder.decode(path, StandardCharsets.UTF_8);
746+
return UriUtils.decode(path, StandardCharsets.UTF_8);
745747
}
746748
catch (Exception ex) {
747749
return "";

0 commit comments

Comments
 (0)