Skip to content

Commit f6fddeb

Browse files
committed
Improve StringUtils.cleanPath
Issue: SPR-11793
1 parent c3ba0a2 commit f6fddeb

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

spring-core/src/main/java/org/springframework/util/StringUtils.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -621,7 +621,12 @@ public static String cleanPath(String path) {
621621
String prefix = "";
622622
if (prefixIndex != -1) {
623623
prefix = pathToUse.substring(0, prefixIndex + 1);
624-
pathToUse = pathToUse.substring(prefixIndex + 1);
624+
if (prefix.contains("/")) {
625+
prefix = "";
626+
}
627+
else {
628+
pathToUse = pathToUse.substring(prefixIndex + 1);
629+
}
625630
}
626631
if (pathToUse.startsWith(FOLDER_SEPARATOR)) {
627632
prefix = prefix + FOLDER_SEPARATOR;

spring-core/src/test/java/org/springframework/util/StringUtilsTests.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,8 @@ public void testCleanPath() {
299299
assertEquals("../mypath/myfile", StringUtils.cleanPath("../mypath/../mypath/myfile"));
300300
assertEquals("../mypath/myfile", StringUtils.cleanPath("mypath/../../mypath/myfile"));
301301
assertEquals("/../mypath/myfile", StringUtils.cleanPath("/../mypath/myfile"));
302+
assertEquals("/mypath/myfile", StringUtils.cleanPath("/a/:b/../../mypath/myfile"));
303+
assertEquals("file:///c:/path/to/the%20file.txt", StringUtils.cleanPath("file:///c:/some/../path/to/the%20file.txt"));
302304
}
303305

304306
public void testPathEquals() {

0 commit comments

Comments
 (0)