diff --git a/app/internal/api/http/users.go b/app/internal/api/http/users.go index 8be6655..bfb0d65 100644 --- a/app/internal/api/http/users.go +++ b/app/internal/api/http/users.go @@ -71,7 +71,7 @@ func decodeJSON(body io.Reader, dst any) error { if err := dec.Decode(dst); err != nil { return &validate.Error{Field: "body", Reason: jsonErrorReason(err)} } - if dec.More() { + if _, err := dec.Token(); !errors.Is(err, io.EOF) { return &validate.Error{Field: "body", Reason: "unexpected data after JSON object"} } return checkExactKeys(data, dst) diff --git a/app/internal/api/http/validation_test.go b/app/internal/api/http/validation_test.go index 8169227..9acb0e6 100644 --- a/app/internal/api/http/validation_test.go +++ b/app/internal/api/http/validation_test.go @@ -21,6 +21,10 @@ func TestValidation_InvalidJSON(t *testing.T) { []byte(`{"text": 123,}`), []byte(``), []byte(`{"text": "ok"}{"text":"bonus"}`), + []byte(`{"text": "ok"}}`), + []byte(`{"text": "ok"}]`), + []byte(`{"text": "ok"} }`), + []byte(`{"text": "ok"} x`), []byte("{\"text\":\"\xff\xfe bad utf8\"}"), } for _, payload := range bad { @@ -104,6 +108,7 @@ func TestValidation_ErrorMessages(t *testing.T) { {http.MethodPost, "/api/v1/users", "", ``, "body: empty body"}, {http.MethodPost, "/api/v1/users", "", `[]`, "body: expected a JSON object"}, {http.MethodPost, "/api/v1/users", "", `{"username":"abc","display_name":"x"} {}`, "body: unexpected data after JSON object"}, + {http.MethodPost, "/api/v1/users", "", `{"username":"abc","display_name":"x"}}`, "body: unexpected data after JSON object"}, {http.MethodPost, "/api/v1/users", "", "{\"username\":\"abc\",\"display_name\":\"\xff\"}", "body: invalid UTF-8"}, {http.MethodPost, "/api/v1/chats", alice.ID, `{"title":"t","member_ids":["` + bob.ID + `"]}`,