fix: reject values that exceed Int32 for all SQL LIMIT params

This commit is contained in:
sam committed 2026-09-24 21:45:27 +10:00
1 parent aa8c4206df
commit 55f16fa7d0
5 files changed
+29 -17

No files matched your search

+5 -3
View File
@@ -251,10 +251,12 @@ func (t *sessionTracker) getActive(channelID, threadID string) string {
}
// parsePositiveInt is a nilerr-safe wrapper: it returns (n, true) on
// success or (0, false) when the string is not a positive integer.
// success or (0, false) when the string is not a positive integer. It parses
// at 32 bits because the value reaches int32 SQL LIMIT parameters; anything
// larger is rejected here rather than wrapping at the conversion.
func parsePositiveInt(s string) (int, bool) {
n, err := strconv.Atoi(s)
return n, err == nil && n > 0
n, err := strconv.ParseInt(s, 10, 32)
return int(n), err == nil && n > 0
}
func sessionTrackerKey(channelID, threadID string) string {
+9 -6
View File
@@ -544,12 +544,15 @@ func TestRouteUndoInvalidN(t *testing.T) {
_ = r.sessionTracker.sessions.Save(context.Background(), sc)
r.sessionTracker.setActive("chat-13", "", sessionID)
reply, err := r.Route(context.Background(), inbound("chat-13", "/undo notanumber"))
if err != nil {
t.Fatalf("Route: %v", err)
}
if !strings.Contains(reply, "Usage: /undo") {
t.Errorf("reply = %q, want usage message", reply)
// An N wider than int32 must be rejected, not wrapped into the SQL LIMIT.
for _, arg := range []string{"notanumber", "4294967297"} {
reply, err := r.Route(context.Background(), inbound("chat-13", "/undo "+arg))
if err != nil {
t.Fatalf("Route(%q): %v", arg, err)
}
if !strings.Contains(reply, "Usage: /undo") {
t.Errorf("/undo %s: reply = %q, want usage message", arg, reply)
}
}
}
@@ -4,6 +4,7 @@ import (
"context"
"errors"
"fmt"
"math"
"slices"
"strings"
"time"
@@ -350,6 +351,9 @@ func (s *postgresSessionStore) RecentMessages(ctx context.Context, sessionID str
if n <= 0 {
return nil, nil
}
if n > math.MaxInt32 {
return nil, fmt.Errorf("sessionstore: recent messages: limit exceeds max int32")
}
q := postgresdb.New(s.pool)
rows, err := q.RecentMessages(ctx, postgresdb.RecentMessagesParams{
SessionID: sessionID, Limit: int32(n),
@@ -364,6 +368,9 @@ func (s *postgresSessionStore) DeleteRecentMessages(ctx context.Context, session
if n <= 0 {
return 0, nil
}
if n > math.MaxInt32 {
return 0, fmt.Errorf("sessionstore: delete recent messages: limit exceeds max int32")
}
q := postgresdb.New(s.pool)
deleted, err := q.DeleteRecentMessages(ctx, postgresdb.DeleteRecentMessagesParams{
SessionID: sessionID, Limit: int32(n),
+7 -7
View File
@@ -6,32 +6,32 @@ import (
)
// defaultCapturesListLimit is used when the limit query param is absent,
// non-numeric, or non-positive. A raw strconv.Atoi zero value (its error
// non-numeric, non-positive, or wider than the int32 SQL LIMIT the store
// binds. A raw strconv.ParseInt zero value (its error
// case) must never reach ListCaptures directly -- SQL "LIMIT 0" returns zero
// rows, which would make an unrecognised or missing limit look identical to
// "no captures exist" instead of "show the default page."
const defaultCapturesListLimit = 100
// handleCaptures lists recent captured events, newest first, for the
// dashboard's event inspector (t2db.2). Token-gated like every other
// dashboard's event inspector. Token-gated like every other
// /api/* route -- captured payloads are visible only to an authenticated
// operator, per docs/prds/webhook-intake-security.md point 5.
// operator.
//
// This is the read half of event capture. The write half is the receiver in
// internal/infrastructure/captureintake, mounted on the bypass mux via
// CaptureIntake; the dashboard displays captures and mounts the route; it
// no longer stores them (archie-core-8cda.5.4).
// CaptureIntake; the dashboard displays captures and mounts the route.
func (s *Server) handleCaptures(w http.ResponseWriter, r *http.Request) {
if s.Captures == nil {
writeJSON(w, map[string]any{"captures": []any{}, "enabled": false})
return
}
limit, err := strconv.Atoi(r.URL.Query().Get("limit"))
limit, err := strconv.ParseInt(r.URL.Query().Get("limit"), 10, 32)
if err != nil || limit <= 0 {
limit = defaultCapturesListLimit
}
captures, err := s.Captures.ListCaptures(r.Context(), limit)
captures, err := s.Captures.ListCaptures(r.Context(), int(limit))
if err != nil {
s.Log.Error("list captures", "err", err)
http.Error(w, "list captures failed", http.StatusInternalServerError)
+1 -1
View File
@@ -105,7 +105,7 @@ func TestHandleCapturesDefaultsLimitWhenMissingOrInvalid(t *testing.T) {
seedCapture(t, srv, "src")
}
for _, limit := range []string{"", "not-a-number", "-5", "0"} {
for _, limit := range []string{"", "not-a-number", "-5", "0", "4294967297"} {
req := httptest.NewRequestWithContext(t.Context(), http.MethodGet, "/api/captures?limit="+limit, nil)
w := httptest.NewRecorder()
srv.Handler().ServeHTTP(w, req)