Skip to content

Potential fix for code scanning alert no. 1: Arbitrary file access during archive extraction ("Zip Slip") #445

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/main/java/org/apache/ibatis/migration/io/DefaultVFS.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,16 @@
if (log.isLoggable(Level.FINER)) {
log.log(Level.FINER, "Jar entry: " + entry.getName());
}
children.add(entry.getName());
String entryName = 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
.
Unsanitized archive entry, which may contain '..', is used in a
file system operation
.

Copilot Autofix

AI about 1 month ago

To fix the issue, we need to ensure that the entry.getName() value is sanitized and validated before it is used in any file system operations or added to the children list. Specifically:

  1. Use File.getCanonicalFile() to normalize the file path and ensure it does not escape the intended base directory.
  2. Perform the validation check immediately after constructing the file path and before adding it to the children list or performing any other operations.
  3. Log a warning and skip any entries that fail the validation.

The fix will involve modifying the code in the list method to ensure that all file paths derived from entry.getName() are properly validated.


Suggested changeset 1
src/main/java/org/apache/ibatis/migration/io/DefaultVFS.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/org/apache/ibatis/migration/io/DefaultVFS.java b/src/main/java/org/apache/ibatis/migration/io/DefaultVFS.java
--- a/src/main/java/org/apache/ibatis/migration/io/DefaultVFS.java
+++ b/src/main/java/org/apache/ibatis/migration/io/DefaultVFS.java
@@ -91,3 +91,4 @@
                 }
-                children.add(entryName);
+                // Add only the sanitized and validated entry name
+                children.add(entryFile.getName());
               }
EOF
@@ -91,3 +91,4 @@
}
children.add(entryName);
// Add only the sanitized and validated entry name
children.add(entryFile.getName());
}
Copilot is powered by AI and may make mistakes. Always verify output.
File entryFile = new File(path, entryName).getCanonicalFile();
File baseDir = new File(path).getCanonicalFile();
if (!entryFile.toPath().startsWith(baseDir.toPath())) {
if (log.isLoggable(Level.WARNING)) {
log.log(Level.WARNING, "Skipping potentially unsafe entry: " + entryName);
}
continue;
}
children.add(entryName);
}
}
} else {
Expand Down
Loading