mirror of
https://github.com/BaaRai/ARTEX.git
synced 2026-11-19 22:50:55 +08:00
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.
This commit is contained in:
@@ -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 `<workDir>/sessions`, so `..` and path separators cannot escape that directory.
|
||||
|
||||
+17
-4
@@ -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)
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user