Skip to content
This repository was archived by the owner on Sep 3, 2025. It is now read-only.

Commit 77e276d

Browse files
committed
Replace String::startsWith with Path::startsWith in isFileOutsideDir + unit test
1 parent 728ff3e commit 77e276d

File tree

2 files changed

+8
-8
lines changed

2 files changed

+8
-8
lines changed

src/main/java/io/whitesource/cure/FileSecurityUtils.java

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,7 @@ public static boolean isFileOutsideDir(
2222
@NonNull final String filePath, @NonNull final String baseDirPath) throws IOException {
2323
File file = new File(filePath);
2424
File baseDir = new File(baseDirPath);
25-
return !file.getCanonicalPath().startsWith(addTrailingSeparator(baseDir.getCanonicalPath()));
26-
}
27-
28-
private static String addTrailingSeparator(String path) {
29-
if (!path.endsWith(File.separator)) {
30-
return path + File.separator;
31-
}
32-
return path;
25+
return !file.getCanonicalFile().toPath().startsWith(baseDir.getCanonicalFile().toPath());
3326
}
3427

3528
/**

src/test/java/io/whitesource/cure/FileSecurityUtilsTests.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,13 @@ void normalize_validInput_successfullyWithResult() {
5050
Assertions.assertEquals(expectedResult, actualResult);
5151
}
5252

53+
@Test
54+
void isFileOutsideDirStartsWithTest() throws IOException {
55+
String taintedInput = "/usr/foo/../foo-bar/bar";
56+
String baseDir = "/usr/foo";
57+
Assertions.assertTrue(FileSecurityUtils.isFileOutsideDir(taintedInput, baseDir));
58+
}
59+
5360
@Test
5461
void normalize_null_successfully() {
5562
Assertions.assertNull(FileSecurityUtils.normalize(null));

0 commit comments

Comments
 (0)