From f4e71561096579adb9675fc8374ec328617a59dd Mon Sep 17 00:00:00 2001 From: notanatol Date: Mon, 4 Mar 2024 11:47:24 -0600 Subject: [PATCH 1/4] fix: store dir error info --- pkg/api/dirs.go | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/pkg/api/dirs.go b/pkg/api/dirs.go index 06aff9c59d6..5a3ad1a0c4a 100644 --- a/pkg/api/dirs.go +++ b/pkg/api/dirs.go @@ -91,6 +91,10 @@ func (s *Service) dirUploadHandler( jsonhttp.BadRequest(w, errEmptyDir) case errors.Is(err, tar.ErrHeader): jsonhttp.BadRequest(w, "invalid filename in tar archive") + case errors.Is(err, ErrMissingContentLenHeader): + jsonhttp.BadRequest(w, "missing Content-Length header") + case errors.Is(err, ErrMissingContentTypeHeader): + jsonhttp.BadRequest(w, "missing Content-Type header") default: jsonhttp.InternalServerError(w, errDirectoryStore) } @@ -266,6 +270,11 @@ type multipartReader struct { r *multipart.Reader } +var ( + ErrMissingContentLenHeader = errors.New("content-length missing") + ErrMissingContentTypeHeader = errors.New("content-type missing") +) + func (m *multipartReader) Next() (*FileInfo, error) { part, err := m.r.NextPart() if err != nil { @@ -284,12 +293,12 @@ func (m *multipartReader) Next() (*FileInfo, error) { contentType := part.Header.Get(ContentTypeHeader) if contentType == "" { - return nil, errors.New("content-type missing") + return nil, ErrMissingContentTypeHeader } contentLength := part.Header.Get(ContentLengthHeader) if contentLength == "" { - return nil, errors.New("content-length missing") + return nil, ErrMissingContentLenHeader } fileSize, err := strconv.ParseInt(contentLength, 10, 64) if err != nil { From 826ac1a76152faa85d657c124ec91c97b2765a7c Mon Sep 17 00:00:00 2001 From: notanatol Date: Mon, 4 Mar 2024 15:48:03 -0600 Subject: [PATCH 2/4] fix: test --- pkg/api/dirs_test.go | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/pkg/api/dirs_test.go b/pkg/api/dirs_test.go index b43e9b3bca4..e06f9f8e56d 100644 --- a/pkg/api/dirs_test.go +++ b/pkg/api/dirs_test.go @@ -75,7 +75,8 @@ func TestDirs(t *testing.T) { t.Run("wrong content type", func(t *testing.T) { tarReader := tarFiles(t, []f{{ data: []byte("some data"), - name: "binary-file", + name: "some-name", + dir: "./dir", }}) // submit valid tar, but with wrong content-type @@ -92,6 +93,26 @@ func TestDirs(t *testing.T) { ) }) + t.Run("missing content type", func(t *testing.T) { + tarReader, mwBoundary := multipartFiles(t, []f{{ + data: []byte("robots text"), + name: "robots.txt", + // header left empty on purpose + }}) + + jsonhttptest.Request(t, client, http.MethodPost, dirUploadResource, http.StatusBadRequest, + jsonhttptest.WithRequestHeader(api.SwarmPostageBatchIdHeader, batchOkStr), + jsonhttptest.WithRequestBody(tarReader), + jsonhttptest.WithRequestHeader(api.SwarmCollectionHeader, "True"), + jsonhttptest.WithRequestHeader(api.ContentTypeHeader, fmt.Sprintf("multipart/form-data; boundary=%q", mwBoundary)), + jsonhttptest.WithExpectedJSONResponse(jsonhttp.StatusResponse{ + Message: "missing Content-Type header", + Code: http.StatusBadRequest, + }), + jsonhttptest.WithRequestHeader(api.ContentTypeHeader, "multipart/form-data"), + ) + }) + // valid tars for _, tc := range []struct { name string From efcf28507f88dd1a85b2c98cc507634da96a30a7 Mon Sep 17 00:00:00 2001 From: notanatol Date: Tue, 5 Mar 2024 09:39:13 -0600 Subject: [PATCH 3/4] fix: made content-length check non mandatory as per RFC --- pkg/api/dirs.go | 24 ++---------------------- pkg/api/dirs_test.go | 20 -------------------- 2 files changed, 2 insertions(+), 42 deletions(-) diff --git a/pkg/api/dirs.go b/pkg/api/dirs.go index 5a3ad1a0c4a..05431c61f01 100644 --- a/pkg/api/dirs.go +++ b/pkg/api/dirs.go @@ -91,10 +91,6 @@ func (s *Service) dirUploadHandler( jsonhttp.BadRequest(w, errEmptyDir) case errors.Is(err, tar.ErrHeader): jsonhttp.BadRequest(w, "invalid filename in tar archive") - case errors.Is(err, ErrMissingContentLenHeader): - jsonhttp.BadRequest(w, "missing Content-Length header") - case errors.Is(err, ErrMissingContentTypeHeader): - jsonhttp.BadRequest(w, "missing Content-Type header") default: jsonhttp.InternalServerError(w, errDirectoryStore) } @@ -270,11 +266,6 @@ type multipartReader struct { r *multipart.Reader } -var ( - ErrMissingContentLenHeader = errors.New("content-length missing") - ErrMissingContentTypeHeader = errors.New("content-type missing") -) - func (m *multipartReader) Next() (*FileInfo, error) { part, err := m.r.NextPart() if err != nil { @@ -285,25 +276,14 @@ func (m *multipartReader) Next() (*FileInfo, error) { if filePath == "" { filePath = part.FormName() } - if filePath == "" { - return nil, errors.New("filepath missing") - } fileName := filepath.Base(filePath) contentType := part.Header.Get(ContentTypeHeader) - if contentType == "" { - return nil, ErrMissingContentTypeHeader - } contentLength := part.Header.Get(ContentLengthHeader) - if contentLength == "" { - return nil, ErrMissingContentLenHeader - } - fileSize, err := strconv.ParseInt(contentLength, 10, 64) - if err != nil { - return nil, errors.New("invalid file size") - } + + fileSize, _ := strconv.ParseInt(contentLength, 10, 64) return &FileInfo{ Path: filePath, diff --git a/pkg/api/dirs_test.go b/pkg/api/dirs_test.go index e06f9f8e56d..078dc89dc00 100644 --- a/pkg/api/dirs_test.go +++ b/pkg/api/dirs_test.go @@ -93,26 +93,6 @@ func TestDirs(t *testing.T) { ) }) - t.Run("missing content type", func(t *testing.T) { - tarReader, mwBoundary := multipartFiles(t, []f{{ - data: []byte("robots text"), - name: "robots.txt", - // header left empty on purpose - }}) - - jsonhttptest.Request(t, client, http.MethodPost, dirUploadResource, http.StatusBadRequest, - jsonhttptest.WithRequestHeader(api.SwarmPostageBatchIdHeader, batchOkStr), - jsonhttptest.WithRequestBody(tarReader), - jsonhttptest.WithRequestHeader(api.SwarmCollectionHeader, "True"), - jsonhttptest.WithRequestHeader(api.ContentTypeHeader, fmt.Sprintf("multipart/form-data; boundary=%q", mwBoundary)), - jsonhttptest.WithExpectedJSONResponse(jsonhttp.StatusResponse{ - Message: "missing Content-Type header", - Code: http.StatusBadRequest, - }), - jsonhttptest.WithRequestHeader(api.ContentTypeHeader, "multipart/form-data"), - ) - }) - // valid tars for _, tc := range []struct { name string From aa661cd19dd35d215483a7e3ebc8cf94ca7cc418 Mon Sep 17 00:00:00 2001 From: notanatol Date: Tue, 5 Mar 2024 10:16:43 -0600 Subject: [PATCH 4/4] fix: revert minor --- pkg/api/dirs_test.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/pkg/api/dirs_test.go b/pkg/api/dirs_test.go index 078dc89dc00..b43e9b3bca4 100644 --- a/pkg/api/dirs_test.go +++ b/pkg/api/dirs_test.go @@ -75,8 +75,7 @@ func TestDirs(t *testing.T) { t.Run("wrong content type", func(t *testing.T) { tarReader := tarFiles(t, []f{{ data: []byte("some data"), - name: "some-name", - dir: "./dir", + name: "binary-file", }}) // submit valid tar, but with wrong content-type