Skip to content

Conversation

rzikm
Copy link
Member

@rzikm rzikm commented Jul 31, 2025

Fixes #117147.
Replaces #118230.

The structure of Zip64 ZIP file is as follows:

  • local file header
  • ....
  • central directory
  • ZIP64 end of central directory record
  • ZIP64 end of central directory locator
  • End of central directory record

The applications reading the ZIP archive are expected to parse it from the end, where presence of ZIP64 fields is indicated by special values in the eocd record.

the problematic input is too short to contain eocd locator, which triggers the assert, this PR adds a stream length check for that.

This PR also fixes some internal methods (SeekBackwardsToSignature(Async)) to now behave as expected. Previous implementation always read at least 4kb even if maxBytesToRead was less. This in turn uncovered a different bug where expected offset of eocd locator was not calculated correctly.

Review of the fuzzing code (ZipArchiveFuzzer) revealed some issues (like disposing the stream between runs and ignoring all exceptions), so this PR fixes improves the fuzzer az well.

@Copilot Copilot AI review requested due to automatic review settings July 31, 2025 12:31
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR fixes a bug in reading Zip64 end of central directory locator by adding proper stream length validation and correcting internal signature seeking methods. The changes address issue #117147 where malformed ZIP archives with insufficient data could trigger assertions.

Key changes:

  • Adds stream length check before attempting to read Zip64 end of central directory locator
  • Fixes SeekBackwardsToSignature methods to respect maxBytesToRead parameter properly
  • Corrects calculation of total bytes read and overlap handling in signature seeking

Reviewed Changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.

File Description
ZipHelper.cs Fixed SeekBackwardsToSignature to properly respect maxBytesToRead and calculate overlap correctly
ZipHelper.Async.cs Applied same fixes to async version of SeekBackwardsToSignature method
ZipArchive.cs Added stream length validation and simplified EOCD position tracking
ZipArchive.Async.cs Applied same validation and position tracking fixes to async version

Copy link
Contributor

Tagging subscribers to this area: @dotnet/area-system-io-compression
See info in area-owners.md if you want to be subscribed.

@rzikm
Copy link
Member Author

rzikm commented Jul 31, 2025

/azp run runtime-libraries-coreclr outerloop

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@MihaZupan
Copy link
Member

@MihuBot fuzz ZipArchive

@rzikm
Copy link
Member Author

rzikm commented Aug 4, 2025

I think we are ready for review.

@rzikm rzikm merged commit 1dd74ea into dotnet:main Aug 5, 2025
79 of 81 checks passed
@github-actions github-actions bot locked and limited conversation to collaborators Sep 5, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[ZipArchiveFuzzer] Assertion failed zip64eocdLocatorProper && zip64EOCDLocator != null
5 participants