Fix directory traversal vulnerability in Expand.extractFile #295
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
This PR fixes a critical directory traversal vulnerability in the
extractFile
method oforg.codehaus.plexus.util.Expand
that could allow malicious ZIP files to write files outside the intended extraction directory.Vulnerability Details
The original implementation used
getAbsolutePath()
with a simplestartsWith()
check for path validation:This approach had multiple security issues:
1. Partial Prefix Match Bypass
The most critical issue was that string prefix matching allows escaping to sibling directories with similar names:
Since
/tmp/app-data/evil.txt
starts with the string/tmp/app
, the check incorrectly passes, allowing files to be written outside the target directory.2. Symlink Bypass
Using
getAbsolutePath()
instead ofgetCanonicalPath()
means symlinks in the path are not resolved, potentially allowing bypasses on systems where the target directory or its parents contain symbolic links.3. Case Sensitivity Issues
On case-insensitive filesystems, the absolute path comparison could fail to detect escapes due to case differences.
The Fix
The fix uses canonical paths with proper separator-aware prefix checking:
Key Improvements:
File.separator
to prevent partial prefix matchesTest Coverage
Added comprehensive test suite in
ExpandTest.java
covering:../
sequencesAll 250 tests pass (245 existing + 5 new security tests).
Impact
This vulnerability could allow an attacker to:
The fix prevents all these attack vectors while maintaining full backward compatibility with legitimate use cases.
Related
This follows the security best practice of using canonical path validation as recommended by OWASP and CWE-22 (Improper Limitation of a Pathname to a Restricted Directory).
Original prompt
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.