Add strict validation to System.IO.Compression.GZipStream #47560
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.
The current implementation of
System.IO.Compression.GzipStream
seems to not be as strict regarding error handling as it could be when it comes to corrupt data.For context, this is a good thread about this exact issue, I believe. There is likely even more info in the old connect ticket but it's been unaccessible for a while.
So I have compared the
System.IO.Compression.GzipStream
implementation with its managed and bindings counterparts, namelyzlib.managed
,ICSharpCode.SharpZipLib
andSharpCompress
when it comes to handling corrupt data.As for test cases, I retrieved the code from the SO question and also added another test which hits a different code path.
This is the other test case https://github.com/mfkl/zlib-validation/blob/main/ConsoleApp1/Program.cs
This is the result:
My understanding is that the
System.IO.Compression.GzipStream
implementation ignoresZLibNative.ErrorCode.BufError
errors in most cases, while other implementions are stricter about it.My proposed fix is to add an overload with a simple boolean which, when set to true, would take into account the BufError returned by the native lib, and throw accordingly.
I've run the compression test suites and made sure I did not break anything. If there is another approach to go at this, feel free to let me know.
Feedback welcome!