Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(CodeQL) Fixed finding: "Arbitrary file access during archive extraction ("Zip Slip") " #2344

Merged
merged 1 commit into from
Nov 27, 2024

Conversation

pixeebot[bot]
Copy link
Contributor

@pixeebot pixeebot bot commented Nov 27, 2024

Remediation

This change fixes "Arbitrary file access during archive extraction ("Zip Slip")
" (id = zipslip) identified by CodeQL.

Details

This change fixes instances of ZipInputStream to protect against malicious entries that attempt to escape their "file root" and overwrite other files on the running filesystem.

Normally, when you're using ZipInputStream it's because you're processing zip files. That code might look like this:

File file = new File(unzipTargetDirectory, zipEntry.getName()); // use file name from zip entry
InputStream is = zip.getInputStream(zipEntry); // get the contents of the zip entry
IOUtils.copy(is, new FileOutputStream(file)); // write the contents to the provided file name

This looks fine when it encounters a normal zip entry within a zip file, looking something like this pseudo-data:

path: data/names.txt
contents: Zeus\nHelen\nLeda...

However, there's nothing to prevent an attacker from sending an evil entry in the zip that looks more like this:

path: ../../../../../etc/passwd
contents: root::0:0:root:/:/bin/sh

Yes, in the above code, which looks like every piece of zip-processing code you can find on the Internet, attackers could overwrite any files to which the application has access. This rule replaces the standard ZipInputStream with a hardened subclass which prevents access to entry paths that attempt to traverse directories above the current directory (which no normal zip file should ever do.) Our changes end up looking something like this:

+ import io.github.pixee.security.ZipSecurity;
  ...
- var zip = new ZipInputStream(is, StandardCharsets.UTF_8);
+ var zip = ZipSecurity.createHardenedInputStream(is, StandardCharsets.UTF_8);
More reading

🧚🤖 Powered by Pixeebot

Feedback | Community | Docs | Codemod ID: codeql:java/zipslip

