Skip to content

Add path traversal guard to FileLocalizer.expand() - #16188

Open
herdiyana256 wants to merge 2 commits into
cdapio:developfrom
herdiyana256:fix-zipslip-filelocalizer-path-traversal
Open

Add path traversal guard to FileLocalizer.expand()#16188
herdiyana256 wants to merge 2 commits into
cdapio:developfrom
herdiyana256:fix-zipslip-filelocalizer-path-traversal

Conversation

@herdiyana256

Copy link
Copy Markdown

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:

Path outputPath = targetPath.resolve(entry.getName());

An entry name containing ../ segments resolves outside the intended target directory.

BundleJarUtil.unJar() already guards against exactly this in the same codebase:

Path output = targetPath.resolve(entryName).normalize();
if (!output.startsWith(targetPath)) {
  throw new IllegalArgumentException(...);
}

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.

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.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.
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.

1 participant