-
-
Notifications
You must be signed in to change notification settings - Fork 111
Detect layer compression by content, not media type #405
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
mattpollock
wants to merge
1
commit into
concourse:master
Choose a base branch
from
mitre-public:fix/sniff-layer-compression
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -2,6 +2,8 @@ package commands | |||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| import ( | ||||||||||||||||||||||||||||||||
| "archive/tar" | ||||||||||||||||||||||||||||||||
| "bufio" | ||||||||||||||||||||||||||||||||
| "bytes" | ||||||||||||||||||||||||||||||||
| "compress/gzip" | ||||||||||||||||||||||||||||||||
| "io" | ||||||||||||||||||||||||||||||||
| "os" | ||||||||||||||||||||||||||||||||
|
|
@@ -195,26 +197,53 @@ func extractLayer(dest string, layer v1.Layer, bar *mpb.Bar, chown bool) error { | |||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| type compressReaderClose func() error | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| // Layer compression magic numbers. | ||||||||||||||||||||||||||||||||
| var ( | ||||||||||||||||||||||||||||||||
| zstdMagic = []byte{0x28, 0xb5, 0x2f, 0xfd} | ||||||||||||||||||||||||||||||||
| gzipMagic = []byte{0x1f, 0x8b} | ||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| // compressionReader returns a tar.Reader over the (possibly compressed) layer. | ||||||||||||||||||||||||||||||||
| // | ||||||||||||||||||||||||||||||||
| // It detects the compression from the layer's own leading bytes rather than | ||||||||||||||||||||||||||||||||
| // trusting the declared media type. Builders and registries don't always label | ||||||||||||||||||||||||||||||||
| // zstd layers with the OCI media type (types.OCILayerZStd); BuildKit pushing | ||||||||||||||||||||||||||||||||
| // with Docker schema2 emits application/vnd.docker.image.rootfs.diff.tar.zstd, | ||||||||||||||||||||||||||||||||
| // which a media-type switch mis-routes to gzip and fails with | ||||||||||||||||||||||||||||||||
| // "gzip: invalid header". Sniffing the magic bytes handles gzip, zstd, and | ||||||||||||||||||||||||||||||||
| // uncompressed tar regardless of how the layer is labeled. mediaType is kept | ||||||||||||||||||||||||||||||||
| // for signature compatibility but no longer drives the decision. | ||||||||||||||||||||||||||||||||
| func compressionReader(layer io.ReadCloser, mediaType types.MediaType, bar *mpb.Bar) (*tar.Reader, compressReaderClose, error) { | ||||||||||||||||||||||||||||||||
|
Comment on lines
+214
to
216
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This func is private and only used in one place. Let's just update the signature and update where it's called, which should be higher up in this file.
Suggested change
And update here: registry-image-resource/commands/rootfs.go Lines 83 to 91 in 1627112
|
||||||||||||||||||||||||||||||||
| var cr io.Reader | ||||||||||||||||||||||||||||||||
| var crc func() error | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| switch mediaType { | ||||||||||||||||||||||||||||||||
| case types.OCILayerZStd: | ||||||||||||||||||||||||||||||||
| reader, err := zstd.NewReader(bar.ProxyReader(layer)) | ||||||||||||||||||||||||||||||||
| br := bufio.NewReader(bar.ProxyReader(layer)) | ||||||||||||||||||||||||||||||||
| magic, err := br.Peek(4) | ||||||||||||||||||||||||||||||||
| if err != nil && err != io.EOF { | ||||||||||||||||||||||||||||||||
| return nil, nil, err | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| switch { | ||||||||||||||||||||||||||||||||
| case bytes.HasPrefix(magic, zstdMagic): | ||||||||||||||||||||||||||||||||
| reader, err := zstd.NewReader(br) | ||||||||||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||||||||||
| return nil, nil, err | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
| cr = reader | ||||||||||||||||||||||||||||||||
| crc = func() error { reader.Close(); return nil } | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| default: | ||||||||||||||||||||||||||||||||
| reader, err := gzip.NewReader(bar.ProxyReader(layer)) | ||||||||||||||||||||||||||||||||
| case bytes.HasPrefix(magic, gzipMagic): | ||||||||||||||||||||||||||||||||
| reader, err := gzip.NewReader(br) | ||||||||||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||||||||||
| return nil, nil, err | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
| cr = reader | ||||||||||||||||||||||||||||||||
| crc = func() error { return reader.Close() } | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| default: | ||||||||||||||||||||||||||||||||
| // Uncompressed tar (or unknown) — read the stream as-is. | ||||||||||||||||||||||||||||||||
| cr = br | ||||||||||||||||||||||||||||||||
| crc = func() error { return nil } | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| tr := tar.NewReader(cr) | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: for easy lookup reference in the future link to https://en.wikipedia.org/wiki/List_of_file_signatures