Skip to content

Commit 68e6b15

Browse files
committed
Merge branch '6.1.x'
2 parents 42c17eb + c97a895 commit 68e6b15

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

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

+13-2
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
* @author Arjen Poutsma
6060
* @author Sam Brannen
6161
* @author Brian Clozel
62+
* @author Sebastien Deleuze
6263
* @since 16 April 2001
6364
*/
6465
public abstract class StringUtils {
@@ -71,6 +72,8 @@ public abstract class StringUtils {
7172

7273
private static final String WINDOWS_FOLDER_SEPARATOR = "\\";
7374

75+
private static final String DOUBLE_BACKLASHES = "\\\\";
76+
7477
private static final String TOP_PATH = "..";
7578

7679
private static final String CURRENT_PATH = ".";
@@ -695,7 +698,7 @@ public static String applyRelativePath(String path, String relativePath) {
695698
* Normalize the path by suppressing sequences like "path/.." and
696699
* inner simple dots.
697700
* <p>The result is convenient for path comparison. For other uses,
698-
* notice that Windows separators ("\") are replaced by simple slashes.
701+
* notice that Windows separators ("\" and "\\") are replaced by simple slashes.
699702
* <p><strong>NOTE</strong> that {@code cleanPath} should not be depended
700703
* upon in a security context. Other mechanisms should be used to prevent
701704
* path-traversal issues.
@@ -707,7 +710,15 @@ public static String cleanPath(String path) {
707710
return path;
708711
}
709712

710-
String normalizedPath = replace(path, WINDOWS_FOLDER_SEPARATOR, FOLDER_SEPARATOR);
713+
String normalizedPath;
714+
// Optimize when there is no backslash
715+
if (path.indexOf('\\') != -1) {
716+
normalizedPath = replace(path, DOUBLE_BACKLASHES, FOLDER_SEPARATOR);
717+
normalizedPath = replace(normalizedPath, WINDOWS_FOLDER_SEPARATOR, FOLDER_SEPARATOR);
718+
}
719+
else {
720+
normalizedPath = path;
721+
}
711722
String pathToUse = normalizedPath;
712723

713724
// Shortcut if there is no work to do

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

+1
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,7 @@ void cleanPath() {
419419
assertThat(StringUtils.cleanPath("file:///c:/some/../path/the%20file.txt")).isEqualTo("file:///c:/path/the%20file.txt");
420420
assertThat(StringUtils.cleanPath("jar:file:///c:\\some\\..\\path\\.\\the%20file.txt")).isEqualTo("jar:file:///c:/path/the%20file.txt");
421421
assertThat(StringUtils.cleanPath("jar:file:///c:/some/../path/./the%20file.txt")).isEqualTo("jar:file:///c:/path/the%20file.txt");
422+
assertThat(StringUtils.cleanPath("jar:file:///c:\\\\some\\\\..\\\\path\\\\.\\\\the%20file.txt")).isEqualTo("jar:file:///c:/path/the%20file.txt");
422423
}
423424

424425
@Test

0 commit comments

Comments
 (0)