fix(postgres): clamp event, token and denial limits to int32

Issue: archie-core-g14d
This commit is contained in:
sam committed 2026-10-13 15:27:36 +11:00
1 parent 33ccfb2505
commit dee78a876a
2 files changed
+9 -4

No files matched your search

+1 -1
View File
@@ -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
+8 -3
View File
@@ -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