Skip to content

compress/gzip: Reader unable to parse headers with long comments #14639

Open

Description

@dsnet

Using go1.6

RFC 1952 for gzip does not specify a length for the comment and name fields.

If FCOMMENT is set, a zero-terminated file comment is
present. This comment is not interpreted; it is only
intended for human consumption. The comment must consist of
ISO 8859-1 (LATIN-1) characters. Line breaks should be
denoted by a single line feed character (10 decimal).

The current implementation is inconsistent:

  • gzip.Writer permits the writing of any length comment/name string in gzip.Header.
  • gzip.Reader fails to read any comment/name string in gzip.Header longer than 511 bytes.

Playground example: https://play.golang.org/p/Zvjf8Q7jXe
Change 512 to 511 in the example, and it works again.

This causes issues reading gzip files produced by GZinga, which produces syntactically valid gzip files, but abuses the comment field to store meta data.

Update: I have no intention of fixing this unless there is someone who needs this functionality. Whatever fix we do will need to be careful that we don't introduce a potential vector for DOS attacks since gzip is a common Transfer-Encoding in HTTP. We don't want an infinitely long comment to cause a server to allocate an infinite amount of memory.

Activity

  1. added this to the Unplanned milestone on Mar 4, 2016
  2. self-assigned this
    on May 9, 2016
  3. dsnet commented on Apr 22, 2017

    @dsnet
    MemberAuthor

    Bumping up milestone since #20083 provides good evidence that this is a real issue.

  4. modified the milestones: Go1.10, Unplanned on Apr 22, 2017
  5. gopherbot commented on Aug 8, 2017

    @gopherbot
    Contributor

    Change https://golang.org/cl/53637 mentions this issue: compress/gzip: permit parsing of GZIP files with long header fields

  6. dsnet commented on Nov 8, 2017

    @dsnet
    MemberAuthor

    Pushing to Go1.11 milestone... but I think a more general API for resource limitation might be the better approach. See #20169 for some discussion.

  7. modified the milestones: Go1.10, Go1.11 on Nov 8, 2017
  8. modified the milestones: Go1.11, Unplanned on Feb 16, 2018
  9. philippfrank commented on Sep 5, 2020

    @philippfrank

    @dsnet This is still an issue. Did it go under?

  10. dsnet commented on Sep 5, 2020

    @dsnet
    MemberAuthor

    Running the snippet on Go1.15 clearly shows it's still an issue.

    Huh, apparently I had a fix for this that I never submitted: https://golang.org/cl/53637

  11. philippfrank commented on Sep 5, 2020

    @philippfrank

    @dsnet Okay, any chance you can push that forward? I am using gzip.NewWriter() to compress arbitrary text files (up to 1GB in size) while being unable to uncompress those using the same gzip.NewReader().

    For anyone else having that issue: a pragmatic solution might be to copy gunzip.go and adjust the maximum buffer size manually. It seems several other gzip modules out there have that exact same issue (because they copied the stdlib code).

  12. dsnet commented on Sep 7, 2020

    @dsnet
    MemberAuthor

    Having reviewed my CL, the reason I didn't push it through is because of concerns with this feature being an trivial avenue for denial-of-service attack where a malicious GZIP file could cause a server to allocate arbitrary amounts of memory.

    We have several options:

    1. We could immediately permit parsing of small headers (I proposed up to 32KiB to match the window size of the DEFLATE algorithm itself). However, this won't help use-cases that abuse the comment field to store machine-parsable information where it can easily exceed 32KiB.
    2. Add a new Reader.MaxFieldSize method so that the user can opt-in to parsing larger headers. Use of this option would still be a bit odd, since you would have to do something like:
    var zr gzip.Reader
    zr.MaxFieldSize(math.MaxInt32)
    if err := zr.Reset(r); err != nil {
        ... // handle err
    }
    ... // make use of zr.Header.Comment

    Since this would probably require an API change, I'm adding the NeedDecision label.
    \cc @FiloSottile since he's probably interested in any possible DOS-related issues.

  13. added
    NeedsDecisionFeedback is required from experts, contributors, and/or the community before a change can be made.
    on Sep 7, 2020
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

    NeedsDecisionFeedback is required from experts, contributors, and/or the community before a change can be made.

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions