Skip to content

Commit 0ab5d20

Browse files
author
drighetto
committed
Debug UT
1 parent 4b8ab93 commit 0ab5d20

File tree

1 file changed

+12
-18
lines changed

1 file changed

+12
-18
lines changed

src/main/java/eu/righettod/SecurityUtils.java

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1157,10 +1157,8 @@ public static String applyURLDecoding(String encodedData, int decodingRoundThres
11571157
* Apply a collection of validations on a string expected to be an system file/folder path:
11581158
* <ul>
11591159
* <li>Does not contains path traversal payload.</li>
1160+
* <li>The canonical path is equals to the absolute path.</li>
11601161
* </ul><br>
1161-
* <p>
1162-
* <b>Note:</b> This implementation is sensitive to the current folder expression <code>./</code> and <code>.\</code> - Therefore <b>it will consider the path as unsafe</b> when it contains such expression.
1163-
* </p>
11641162
*
11651163
* @param path String expected to be a valid system file/folder path.
11661164
* @return True only if the string pass all validations.
@@ -1176,22 +1174,18 @@ public static boolean isPathSafe(String path) {
11761174
if (path != null && !path.isEmpty()) {
11771175
//URL decode the path if case of data coming from a web context
11781176
String decodedPath = applyURLDecoding(path, decodingRoundThreshold);
1179-
//Remove any path escaping sequence
1180-
if (File.separatorChar == '/') {
1181-
decodedPath = decodedPath.replace("\\", "");
1182-
} else {
1183-
decodedPath = decodedPath.replace("\\\\", "");
1177+
//Ensure that no path traversal expression is present
1178+
if (!decodedPath.contains("..")) {
1179+
File f = new File(decodedPath);
1180+
String canonicalPath = f.getCanonicalPath();
1181+
String absolutePath = f.getAbsolutePath();
1182+
System.out.println("---");
1183+
System.out.printf("IN PATH : %s\n", path);
1184+
System.out.printf("DECODED PATH: %s\n", decodedPath);
1185+
System.out.printf("CANONICAL PATH: %s\n", canonicalPath);
1186+
System.out.printf("ABSOLUTE PATH: %s\n", absolutePath);
1187+
isSafe = canonicalPath.equals(absolutePath);
11841188
}
1185-
//Ensure that no path traversal path is present
1186-
File f = new File(decodedPath);
1187-
String canonicalPath = f.getCanonicalPath();
1188-
String absolutePath = f.getAbsolutePath();
1189-
System.out.println("---");
1190-
System.out.printf("IN PATH : %s\n", path);
1191-
System.out.printf("DECODED PATH: %s\n", decodedPath);
1192-
System.out.printf("CANONICAL PATH: %s\n", canonicalPath);
1193-
System.out.printf("ABSOLUTE PATH: %s\n", absolutePath);
1194-
isSafe = canonicalPath.equals(absolutePath);
11951189
}
11961190
} catch (Exception e) {
11971191
isSafe = false;

0 commit comments

Comments
 (0)