Skip to content

Commit

Permalink
swarm/api/http: Content-Type validation (ethersphere#527)
Browse files Browse the repository at this point in the history
  • Loading branch information
nizsheanez committed Sep 22, 2018
1 parent ae075b6 commit 1d8c2b3
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
13 changes: 10 additions & 3 deletions swarm/api/http/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,9 +252,9 @@ func (s *Server) HandlePostRaw(w http.ResponseWriter, r *http.Request) {
return
}

if _, err := api.ValidateContentTypeHeader(r); err != nil {
if r.Header.Get("Content-Length") == "" {
postRawFail.Inc(1)
RespondError(w, r, err.Error(), http.StatusBadRequest)
RespondError(w, r, "missing Content-Length header in request", http.StatusBadRequest)
return
}

Expand Down Expand Up @@ -282,7 +282,14 @@ func (s *Server) HandlePostFiles(w http.ResponseWriter, r *http.Request) {
log.Debug("handle.post.files", "ruid", ruid)
postFilesCount.Inc(1)

contentType, params, err := mime.ParseMediaType(r.Header.Get("Content-Type"))
contentType, err := api.ValidateContentTypeHeader(r)
if err != nil {
postFilesFail.Inc(1)
RespondError(w, r, err.Error(), http.StatusBadRequest)
return
}

contentType, params, err := mime.ParseMediaType(contentType)
if err != nil {
postFilesFail.Inc(1)
RespondError(w, r, err.Error(), http.StatusBadRequest)
Expand Down
2 changes: 1 addition & 1 deletion swarm/api/http/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1120,7 +1120,7 @@ func TestModify(t *testing.T) {
res, body := httpDo(testCase.method, testCase.uri, reqBody, testCase.headers, testCase.verbose, t)

if res.StatusCode != testCase.expectedStatusCode {
t.Fatalf("expected status code %d but got %d", testCase.expectedStatusCode, res.StatusCode)
t.Fatalf("expected status code %d but got %d, %s", testCase.expectedStatusCode, res.StatusCode, body)
}
if testCase.assertResponseBody != "" && !strings.Contains(body, testCase.assertResponseBody) {
t.Log(body)
Expand Down

0 comments on commit 1d8c2b3

Please sign in to comment.