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
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:
@@ -50,7 +50,9 @@ func NewAttemptLimiter(max int, window time.Duration) *AttemptLimiter {
|
||||
|
||||
// Blocked reports whether key has used its attempts for the current window,
|
||||
// and if so how long until the window resets. It records nothing, so a check
|
||||
// can run before the expensive work and the outcome be recorded after.
|
||||
// can run before the expensive work and a failure be recorded after, which is
|
||||
// the shape for a limit that counts only failures. Where every attempt
|
||||
// counts, Allow checks and counts in one step.
|
||||
func (l *AttemptLimiter) Blocked(key string) (bool, time.Duration) {
|
||||
if l == nil || key == "" {
|
||||
return false, 0
|
||||
@@ -72,6 +74,31 @@ func (l *AttemptLimiter) Blocked(key string) (bool, time.Duration) {
|
||||
return true, b.start.Add(l.window).Sub(now)
|
||||
}
|
||||
|
||||
// Allow counts one attempt against key and reports whether it was within the
|
||||
// limit, and if not how long until the window resets. The check and the count
|
||||
// happen together under the lock, so a burst of parallel requests cannot all
|
||||
// read the count before any of them adds to it. A refused attempt is not
|
||||
// counted.
|
||||
func (l *AttemptLimiter) Allow(key string) (bool, time.Duration) {
|
||||
if l == nil || key == "" {
|
||||
return true, 0
|
||||
}
|
||||
l.mu.Lock()
|
||||
defer l.mu.Unlock()
|
||||
now := l.now()
|
||||
b, ok := l.buckets[key]
|
||||
if !ok || now.Sub(b.start) >= l.window {
|
||||
l.buckets[key] = &attemptBucket{count: 1, start: now}
|
||||
l.maybeSweep(now)
|
||||
return true, 0
|
||||
}
|
||||
if b.count >= l.max {
|
||||
return false, b.start.Add(l.window).Sub(now)
|
||||
}
|
||||
b.count++
|
||||
return true, 0
|
||||
}
|
||||
|
||||
// Record counts one attempt against key.
|
||||
func (l *AttemptLimiter) Record(key string) {
|
||||
if l == nil || key == "" {
|
||||
|
||||
Reference in New Issue
Block a user