From dee78a876ad13866c66dde27ae4b68090ac97485 Mon Sep 17 00:00:00 2001 From: Sam Catlow Date: Tue, 13 Oct 2026 15:27:36 +1100 Subject: [PATCH] fix(postgres): clamp event, token and denial limits to int32 Issue: archie-core-g14d --- internal/infrastructure/postgres/access.go | 2 +- internal/infrastructure/postgres/events.go | 11 ++++++++--- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/internal/infrastructure/postgres/access.go b/internal/infrastructure/postgres/access.go index aa582659a..21e9a92ee 100644 --- a/internal/infrastructure/postgres/access.go +++ b/internal/infrastructure/postgres/access.go @@ -219,7 +219,7 @@ func (s *Store) RecordDenial(ctx context.Context, d access.Denial) error { // ListDenials returns an org's most recent denials, newest first. func (s *Store) ListDenials(ctx context.Context, orgID org.OrgID, limit int) ([]access.Denial, error) { rows, err := s.queries().ListDenials(ctx, postgresdb.ListDenialsParams{ - OrgID: string(orgID), Limit: int32(limit), + OrgID: string(orgID), Limit: clampInt32(limit), }) if err != nil { return nil, err diff --git a/internal/infrastructure/postgres/events.go b/internal/infrastructure/postgres/events.go index ec3982c4e..8cbe662ba 100644 --- a/internal/infrastructure/postgres/events.go +++ b/internal/infrastructure/postgres/events.go @@ -4,6 +4,7 @@ import ( "context" "encoding/json" "fmt" + "math" "time" "github.com/samcharles93/archie-core/internal/domain/org" @@ -65,7 +66,7 @@ func (s *Store) EventsSince(ctx context.Context, cursor string, limit int) ([]ev return s.listEventsFromBeginning(ctx, limit) } rows, err := s.queries().ListEventsAfter(ctx, postgresdb.ListEventsAfterParams{ - At: atTime, ID: id, PageLimit: int32(limit), ScopeOrg: scopeOrg(ctx), + At: atTime, ID: id, PageLimit: clampInt32(limit), ScopeOrg: scopeOrg(ctx), }) if err != nil { return nil, err @@ -74,7 +75,7 @@ func (s *Store) EventsSince(ctx context.Context, cursor string, limit int) ([]ev } func (s *Store) listEventsFromBeginning(ctx context.Context, limit int) ([]events.Event, error) { - rows, err := s.queries().ListEventsFromBeginning(ctx, postgresdb.ListEventsFromBeginningParams{PageLimit: int32(limit), ScopeOrg: scopeOrg(ctx)}) + rows, err := s.queries().ListEventsFromBeginning(ctx, postgresdb.ListEventsFromBeginningParams{PageLimit: clampInt32(limit), ScopeOrg: scopeOrg(ctx)}) if err != nil { return nil, err } @@ -141,7 +142,7 @@ func (s *Store) StageStats(ctx context.Context) ([]storecontract.StageStat, erro // TokensByDay sums task token spend per UTC day, using agent_finish events as // the per-run accounting source and tasks.tokens_used as the durable fallback. func (s *Store) TokensByDay(ctx context.Context, days int) ([]storecontract.DayTokens, error) { - rows, err := s.queries().TokensByDay(ctx, postgresdb.TokensByDayParams{Days: int32(days), ScopeOrg: scopeOrg(ctx)}) + rows, err := s.queries().TokensByDay(ctx, postgresdb.TokensByDayParams{Days: clampInt32(days), ScopeOrg: scopeOrg(ctx)}) if err != nil { return nil, err } @@ -151,3 +152,7 @@ func (s *Store) TokensByDay(ctx context.Context, days int) ([]storecontract.DayT } return tokens, nil } + +// clampInt32 bounds a caller-supplied count to what a sqlc int32 parameter holds, +// so an oversized limit returns the deepest page instead of wrapping negative. +func clampInt32(n int) int32 { return int32(min(n, math.MaxInt32)) } //nolint:gosec // bounded above