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
+57
View File
@@ -303,3 +303,60 @@ func TestSetupToken_MatchesOnlyItself(t *testing.T) {
t.Error("a nil token must fail closed")
}
}
// Sign-ups racing on one invite make exactly one account: the others are
// told the invite is used, and nothing of theirs is left behind.
func TestRegister_OneInviteMakesOneAccountUnderARace(t *testing.T) {
if os.Getenv("MINSTREL_TEST_DATABASE_URL") == "" {
t.Skip("MINSTREL_TEST_DATABASE_URL not set")
}
h, pool := testHandlers(t)
resetInvites(t, h)
admin := seedUser(t, pool, "raceadmin", "pw", true)
token := "test-invite-race"
if _, err := pool.Exec(context.Background(),
`INSERT INTO user_invites (token, invited_by, expires_at)
VALUES ($1, $2, now() + interval '24 hours')`,
token, admin.ID,
); err != nil {
t.Fatalf("seed invite: %v", err)
}
const N = 5
codes := make([]int, N)
var wg sync.WaitGroup
for i := 0; i < N; i++ {
wg.Add(1)
go func(idx int) {
defer wg.Done()
body := `{"username":"inviterace` + string(rune('A'+idx)) + `","password":"abcd1234","invite_token":"` + token + `"}`
req := httptest.NewRequest(http.MethodPost, "/api/auth/register", bytes.NewReader([]byte(body)))
rec := httptest.NewRecorder()
h.handleRegister(rec, req)
codes[idx] = rec.Code
}(i)
}
wg.Wait()
ok := 0
for _, c := range codes {
switch c {
case http.StatusOK:
ok++
case http.StatusBadRequest:
default:
t.Errorf("unexpected status %d", c)
}
}
if ok != 1 {
t.Fatalf("%d sign-ups succeeded on one invite, want exactly 1 (codes %v)", ok, codes)
}
var made int
if err := pool.QueryRow(context.Background(),
`SELECT count(*) FROM users WHERE username LIKE 'inviterace%'`).Scan(&made); err != nil {
t.Fatalf("count: %v", err)
}
if made != 1 {
t.Fatalf("%d accounts exist after the race, want 1", made)
}
}