Skip to content

Commit

Permalink
Merge pull request moby#41842 from jchorl/master
Browse files Browse the repository at this point in the history
Reject null manifests during tar import
  • Loading branch information
thaJeztah authored Feb 9, 2021
2 parents 7916404 + 654f854 commit 1c39b1c
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 1 deletion.
2 changes: 1 addition & 1 deletion daemon/images/image_exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func (i *ImageService) ExportImage(names []string, outStream io.Writer) error {
}

// LoadImage uploads a set of images into the repository. This is the
// complement of ImageExport. The input stream is an uncompressed tar
// complement of ExportImage. The input stream is an uncompressed tar
// ball containing images and metadata.
func (i *ImageService) LoadImage(inTar io.ReadCloser, outStream io.Writer, quiet bool) error {
imageExporter := tarexport.NewTarExporter(i.imageStore, i.layerStores, i.referenceStore, i)
Expand Down
14 changes: 14 additions & 0 deletions image/tarexport/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ func (l *tarexporter) Load(inTar io.ReadCloser, outStream io.Writer, quiet bool)
return err
}

if err := validateManifest(manifest); err != nil {
return err
}

var parentLinks []parentLink
var imageIDsStr string
var imageRefCount int
Expand Down Expand Up @@ -430,3 +434,13 @@ func checkCompatibleOS(imageOS string) error {

return system.ValidatePlatform(p)
}

func validateManifest(manifest []manifestItem) error {
// a nil manifest usually indicates a bug, so don't just silently fail.
// if someone really needs to pass an empty manifest, they can pass [].
if manifest == nil {
return errors.New("invalid manifest, manifest cannot be null (but can be [])")
}

return nil
}
37 changes: 37 additions & 0 deletions image/tarexport/load_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package tarexport

import (
"testing"

"gotest.tools/v3/assert"
is "gotest.tools/v3/assert/cmp"
)

func TestValidateManifest(t *testing.T) {
cases := map[string]struct {
manifest []manifestItem
valid bool
errContains string
}{
"nil": {
manifest: nil,
valid: false,
errContains: "manifest cannot be null",
},
"non-nil": {
manifest: []manifestItem{},
valid: true,
},
}

for name, tc := range cases {
t.Run(name, func(t *testing.T) {
err := validateManifest(tc.manifest)
if tc.valid {
assert.Check(t, is.Nil(err))
} else {
assert.Check(t, is.ErrorContains(err, tc.errContains))
}
})
}
}

0 comments on commit 1c39b1c

Please sign in to comment.