From 8bb0350f6dc5b3db3a3fbedc93fdf482a392eb5c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9D=D1=83=D1=80=D0=BB=D1=8B=D0=B3=D0=B0=D1=8F=D0=BD?= =?UTF-8?q?=D0=BE=D0=B2=D0=B0=20=D0=95=D0=BA=D0=B0=D1=82=D0=B5=D1=80=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Thu, 29 Oct 2026 14:34:10 +0300 Subject: [PATCH] fixed a bug in JSON validation --- app/internal/api/http/users.go | 2 +- app/internal/api/http/validation_test.go | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/app/internal/api/http/users.go b/app/internal/api/http/users.go index 8be66559..bfb0d65c 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 81692277..9acb0e66 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 + `"]}`,