From d4ae99d23238dc84d4f67dcb6fc1322603d4cba3 Mon Sep 17 00:00:00 2001 From: Xiaodong Ye Date: Mon, 26 Aug 2024 16:02:25 +0800 Subject: [PATCH] Add extra check for the content type in the manifest Signed-off-by: Xiaodong Ye --- docker/docker_client.go | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/docker/docker_client.go b/docker/docker_client.go index 97d97fed5f..ba19fe6daa 100644 --- a/docker/docker_client.go +++ b/docker/docker_client.go @@ -986,7 +986,8 @@ func (c *dockerClient) fetchManifest(ctx context.Context, ref dockerReference, t if err != nil { return nil, "", err } - logrus.Debugf("Content-Type from manifest GET is %q", res.Header.Get("Content-Type")) + contentType := res.Header.Get("Content-Type") + logrus.Debugf("Content-Type from manifest GET is %q", contentType) defer res.Body.Close() if res.StatusCode != http.StatusOK { return nil, "", fmt.Errorf("reading manifest %s in %s: %w", tagOrDigest, ref.ref.Name(), registryHTTPResponseToError(res)) @@ -996,7 +997,18 @@ func (c *dockerClient) fetchManifest(ctx context.Context, ref dockerReference, t if err != nil { return nil, "", err } - return manblob, simplifyContentType(res.Header.Get("Content-Type")), nil + // Extra check for the content type in the manifest + if contentType == "text/plain" { + man := make(map[string]any) + if err := json.Unmarshal(manblob, &man); err != nil { + return nil, "", fmt.Errorf("decoding manifest %s in %s: %w", tagOrDigest, ref.ref.Name(), err) + } + if mediaType, ok := man["mediaType"]; ok { + contentType = mediaType.(string) + logrus.Debugf("Content-Type from manifest JSON is %q", contentType) + } + } + return manblob, simplifyContentType(contentType), nil } // getExternalBlob returns the reader of the first available blob URL from urls, which must not be empty.