Fix Zip Slip vulnerability in JAR extraction #453
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
Fixes a Zip Slip vulnerability in
JarUtil.extract()
that could allow arbitrary file writes outside the intended extraction directory when processing malicious JAR files.Vulnerability Details
The Zip Slip vulnerability is a critical security issue that occurs during archive extraction when path traversal sequences in entry names are not properly validated. An attacker could craft a malicious JAR file with entries like
../../etc/passwd
orsubdir/../../evil.txt
to write files outside the intended extraction directory, potentially overwriting system files or placing malicious content in sensitive locations.Root Cause
The original code attempted to validate paths but did not normalize them before the security check:
This allowed paths containing
..
segments to bypass validation becauseresolve()
does not automatically normalize the path.Changes Made
1. Normalize resolved paths before validation
Added
.normalize()
to canonicalize the resolved path before the security check:This ensures all
..
and.
path segments are resolved, making thestartsWith()
check effective at detecting path traversal attempts.2. Ensure parent directories exist
Added parent directory creation before writing files:
This ensures files can be extracted even when their parent directories are not explicitly listed as separate entries in the JAR.
Testing
Added comprehensive test coverage in
JarUtilTest.java
:../../evil.txt
) are properly blockedsubdir/../../evil.txt
) are properly blockedAll tests pass successfully, confirming the vulnerability is fixed without breaking existing functionality.
Impact
This is a minimal, surgical fix that:
Resolves #1 (code scanning alert for Zip Slip vulnerability)
Warning
Firewall rules blocked me from connecting to one or more addresses (expand for details)
I tried to connect to the following addresses, but was blocked by firewall rules:
repo.jenkins-ci.org
/usr/lib/jvm/temurin-17-jdk-amd64/bin/java --enable-native-access=ALL-UNNAMED -classpath /usr/share/apache-maven-3.9.11/boot/plexus-classworlds-2.9.0.jar -Dclassworlds.conf=/usr/share/apache-maven-3.9.11/bin/m2.conf -Dmaven.home=/usr/share/apache-maven-3.9.11 -Dlibrary.jansi.path=/usr/share/apache-maven-3.9.11/lib/jansi-native -Dmaven.multiModuleProjectDirectory=/home/REDACTED/work/plexus-compiler/plexus-compiler/plexus-compiler-its/target/it/MCOMPILER-346-mre org.codehaus.plexus.classworlds.launcher.Launcher -B -D maven.repo.local=/home/REDACTED/work/plexus-compiler/plexus-compiler/plexus-compiler-its/target/local-repo -s /home/REDACTED/work/plexus-compiler/plexus-compiler/plexus-compiler-its/target/it/interpolated-settings.xml clean compile
(dns block)If you need me to access, download, or install something from one of these locations, you can either:
Original prompt
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.