Skip to content

Commit

Permalink
imageutil: make mediatype detection more stricter
Browse files Browse the repository at this point in the history
Signed-off-by: Tonis Tiigi <tonistiigi@gmail.com>
  • Loading branch information
tonistiigi committed Nov 17, 2021
1 parent 244e8cd commit bc07b2b
Showing 1 changed file with 26 additions and 6 deletions.
32 changes: 26 additions & 6 deletions util/imageutil/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,19 +183,39 @@ func DetectManifestMediaType(ra content.ReaderAt) (string, error) {

func DetectManifestBlobMediaType(dt []byte) (string, error) {
var mfst struct {
MediaType string `json:"mediaType"`
MediaType *string `json:"mediaType"`
Config json.RawMessage `json:"config"`
Manifests json.RawMessage `json:"manifests"`
Layers json.RawMessage `json:"layers"`
}

if err := json.Unmarshal(dt, &mfst); err != nil {
return "", err
}

if mfst.MediaType != "" {
return mfst.MediaType, nil
mt := images.MediaTypeDockerSchema2ManifestList

if mfst.Config != nil || mfst.Layers != nil {
mt = images.MediaTypeDockerSchema2Manifest

if mfst.Manifests != nil {
return "", errors.Errorf("invalid ambiguous manifest and manifest list")
}
}
if mfst.Config != nil {
return images.MediaTypeDockerSchema2Manifest, nil

if mfst.MediaType != nil {
switch *mfst.MediaType {
case images.MediaTypeDockerSchema2ManifestList, specs.MediaTypeImageIndex:
if mt != images.MediaTypeDockerSchema2ManifestList {
return "", errors.Errorf("mediaType in manifest does not match manifest contents")
}
mt = *mfst.MediaType
case images.MediaTypeDockerSchema2Manifest, specs.MediaTypeImageManifest:
if mt != images.MediaTypeDockerSchema2Manifest {
return "", errors.Errorf("mediaType in manifest does not match manifest contents")
}
mt = *mfst.MediaType
}
}
return images.MediaTypeDockerSchema2ManifestList, nil
return mt, nil
}

0 comments on commit bc07b2b

Please sign in to comment.