Skip to content

Enforce canonical path prefix check in ZipFiles.unzipFile - #41497

Open
derrickaw wants to merge 1 commit into
masterfrom
codescan/java-zipslip-zipfiles
Open

derrickaw wants to merge 1 commit into
masterfrom
codescan/java-zipslip-zipfiles

Conversation

@derrickaw

Copy link
Copy Markdown
Collaborator

Note: this PR was auto-generated by tooling. Before moving it out of draft, the author has done a full review of the PR, specifically evaluating it on the following criteria:

  • The PR remediates confirmed, live Code Scanning finding(s) (CodeQL / zizmor) on master without altering intended public API or workflow semantics
  • The fix is minimal, idiomatic, and avoids suppressing alerts or masking underlying defects
  • Relevant unit tests, linters, and CI checks have been verified

If you have concerns with the PR or with the underlying methodology used to generate this, please comment below - they will be addressed by the author not the tooling :)

Summary

  • Resolves CodeQL java/zipslip (CWE-022, High/Error, alert #89) in org.apache.beam.sdk.util.ZipFiles.unzipFile.
  • Replaces manual .. string-splitting in checkName with canonical Path.startsWith verification (targetFile.getCanonicalFile().toPath().startsWith(canonicalTargetDir)) prior to creating directories or writing extracted files, preventing directory traversal, sibling-prefix bypasses (../target_sibling/...), and symlink escapes outside targetDirectory.
  • Adds regression unit tests in ZipFilesTest covering Zip Slip traversal entries (../escaped.txt, nested/../../escaped.txt, ../target_sibling/escaped.txt) while preserving valid filenames containing double dots (foo..bar.txt).

Verification

  • ./gradlew :sdks:java:core:spotlessCheck :sdks:java:core:test --tests org.apache.beam.sdk.util.ZipFilesTest

@derrickaw

Copy link
Copy Markdown
Collaborator Author

/gemini review

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

📋 Review Summary

This PR replaces the manual .. path component check in ZipFiles.unzipFile with canonical Path.startsWith validation against the target directory, resolving the CodeQL java/zipslip finding. The implementation and accompanying unit tests in ZipFilesTest are clean, accurate, and thoroughly cover traversal and sibling-prefix edge cases.

🔍 General Feedback

  • Using targetFile.getCanonicalFile().toPath().startsWith(canonicalTargetDir) properly enforces path-component prefix matching rather than raw string prefix matching, preventing sibling directory bypasses (../target_sibling/...) as well as symlink escapes.
  • Regression tests cover both rejected traversal paths and valid filenames containing ...

💡 Tip: Comment /gemini review on this pull request to trigger a new Gemini code review.

@github-actions github-actions Bot added java core size/s S: 10-49 lines changed labels Nov 20, 2026
@derrickaw derrickaw added the ready-for-human Draft PR with all checks passing and all review comments resolved, ready for human review label Nov 20, 2026
@codecov

codecov Bot commented Nov 20, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 59.38%. Comparing base (5d834f2) to head (64557d7).
⚠️ Report is 153 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff            @@
##             master   #41497   +/-   ##
=========================================
  Coverage     59.38%   59.38%           
  Complexity     5137     5137           
=========================================
  Files          1470     1470           
  Lines        187512   187512           
  Branches       3819     3819           
=========================================
  Hits         111351   111351           
  Misses        72756    72756           
  Partials       3405     3405           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@derrickaw derrickaw removed the ready-for-human Draft PR with all checks passing and all review comments resolved, ready for human review label Nov 21, 2026
@derrickaw
derrickaw marked this pull request as ready for review November 21, 2026 02:24
@github-actions

Copy link
Copy Markdown
Contributor

Assigning reviewers:

R: @Abacn for label java.
Backup reviewer: damccorm (if the main reviewer is unresponsive, you can escalate to them by tagging their username)

This pull request likely touches a core component ("core" label). Please review with scrutiny.

Note: If you would like to opt out of this review, comment assign to next reviewer.

Available commands:

  • stop reviewer notifications - opt out of the automated review tooling
  • remind me after tests pass - tag the comment author after tests pass
  • waiting on author - shift the attention set back to the author (any comment or push by the author will return the attention set to the reviewers)

The PR bot will only process comments in the main thread (not review comments).

@github-actions

Copy link
Copy Markdown
Contributor

Reminder, please take a look at this pr: @Abacn

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant