Skip to content

GZipStream footer not written in CreateGzipBytes due to Dispose order #66213

Description

@ThibaultLesuisse

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

We noticed that the resource-collection.x.js file misses the GZIP footer so that some decompression tools fail. GZIP itself returns "gzip: unexpected end of file". It could of course be that our setup is missing something and that the contents of resource-collection.x.js is corrupt because of this. I've checked with a few LLM's that tell me this could be a bug so here I am.

The CreateGzipBytes method does not properly write the GZIP footer because Dispose() on the GZipStream occurs after calling ToArray(). This means the output byte array is missing the footer, which results in incomplete or corrupted GZIP data. Behavior of the GZIP library is explained here dotnet-runtime

Code Location:
src/Components/Endpoints/src/Builder/ResourceCollectionUrlEndpoint.cs#L82

Current implementation:

private static byte[] CreateGzipBytes(byte[] bytes)
{
    using var gzipContent = new MemoryStream();
    using var gzipStream = new GZipStream(gzipContent, CompressionLevel.Optimal, leaveOpen: true);
    gzipStream.Write(bytes);
    gzipStream.Flush();
    return gzipContent.ToArray();
}

Problem:
Calling ToArray() before disposing the GZipStream excludes the GZIP footer from the result, violating the GZIP format and breaking decompression.

Expected Behavior

I expect the returned byte array to include the GZIP footer so it can be decompressed by clients.

The code should dispose/close the GZipStream (which writes the footer) before reading from the MemoryStream:

private static byte[] CreateGzipBytes(byte[] bytes)
{
    using var gzipContent = new MemoryStream();
    using (var gzipStream = new GZipStream(gzipContent, CompressionLevel.Optimal, leaveOpen: true))
    {
        gzipStream.Write(bytes);
    }
    return gzipContent.ToArray();
}

Steps To Reproduce

  1. Setup a Blazor Web App
  2. Make sure the Accept-Encoding Header is set to contain gzip
  3. Use curl to manually fetch the resource-collection.xxxx.js file
  4. use tar or gzip to check for the integrity of the file
  5. tar -xzf .\output.gz results in tar.exe: Error opening archive: Unrecognized archive format

Optional: Compare byte output before/after switching disposal order.

Exceptions (if any)

Decompression fails (e.g., with System.IO.InvalidDataException: The archive entry was compressed using an unsupported compression method. or similar depending on the tool/client).

.NET Version

10.0.5

Anything else?

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions