Add path traversal guard to FileLocalizer.expand() - #16188
Open
herdiyana256 wants to merge 2 commits into
Open
Conversation
Zip entry names extracted by FileLocalizer.expand() were resolved against the target directory with no normalization or containment check, so an entry name such as ../../evil.txt would resolve and write outside the intended target directory. This applies the same normalize-and-startsWith guard already used by BundleJarUtil.unJar() for the same class of input, and adds a FileLocalizerTest covering a rejected traversal entry and a normal entry that still extracts correctly.
There was a problem hiding this comment.
Code Review
This pull request introduces path traversal protection (Zip Slip mitigation) in FileLocalizer.java by normalizing the output path of zip entries and verifying that they remain within the target directory. It also adds corresponding unit tests in FileLocalizerTest.java. Feedback on the changes highlights a potential issue where targetPath is not normalized, which could lead to false-positive path traversal detections if the application is run from a path containing relative components. It is recommended to normalize both the target path and the output path to their absolute, normalized forms before performing the prefix check.
Files.createDirectories() returns the path as given, so if targetDir is relative or contains redundant components, comparing it against a normalized outputPath in startsWith() can reject legitimate entries. Resolve targetPath to its absolute, normalized form once before the loop so both sides of the comparison are in the same form.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
FileLocalizer.expand() unpacks archive resources into a target directory during pod localization. It resolves each zip entry name against the target directory with no normalization or containment check:
An entry name containing
../segments resolves outside the intended target directory.BundleJarUtil.unJar() already guards against exactly this in the same codebase:
This PR applies the same normalize-and-startsWith check to FileLocalizer.expand(), so any entry that would resolve outside the target directory is rejected with an IOException before any file operation happens.
Added FileLocalizerTest with two cases: a traversal entry is rejected and no file is written outside the target directory, and a normal entry still extracts to the expected path with the expected content.
Verification: I was not able to complete a full reactor build of cdap-kubernetes in my local environment (dependency resolution across the full module graph did not finish). I verified the exact patched method body in isolation (same logic, JDK-only) against both a malicious entry name and a normal entry, and it behaves as expected in both cases. The added FileLocalizerTest exercises the real class and should run under the project's normal CI.