| 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) |