@pixeebot pixeebot bot requested a review from Frooodle as a code owner November 27, 2024 03:43
@dosubot dosubot bot added the size:S This PR changes 10-29 lines, ignoring generated files. label Nov 27, 2024
@github-actions github-actions bot added the Java Pull requests that update Java code label Nov 27, 2024
@dosubot dosubot bot added the Bug Something isn't working label Nov 27, 2024
@@ -105,7 +105,7 @@
new ByteArrayInputStream(Files.readAllBytes(zipFilePath)))) {
ZipEntry entry = zipIn.getNextEntry();
while (entry != null) {
Path filePath = tempUnzippedDir.resolve(entry.getName());
Path filePath = tempUnzippedDir.resolve(sanitizeZipFilename(entry.getName()));

Check failure

Code scanning / CodeQL

Arbitrary file access during archive extraction ("Zip Slip") High

Unsanitized archive entry, which may contain '..', is used in a
file system operation
.
Unsanitized archive entry, which may contain '..', is used in a
file system operation
.
Unsanitized archive entry, which may contain '..', is used in a
file system operation
.

Copilot Autofix AI about 2 months ago

To fix the problem, we need to ensure that the output paths constructed from zip archive entries are validated to prevent writing files to unexpected locations. This can be achieved by verifying that the normalized full path of the output file starts with a prefix that matches the destination directory. We will use java.nio.file.Path.normalize() and java.nio.file.Path.startsWith(..) for this purpose.

  1. Modify the sanitizeHtmlFilesInZip method to include a check that ensures the normalized path of the extracted file starts with the temporary directory path.
  2. Update the sanitizeZipFilename method to be more robust in handling directory traversal sequences.
Suggested changeset 1
src/main/java/stirling/software/SPDF/utils/FileToPdf.java

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/src/main/java/stirling/software/SPDF/utils/FileToPdf.java b/src/main/java/stirling/software/SPDF/utils/FileToPdf.java
--- a/src/main/java/stirling/software/SPDF/utils/FileToPdf.java
+++ b/src/main/java/stirling/software/SPDF/utils/FileToPdf.java
@@ -107,3 +107,6 @@
             while (entry != null) {
-                Path filePath = tempUnzippedDir.resolve(sanitizeZipFilename(entry.getName()));
+                Path filePath = tempUnzippedDir.resolve(sanitizeZipFilename(entry.getName())).normalize();
+                if (!filePath.startsWith(tempUnzippedDir)) {
+                    throw new IOException("Bad zip entry: " + entry.getName());
+                }
                 if (!entry.isDirectory()) {
@@ -248,4 +251,5 @@
         }
-        while (entryName.contains("../") || entryName.contains("..\\")) {
-            entryName = entryName.replace("../", "").replace("..\\", "");
+        entryName = entryName.replace("\\", "/");
+        while (entryName.contains("../")) {
+            entryName = entryName.replace("../", "");
         }
EOF
@@ -107,3 +107,6 @@
while (entry != null) {
Path filePath = tempUnzippedDir.resolve(sanitizeZipFilename(entry.getName()));
Path filePath = tempUnzippedDir.resolve(sanitizeZipFilename(entry.getName())).normalize();
if (!filePath.startsWith(tempUnzippedDir)) {
throw new IOException("Bad zip entry: " + entry.getName());
}
if (!entry.isDirectory()) {
@@ -248,4 +251,5 @@
}
while (entryName.contains("../") || entryName.contains("..\\")) {
entryName = entryName.replace("../", "").replace("..\\", "");
entryName = entryName.replace("\\", "/");
while (entryName.contains("../")) {
entryName = entryName.replace("../", "");
}
Copilot is powered by AI and may make mistakes. Always verify output.
Positive Feedback
Negative Feedback

Provide additional feedback

Please help us improve GitHub Copilot by sharing more details about this comment.

Please select one or more of the options
@@ -175,7 +175,7 @@
ZipSecurity.createHardenedInputStream(new ByteArrayInputStream(fileBytes))) {
ZipEntry entry = zipIn.getNextEntry();
while (entry != null) {
Path filePath = tempDirectory.resolve(entry.getName());
Path filePath = tempDirectory.resolve(sanitizeZipFilename(entry.getName()));

Check failure

Code scanning / CodeQL

Arbitrary file access during archive extraction ("Zip Slip") High

Unsanitized archive entry, which may contain '..', is used in a
file system operation
.
Unsanitized archive entry, which may contain '..', is used in a
file system operation
.
Unsanitized archive entry, which may contain '..', is used in a
file system operation
.

Copilot Autofix AI about 2 months ago

To fix the problem, we need to ensure that the output paths constructed from zip archive entries are validated to prevent writing files to unexpected locations. This can be achieved by verifying that the normalized full path of the output file starts with a prefix that matches the destination directory. We will use java.nio.file.Path.normalize() and java.nio.file.Path.startsWith(..) for this purpose.

  1. Modify the unzipAndGetMainHtml method to validate the constructed file paths.
  2. Ensure that the sanitizeZipFilename method is robust enough to handle all potential directory traversal sequences.
Suggested changeset 1
src/main/java/stirling/software/SPDF/utils/FileToPdf.java

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/src/main/java/stirling/software/SPDF/utils/FileToPdf.java b/src/main/java/stirling/software/SPDF/utils/FileToPdf.java
--- a/src/main/java/stirling/software/SPDF/utils/FileToPdf.java
+++ b/src/main/java/stirling/software/SPDF/utils/FileToPdf.java
@@ -177,3 +177,6 @@
             while (entry != null) {
-                Path filePath = tempDirectory.resolve(sanitizeZipFilename(entry.getName()));
+                Path filePath = tempDirectory.resolve(sanitizeZipFilename(entry.getName())).normalize();
+                if (!filePath.startsWith(tempDirectory)) {
+                    throw new IOException("Bad zip entry: " + entry.getName());
+                }
                 if (entry.isDirectory()) {
EOF
@@ -177,3 +177,6 @@
while (entry != null) {
Path filePath = tempDirectory.resolve(sanitizeZipFilename(entry.getName()));
Path filePath = tempDirectory.resolve(sanitizeZipFilename(entry.getName())).normalize();
if (!filePath.startsWith(tempDirectory)) {
throw new IOException("Bad zip entry: " + entry.getName());
}
if (entry.isDirectory()) {
Copilot is powered by AI and may make mistakes. Always verify output.
Positive Feedback
Negative Feedback

Provide additional feedback

Please help us improve GitHub Copilot by sharing more details about this comment.

Please select one or more of the options
@Frooodle Frooodle merged commit d832a90 into main Nov 27, 2024
9 of 10 checks passed
@pixeebot pixeebot bot deleted the pixeebot/drip-2024-11-27-codeql-java/zipslip branch November 27, 2024 07:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Something isn't working Java Pull requests that update Java code size:S This PR changes 10-29 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant