fix(auth): one invite makes one account, and a burst of sign-ups can't outrun the limit (#8106)
release / go (push) Successful in 2m8s
release / web (push) Successful in 1m13s
release / govulncheck (push) Successful in 19s
release / integration (push) Successful in 4m7s
release / android (push) Successful in 4m15s
release / Build signed APK (push) Successful in 4m46s
release / Attach APK to the Release (tag releases only) (push) Skipped
release / Build + push container image (push) Successful in 25s
release / Verify release artifacts (tag releases only) (push) Skipped

Registration inserted the user and only then claimed the invite. When the
claim lost a race it kept the account anyway, so parallel sign-ups on one
invite link each made an account. The insert and the claim now run in one
transaction: RedeemInvite's row lock makes the second sign-up wait for the
first, find the invite used, and roll its account back with invite_invalid.

The register and forgot-password limits checked and counted in two steps,
so a parallel burst all read the count before any attempt was added. They
now call AttemptLimiter.Allow, which does both under the lock.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-26 23:45:07 -05:00
co-authored by Claude Opus 5.5
parent 1b13aa8b7f
commit 1c28e17dfc
5 changed files with 170 additions and 37 deletions
+33
View File
@@ -1,6 +1,8 @@
package auth
import (
"sync"
"sync/atomic"
"testing"
"time"
)
@@ -112,3 +114,34 @@ func TestLoginGuard_AddressLimitSpansAccounts(t *testing.T) {
t.Fatal("a success must not clear the address count")
}
}
// A parallel burst on one key gets exactly max attempts through: the check
// and the count are one step, so no request reads the count before another
// has added to it.
func TestAttemptLimiter_AllowAdmitsExactlyMaxFromAParallelBurst(t *testing.T) {
l, c := newTestLimiter(10, time.Hour)
var allowed atomic.Int32
var wg sync.WaitGroup
for i := 0; i < 100; i++ {
wg.Add(1)
go func() {
defer wg.Done()
if ok, _ := l.Allow("k"); ok {
allowed.Add(1)
}
}()
}
wg.Wait()
if n := allowed.Load(); n != 10 {
t.Fatalf("allowed %d of a 100-request burst, want exactly 10", n)
}
c.t = c.t.Add(20 * time.Minute)
ok, wait := l.Allow("k")
if ok || wait != 40*time.Minute {
t.Fatalf("Allow past the limit = %v, wait %v; want refused with the 40m left", ok, wait)
}
c.t = c.t.Add(40 * time.Minute)
if ok, _ := l.Allow("k"); !ok {
t.Fatal("refused once the window had passed")
}
}