Skip to content

Commit

Permalink
Merge branch '6.1.x'
Browse files Browse the repository at this point in the history
  • Loading branch information
sdeleuze committed Jun 10, 2024
2 parents 42c17eb + c97a895 commit 68e6b15
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
* @author Arjen Poutsma
* @author Sam Brannen
* @author Brian Clozel
* @author Sebastien Deleuze
* @since 16 April 2001
*/
public abstract class StringUtils {
Expand All @@ -71,6 +72,8 @@ public abstract class StringUtils {

private static final String WINDOWS_FOLDER_SEPARATOR = "\\";

private static final String DOUBLE_BACKLASHES = "\\\\";

private static final String TOP_PATH = "..";

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

String normalizedPath = replace(path, WINDOWS_FOLDER_SEPARATOR, FOLDER_SEPARATOR);
String normalizedPath;
// Optimize when there is no backslash
if (path.indexOf('\\') != -1) {
normalizedPath = replace(path, DOUBLE_BACKLASHES, FOLDER_SEPARATOR);
normalizedPath = replace(normalizedPath, WINDOWS_FOLDER_SEPARATOR, FOLDER_SEPARATOR);
}
else {
normalizedPath = path;
}
String pathToUse = normalizedPath;

// Shortcut if there is no work to do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,7 @@ void cleanPath() {
assertThat(StringUtils.cleanPath("file:///c:/some/../path/the%20file.txt")).isEqualTo("file:///c:/path/the%20file.txt");
assertThat(StringUtils.cleanPath("jar:file:///c:\\some\\..\\path\\.\\the%20file.txt")).isEqualTo("jar:file:///c:/path/the%20file.txt");
assertThat(StringUtils.cleanPath("jar:file:///c:/some/../path/./the%20file.txt")).isEqualTo("jar:file:///c:/path/the%20file.txt");
assertThat(StringUtils.cleanPath("jar:file:///c:\\\\some\\\\..\\\\path\\\\.\\\\the%20file.txt")).isEqualTo("jar:file:///c:/path/the%20file.txt");
}

@Test
Expand Down

0 comments on commit 68e6b15

Please sign in to comment.