Security Hardening: Prevent Zip Slip (Arbitrary File Write) in ExtractUtil #337
+39
−7
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.
This PR patches a potential Zip Slip vulnerability in ExtractUtil.java by validating that the destination path of extracted archive entries resolves strictly within the intended target directory.
Previously, malicious archives containing directory traversal sequences (EX: ../../evil.txt) could potentially write files outside the working directory during extraction.
Changes:
ExtractUtil.java: Added normalize() and startsWith() checks to sanitise entry paths before writing. Throws IOException if a traversal attempt is detected.
ExtractUtilTest.java: Added a regression test (testExtractArchive_zipSlipVulnerability) to verify that path traversal attempts are correctly blocked.
Context & Background:
This issue was originally reported to the Google VRP (Report #474540831). While the security team determined that malicious build rules fall outside the immediate threat model, they acknowledged that the victim relies on the tool to handle upstream archives safely.
I am submitting this fix as a Defense in Depth measure. It protects developers from compromised upstream repositories or "Honeypot" archives, ensuring that even if a user configures Copybara to fetch from an untrusted source, the tool does not facilitate arbitrary file writes on the host machine.
Existing tests passed.
New test case confirms the fix blocks the exploit.
For better context, I am attaching the PoC video that was originally submitted with VRP Report #474540831.
Video demonstrates the Zip Slip vulnerability in action: simply running Copybara against a malicious archive results in a file being written outside the destination directory (Arbitrary File Write), confirming the need for the path validation logic added in this PR.
2026-01-09.19-13-45.mp4