Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add handling for empty content type in request header #2433

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion bind.go
Expand Up @@ -90,6 +90,8 @@ func (b *DefaultBinder) BindBody(c Context, i interface{}) (err error) {
if err = b.bindData(i, params, "form"); err != nil {
return NewHTTPError(http.StatusBadRequest, err.Error()).SetInternal(err)
}
case ctype == "":
return ErrEmptyContentType
aldas marked this conversation as resolved.
Show resolved Hide resolved
default:
return ErrUnsupportedMediaType
}
Expand All @@ -114,7 +116,7 @@ func (b *DefaultBinder) Bind(i interface{}, c Context) (err error) {
// Only bind query parameters for GET/DELETE/HEAD to avoid unexpected behavior with destination struct binding from body.
// For example a request URL `&id=1&lang=en` with body `{"id":100,"lang":"de"}` would lead to precedence issues.
// The HTTP method check restores pre-v4.1.11 behavior to avoid these problems (see issue #1670)
method := c.Request().Method
method := c.Request().Method
if method == http.MethodGet || method == http.MethodDelete || method == http.MethodHead {
if err = b.BindQueryParams(c, i); err != nil {
return err
Expand Down
9 changes: 9 additions & 0 deletions bind_test.go
Expand Up @@ -429,6 +429,10 @@ func TestBindUnsupportedMediaType(t *testing.T) {
testBindError(t, strings.NewReader(invalidContent), MIMEApplicationJSON, &json.SyntaxError{})
}

func TestBindErrEmptyContentType(t *testing.T) {
testBindError(t, strings.NewReader(invalidContent), "", errors.New("missing content type header"))
}

func TestBindbindData(t *testing.T) {
ts := new(bindTestStruct)
b := new(DefaultBinder)
Expand Down Expand Up @@ -674,6 +678,11 @@ func testBindError(t *testing.T, r io.Reader, ctype string, expectedInternal err
assert.Equal(t, http.StatusBadRequest, err.(*HTTPError).Code)
assert.IsType(t, expectedInternal, err.(*HTTPError).Internal)
}
case ctype == "": // no content type
if assert.IsType(t, new(HTTPError), err) {
assert.Equal(t, ErrEmptyContentType, err)
assert.IsType(t, expectedInternal, err.(*HTTPError).Internal)
}
default:
if assert.IsType(t, new(HTTPError), err) {
assert.Equal(t, ErrUnsupportedMediaType, err)
Expand Down
3 changes: 2 additions & 1 deletion binder_test.go
Expand Up @@ -4,7 +4,6 @@ import (
"encoding/json"
"errors"
"fmt"
"github.com/stretchr/testify/assert"
"io"
"math/big"
"net/http"
Expand All @@ -13,6 +12,8 @@ import (
"strings"
"testing"
"time"

"github.com/stretchr/testify/assert"
)

func createTestContext(URL string, body io.Reader, pathParams map[string]string) Context {
Expand Down
1 change: 1 addition & 0 deletions echo.go
Expand Up @@ -338,6 +338,7 @@ var (
ErrCookieNotFound = errors.New("cookie not found")
ErrInvalidCertOrKeyType = errors.New("invalid cert or key type, must be string or []byte")
ErrInvalidListenerNetwork = errors.New("invalid listener network")
ErrEmptyContentType = ErrUnsupportedMediaType.WithInternal(errors.New("missing content type header"))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please do not add new public error - I do not think this case is something that you would actively check. By making it public instead of just returning ErrUnsupportedMediaType.WithInternal(errors.New("missing content type header")) we are making contract library users that there is logic somewhere that returns that error and we can not change it. This is not probably this kind of situation where we should make such strong commitment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @aldas , Does this PR need some cleaning before becoming eligible for merge? I would like to take this, if possible.

)

// Error handlers
Expand Down