Add path traversal guard to FileLocalizer.expand() - #16188
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.
|
Gentle bump on this one. It adds a path-traversal guard to FileLocalizer.expand() and has been open since mid-July with no review yet. Could someone take a look when convenient? @sahusanket @GnsP. Thanks. |
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.