Skip to content

archive/zip: provide way to limit number of entries in an archive #78367

Description

@neild

The archive/zip.Reader.File field contains an exported field containing a list of files in the archive. This field is populated by zip.NewReader. A ZIP archive can contain an arbitrarily large number of files, which means opening a ZIP archive can allocate an arbitrarily large amount of memory for the file list.

While this is arguably a vulnerability, it's one which is inherent in the zip.Reader API. We could put a limit on how many file entries we're willing to create, but it's not clear if there is any default limit we could choose that wouldn't be both too large in some situations (allows excessive memory allocation) and too small in others (rejects valid archives containing many files).

The number of file entries is bounded by the size of the ZIP archive itself, but there's no easy way for a user to distinguish between an archive with a small number of large files and one with a large number of small files.

We should provide a way to safely open a possibly-malicious archive.

A few possibilities might include:

  • A GODEBUG=zipmaxnumfiles=N setting to set a global archive size limit. (Possibly with a default value? But as described above, it's hard to pick a good default here.)
  • A function which reports how many files an archive contains, so the user can decide whether to open it or not.
  • A new zip.NewReaderOpts function that takes options, including a limit on the number of files in the archive.
  • A replacement for zip.NewReader that does not populate the Reader.File list, with some other way of accessing the files. (A method returning the number of files and another returning an iterator, maybe?)

This issue is related to #33036, but somewhat distinct: That issue is (I think) about limiting the amount of data read from an archive file, rather than the number of files within an archive.

Thanks to Jakub Ciolek for reporting this issue.

Metadata

Metadata

Assignees

No one assigned

    Labels

    LibraryProposalIssues describing a requested change to the Go standard library or x/ libraries, but not to a toolNeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions