From 98ffd9ff459238703f196328dc873436f46771ff 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 pagination --- app/internal/api/http/pagination_test.go | 37 ++++++++++++++++++++++++ app/internal/domain/page.go | 12 ++++++++ 2 files changed, 49 insertions(+) diff --git a/app/internal/api/http/pagination_test.go b/app/internal/api/http/pagination_test.go index cdd348a..0eaec70 100644 --- a/app/internal/api/http/pagination_test.go +++ b/app/internal/api/http/pagination_test.go @@ -1,6 +1,7 @@ package http import ( + "encoding/base64" "fmt" "net/http" "net/url" @@ -87,3 +88,39 @@ func TestPagination_CursorBoundToChat(t *testing.T) { resp.Body.Close() } } + +func TestPagination_IncompleteCursor(t *testing.T) { + r := newTestRig(t) + alice, _, _, chat := bootstrap(r) + r.send(alice, chat.ID, "m0") + + enc := func(s string) string { return base64.RawURLEncoding.EncodeToString([]byte(s)) } + msgScope := `"s":"chat:` + chat.ID + `"` + chatsScope := `"s":"chats:` + alice.ID + `"` + ts := `"t":"2026-01-01T00:00:00Z"` + id := `"i":"` + chat.ID + `"` + + cases := map[string]string{ + "только scope": enc(`{` + msgScope + `}`), + "без i": enc(`{` + ts + `,` + msgScope + `}`), + "без t": enc(`{` + id + `,` + msgScope + `}`), + "i не UUID": enc(`{` + ts + `,"i":"zzz",` + msgScope + `}`), + "нулевое t": enc(`{"t":"0001-01-01T00:00:00Z",` + id + `,` + msgScope + `}`), + "лишнее поле": enc(`{` + ts + `,` + id + `,` + msgScope + `,"x":1}`), + "данные после объекта": enc(`{` + ts + `,` + id + `,` + msgScope + `}}`), + "base64 с паддингом": base64.URLEncoding.EncodeToString([]byte(`{` + ts + `,` + id + `,` + msgScope + `}`)), + "пустой объект": enc(`{}`), + "null": enc(`null`), + } + for name, cur := range cases { + resp := r.do(http.MethodGet, "/api/v1/chats/"+chat.ID+"/messages?cursor="+url.QueryEscape(cur), alice.ID, nil, nil) + if resp.StatusCode != http.StatusBadRequest { + t.Errorf("messages, %s: ожидался 400, получен %d", name, resp.StatusCode) + } + resp.Body.Close() + } + + resp := r.do(http.MethodGet, "/api/v1/chats?cursor="+url.QueryEscape(enc(`{`+chatsScope+`}`)), alice.ID, nil, nil) + expectStatus(t, resp, http.StatusBadRequest) + resp.Body.Close() +} diff --git a/app/internal/domain/page.go b/app/internal/domain/page.go index 935e700..494a7a4 100644 --- a/app/internal/domain/page.go +++ b/app/internal/domain/page.go @@ -4,6 +4,8 @@ import ( "encoding/base64" "encoding/json" "time" + + "github.com/google/uuid" ) type Page[T any] struct { @@ -51,5 +53,15 @@ func DecodeCursor(s string) (Cursor, error) { if err := json.Unmarshal(raw, &c); err != nil { return Cursor{}, ErrInvalidArgument } + + if c.CreatedAt.IsZero() || c.Scope == "" { + return Cursor{}, ErrInvalidArgument + } + if _, err := uuid.Parse(c.ID); err != nil || len(c.ID) != 36 { + return Cursor{}, ErrInvalidArgument + } + if EncodeCursor(c) != s { + return Cursor{}, ErrInvalidArgument + } return c, nil }