From bf6feefbdfd651a65522a6fc3ca4753c0e876080 Mon Sep 17 00:00:00 2001 From: Coff0xc <184964491+Coff0xc@users.noreply.github.com> Date: Wed, 18 Nov 2026 17:49:28 +0000 Subject: [PATCH] fix(server): cap conversation message page size GET /api/conversations/{id}/messages passed limit through to the database with no upper bound, so a huge query could load the whole transcript. Clamp history pages to 200 and keep the since tail at 1000. --- CHANGELOG.md | 1 + server/conversations.go | 21 +++++++++++++++++---- server/conversations_limit_test.go | 21 +++++++++++++++++++++ 3 files changed, 39 insertions(+), 4 deletions(-) create mode 100644 server/conversations_limit_test.go diff --git a/CHANGELOG.md b/CHANGELOG.md index 9ee6faa..e4076e0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -50,6 +50,7 @@ All notable changes to this project are documented in this file. The format is b ### Fixed - **Retry-After overflow**: a 429/503 `Retry-After` of huge seconds no longer wraps `time.Duration` into a tiny delay. Values above `MaxDelay` are not retried. +- **Conversation message pages**: `GET /api/conversations/{id}/messages` clamps `limit` (200 for history, 1000 for `?since=`), so a huge query cannot load the whole transcript in one response. - **Findings CSV formula cells**: `FindingsCSV` prefixes name, class, summary, task, and other text fields that start with `=`, `+`, `-`, `@`, tab, or CR so Excel/LibreOffice do not evaluate them as formulas when the export is opened. - **Asset service links**: the Services tab only uses an asset URL as `href` when it is `http:` or `https:`. `javascript:`, `data:`, and other schemes render as text. - **Chat session directories**: `ChatAgent.Chat` accepts only `[A-Za-z0-9_-]` session ids (at most 64 bytes) before joining them under `/sessions`, so `..` and path separators cannot escape that directory. diff --git a/server/conversations.go b/server/conversations.go index 7c0d916..cb810ae 100644 --- a/server/conversations.go +++ b/server/conversations.go @@ -311,16 +311,16 @@ func (s *Server) pgConversationMessages(w http.ResponseWriter, r *http.Request) return } q := r.URL.Query() - limit := atoiDefault(q.Get("limit"), 200) s.chatMu.Lock() running := s.chatBusy[s.convBusyKey(c.ID)] s.chatMu.Unlock() // Incremental tail: ?since=N returns steps after id N (ASC) — used by the live - // poll and the post-send fetch. A generous cap so a burst is never dropped. + // poll and the post-send fetch. Capped so a huge limit cannot materialise the + // whole transcript in one response. if sv := q.Get("since"); sv != "" { since, _ := strconv.ParseInt(sv, 10, 64) - items, cursor, err := pg.ConvActivityList(c.ID, since, max(limit, 1000)) + items, cursor, err := pg.ConvActivityList(c.ID, since, 1000) if err != nil { writeErr(w, 500, err.Error()) return @@ -333,7 +333,7 @@ func (s *Server) pgConversationMessages(w http.ResponseWriter, r *http.Request) // the page of older steps ending before id N. hasMore lets the client stop // loading earlier history once the top of the thread is reached. before, _ := strconv.ParseInt(q.Get("before"), 10, 64) - items, hasMore, err := pg.ConvActivityPage(c.ID, before, limit) + items, hasMore, err := pg.ConvActivityPage(c.ID, before, convMessageLimit(q.Get("limit"), 200, 200)) if err != nil { writeErr(w, 500, err.Error()) return @@ -345,6 +345,19 @@ func (s *Server) pgConversationMessages(w http.ResponseWriter, r *http.Request) writeJSON(w, 200, map[string]any{"items": activityDTOs(items), "cursor": cursor, "running": running, "hasMore": hasMore}) } +// convMessageLimit clamps a page size. Missing or invalid values use def; the +// result is at most cap so a huge query cannot materialise the whole transcript. +func convMessageLimit(raw string, maxN, def int) int { + n := atoiDefault(raw, def) + if n < 1 { + n = def + } + if n > maxN { + n = maxN + } + return n +} + // pgConversationMsgDetail lazily returns one step's full detail blob. func (s *Server) pgConversationMsgDetail(w http.ResponseWriter, r *http.Request) { pg, c, ok := s.convByID(w, r) diff --git a/server/conversations_limit_test.go b/server/conversations_limit_test.go new file mode 100644 index 0000000..7813a7d --- /dev/null +++ b/server/conversations_limit_test.go @@ -0,0 +1,21 @@ +package server + +import "testing" + +func TestConvMessageLimit(t *testing.T) { + if got := convMessageLimit("", 200, 200); got != 200 { + t.Fatalf("default: %d", got) + } + if got := convMessageLimit("50", 200, 200); got != 50 { + t.Fatalf("50: %d", got) + } + if got := convMessageLimit("2147483646", 200, 200); got != 200 { + t.Fatalf("huge history: %d", got) + } + if got := convMessageLimit("2147483646", 1000, 1000); got != 1000 { + t.Fatalf("huge since: %d", got) + } + if got := convMessageLimit("-1", 200, 200); got != 200 { + t.Fatalf("negative: %d", got) + } +}