Skip to content

Add checks for malformed sizes for ZIP64 files#129121

Open
alinpahontu2912 wants to merge 4 commits into
dotnet:mainfrom
alinpahontu2912:takeover/zip-negative-size
Open

Add checks for malformed sizes for ZIP64 files#129121
alinpahontu2912 wants to merge 4 commits into
dotnet:mainfrom
alinpahontu2912:takeover/zip-negative-size

Conversation

@alinpahontu2912

Copy link
Copy Markdown
Member

Throw early if Zip64 metadata is malformed
Follow up PR for: #127990

wfurt and others added 4 commits May 8, 2026 20:24
- Reject negative compressed/uncompressed sizes in IsOpenableInitialVerifications so a malformed Zip64 entry surfaces as InvalidDataException instead of ArgumentOutOfRangeException.
- Tighten test catch to InvalidDataException only.
- Simplify test comments to describe externally observable behavior.
- Rename zip64 size locals and centralDirectoryOffset for clarity.
@dotnet-policy-service

Copy link
Copy Markdown
Contributor

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

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

Pull request overview

This PR tightens ZIP64 parsing in System.IO.Compression by rejecting malformed ZIP64 extra-field values that can’t be represented by the library’s long-based surface area, ensuring corrupt metadata is rejected during parsing rather than surfacing as negative sizes later.

Changes:

  • Validate ZIP64 uncompressed size, compressed size, and local header offset immediately when each field is read (so early-exit paths can’t skip validation).
  • Add a regression test that crafts a minimal ZIP containing a ZIP64 uncompressed size of 0xFFFF_FFFF_FFFF_FFFF (interpreted as -1L) and asserts InvalidDataException.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
src/libraries/System.IO.Compression/src/System/IO/Compression/ZipBlocks.cs Moves ZIP64 “too large for Int64” validation to field-read time (including compressed size and local header offset).
src/libraries/System.IO.Compression/tests/ZipArchive/zip_InvalidParametersAndStrangeFiles.cs Adds a crafted ZIP regression test for a negative ZIP64 uncompressed size value.

Comment on lines 284 to 310
@@ -290,6 +303,10 @@ private static bool TryGetZip64BlockFromGenericExtraField(ZipGenericExtraField e
if (readLocalHeaderOffset)
{
zip64Block._localHeaderOffset = BinaryPrimitives.ReadInt64LittleEndian(data);
if (zip64Block._localHeaderOffset < 0)
{
throw new InvalidDataException(SR.FieldTooBigLocalHeaderOffset);
}
data = data.Slice(FieldLengths.LocalHeaderOffset);

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

LGTM

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.

5 participants