From f18624b17df79644105ef47e024eea679311c1cf Mon Sep 17 00:00:00 2001 From: Chenyme <118253778+chenyme@users.noreply.github.com> Date: Wed, 21 Oct 2026 19:43:49 +0800 Subject: [PATCH] fix(console): improve parsing logic for console usage status and add tests for edge cases --- .../provider/console/console_protocol_test.go | 6 +++ .../internal/infra/provider/console/workos.go | 52 ++++++++++++++----- 2 files changed, 44 insertions(+), 14 deletions(-) diff --git a/backend/internal/infra/provider/console/console_protocol_test.go b/backend/internal/infra/provider/console/console_protocol_test.go index ca257c35..57300958 100644 --- a/backend/internal/infra/provider/console/console_protocol_test.go +++ b/backend/internal/infra/provider/console/console_protocol_test.go @@ -732,4 +732,10 @@ func TestParseConsoleUsageStatusBoundsIntegers(t *testing.T) { if windows := consoleQuotaWindows(1, status, time.Now()); windows[0].Remaining != 0 || windows[0].UsagePercent != 100 { t.Fatalf("windows = %#v", windows[0]) } + if _, err := parseConsoleUsageStatus([]byte(`{"weeklyIncludedUsage":{"budgetCents":100,"usedCents":"-99999999999"}}`)); err == nil { + t.Fatal("negative overflow usedCents accepted") + } + if status, err := parseConsoleUsageStatus([]byte(`{"weeklyIncludedUsage":{"budgetCents":"100","usedCents":37.0}}`)); err != nil || status.BudgetCents != 100 || status.UsedCents != 37 { + t.Fatalf("string/float cents = %#v, %v", status, err) + } } diff --git a/backend/internal/infra/provider/console/workos.go b/backend/internal/infra/provider/console/workos.go index 6d479126..8282fc8d 100644 --- a/backend/internal/infra/provider/console/workos.go +++ b/backend/internal/infra/provider/console/workos.go @@ -34,8 +34,6 @@ const ( workosMaxRedirects = 12 workosBodyLimit = 1 << 20 workosSessionMaxBytes = 16 << 10 - // maxConsoleUsageCents bounds upstream cents to the int32 quota columns. - maxConsoleUsageCents = math.MaxInt32 ) var ( @@ -434,30 +432,56 @@ func parseConsoleUsageStatus(data []byte) (consoleUsageStatus, error) { if payload.Weekly == nil { return consoleUsageStatus{}, errors.New("Console usage-status 缺少 weeklyIncludedUsage") } - budget, ok := jsonInteger(payload.Weekly["budgetCents"]) - if !ok || budget < 0 || budget > maxConsoleUsageCents { + budget, ok, overflow := jsonCents(payload.Weekly["budgetCents"]) + if !ok || overflow || budget < 0 { return consoleUsageStatus{}, errors.New("Console usage-status budgetCents 无效") } // proto3 JSON omits zero values, so a missing usedCents means 0. - used, ok := jsonInteger(payload.Weekly["usedCents"]) - if !ok { + used, ok, overflow := jsonCents(payload.Weekly["usedCents"]) + switch { + case overflow: + // Overspend only ever means "nothing remaining"; saturating keeps the + // value inside the int32 quota columns without changing that outcome. + used = math.MaxInt32 + case !ok: used = 0 - } - if used < 0 { + case used < 0: return consoleUsageStatus{}, errors.New("Console usage-status usedCents 无效") } - // Overspend only ever means "nothing remaining"; clamping keeps the value - // inside the int32 quota columns without changing that outcome. - if used > maxConsoleUsageCents { - used = maxConsoleUsageCents - } - result := consoleUsageStatus{BudgetCents: int(budget), UsedCents: int(used)} + result := consoleUsageStatus{BudgetCents: budget, UsedCents: used} if resetsAt, ok := jsonTimestamp(payload.Weekly["resetsAt"]); ok { result.ResetsAt = &resetsAt } return result, nil } +// jsonCents parses a cents amount with a 32-bit bound, matching the quota +// columns. overflow reports a well-formed value outside that range. +func jsonCents(raw json.RawMessage) (int, bool, bool) { + if len(raw) == 0 || string(raw) == "null" { + return 0, false, false + } + text := strings.TrimSpace(string(raw)) + var quoted string + if json.Unmarshal(raw, "ed) == nil { + text = strings.TrimSpace(quoted) + } + text = strings.TrimSuffix(strings.TrimSuffix(text, ".0"), ".00") + value, err := strconv.ParseInt(text, 10, 32) + if err != nil { + var numError *strconv.NumError + if errors.As(err, &numError) && errors.Is(numError.Err, strconv.ErrRange) { + if strings.HasPrefix(text, "-") { + // Negative amounts are invalid regardless of magnitude. + return -1, true, false + } + return 0, true, true + } + return 0, false, false + } + return int(value), true, false +} + // jsonInteger accepts both JSON numbers and the decimal strings proto3 JSON // uses for 64-bit integers. func jsonInteger(raw json.RawMessage) (int64, bool) {