dotnet / runtime

.NET is a cross-platform runtime for cloud, mobile, desktop, and IoT apps.
https://docs.microsoft.com/dotnet/core/
MIT License
15.27k stars 4.73k forks source link

System.IO.Compression.ZipArchive hangs on a corrupt/invalid zip stream from Azure Storage #36355

Open AbhishekTripathi opened 4 years ago

AbhishekTripathi commented 4 years ago

I have found that the ZipArchive goes in an infinite loop when a zip stream is provided from Azure Storage. Below is a code snippet which is easy to reproduce by using any arbitrary file and renaming to zip or using a corrupt zip itself. Using System.IO.Compression v4.3.0. Note that when the files exist on local disk, the behavior is as expected and I get proper exception.

var badBlobPath = @"https://<storage>.blob.core.windows.net/<container>/somefile.zip";
var blobStream = new Microsoft.Azure.Storage.Blob.CloudBlockBlob(new Uri(badBlobPath));
var stream = blobStream.OpenRead();
//below line goes in infinite loop.
var zipArchive = new ZipArchive(stream, ZipArchiveMode.Read, false);
carlossanlop commented 4 years ago

@AbhishekTripathi thanks for reporting. Do you have the callstack where the loop happens?

AbhishekTripathi commented 4 years ago

I may have to debug the nuget source but the function never returns. Seems it gets stuck in infinite loop.

graviteja94 commented 1 year ago

Any update on the fix/alternative workaround for this issue?

danmoseley commented 1 year ago

Can you share callstack @graviteja94 ?

graviteja94 commented 1 year ago

Hi @danmoseley Could you see the attached screenshot?

image

upon executing the using statement, for a corrupted zip it never goes to the next line and the call stack is also empty

image

basically my code keeps running indefinitely without throwing any exception.

I'm not sure if I provided the information you requested, can you point me out on how to collect the call stack?

danmoseley commented 1 year ago

Hi @graviteja94 I meant, break in at the point of the hang and get a stack. but if the hang is just looping in your code, of course that's not useful.

I'll let the area owners for compression speak to the issue as it's not my area

jzirkelbach commented 8 months ago

I'm currently attempting to read a corrupt zip file. When manually testing the archive file by 7-Zip, it's identified with an Unexpected end of data and has a Data error for only one of its many files. When attempting to access this zip file via ZipArchive, I'm observing an infinite loop at the following location: https://github.com/dotnet/runtime/blob/main/src/libraries/System.IO.Compression/src/System/IO/Compression/ZipHelper.cs#L119-L139 with callstack

>   System.IO.Compression.dll!System.IO.Compression.ZipHelper.SeekBackwardsToSignature(System.IO.Stream stream, uint signatureToFind) Line 89   C#
    System.IO.Compression.dll!System.IO.Compression.ZipArchive.ReadEndOfCentralDirectory() Line 390 C#
    System.IO.Compression.dll!System.IO.Compression.ZipArchive.Init(System.IO.Stream stream, System.IO.Compression.ZipArchiveMode mode, bool leaveOpen) Line 340    C#
    System.IO.Compression.dll!System.IO.Compression.ZipArchive.ZipArchive(System.IO.Stream stream, System.IO.Compression.ZipArchiveMode mode) Line 100  C#

image

I hope this helps.

danmoseley commented 8 months ago

@jzirkelbach thanks, that does help. Any interest in investigating ?

jzirkelbach commented 8 months ago

@danmoseley, I'm getting more involved with compression work, but I don't have the availability to reliably commit time. I'll continue to watch this issue and contribute when and where I can.