Skip to content

Commit

Permalink
Decode static resource path with UriUtils
Browse files Browse the repository at this point in the history
Closes gh-33859
  • Loading branch information
rstoyanchev committed Nov 12, 2024
1 parent 49a63e2 commit e78179b
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -149,21 +149,22 @@ else if (path.charAt(i) > ' ' && path.charAt(i) != 127) {

private static String normalizePath(String path) {
String result = path;
result = decode(result);
if (result.contains("%")) {
result = decode(result);
if (result.contains("%")) {
result = decode(result);
}
if (result.contains("../")) {
return StringUtils.cleanPath(result);
}
}
if (!StringUtils.hasText(result)) {
return result;
}
if (result.contains("../")) {
return StringUtils.cleanPath(result);
}
return path;
}

private static String decode(String path) {
try {
return URLDecoder.decode(path, StandardCharsets.UTF_8);
return UriUtils.decode(path, StandardCharsets.UTF_8);
}
catch (Exception ex) {
return "";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
import org.springframework.web.server.MethodNotAllowedException;
import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.server.WebHandler;
import org.springframework.web.util.UriUtils;
import org.springframework.web.util.pattern.PathPattern;

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

private static String normalizePath(String path) {
String result = path;
result = decode(result);
if (result.contains("%")) {
result = decode(result);
if (result.contains("%")) {
result = decode(result);
}
if (result.contains("../")) {
return StringUtils.cleanPath(result);
}
}
if (!StringUtils.hasText(result)) {
return result;
}
if (result.contains("../")) {
return StringUtils.cleanPath(result);
}
return path;
}

private static String decode(String path) {
try {
return URLDecoder.decode(path, StandardCharsets.UTF_8);
return UriUtils.decode(path, StandardCharsets.UTF_8);
}
catch (Exception ex) {
return "";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,21 +150,22 @@ else if (path.charAt(i) > ' ' && path.charAt(i) != 127) {

private static String normalizePath(String path) {
String result = path;
result = decode(result);
if (result.contains("%")) {
result = decode(result);
if (result.contains("%")) {
result = decode(result);
}
if (result.contains("../")) {
return StringUtils.cleanPath(result);
}
}
if (!StringUtils.hasText(result)) {
return result;
}
if (result.contains("../")) {
return StringUtils.cleanPath(result);
}
return path;
}

private static String decode(String path) {
try {
return URLDecoder.decode(path, StandardCharsets.UTF_8);
return UriUtils.decode(path, StandardCharsets.UTF_8);
}
catch (Exception ex) {
return "";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
import org.springframework.web.cors.CorsConfigurationSource;
import org.springframework.web.servlet.HandlerMapping;
import org.springframework.web.servlet.support.WebContentGenerator;
import org.springframework.web.util.UriUtils;
import org.springframework.web.util.UrlPathHelper;

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

private static String normalizePath(String path) {
String result = path;
result = decode(result);
if (result.contains("%")) {
result = decode(result);
if (result.contains("%")) {
result = decode(result);
}
if (result.contains("../")) {
return StringUtils.cleanPath(result);
}
}
if (!StringUtils.hasText(result)) {
return result;
}
if (result.contains("../")) {
return StringUtils.cleanPath(result);
}
return path;
}

private static String decode(String path) {
try {
return URLDecoder.decode(path, StandardCharsets.UTF_8);
return UriUtils.decode(path, StandardCharsets.UTF_8);
}
catch (Exception ex) {
return "";
Expand Down

0 comments on commit e78179b

Please sign in to comment.