Cap the amount of data util.Unzip extracts - #4207
Open
4RH1T3CT0R7 wants to merge 2 commits into
Open
Conversation
Unzip copied every archive entry with io.Copy and no bound on the output, so a small crafted archive handed to a plugin could expand until the disk filled. The function already rejects entries that escape the destination directory; this adds the matching check for size. Extraction now has a 1 GiB budget per archive. Each entry's declared uncompressed size is checked against what is left before anything is written, and the bytes actually written are subtracted afterwards. archive/zip refuses to read past the size declared in an entry's header, so the declared size is a real upper bound and no second limit is needed on the copy. Exceeding the budget returns an error in the same style as the traversal check. The new test for a plain archive also showed that Unzip created the parent directory of a file entry with the file's own mode. Archives without directory entries, such as ones written by Go's archive/zip, ended up with an unsearchable 0644 directory on Unix and every file inside it failed to open. Parent directories now get 0755, the same as the destination directory. Fixes micro-editor#4161
4RH1T3CT0R7
force-pushed
the
fix/4161
branch
from
September 3, 2026 17:21
a2d2c68 to
50de498
Compare
JoeKar
reviewed
Sep 3, 2026
| @@ -639,6 +642,8 @@ func Unzip(src, dest string) error { | |||
|
|
|||
| os.MkdirAll(dest, 0755) | |||
Member
There was a problem hiding this comment.
Instead of 0755 I'm thinking about a DirMode analogue to:
Line 52 in 393cf24
This is then valid for the newly introduced 0755 in line 672 too as well as in:
micro/internal/config/plugin_installer.go
Line 423 in 393cf24
Author
There was a problem hiding this comment.
Good idea, done in ff05d1a: a DirMode constant next to FileMode, used for both MkdirAll calls in Unzip and for dirPerm in the plugin installer.
Unzip and the plugin installer both created directories with a bare 0755, so give it a DirMode constant next to FileMode and use it in all three places.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
util.Unzipininternal/util/util.gocopies each archive entry withio.Copyand never checks how much it has written, so a plugin that unpacks a downloaded archive can expand a small zip bomb until the disk is full. The function already rejects entries whose path escapes the destination, but had no equivalent check on size.Each
Unzipcall now has a 1 GiB budget, and an entry whose declared uncompressed size does not fit fails withfile too large: <path>before anything is written; the declared size is trustworthy becausearchive/ziprefuses to read past it. Parent directories are now created with 0755 instead of the entry's file mode, which left them unenterable on Unix. New tests cover a normal archive, an overstated header and an understated one.Fixes #4161