Skip to content

Commit 748167b

Browse files
committed
Improve StringUtils.cleanPath
Issue: SPR-11793
1 parent 3e70013 commit 748167b

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
@@ -622,7 +622,12 @@ public static String cleanPath(String path) {
622622
String prefix = "";
623623
if (prefixIndex != -1) {
624624
prefix = pathToUse.substring(0, prefixIndex + 1);
625-
pathToUse = pathToUse.substring(prefixIndex + 1);
625+
if (prefix.contains("/")) {
626+
prefix = "";
627+
}
628+
else {
629+
pathToUse = pathToUse.substring(prefixIndex + 1);
630+
}
626631
}
627632
if (pathToUse.startsWith(FOLDER_SEPARATOR)) {
628633
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)