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

Conversation

hazendaz
Copy link
Member

Potential fix for https://github.com/mybatis/migrations/security/code-scanning/1

To fix the issue, we need to validate the entry.getName() value to ensure it does not contain directory traversal sequences (../) or absolute paths. This can be achieved by normalizing the constructed path and verifying that it remains within the intended base directory. Specifically:

  1. Use java.nio.file.Path to construct and normalize the path.
  2. Check that the normalized path starts with the intended base directory.
  3. If the validation fails, skip the entry or throw an exception.

The fix will be applied in the listResources method (or equivalent logic) where entry.getName() is processed.


Suggested fixes powered by Copilot Autofix. Review carefully before merging.

…ring archive extraction ("Zip Slip")

Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
@hazendaz hazendaz self-assigned this Apr 28, 2025
@hazendaz hazendaz marked this pull request as ready for review April 28, 2025 16:49
@coveralls
Copy link

Coverage Status

coverage: 80.624% (-0.2%) from 80.873%
when pulling 2d1114b on alert-autofix-1
into 66ba52a on master.

@@ -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 11 days 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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants