Add CRC32 validation when reading zip archive entries#124766
Add CRC32 validation when reading zip archive entries#124766alinpahontu2912 wants to merge 5 commits intodotnet:mainfrom
Conversation
Add CrcValidatingReadStream to ZipCustomStreams that wraps decompressed entry streams and validates CRC32 checksums as data is read. The stream computes a running CRC32 over all bytes read and compares it against the expected CRC from the zip entry header when the expected number of bytes has been read. Key design decisions: - Seeking is delegated to the base stream but abandons CRC tracking, since CRC validation requires sequential reading from the start. - Flush throws NotSupportedException consistent with read-only streams. - Corrupted entries (tampered uncompressed size) now correctly throw InvalidDataException when CRC mismatch is detected. Update existing corrupted stream tests to expect InvalidDataException from CRC validation instead of silently accepting tampered data. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
This PR adds CRC32 validation when reading ZIP archive entries to detect data corruption. A new CrcValidatingReadStream wrapper class is introduced that computes a running CRC32 checksum as data is read and validates it against the expected CRC from the ZIP entry header once all expected bytes have been read.
Changes:
- Added
CrcValidatingReadStreamclass that wraps decompressed entry streams and validates CRC32 checksums - Updated
ZipArchiveEntryto wrap entry streams withCrcValidatingReadStreamin read mode - Updated corrupted stream tests to expect
InvalidDataExceptionfrom CRC validation instead of silently accepting tampered data - Added
CrcMismatchresource string for the validation error message
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
src/libraries/System.IO.Compression/src/System/IO/Compression/ZipCustomStreams.cs |
Added new CrcValidatingReadStream class that validates CRC32 checksums as data is read from ZIP entry streams |
src/libraries/System.IO.Compression/src/System/IO/Compression/ZipArchiveEntry.cs |
Updated OpenInReadMode and OpenInReadModeGetDataCompressor to return CrcValidatingReadStream and wrap decompressed streams with CRC validation |
src/libraries/System.IO.Compression/src/Resources/Strings.resx |
Added CrcMismatch error message for CRC validation failures |
src/libraries/System.IO.Compression/tests/ZipArchive/zip_InvalidParametersAndStrangeFiles.cs |
Updated corrupted stream tests to expect InvalidDataException when reading tampered entries, simplified tests to focus on CRC validation |
src/libraries/System.IO.Compression/src/System/IO/Compression/ZipCustomStreams.cs
Show resolved
Hide resolved
src/libraries/System.IO.Compression/src/System/IO/Compression/ZipCustomStreams.cs
Show resolved
Hide resolved
src/libraries/System.IO.Compression/src/System/IO/Compression/ZipCustomStreams.cs
Show resolved
Hide resolved
src/libraries/System.IO.Compression/src/System/IO/Compression/ZipCustomStreams.cs
Outdated
Show resolved
Hide resolved
|
Should this be marked as breaking change since dealing with corrupted files has now changed ? |
Yes, just like we did for #118577 |
|
Added When you commit this breaking change:
Tagging @dotnet/compat for awareness of the breaking change. |
src/libraries/System.IO.Compression/src/System/IO/Compression/ZipCustomStreams.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.IO.Compression/src/System/IO/Compression/ZipCustomStreams.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.IO.Compression/src/System/IO/Compression/ZipCustomStreams.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.IO.Compression/src/System/IO/Compression/ZipCustomStreams.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.IO.Compression/src/System/IO/Compression/ZipCustomStreams.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.IO.Compression/src/System/IO/Compression/ZipCustomStreams.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.IO.Compression/tests/ZipArchive/zip_InvalidParametersAndStrangeFiles.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.IO.Compression/tests/ZipArchive/zip_InvalidParametersAndStrangeFiles.cs
Show resolved
Hide resolved
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Add CrcValidatingReadStream to ZipCustomStreams that wraps decompressed entry streams and validates CRC32 checksums as data is read. The stream computes a running CRC32 over all bytes read and compares it against the expected CRC from the zip entry header when the expected number of bytes has been read.
Update existing corrupted stream tests to expect InvalidDataException from CRC validation instead of silently accepting tampered data.