| index 1e0bc664a..ddc5e46ed 100644 |
| --- a/internal/console/emailsignin_limits_test.go |
| +++ b/internal/console/emailsignin_limits_test.go |
| @@ -2,6 +2,8 @@ package console |
| |
| import ( |
| "fmt" |
| + "sync" |
| + "sync/atomic" |
| "testing" |
| "time" |
| ) |
| @@ -179,31 +181,49 @@ func TestStepUpSendSeparate(t *testing.T) { |
| } |
| } |
| |
| +// wrongGuess reserves one guess and keeps it, as a wrong code does. |
| +func wrongGuess(t *testing.T, l *emailLimits, addr, ip string) { |
| + t.Helper() |
| + done, ok := l.reserveGuess(addr, ip) |
| + if !ok { |
| + t.Fatalf("guess for %s from %s refused", addr, 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) |
| + done(true) |
| + return ok |
| +} |
| + |
| func TestCodeGuessCaps(t *testing.T) { |
| c := newLimitClock() |
| l := newTestLimits(c) |
| const addr = "bob@example.com" |
| |
| - if !l.codeAllowed(addr, "1.1.1.1") { |
| + if !guessAllowed(l, addr, "1.1.1.1") { |
| t.Fatal("fresh address not allowed") |
| } |
| for i := 0; i < 9; i++ { |
| - l.codeFailed(addr, fmt.Sprintf("9.9.9.%d", i)) |
| + wrongGuess(t, l, addr, fmt.Sprintf("9.9.9.%d", i)) |
| } |
| - if !l.codeAllowed(addr, "1.1.1.1") { |
| + if !guessAllowed(l, addr, "1.1.1.1") { |
| t.Fatal("blocked after 9 wrong codes") |
| } |
| - l.codeFailed(addr, "9.9.9.99") |
| + wrongGuess(t, l, addr, "9.9.9.99") |
| // Ten wrong codes against one address block it from any IP. |
| - if l.codeAllowed(addr, "1.1.1.1") || l.codeAllowed(addr, "4.4.4.4") { |
| + if guessAllowed(l, addr, "1.1.1.1") || guessAllowed(l, addr, "4.4.4.4") { |
| t.Fatal("address allowed after 10 wrong codes") |
| } |
| // Another address from a fresh IP is unaffected. |
| - if !l.codeAllowed("other@example.com", "1.1.1.1") { |
| + if !guessAllowed(l, "other@example.com", "1.1.1.1") { |
| t.Fatal("other address blocked") |
| } |
| c.advance(24*time.Hour + time.Second) |
| - if !l.codeAllowed(addr, "1.1.1.1") { |
| + if !guessAllowed(l, addr, "1.1.1.1") { |
| t.Fatal("address still blocked after 24h") |
| } |
| } |
| @@ -212,25 +232,99 @@ func TestCodeGuessIPCap(t *testing.T) { |
| c := newLimitClock() |
| l := newTestLimits(c) |
| for i := 0; i < 29; i++ { |
| - l.codeFailed(fmt.Sprintf("a%d@example.com", i), "5.5.5.5") |
| + wrongGuess(t, l, fmt.Sprintf("a%d@example.com", i), "5.5.5.5") |
| } |
| - if !l.codeAllowed("fresh@example.com", "5.5.5.5") { |
| + if !guessAllowed(l, "fresh@example.com", "5.5.5.5") { |
| t.Fatal("IP blocked after 29 wrong codes") |
| } |
| - l.codeFailed("a29@example.com", "5.5.5.5") |
| + wrongGuess(t, l, "a29@example.com", "5.5.5.5") |
| // 30 wrong codes from one IP block that IP for any address. |
| - if l.codeAllowed("fresh@example.com", "5.5.5.5") { |
| + if guessAllowed(l, "fresh@example.com", "5.5.5.5") { |
| t.Fatal("IP allowed after 30 wrong codes") |
| } |
| - if !l.codeAllowed("fresh@example.com", "6.6.6.6") { |
| + if !guessAllowed(l, "fresh@example.com", "6.6.6.6") { |
| t.Fatal("another IP blocked") |
| } |
| c.advance(time.Hour + time.Second) |
| - if !l.codeAllowed("fresh@example.com", "5.5.5.5") { |
| + if !guessAllowed(l, "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. |
| +func TestCodeGuessRightCodeRefunds(t *testing.T) { |
| + c := newLimitClock() |
| + l := newTestLimits(c) |
| + const addr = "bob@example.com" |
| + for i := 0; i < 9; i++ { |
| + wrongGuess(t, l, addr, "1.1.1.1") |
| + } |
| + for i := 0; i < 5; i++ { |
| + done, ok := l.reserveGuess(addr, "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.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(false) |
| + done(true) // too late: done acts once |
| + if guessAllowed(l, addr, "1.1.1.1") { |
| + t.Fatal("address 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. |
| +func TestCodeGuessConcurrentBoundary(t *testing.T) { |
| + for _, tc := range []struct { |
| + name string |
| + addr, 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}, |
| + } { |
| + 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)) |
| + } |
| + const n = 32 |
| + var ( |
| + start = make(chan struct{}) |
| + wg sync.WaitGroup |
| + allowed atomic.Int32 |
| + ) |
| + for i := 0; i < n; i++ { |
| + wg.Add(1) |
| + go func() { |
| + defer wg.Done() |
| + <-start |
| + if done, ok := l.reserveGuess(tc.addr(i), tc.ip(i)); ok { |
| + allowed.Add(1) |
| + done(false) |
| + } |
| + }() |
| + } |
| + close(start) |
| + wg.Wait() |
| + if got := allowed.Load(); got != 1 { |
| + t.Fatalf("%d of %d concurrent guesses allowed at the boundary, want exactly 1", got, n) |
| + } |
| + }) |
| + } |
| +} |
| + |
| func TestSigninSendSubmitCap(t *testing.T) { |
| c := newLimitClock() |
| l := newTestLimits(c) |