Skip to content

Commit

Permalink
Replace String::startsWith with Path::startsWith in isFileOutsideDir …
Browse files Browse the repository at this point in the history
…+ unit test
  • Loading branch information
shukiavraham committed May 14, 2022
1 parent 728ff3e commit 77e276d
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
9 changes: 1 addition & 8 deletions src/main/java/io/whitesource/cure/FileSecurityUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,7 @@ public static boolean isFileOutsideDir(
@NonNull final String filePath, @NonNull final String baseDirPath) throws IOException {
File file = new File(filePath);
File baseDir = new File(baseDirPath);
return !file.getCanonicalPath().startsWith(addTrailingSeparator(baseDir.getCanonicalPath()));
}

private static String addTrailingSeparator(String path) {
if (!path.endsWith(File.separator)) {
return path + File.separator;
}
return path;
return !file.getCanonicalFile().toPath().startsWith(baseDir.getCanonicalFile().toPath());
}

/**
Expand Down
7 changes: 7 additions & 0 deletions src/test/java/io/whitesource/cure/FileSecurityUtilsTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@ void normalize_validInput_successfullyWithResult() {
Assertions.assertEquals(expectedResult, actualResult);
}

@Test
void isFileOutsideDirStartsWithTest() throws IOException {
String taintedInput = "/usr/foo/../foo-bar/bar";
String baseDir = "/usr/foo";
Assertions.assertTrue(FileSecurityUtils.isFileOutsideDir(taintedInput, baseDir));
}

@Test
void normalize_null_successfully() {
Assertions.assertNull(FileSecurityUtils.normalize(null));
Expand Down

0 comments on commit 77e276d

Please sign in to comment.