0
Fork 0
mirror of https://github.com/project-zot/zot.git synced 2024-12-16 21:56:37 -05:00

fix: handle parameters in Content-Type header

Signed-off-by: Andreea-Lupu <andreealupu1470@yahoo.com>
This commit is contained in:
Andreea-Lupu 2023-11-16 16:42:48 +02:00
parent 4fb1e756c4
commit 8436c57bb5
No known key found for this signature in database
GPG key ID: 62D8F3B625A3D4BC
3 changed files with 35 additions and 4 deletions

View file

@ -663,7 +663,7 @@ func (rh *RouteHandler) UpdateManifest(response http.ResponseWriter, request *ht
return
}
mediaType := request.Header.Get("Content-Type")
mediaType := zcommon.GetContentType(request)
if !storageCommon.IsSupportedMediaType(mediaType) {
err := apiErr.NewError(apiErr.MANIFEST_INVALID).AddDetail(map[string]string{"mediaType": mediaType})
zcommon.WriteJSON(response, http.StatusUnsupportedMediaType, apiErr.NewErrorList(err))

View file

@ -7,6 +7,7 @@ import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"net/http/httptest"
@ -211,12 +212,12 @@ func TestRoutes(t *testing.T) {
})
Convey("UpdateManifest ", func() {
testUpdateManifest := func(urlVars map[string]string, ism *mocks.MockedImageStore) int {
testUpdateManifest := func(urlVars map[string]string, contentType string, ism *mocks.MockedImageStore) int {
ctlr.StoreController.DefaultStore = ism
str := []byte("test")
request, _ := http.NewRequestWithContext(context.TODO(), http.MethodPut, baseURL, bytes.NewBuffer(str))
request = mux.SetURLVars(request, urlVars)
request.Header.Add("Content-Type", ispec.MediaTypeImageManifest)
request.Header.Add("Content-Type", contentType)
response := httptest.NewRecorder()
rthdlr.UpdateManifest(response, request)
@ -226,12 +227,14 @@ func TestRoutes(t *testing.T) {
return resp.StatusCode
}
contentType := ispec.MediaTypeImageManifest
// repo not found
statusCode := testUpdateManifest(
map[string]string{
"name": "test",
"reference": "reference",
},
contentType,
&mocks.MockedImageStore{
PutImageManifestFn: func(repo, reference, mediaType string, body []byte) (godigest.Digest,
godigest.Digest, error,
@ -246,7 +249,7 @@ func TestRoutes(t *testing.T) {
"name": "test",
"reference": "reference",
},
contentType,
&mocks.MockedImageStore{
PutImageManifestFn: func(repo, reference, mediaType string, body []byte) (godigest.Digest,
godigest.Digest, error,
@ -261,6 +264,7 @@ func TestRoutes(t *testing.T) {
"name": "test",
"reference": "reference",
},
contentType,
&mocks.MockedImageStore{
PutImageManifestFn: func(repo, reference, mediaType string, body []byte) (godigest.Digest,
godigest.Digest, error,
@ -275,6 +279,7 @@ func TestRoutes(t *testing.T) {
"name": "test",
"reference": "reference",
},
contentType,
&mocks.MockedImageStore{
PutImageManifestFn: func(repo, reference, mediaType string, body []byte) (godigest.Digest,
godigest.Digest, error,
@ -290,6 +295,7 @@ func TestRoutes(t *testing.T) {
"name": "test",
"reference": "reference",
},
contentType,
&mocks.MockedImageStore{
PutImageManifestFn: func(repo, reference, mediaType string, body []byte) (godigest.Digest,
godigest.Digest, error,
@ -298,6 +304,24 @@ func TestRoutes(t *testing.T) {
},
})
So(statusCode, ShouldEqual, http.StatusInternalServerError)
// multiple parameters in "Content-Type" header
contentType = fmt.Sprintf("%s; %s", ispec.MediaTypeImageManifest, "chartset=utf-8")
statusCode = testUpdateManifest(
map[string]string{
"name": "test",
"reference": "reference",
},
contentType,
&mocks.MockedImageStore{
PutImageManifestFn: func(repo, reference, mediaType string, body []byte) (godigest.Digest,
godigest.Digest, error,
) {
return "", "", nil
},
})
So(statusCode, ShouldNotEqual, http.StatusUnsupportedMediaType)
So(statusCode, ShouldEqual, http.StatusInternalServerError)
})
Convey("DeleteManifest", func() {

View file

@ -148,3 +148,10 @@ func QueryHasParams(values url.Values, params []string) bool {
return true
}
func GetContentType(r *http.Request) string {
contentType := r.Header.Get("Content-Type")
contentTypeParams := strings.Split(contentType, ";")
return contentTypeParams[0]
}