Skip to content

Commit 25825f3

Browse files
author
xiekeyang
committed
validate media types in manifest list
Signed-off-by: xiekeyang <[email protected]>
1 parent c074bcb commit 25825f3

1 file changed

Lines changed: 36 additions & 2 deletions

File tree

schema/validator.go

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ type Validator string
3333
type validateMediaTypeFunc func(r io.Reader, strict bool) error
3434

3535
var mapValidateMediaTypes = map[Validator]validateMediaTypeFunc{
36-
MediaTypeManifest: validateMediaTypeInManifest,
36+
MediaTypeManifestList: validateMediaTypeInManifestList,
37+
MediaTypeManifest: validateMediaTypeInManifest,
3738
}
3839

3940
// ValidationError contains all the errors that happened during validation.
@@ -92,6 +93,39 @@ func (v unimplemented) Validate(src io.Reader, strict bool) error {
9293
return fmt.Errorf("%s: unimplemented", v)
9394
}
9495

96+
func validateMediaTypeInManifestList(r io.Reader, strict bool) error {
97+
header := v1.ManifestList{}
98+
99+
buf, err := ioutil.ReadAll(r)
100+
if err != nil {
101+
return errors.Wrapf(err, "error reading the io stream")
102+
}
103+
104+
err = json.Unmarshal(buf, &header)
105+
if err != nil {
106+
return errors.Wrap(err, "manifest list format mismatch")
107+
}
108+
109+
if header.MediaType != string(v1.MediaTypeImageManifestList) {
110+
msg := fmt.Sprintf("manifest list has an unknown media type: %s", header.MediaType)
111+
if strict {
112+
return fmt.Errorf("%s", msg)
113+
}
114+
fmt.Printf("warning: %s\n", msg)
115+
}
116+
117+
for _, manifest := range header.Manifests {
118+
if manifest.MediaType != string(v1.MediaTypeImageManifest) {
119+
msg := fmt.Sprintf("manifest %s has an unknown media type: %s", manifest.Digest, manifest.MediaType)
120+
if strict {
121+
return fmt.Errorf("%s", msg)
122+
}
123+
fmt.Printf("warning: %s\n", msg)
124+
}
125+
}
126+
return nil
127+
}
128+
95129
func validateMediaTypeInManifest(r io.Reader, strict bool) error {
96130
header := v1.Manifest{}
97131

@@ -106,7 +140,7 @@ func validateMediaTypeInManifest(r io.Reader, strict bool) error {
106140
}
107141

108142
if header.MediaType != string(v1.MediaTypeImageManifest) {
109-
msg := fmt.Sprintf("manifest has an unknown media type: %s\n", header.MediaType)
143+
msg := fmt.Sprintf("manifest has an unknown media type: %s", header.MediaType)
110144
if strict {
111145
return fmt.Errorf("%s", msg)
112146
}

0 commit comments

Comments
 (0)