reject null manifests

Signed-off-by: Josh Chorlton <jchorlton@gmail.com>
This commit is contained in:
Josh Chorlton 2020-12-26 00:55:24 +00:00
parent 8891c58a43
commit 654f854fae
3 changed files with 52 additions and 1 deletions

View file

@ -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)

View file

@ -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
@ -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
}

View file

@ -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))
}
})
}
}