Skip to content

Add new zip archive comment check - #131789

Closed
alinpahontu2912 wants to merge 3 commits into
dotnet:mainfrom
alinpahontu2912:alinpahontu2912/zip-comment-overflow-throw
Closed

alinpahontu2912 wants to merge 3 commits into
dotnet:mainfrom
alinpahontu2912:alinpahontu2912/zip-comment-overflow-throw

Conversation

@alinpahontu2912

Copy link
Copy Markdown
Member

Add check for comment size in zip archive

@alinpahontu2912 alinpahontu2912 added this to the Future milestone Aug 4, 2026
@alinpahontu2912 alinpahontu2912 self-assigned this Aug 4, 2026
Copilot AI lite review requested due to automatic review settings August 4, 2026 09:59
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 3 pipeline(s).
13 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

@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

Adds validation for ZipArchive.Comment so oversized comments are rejected rather than silently truncated, and introduces a new resource string for the exception message.

Changes:

  • ZipArchive.Comment now throws ArgumentException when the encoded comment exceeds ushort.MaxValue bytes.
  • Adds SR.CommentTooLong resource string used by the new exception.

Reviewed changes

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

File Description
src/libraries/System.IO.Compression/src/System/IO/Compression/ZipArchive.cs Validates encoded archive comment length and throws when it exceeds the EOCD limit.
src/libraries/System.IO.Compression/src/Resources/Strings.resx Adds a new localized error string for oversized archive comments.

Comment thread src/libraries/System.IO.Compression/src/Resources/Strings.resx Outdated
Comment thread src/libraries/System.IO.Compression/src/Resources/Strings.resx Outdated
@alinpahontu2912
alinpahontu2912 marked this pull request as draft August 4, 2026 11:08
Copilot AI review requested due to automatic review settings August 4, 2026 12:13

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

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

Suppressed comments (3)

src/libraries/System.IO.Compression/src/Resources/Strings.resx:181

  • The new resource string is incorrect/misleading: the ZIP comment length field is a 16-bit byte count (max 65,535 bytes), not “2^16 bits” (which would be 8,192 bytes). Consider updating the message to be accurate and actionable.
    <value>Comments cannot require more than 2^16 bits.</value>

src/libraries/System.IO.Compression/src/System/IO/Compression/ZipArchive.cs:222

  • This changes ZipArchive.Comment from silently truncating (per the previous remarks and setter behavior) to throwing when the encoded comment is too long. That’s a behavioral breaking change for a public API; it likely needs explicit compat/breaking-change consideration (and potentially a different approach, e.g., keep truncation or gate the throw behind a switch).
                byte[] encodedComment = ZipHelper.GetEncodedTruncatedBytesFromString(value, EntryNameAndCommentEncoding, 0 /* No truncation */, out _);

                if (encodedComment.Length > ZipEndOfCentralDirectoryBlock.ZipFileCommentMaxLength)
                {
                    throw new ArgumentException(SR.CommentTooLong, nameof(Comment));
                }

src/libraries/System.IO.Compression/tests/ZipArchive/zip_ReadTests.cs:1152

  • The new tests only cover ASCII length ==/>(ushort.MaxValue) characters. Since the production check is on encoded byte length, it would be good to add coverage for a multi-byte UTF-8 character pushing the encoded comment over the 65,535-byte limit (even when the character count is below ushort.MaxValue).
            // ALettersUShortMaxValue is exactly ushort.MaxValue ASCII characters, so its encoded
            // length is exactly the maximum allowed archive comment length.
            using MemoryStream ms = new MemoryStream();
            using ZipArchive archive = new ZipArchive(ms, ZipArchiveMode.Create, leaveOpen: true);

@alinpahontu2912
alinpahontu2912 marked this pull request as ready for review September 1, 2026 13:01
Copilot AI review requested due to automatic review settings September 1, 2026 13:01
@alinpahontu2912
alinpahontu2912 marked this pull request as draft September 1, 2026 13:01
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 3 pipeline(s).
13 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

@alinpahontu2912

Copy link
Copy Markdown
Member Author

@copilot this causes a breaking change since previously the comment was silently truncated. Now an error is thrown

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.

🟡 Changes recommended

Once you've addressed the issues Copilot identified, you can request another Copilot review.

Review details

Suppressed comments (1)

src/libraries/System.IO.Compression/src/System/IO/Compression/ZipArchive.cs:211

  • This changes a public API’s behavior from silently truncating an oversized comment (via GetEncodedTruncatedBytesFromString(..., ZipFileCommentMaxLength, ...)) to throwing ArgumentException at set-time. That’s a potentially breaking behavioral change for callers relying on truncation; it likely needs explicit compatibility/breaking-change consideration (and documentation) before merging.
        /// <summary>
        /// Gets or sets the optional archive comment.
        /// </summary>
        /// <remarks>
        /// The comment encoding is determined by the <c>entryNameEncoding</c> parameter of the <see cref="ZipArchive(Stream,ZipArchiveMode,bool,Encoding?)"/> constructor.
        /// </remarks>
        /// <exception cref="ArgumentException">The encoded comment exceeds <see cref="ushort.MaxValue"/> bytes.</exception>
        [AllowNull]
  • Files reviewed: 3/3 changed files
  • Comments generated: 2
  • Review effort level: Lite

Comment on lines +180 to +182
<data name="CommentTooLong" xml:space="preserve">
<value>Comments cannot require more than 2^16 bits.</value>
</data>
Comment on lines +217 to 225
byte[] encodedComment = ZipHelper.GetEncodedTruncatedBytesFromString(value, EntryNameAndCommentEncoding, 0 /* No truncation */, out _);

if (encodedComment.Length > ZipEndOfCentralDirectoryBlock.ZipFileCommentMaxLength)
{
throw new ArgumentException(SR.CommentTooLong, nameof(Comment));
}

_archiveComment = encodedComment;
Changed |= ChangeState.DynamicLengthMetadata;
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.

3 participants