Skip to content

Commit 98bb779

Browse files
committed
slightly more readable version
1 parent 291aedd commit 98bb779

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

src/main/java/io/github/pixee/security/ZipSecurity.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,8 @@ public ZipEntry getNextEntry() throws IOException {
6868

6969
private boolean containsEscapesAndTargetsBelowRoot(final String name) {
7070
if (name.contains("../") || name.contains("..\\")) {
71-
final File fileWithEscapes = new File(name);
7271
try {
73-
if (isBelowOrSisterToCurrentDirectory(fileWithEscapes)) {
72+
if (isBelowOrSisterToCurrentDirectory(name)) {
7473
return true;
7574
}
7675
} catch (IOException e) {
@@ -80,11 +79,14 @@ private boolean containsEscapesAndTargetsBelowRoot(final String name) {
8079
return false;
8180
}
8281

83-
private boolean isBelowOrSisterToCurrentDirectory(final File fileWithEscapes) throws IOException {
84-
final File currentDirectory = new File("");
85-
Path currentPathRoot = currentDirectory.getCanonicalFile().toPath();
86-
Path pathWithEscapes = fileWithEscapes.getCanonicalFile().toPath();
87-
return pathWithEscapes.startsWith(currentPathRoot) || pathWithEscapes.getParent().equals(currentPathRoot.getParent());
82+
private boolean isBelowOrSisterToCurrentDirectory(final String untrustedFileWithEscapes) throws IOException {
83+
// Get the absolute path of the current directory
84+
final File currentDirectory = new File("").getCanonicalFile();
85+
final Path currentPathRoot = currentDirectory.toPath();
86+
// Get the absolute path of the untrusted file
87+
final File untrustedFile = new File(currentDirectory, untrustedFileWithEscapes);
88+
final Path pathWithEscapes = untrustedFile.getCanonicalFile().toPath();
89+
return !pathWithEscapes.startsWith(currentPathRoot);
8890
}
8991

9092
private boolean isRootFileEntry(final String name) {

0 commit comments

Comments
 (0)