carlos / platform Public

console: check the CLI code budget before resolving a code

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev pushed by paul@keymail.dev 7567c15644093a82060d0585fb8ab1938c6257c5 parent 45d1c24
2 files changed, +67 −16
  • internal/console/authcli.go +32 −16
  • internal/console/authcli_test.go +35 −0
diff --git a/internal/console/authcli.go b/internal/console/authcli.go
index 589f5d1d5..f2040485c 100644
--- a/internal/console/authcli.go
+++ b/internal/console/authcli.go
@@ -333,8 +333,8 @@ func retryAfter(w http.ResponseWriter, d time.Duration) {
// cliLimits is the set of limiters the CLI endpoints share, hung off the
// Server so a test gets a fresh set per fixture.
type cliLimits struct {
- managedEnrolIP, managedEnrolGlobal *rateLimiter
- start, poll, pollIP, guessAddr, guessIP, write, ship, instances, create *rateLimiter
+ managedEnrolIP, managedEnrolGlobal *rateLimiter
+ start, poll, pollIP, guessAddr, guessIP, lookAddr, lookIP, write, ship, instances, create *rateLimiter
// db is the `carlos db` door's own budget, keyed by token (cliDBBurst,
// apidb.go): a restore is dozens of reads, a different shape from the
// instance verbs.
@@ -377,8 +377,12 @@ func newCLILimits() *cliLimits {
// Wrong codes are counted against the signed-in ADDRESS and the client
// IP, never the session token: a session is re-minted by signing in
// again, so a token-keyed counter is one the attacker resets at will.
+ // guessAddr/guessIP are the DECISION budget (the POST that resolves a
+ // typed code); lookAddr/lookIP are the GET lookup budget.
guessAddr: newRateLimiter(cliGuessBurst, cliRateWindow),
guessIP: newRateLimiter(cliGuessBurst, cliRateWindow),
+ lookAddr: newRateLimiter(cliGuessBurst, cliRateWindow),
+ lookIP: newRateLimiter(cliGuessBurst, cliRateWindow),
write: newRateLimiter(cliWriteBurst, cliRateWindow),
// Ship gets its own budget, on its own window, keyed by token rather
// than address (see handleAPIShip) — an upload is a different shape
@@ -1606,6 +1610,21 @@ func (s *Server) handleCLIApprovePage(w http.ResponseWriter, r *http.Request) {
s.renderCLI(w, http.StatusOK, "cli_approve", p)
}
+// cliBudgetOpen reports whether neither key is over its guess budget. It is
+// read-only (rateLimiter.tripped), so asking never spends: callers check it
+// BEFORE resolving a code and spend only on a miss with cliBudgetSpend.
+// The check-then-spend gap lets a burst of concurrent requests overshoot the
+// budget by at most the number in flight, which is acceptable here.
+func (s *Server) cliBudgetOpen(addrL, ipL *rateLimiter, addr, ip string, now time.Time) bool {
+ return !addrL.tripped(addr, now) && !ipL.tripped(ip, now)
+}
+
+// cliBudgetSpend records one miss against both keys.
+func (s *Server) cliBudgetSpend(addrL, ipL *rateLimiter, addr, ip string, now time.Time) {
+ addrL.allow(addr, now)
+ ipL.allow(ip, now)
+}
+
// handleCLIApprove is both steps a human takes, distinguished by action:
//
// continue — a typed code is looked up and the terminal asking is DESCRIBED
@@ -1639,17 +1658,20 @@ func (s *Server) handleCLIApprove(w http.ResponseWriter, r *http.Request) {
// binds THEIR terminal to YOUR address, which is the inverted-payoff half
// of this flow and the reason the counter has to exist even though ~39
// bits in ten minutes is not findable.
- badCode := func() bool {
- ip := s.clientIP(r)
- okAddr := s.cli.guessAddr.allow(addr, now)
- okIP := s.cli.guessIP.allow(ip, now)
- return okAddr && okIP
- }
+ //
+ // The budget is checked BEFORE any resolution, so a caller past the limit
+ // is refused whether or not the code is live and learns nothing.
+ ip := s.clientIP(r)
tooMany := func() {
s.renderMessage(w, r, http.StatusTooManyRequests, "Too many attempts",
"Too many bad login codes. Wait a minute, then try again.")
}
+ if !s.cliBudgetOpen(s.cli.guessAddr, s.cli.guessIP, addr, ip, now) {
+ tooMany()
+ return
+ }
+
switch action {
case "approve", "deny":
// Passkey step-up on APPROVE only (spec §7a, stepup.go). Approving
@@ -1670,10 +1692,7 @@ func (s *Server) handleCLIApprove(w http.ResponseWriter, r *http.Request) {
err = s.store.ApproveCLIAuth(code, addr, now)
}
if err != nil {
- if !badCode() {
- tooMany()
- return
- }
+ s.cliBudgetSpend(s.cli.guessAddr, s.cli.guessIP, addr, ip, now)
p.Error = "That code has expired or was already used. Run carlos auth login again for a fresh one."
s.renderCLI(w, http.StatusOK, "cli_approve", p)
return
@@ -1687,10 +1706,7 @@ func (s *Server) handleCLIApprove(w http.ResponseWriter, r *http.Request) {
// "continue": describe the terminal, then let the human decide.
req, err := s.store.CLIAuthByUserCode(code, now)
if err != nil {
- if !badCode() {
- tooMany()
- return
- }
+ s.cliBudgetSpend(s.cli.guessAddr, s.cli.guessIP, addr, ip, now)
p.Error = "No login is waiting on that code — check what your terminal printed. " +
"Codes last ten minutes."
s.renderCLI(w, http.StatusOK, "cli_approve", p)
diff --git a/internal/console/authcli_test.go b/internal/console/authcli_test.go
index 14580fbd8..dbd54d3c6 100644
--- a/internal/console/authcli_test.go
+++ b/internal/console/authcli_test.go
@@ -450,6 +450,41 @@ func TestCLIApproveRateLimitsWrongCodes(t *testing.T) {
}
}
+// The decision budget is checked BEFORE a code is resolved, so a caller past
+// the limit cannot learn whether a code is live: the VALID code is refused
+// exactly like a wrong one.
+func TestCLIDecisionBudgetRefusesValidCodeOnceSpent(t *testing.T) {
+ for _, action := range []string{"continue", "approve", "deny"} {
+ t.Run(action, func(t *testing.T) {
+ fx := newConsoleFixture(t)
+ browser := newBrowser(t)
+ signUp(t, fx, browser, "code-budget-"+action)
+ csrf := sessionToken(t, fx, browser)
+ for i := 0; i < cliGuessBurst; i++ {
+ status, _, _ := postForm(t, browser, fx.ts.URL+"/auth/cli/approve",
+ url.Values{"code": {"AAAA-AAAA"}, "action": {"continue"}, "csrf": {csrf}})
+ if status != http.StatusOK {
+ t.Fatalf("wrong code %d = %d, want 200 inside the budget", i, status)
+ }
+ }
+ _, start := postJSON(t, fx.ts.URL+"/auth/cli/start", map[string]string{"label": "budget@laptop"}, "")
+ userCode := str(t, start, "user_code")
+ code, _, body := postGated(t, fx, browser, fx.ts.URL+"/auth/cli/approve",
+ url.Values{"code": {userCode}, "action": {action}, "csrf": {csrf}})
+ if code != http.StatusTooManyRequests {
+ t.Fatalf("valid code past the budget = %d, want 429", code)
+ }
+ if strings.Contains(body, "budget@laptop") {
+ t.Fatalf("a refused lookup described the terminal:\n%s", body)
+ }
+ _, poll := postJSON(t, fx.ts.URL+"/auth/cli/poll", map[string]string{"device_code": str(t, start, "device_code")}, "")
+ if str(t, poll, "status") != CLIAuthPending {
+ t.Fatalf("poll = %v, want the request untouched and pending", poll)
+ }
+ })
+ }
+}
+
func TestCLIPollUnknownDeviceCode(t *testing.T) {
fx := newConsoleFixture(t)
status, poll := postJSON(t, fx.ts.URL+"/auth/cli/poll", map[string]string{"device_code": "nope"}, "")