Skip to content

Commit

Permalink
Merge pull request #1223 from kermitt2/alert-autofix-61
Browse files Browse the repository at this point in the history
Fix code scanning alert no. 61: Arbitrary file access during archive extraction ("Zip Slip")
  • Loading branch information
lfoppiano authored Jan 10, 2025
2 parents 7c0bccf + 2cc090f commit 2ab61a6
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions grobid-service/src/main/java/org/grobid/service/util/ZipUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,19 +80,27 @@ public static final void main(String[] args) {
+ entry.getName());
// This is not robust, just for demonstration purposes.

(new File(tempDir.getAbsolutePath() + File.separator
+ entry.getName())).mkdir();
File dir = new File(tempDir.getAbsolutePath() + File.separator + entry.getName()).getCanonicalFile();
if (!dir.toPath().startsWith(tempDir.toPath())) {
throw new IOException("Bad zip entry: " + entry.getName());
}
dir.mkdir();
continue;
}

System.err.println("Extracting file: " + entry.getName());

copyInputStream(
zipFile.getInputStream(entry),
new BufferedOutputStream(new FileOutputStream(tempDir
new BufferedOutputStream(new FileOutputStream(new File(tempDir
.getAbsolutePath()
+ File.separator
+ entry.getName())));
+ entry.getName()).getCanonicalFile())));
File outFile = new File(tempDir.getAbsolutePath() + File.separator + entry.getName()).getCanonicalFile();
if (!outFile.toPath().startsWith(tempDir.toPath())) {
throw new IOException("Bad zip entry: " + entry.getName());
}
copyInputStream(zipFile.getInputStream(entry), new BufferedOutputStream(new FileOutputStream(outFile)));
}

zipFile.close();
Expand Down

0 comments on commit 2ab61a6

Please sign in to comment.