carlos / platform Public

console: drop the per-address wrong-code lockout (H4-M6 review 1)

Astra: two sent attacker flows still spent the address budget, and the
held/sent split let a requester tell a known address from an unknown
one. Wrong codes now count per client IP only; step-up keeps a budget
keyed to the session that asked. The bound is per-challenge attempts
times the per-address send limit: 100 guesses a day, written down in
emailsignin_limits.go. Regressions for both scenarios.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev pushed by paul@keymail.dev 8fdb4e18481b8538e720cfa5bfc18ed1af07421a parent 5d5a6bd
7 files changed, +285 −202
  • internal/console/demo_claim_auth_test.go +2 −10
  • internal/console/emailsignin.go +8 −9
  • internal/console/emailsignin_limits.go +47 −52
  • internal/console/emailsignin_limits_test.go +76 −120
  • internal/console/emailsignin_lockout_test.go +139 −0
  • internal/console/emailsignin_store.go +4 −4
  • internal/console/emailsignin_test.go +9 −7
diff --git a/internal/console/demo_claim_auth_test.go b/internal/console/demo_claim_auth_test.go
index dfd0146d9..1707ef27b 100644
--- a/internal/console/demo_claim_auth_test.go
+++ b/internal/console/demo_claim_auth_test.go
@@ -329,7 +329,7 @@ func TestDemoClaimSharedLimits(t *testing.T) {
}
})
}
- for _, cap := range []string{"challenge", "address", "ip"} {
+ for _, cap := range []string{"challenge", "ip"} {
t.Run("guess_"+cap, func(t *testing.T) {
s, m, token := claimTestServer(t)
flow, _ := claimStart(t, s, token, "shop@example.com", "")
@@ -341,17 +341,9 @@ func TestDemoClaimSharedLimits(t *testing.T) {
t.Fatal("bad code worked")
}
}
- case "address":
- for i := 0; i < 10; i++ {
- done, ok := s.limits.reserveGuess("shop@example.com", "other-ip")
- if !ok {
- t.Fatal("early guess refusal")
- }
- done(false)
- }
case "ip":
for i := 0; i < 30; i++ {
- done, ok := s.limits.reserveGuess(fmt.Sprintf("shop%d@example.com", i), "192.0.2.10")
+ done, ok := s.limits.reserveCodeGuess(&EmailChallenge{Purpose: ChallengeDemoClaim, Address: fmt.Sprintf("shop%d@example.com", i)}, "192.0.2.10")
if !ok {
t.Fatal("early IP guess refusal")
}
diff --git a/internal/console/emailsignin.go b/internal/console/emailsignin.go
index ff2e4b773..b0f8486ee 100644
--- a/internal/console/emailsignin.go
+++ b/internal/console/emailsignin.go
@@ -435,12 +435,11 @@ func (s *Server) handleEmailLink(w http.ResponseWriter, r *http.Request) {
// handleEmailCode is POST /auth/email/code (spec 5.2): the typed code is
// checked only against the newest live sign-in challenge of this browser's
// flow. A challenge that was sent and one a refused request recorded unsent
-// answer alike, so the code box says nothing about throttling. Wrong codes
-// count against the challenge's address and the client IP across every
-// challenge (spec 5), a held challenge's apart from the sent ones' so a
-// stranger cannot spend the owner's budget (reserveCodeGuess); past either
-// cap the code is refused unchecked and nothing is burned, so the emailed
-// link still works.
+// answer alike, so the code box says nothing about throttling or about the
+// address. Five wrong codes burn the challenge; wrong codes also count
+// against the client IP across every challenge, and past that cap the code
+// is refused unchecked and nothing is burned, so the emailed link still
+// works. Nothing counts per address: see reserveCodeGuess for the bound.
func (s *Server) handleEmailCode(w http.ResponseWriter, r *http.Request) {
if !s.sameOrigin(r) {
s.renderMessage(w, r, http.StatusForbidden, "Refused", "Cross-site request blocked.")
@@ -619,9 +618,9 @@ func (s *Server) stepUpLink(w http.ResponseWriter, r *http.Request, link string,
// handleStepUpEmailCode is POST /auth/email/stepup/code: the typed code is
// checked only against the newest live step-up challenge made for this
-// browser's session. Wrong codes count against the
-// challenge's address and the client IP, as for sign-in codes, in a step-up
-// budget of their own that sign-in guesses cannot spend (reserveCodeGuess).
+// browser's session. Wrong codes count against the client IP, as for
+// sign-in codes, and against this session's own step-up budget, which no
+// other browser can spend (reserveCodeGuess).
func (s *Server) handleStepUpEmailCode(w http.ResponseWriter, r *http.Request) {
addr, raw, ok := s.session(r)
if !ok {
diff --git a/internal/console/emailsignin_limits.go b/internal/console/emailsignin_limits.go
index 771243e16..d7701f7b9 100644
--- a/internal/console/emailsignin_limits.go
+++ b/internal/console/emailsignin_limits.go
@@ -24,7 +24,7 @@ const (
newPersonPerIPHour = 10 // new people per IP
newPersonGlobalHour = 200 // new people console-wide
stepUpPerAddrHour = 10 // step-up emails per address
- wrongCodesPerAddrDay = 10 // wrong codes per address, per budget (see reserveCodeGuess)
+ wrongCodesPerSessDay = 10 // wrong step-up codes per session
wrongCodesPerIPHour = 30 // wrong codes per IP
// Alert confirmations (fleet alerts spec §5.3): every verify row that
@@ -139,9 +139,7 @@ type emailLimits struct {
ipSends keyedWindow // sign-in emails, by IP
globalSend []time.Time // sign-in emails to unknown addresses, console-wide
stepUps keyedWindow // step-up emails, by address
- addrWrong keyedWindow // wrong codes against sent sign-in challenges, by address
- heldWrong keyedWindow // wrong codes against held (unsent) sign-in challenges, by address
- stepWrong keyedWindow // wrong step-up codes, by address
+ stepWrong keyedWindow // wrong step-up codes, by session hash
ipWrong keyedWindow // wrong codes, by IP
alertConfirms keyedWindow // alert confirmation sends, by alert scope
@@ -176,7 +174,7 @@ func (l *emailLimits) submit(ip string) bool {
}
func (l *emailLimits) sweepWindows() []*keyedWindow {
- return []*keyedWindow{&l.submits, &l.addrSends, &l.ipSends, &l.stepUps, &l.addrWrong, &l.heldWrong, &l.stepWrong, &l.ipWrong, &l.alertConfirms, &l.ipNewPeople}
+ return []*keyedWindow{&l.submits, &l.addrSends, &l.ipSends, &l.stepUps, &l.stepWrong, &l.ipWrong, &l.alertConfirms, &l.ipNewPeople}
}
// newPerson reserves one new-person slot across all sign-in paths (social
@@ -320,66 +318,61 @@ func (l *emailLimits) stepUpSend(addr string) bool {
return true
}
-// reserveGuess is reserveCodeGuess for a code typed against a sent sign-in
-// challenge for addr: the sign-in budget, charged to addr and ip.
-// Callers pass addr canonical (trimmed, lower-cased).
-func (l *emailLimits) reserveGuess(addr, ip string) (done func(ok bool), allowed bool) {
- return l.reserveGuessIn(&l.addrWrong, nil, addr, ip)
-}
-
// reserveCodeGuess is the guessing cap for a code typed against c, as one
// step, so concurrent guesses cannot all pass on the same count. Under the
-// lock it refuses (allowed false) once c's address has 10 wrong codes in 24
-// hours in the budget that applies, or ip has 30 in an hour; otherwise it
-// records one guess against both at once and returns done, through which the
-// caller reports the verdict: done(true), a right code, takes the reservation
-// back; done(false), any other outcome, keeps it as a wrong code. done acts
-// once; later calls do nothing. Refused, done is a no-op.
+// lock it refuses (allowed false) once ip has 30 wrong codes in an hour or,
+// for a step-up, c's session has 10 in 24 hours; otherwise it records one
+// guess and returns done, through which the caller reports the verdict:
+// done(true), a right code, takes the reservation back; done(false), any
+// other outcome, keeps it as a wrong code. done acts once; later calls do
+// nothing. Refused, done is a no-op.
//
-// Wrong codes are bounded per address across all its challenges (spec §5), so
-// a fresh challenge buys no fresh guesses. But the address budget is one a
-// stranger who knows only the address could spend, locking the owner's own
-// right code out for a day (H4-M6). So it is split by who could know a code:
-// - Sent sign-in challenges (and demo claims) share addrWrong. Only these
-// can be guessed into: their code is in the owner's mailbox.
-// - A held challenge (Store.HoldEmailChallenge: a refused request's, whose
-// code nobody is told) never redeems a code, so guessing it risks
-// nothing. Its wrong codes go to heldWrong, and the cap refuses a held
-// challenge on addrWrong plus heldWrong, so it is throttled exactly as a
-// sent one would be, while the owner's sent challenge reads addrWrong
-// alone.
-// - Step-up challenges go to stepWrong: only the address's own session can
-// ask for one, so sign-in guesses never lock it, nor it them.
+// There is no per-address budget (H4-M6). Anyone who knows an address can
+// open sign-in flows for it, so a budget keyed by the address was one a
+// stranger could spend, locking the owner's own right code out for a day;
+// and since a held challenge and a sent one had to be charged alike or tell
+// the requester which it was, keeping it meant also keeping that lockout.
+// Guessing is bounded instead by what a guess can reach:
+// - A code is checked only against the newest challenge of the guessing
+// browser's own flow, and the fifth wrong code burns it
+// (emailCodeAttempts, counted in the row).
+// - A held challenge (Store.HoldEmailChallenge) never redeems a code: its
+// code was never sent. Guessing it is worthless, so its number is
+// unbounded without harm.
+// - A sent challenge exists only when signinSend lets an email go to the
+// address: at most signinPerAddrDay (20) a day, demo claims included.
//
-// The IP budget covers them all.
+// So an address faces at most 20 x 5 = 100 guesses a day at codes that
+// redeem, each right with probability 1 in 10^6: at most 1e-4 a day, about
+// 3.6% over a year of continuous attack on one address. A restart resets the
+// send windows (see the top of this file) and buys a fresh day; the IP
+// budget below caps how fast any one client gets there. Step-up codes go to
+// a budget keyed by the session that asked (only that session can type
+// them), so no one else can spend it.
func (l *emailLimits) reserveCodeGuess(c *EmailChallenge, ip string) (done func(ok bool), allowed bool) {
- switch {
- case c.Purpose == ChallengeStepUp:
- return l.reserveGuessIn(&l.stepWrong, nil, c.Address, ip)
- case c.held:
- return l.reserveGuessIn(&l.heldWrong, &l.addrWrong, c.Address, ip)
+ if c.Purpose == ChallengeStepUp {
+ return l.reserveGuessIn(&l.stepWrong, c.SessionHash, ip)
}
- return l.reserveGuessIn(&l.addrWrong, nil, c.Address, ip)
+ return l.reserveGuessIn(nil, "", ip)
}
-// reserveGuessIn is reserveCodeGuess over one address window, w, charged and
-// counted; also, when not nil, counted but never charged here.
-func (l *emailLimits) reserveGuessIn(w, also *keyedWindow, addr, ip string) (done func(ok bool), allowed bool) {
+// reserveGuessIn is reserveCodeGuess over the IP window and, when w is not
+// nil, w's key, capped at wrongCodesPerSessDay a day.
+func (l *emailLimits) reserveGuessIn(w *keyedWindow, key, ip string) (done func(ok bool), allowed bool) {
l.mu.Lock()
defer l.mu.Unlock()
now := l.clock()
- day := now.Add(-24 * time.Hour)
- w.trim(addr, now)
l.ipWrong.trim(ip, now)
- n := w.count(addr, day)
- if also != nil {
- also.trim(addr, now)
- n += also.count(addr, day)
- }
- if n >= wrongCodesPerAddrDay || l.ipWrong.count(ip, now.Add(-time.Hour)) >= wrongCodesPerIPHour {
+ if l.ipWrong.count(ip, now.Add(-time.Hour)) >= wrongCodesPerIPHour {
return func(bool) {}, false
}
- w.add(addr, now)
+ if w != nil {
+ w.trim(key, now)
+ if w.count(key, now.Add(-24*time.Hour)) >= wrongCodesPerSessDay {
+ return func(bool) {}, false
+ }
+ w.add(key, now)
+ }
l.ipWrong.add(ip, now)
var once sync.Once
return func(ok bool) {
@@ -389,7 +382,9 @@ func (l *emailLimits) reserveGuessIn(w, also *keyedWindow, addr, ip string) (don
}
l.mu.Lock()
defer l.mu.Unlock()
- w.remove(addr, now)
+ if w != nil {
+ w.remove(key, now)
+ }
l.ipWrong.remove(ip, now)
})
}, true
diff --git a/internal/console/emailsignin_limits_test.go b/internal/console/emailsignin_limits_test.go
index eaecf6e49..b8aa5de08 100644
--- a/internal/console/emailsignin_limits_test.go
+++ b/internal/console/emailsignin_limits_test.go
@@ -307,50 +307,79 @@ func TestStepUpSendSeparate(t *testing.T) {
}
}
+// signinCode and stepUpCode are challenges a code can be typed against.
+func signinCode(addr string) *EmailChallenge {
+ return &EmailChallenge{Purpose: ChallengeSignIn, Address: addr}
+}
+
+func stepUpCode(addr, session string) *EmailChallenge {
+ return &EmailChallenge{Purpose: ChallengeStepUp, Address: addr, SessionHash: session}
+}
+
// wrongGuess reserves one guess and keeps it, as a wrong code does.
-func wrongGuess(t *testing.T, l *emailLimits, addr, ip string) {
+func wrongGuess(t *testing.T, l *emailLimits, c *EmailChallenge, ip string) {
t.Helper()
- done, ok := l.reserveGuess(addr, ip)
+ done, ok := l.reserveCodeGuess(c, ip)
if !ok {
- t.Fatalf("guess for %s from %s refused", addr, ip)
+ t.Fatalf("guess for %s from %s refused", c.Address, ip)
}
done(false)
}
// guessAllowed reports whether a guess would be allowed now, taking the
// reservation straight back as a right code does.
-func guessAllowed(l *emailLimits, addr, ip string) bool {
- done, ok := l.reserveGuess(addr, ip)
+func guessAllowed(l *emailLimits, c *EmailChallenge, ip string) bool {
+ done, ok := l.reserveCodeGuess(c, ip)
done(true)
return ok
}
-func TestCodeGuessCaps(t *testing.T) {
+// No per-address budget (H4-M6): any number of wrong codes against an
+// address, from IPs under their own cap, leave its next code checkable, sent
+// or held, sign-in or step-up.
+func TestCodeGuessNoAddressBudget(t *testing.T) {
c := newLimitClock()
l := newTestLimits(c)
const addr = "bob@example.com"
+ held := &EmailChallenge{Purpose: ChallengeSignIn, Address: addr, held: true}
+ for i := 0; i < 100; i++ {
+ wrongGuess(t, l, signinCode(addr), fmt.Sprintf("9.9.%d.1", i))
+ wrongGuess(t, l, held, fmt.Sprintf("9.9.%d.2", i))
+ wrongGuess(t, l, &EmailChallenge{Purpose: ChallengeDemoClaim, Address: addr}, fmt.Sprintf("9.9.%d.3", i))
+ }
+ if !guessAllowed(l, signinCode(addr), "1.1.1.1") || !guessAllowed(l, held, "1.1.1.1") {
+ t.Fatal("wrong codes from other IPs locked the address's sign-in")
+ }
+ if !guessAllowed(l, stepUpCode(addr, "bob-session"), "1.1.1.1") {
+ t.Fatal("sign-in wrong codes locked the address's step-up")
+ }
+}
- if !guessAllowed(l, addr, "1.1.1.1") {
- t.Fatal("fresh address not allowed")
+// A step-up's budget is its session's: ten wrong codes in a day lock that
+// session's step-up from any IP, and nothing else.
+func TestCodeGuessStepUpSessionCap(t *testing.T) {
+ c := newLimitClock()
+ l := newTestLimits(c)
+ const addr = "bob@example.com"
+ for i := 0; i < wrongCodesPerSessDay-1; i++ {
+ wrongGuess(t, l, stepUpCode(addr, "s1"), fmt.Sprintf("9.9.9.%d", i))
}
- for i := 0; i < 9; i++ {
- wrongGuess(t, l, addr, fmt.Sprintf("9.9.9.%d", i))
+ if !guessAllowed(l, stepUpCode(addr, "s1"), "1.1.1.1") {
+ t.Fatal("session blocked after 9 wrong codes")
}
- if !guessAllowed(l, addr, "1.1.1.1") {
- t.Fatal("blocked after 9 wrong codes")
+ wrongGuess(t, l, stepUpCode(addr, "s1"), "9.9.9.99")
+ if guessAllowed(l, stepUpCode(addr, "s1"), "1.1.1.1") || guessAllowed(l, stepUpCode(addr, "s1"), "4.4.4.4") {
+ t.Fatal("session allowed after 10 wrong codes")
}
- wrongGuess(t, l, addr, "9.9.9.99")
- // Ten wrong codes against one address block it from any IP.
- if guessAllowed(l, addr, "1.1.1.1") || guessAllowed(l, addr, "4.4.4.4") {
- t.Fatal("address allowed after 10 wrong codes")
+ if !guessAllowed(l, stepUpCode(addr, "s2"), "1.1.1.1") {
+ t.Fatal("another session of the same address blocked")
}
- // Another address from a fresh IP is unaffected.
- if !guessAllowed(l, "other@example.com", "1.1.1.1") {
- t.Fatal("other address blocked")
+ if !guessAllowed(l, signinCode(addr), "1.1.1.1") {
+ t.Fatal("a step-up lockout locked sign-in")
}
c.advance(24*time.Hour + time.Second)
- if !guessAllowed(l, addr, "1.1.1.1") {
- t.Fatal("address still blocked after 24h")
+ if !guessAllowed(l, stepUpCode(addr, "s1"), "1.1.1.1") {
+ t.Fatal("session still blocked after 24h")
}
}
@@ -358,72 +387,73 @@ func TestCodeGuessIPCap(t *testing.T) {
c := newLimitClock()
l := newTestLimits(c)
for i := 0; i < 29; i++ {
- wrongGuess(t, l, fmt.Sprintf("a%d@example.com", i), "5.5.5.5")
+ wrongGuess(t, l, signinCode(fmt.Sprintf("a%d@example.com", i)), "5.5.5.5")
}
- if !guessAllowed(l, "fresh@example.com", "5.5.5.5") {
+ if !guessAllowed(l, signinCode("fresh@example.com"), "5.5.5.5") {
t.Fatal("IP blocked after 29 wrong codes")
}
- wrongGuess(t, l, "a29@example.com", "5.5.5.5")
- // 30 wrong codes from one IP block that IP for any address.
- if guessAllowed(l, "fresh@example.com", "5.5.5.5") {
+ wrongGuess(t, l, stepUpCode("a29@example.com", "s"), "5.5.5.5")
+ // 30 wrong codes from one IP, of any kind, block that IP for any code.
+ if guessAllowed(l, signinCode("fresh@example.com"), "5.5.5.5") || guessAllowed(l, stepUpCode("x@example.com", "t"), "5.5.5.5") {
t.Fatal("IP allowed after 30 wrong codes")
}
- if !guessAllowed(l, "fresh@example.com", "6.6.6.6") {
+ if !guessAllowed(l, signinCode("fresh@example.com"), "6.6.6.6") {
t.Fatal("another IP blocked")
}
c.advance(time.Hour + time.Second)
- if !guessAllowed(l, "fresh@example.com", "5.5.5.5") {
+ if !guessAllowed(l, signinCode("fresh@example.com"), "5.5.5.5") {
t.Fatal("IP still blocked after an hour")
}
}
-// A right code takes its reservation back, against both keys, once.
+// A right code takes its reservation back, against every key, once.
func TestCodeGuessRightCodeRefunds(t *testing.T) {
c := newLimitClock()
l := newTestLimits(c)
- const addr = "bob@example.com"
+ step := stepUpCode("bob@example.com", "s1")
for i := 0; i < 9; i++ {
- wrongGuess(t, l, addr, "1.1.1.1")
+ wrongGuess(t, l, step, "1.1.1.1")
}
for i := 0; i < 5; i++ {
- done, ok := l.reserveGuess(addr, "1.1.1.1")
+ done, ok := l.reserveCodeGuess(step, "1.1.1.1")
if !ok {
t.Fatalf("right code %d refused after 9 wrong", i+1)
}
done(true)
done(true) // a second report changes nothing
}
- if n := l.addrWrong.count(addr, c.now().Add(-24*time.Hour)); n != 9 {
- t.Fatalf("address holds %d guesses, want the 9 wrong", n)
+ if n := l.stepWrong.count("s1", c.now().Add(-24*time.Hour)); n != 9 {
+ t.Fatalf("session holds %d guesses, want the 9 wrong", n)
}
if n := l.ipWrong.count("1.1.1.1", c.now().Add(-time.Hour)); n != 9 {
t.Fatalf("IP holds %d guesses, want the 9 wrong", n)
}
// A kept reservation is the tenth wrong code.
- done, _ := l.reserveGuess(addr, "1.1.1.1")
+ done, _ := l.reserveCodeGuess(step, "1.1.1.1")
done(false)
done(true) // too late: done acts once
- if guessAllowed(l, addr, "1.1.1.1") {
- t.Fatal("address allowed after 10 kept guesses")
+ if guessAllowed(l, step, "1.1.1.1") {
+ t.Fatal("session allowed after 10 kept guesses")
}
}
-// The cap is one step: with nine wrong codes recorded, any number of guesses
-// arriving together let exactly one through.
+// The cap is one step: one short of a cap, any number of guesses arriving
+// together let exactly one through.
func TestCodeGuessConcurrentBoundary(t *testing.T) {
for _, tc := range []struct {
- name string
- addr, ip func(i int) string
- have int
+ name string
+ code func(i int) *EmailChallenge
+ ip func(i int) string
+ have int
}{
- {"address", func(int) string { return "bob@example.com" }, func(i int) string { return fmt.Sprintf("10.0.0.%d", i) }, wrongCodesPerAddrDay - 1},
- {"ip", func(i int) string { return fmt.Sprintf("u%d@example.com", i) }, func(int) string { return "5.5.5.5" }, wrongCodesPerIPHour - 1},
+ {"session", func(int) *EmailChallenge { return stepUpCode("bob@example.com", "s1") }, func(i int) string { return fmt.Sprintf("10.0.0.%d", i) }, wrongCodesPerSessDay - 1},
+ {"ip", func(i int) *EmailChallenge { return signinCode(fmt.Sprintf("u%d@example.com", i)) }, func(int) string { return "5.5.5.5" }, wrongCodesPerIPHour - 1},
} {
t.Run(tc.name, func(t *testing.T) {
c := newLimitClock()
l := newTestLimits(c)
for i := 0; i < tc.have; i++ {
- wrongGuess(t, l, tc.addr(1000+i), tc.ip(1000+i))
+ wrongGuess(t, l, tc.code(1000+i), tc.ip(1000+i))
}
const n = 32
var (
@@ -436,7 +466,7 @@ func TestCodeGuessConcurrentBoundary(t *testing.T) {
go func() {
defer wg.Done()
<-start
- if done, ok := l.reserveGuess(tc.addr(i), tc.ip(i)); ok {
+ if done, ok := l.reserveCodeGuess(tc.code(i), tc.ip(i)); ok {
allowed.Add(1)
done(false)
}
@@ -550,77 +580,3 @@ func TestSigninSendSweepCleansStaleAcrossCalls(t *testing.T) {
t.Fatal("stepUps or alertConfirms not swept")
}
}
-
-// The wrong-code budget is split by who could know the code (H4-M6): a held
-// challenge's wrong codes never lock the owner's sent one, yet a held one is
-// throttled as a sent one would be; step-up has its own budget; the IP
-// budget covers all.
-func TestReserveCodeGuessSplitsTheAddressBudget(t *testing.T) {
- const addr = "bob@example.com"
- sent := &EmailChallenge{Purpose: ChallengeSignIn, Address: addr}
- held := &EmailChallenge{Purpose: ChallengeSignIn, Address: addr, held: true}
- step := &EmailChallenge{Purpose: ChallengeStepUp, Address: addr}
- guess := func(l *emailLimits, c *EmailChallenge, ip string) bool {
- done, ok := l.reserveCodeGuess(c, ip)
- done(false)
- return ok
- }
-
- // A stranger spends ten wrong codes on held challenges.
- l := newTestLimits(newLimitClock())
- for i := 0; i < wrongCodesPerAddrDay; i++ {
- if !guess(l, held, fmt.Sprintf("203.0.113.%d", i)) {
- t.Fatalf("held guess %d refused early", i+1)
- }
- }
- if guess(l, held, "203.0.113.200") {
- t.Fatal("held challenge not throttled past the address cap")
- }
- if !guess(l, sent, "198.51.100.1") {
- t.Fatal("held-challenge guesses locked the owner's sent challenge")
- }
- if !guess(l, step, "198.51.100.1") {
- t.Fatal("sign-in guesses locked the owner's step-up")
- }
-
- // Ten wrong codes on sent challenges lock sent and held alike, not step-up.
- l = newTestLimits(newLimitClock())
- for i := 0; i < wrongCodesPerAddrDay; i++ {
- if !guess(l, sent, fmt.Sprintf("203.0.113.%d", i)) {
- t.Fatalf("sent guess %d refused early", i+1)
- }
- }
- if guess(l, sent, "203.0.113.200") || guess(l, held, "203.0.113.200") {
- t.Fatal("address not throttled after ten wrong codes on sent challenges")
- }
- if !guess(l, step, "198.51.100.1") {
- t.Fatal("sign-in guesses locked the owner's step-up")
- }
-
- // Step-up's own ten lock step-up only.
- l = newTestLimits(newLimitClock())
- for i := 0; i < wrongCodesPerAddrDay; i++ {
- if !guess(l, step, "198.51.100.1") {
- t.Fatalf("step-up guess %d refused early", i+1)
- }
- }
- if guess(l, step, "198.51.100.2") {
- t.Fatal("step-up not throttled past its address cap")
- }
- if !guess(l, sent, "198.51.100.3") {
- t.Fatal("step-up guesses locked sign-in")
- }
-
- // One IP's thirty wrong codes, across any budgets, refuse it everywhere.
- l = newTestLimits(newLimitClock())
- for i := 0; i < wrongCodesPerIPHour; i++ {
- c := []*EmailChallenge{sent, held, step}[i%3]
- c = &EmailChallenge{Purpose: c.Purpose, Address: fmt.Sprintf("u%d@example.com", i), held: c.held}
- if !guess(l, c, "192.0.2.10") {
- t.Fatalf("IP guess %d refused early", i+1)
- }
- }
- if guess(l, &EmailChallenge{Purpose: ChallengeSignIn, Address: "fresh@example.com", held: true}, "192.0.2.10") {
- t.Fatal("IP not throttled after thirty wrong codes")
- }
-}
diff --git a/internal/console/emailsignin_lockout_test.go b/internal/console/emailsignin_lockout_test.go
new file mode 100644
index 000000000..184383979
--- /dev/null
+++ b/internal/console/emailsignin_lockout_test.go
@@ -0,0 +1,139 @@
+package console
+
+// Review round 1 on H4-M5/H4-M6 (Astra): an unauthenticated requester must
+// not exhaust another browser's authentication budget, nor learn whether the
+// console knows an address.
+
+import (
+ "fmt"
+ "net/http"
+ "net/http/httptest"
+ "net/url"
+ "strings"
+ "sync/atomic"
+ "testing"
+ "time"
+)
+
+// skewLimits moves fx's limit clock (only) forward on demand, so a test can
+// step past the 60-second resend gap without sleeping. Challenges keep real
+// time, so nothing expires.
+func skewLimits(fx *consoleFixture) (advance func(time.Duration)) {
+ var off atomic.Int64
+ fx.srv.limits.now = func() time.Time { return time.Now().Add(time.Duration(off.Load())) }
+ return func(d time.Duration) { off.Add(int64(d)) }
+}
+
+// postAs posts form to path through the production router from ip, holding
+// only its own flow cookie, as a script would.
+func postAs(t *testing.T, fx *consoleFixture, ip, flow, path string, form url.Values) (int, string) {
+ t.Helper()
+ r := httptest.NewRequest(http.MethodPost, path, strings.NewReader(form.Encode()))
+ r.RemoteAddr = ip
+ r.Header.Set("Content-Type", "application/x-www-form-urlencoded")
+ r.AddCookie(&http.Cookie{Name: flowCookie, Value: flow})
+ w := httptest.NewRecorder()
+ fx.srv.ServeHTTP(w, r)
+ return w.Code, w.Body.String()
+}
+
+// Astra's scenario: the attacker waits out the resend gap twice, so both of
+// its flows get SENT challenges (two real emails to the victim), and spends
+// five wrong codes on each. The victim's own code, in its own flow, still
+// signs in.
+func TestSentFlowsCannotLockTheOwnersCode(t *testing.T) {
+ fx, m := newEmailFixture(t, true)
+ advance := skewLimits(fx)
+ const addr = "victim@example.com"
+ lookupAnswers(fx, addr, http.StatusNotFound)
+ victim := newBrowser(t)
+ code, _ := askSignin(t, fx, m, victim, addr, "")
+
+ for i, flow := range []string{
+ "attacker-sent-flow-one-aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
+ "attacker-sent-flow-two-bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb",
+ } {
+ advance(emailResendGap + time.Second)
+ before := len(m.sentCopy())
+ if status, _ := postAs(t, fx, huntM6AttackerAddr, flow, "/auth/continue", url.Values{"address": {addr}}); status != http.StatusOK {
+ t.Fatalf("attacker continue %d = %d", i+1, status)
+ }
+ waitChallengeSent(t, fx, 1)
+ if len(m.sentCopy()) != before+1 {
+ t.Fatalf("attacker flow %d was not sent an email; the scenario needs sent challenges", i+1)
+ }
+ for j := 1; j <= emailCodeAttempts; j++ {
+ want := "check.code_wrong"
+ if j == emailCodeAttempts {
+ want = "check.code_used_up"
+ }
+ status, body := postAs(t, fx, huntM6AttackerAddr, flow, "/auth/email/code", url.Values{"code": {"zzzzzz"}})
+ if status != http.StatusOK || !strings.Contains(body, esc(copyText(want))) {
+ t.Fatalf("attacker junk code %d in flow %d = %d, want %s", j, i+1, status, want)
+ }
+ }
+ }
+ status, loc, body := postCode(t, fx, victim, code)
+ if status != http.StatusSeeOther || !strings.HasPrefix(loc, "/a/") {
+ t.Fatalf("victim's right code after ten wrong codes on two sent attacker flows = %d -> %q (throttled: %v); want 303 into an account",
+ status, loc, strings.Contains(body, esc(copyText("check.code_throttled"))))
+ }
+}
+
+// Astra's oracle: with the console-wide stranger budget full and the
+// attacker's IP out of sends, a third flow from a fresh IP is SENT for a
+// known address and HELD for an unknown one. Every response the attacker
+// sees, at every step, must be byte-identical either way.
+func TestKnownAndUnknownAddressAnswerAlike(t *testing.T) {
+ const target = "target@example.com"
+ run := func(known bool) []string {
+ fx, _ := newEmailFixture(t, true)
+ lookupAnswers(fx, target, http.StatusNotFound)
+ if known {
+ if err := fx.st.CreateAccount(42, "cdef", "target", target, time.Now()); err != nil {
+ t.Fatal(err)
+ }
+ }
+ const attackerIP, freshIP = "203.0.113.66:40000", "198.51.100.7:40000"
+ // Spend the attacker IP's sends, then the rest of the stranger budget.
+ for i := 0; i < signinPerIPHour; i++ {
+ if !fx.srv.limits.signinSend(fmt.Sprintf("a%d@example.org", i), "203.0.113.66") {
+ t.Fatal("setup: attacker send refused")
+ }
+ }
+ for i := signinPerIPHour; i < signinGlobalHour; i++ {
+ if !fx.srv.limits.signinSend(fmt.Sprintf("g%d@example.org", i), fmt.Sprintf("10.1.%d.%d", i/250, i%250)) {
+ t.Fatal("setup: filler send refused")
+ }
+ }
+ var seen []string
+ step := func(ip, flow, path string, form url.Values) {
+ status, body := postAs(t, fx, ip, flow, path, form)
+ seen = append(seen, fmt.Sprintf("%s %s %d\n%s", ip, path, status, strings.ReplaceAll(body, fx.ts.URL, "ORIGIN")))
+ }
+ flows := []struct{ ip, flow string }{
+ {attackerIP, "oracle-flow-one-aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"},
+ {attackerIP, "oracle-flow-two-bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"},
+ {freshIP, "oracle-flow-three-ccccccccccccccccccccccccccccccccccc"},
+ }
+ for _, f := range flows {
+ step(f.ip, f.flow, "/auth/continue", url.Values{"address": {target}})
+ for j := 0; j <= emailCodeAttempts; j++ { // one past the burn
+ step(f.ip, f.flow, "/auth/email/code", url.Values{"code": {"zzzzzz"}, "address": {target}})
+ }
+ }
+ if known {
+ waitChallengeSent(t, fx, 1) // flow three's email to the known address
+ }
+ return seen
+ }
+ known, unknown := run(true), run(false)
+ if len(known) != len(unknown) {
+ t.Fatalf("%d steps known, %d unknown", len(known), len(unknown))
+ }
+ for i := range known {
+ if known[i] != unknown[i] {
+ t.Errorf("step %d differs between a known and an unknown address:\nknown:\n%s\nunknown:\n%s", i+1, known[i], unknown[i])
+ }
+ }
+}
diff --git a/internal/console/emailsignin_store.go b/internal/console/emailsignin_store.go
index 6e0ced18d..d2c440f1c 100644
--- a/internal/console/emailsignin_store.go
+++ b/internal/console/emailsignin_store.go
@@ -51,7 +51,7 @@ type EmailChallenge struct {
row string
// held, set by LiveEmailChallenge, is a challenge HoldEmailChallenge
// stored for a refused request: its code was never sent, and no code
- // redeems it. reserveCodeGuess charges its wrong codes apart.
+ // redeems it.
held bool
}
@@ -164,9 +164,9 @@ func (s *Store) CreateEmailChallenge(c EmailChallenge, now time.Time) (link, cod
// so the email already sent keeps working; otherwise it stores c with a link
// and code nobody is ever told, so a typed code reads exactly as it would
// against a sent one, and nothing the mailbox could redeem exists. The row is
-// marked held: no code ever redeems it (TakeEmailCode), and its wrong codes
-// are charged apart from the address's sent challenges (reserveCodeGuess),
-// so guessing at it can neither sign anyone in nor lock the owner out.
+// marked held: no code ever redeems it (TakeEmailCode), so guessing at it is
+// worthless, and only sent challenges count toward the guessing bound
+// (reserveCodeGuess).
func (s *Store) HoldEmailChallenge(c EmailChallenge, now time.Time) error {
col, ok := challengeBinding(c.Purpose)
if !ok {
diff --git a/internal/console/emailsignin_test.go b/internal/console/emailsignin_test.go
index 22b8178bd..cfb5d3973 100644
--- a/internal/console/emailsignin_test.go
+++ b/internal/console/emailsignin_test.go
@@ -922,8 +922,10 @@ func TestEmailCodeCrossDevice(t *testing.T) {
}
}
-// Spec r2-2: wrong codes count per address across fresh challenges. Past the
-// cap the code box refuses without checking, while a link still signs in.
+// Five wrong codes burn a challenge, and a fresh one (which costs an email to
+// the address, under its send limits) has its own five. Wrong codes count per
+// client IP across challenges, never per address (H4-M6); past the IP cap
+// the code box refuses without checking, while a link still signs in.
func TestEmailCodeGuessCap(t *testing.T) {
fx := newConsoleFixture(t)
const addr = "bob@example.com"
@@ -952,13 +954,13 @@ func TestEmailCodeGuessCap(t *testing.T) {
}
}
}
- _, code := fresh()
- wrong(5, code)
- _, code = fresh()
- wrong(5, code)
+ for i := 0; i < wrongCodesPerIPHour/emailCodeAttempts; i++ {
+ _, code := fresh()
+ wrong(emailCodeAttempts, code)
+ }
link, code := fresh()
- // The eleventh wrong code, and even the right one, is throttled unchecked.
+ // The IP's next wrong code, and even the right one, is throttled unchecked.
for _, try := range []string{wrongCode(code), code} {
status, _, body := postCode(t, fx, c, try)
if status != http.StatusOK || !strings.Contains(body, esc(copyText("check.code_throttled"))) {