High-performance browser automation bridge and multi-instance orchestrator with advanced stealth injection and real-time dashboard.
fix: sweep expired keys from the login attempt limiter instead of retaining them forever
Luigi Agostie73dc42e99d6
diff --git a/internal/authn/rate_limit.go b/internal/authn/rate_limit.goindex 044bd510..6f461a57 100644--- a/internal/authn/rate_limit.go+++ b/internal/authn/rate_limit.go@@ -22,6 +22,7 @@ type AttemptLimiter struct {maxAttempts intattempts map[string][]time.Timenow func() time.Time+ lastSweep time.Time}func NewAttemptLimiter(cfg AttemptLimiterConfig) *AttemptLimiter {@@ -81,10 +82,35 @@ func (l *AttemptLimiter) RecordFailure(key string) {l.mu.Lock()defer l.mu.Unlock()+ l.sweepLocked(now)filtered := l.pruneLocked(key, now)l.attempts[key] = append(filtered, now)}+// sweepLocked drops every key whose attempts have all aged out. pruneLocked+// only ever touches the key being looked at, so without this a failed login+// from each of many distinct peers leaves an entry alive indefinitely — the+// key is never revisited, so it is never pruned. Rate-limited by window so a+// burst of failures cannot turn each one into a full map scan.+func (l *AttemptLimiter) sweepLocked(now time.Time) {+ if now.Sub(l.lastSweep) < l.window {+ return+ }+ l.lastSweep = now+ for key, hits := range l.attempts {+ live := false+ for _, ts := range hits {+ if now.Sub(ts) < l.window {+ live = true+ break+ }+ }+ if !live {+ delete(l.attempts, key)+ }+ }+}+func (l *AttemptLimiter) Reset(key string) {if l == nil {returndiff --git a/internal/authn/rate_limit_retention_test.go b/internal/authn/rate_limit_retention_test.gonew file mode 100644index 00000000..9ea9f6e3--- /dev/null+++ b/internal/authn/rate_limit_retention_test.go@@ -0,0 +1,54 @@+package authn++import (+ "fmt"+ "testing"+ "time"+)++// Attempts are pruned only when the same key is seen again, so a burst of+// failed logins from many distinct peers would otherwise leave one map entry+// per peer alive forever in a long-running daemon.+func TestAttemptLimiterDoesNotRetainExpiredKeys(t *testing.T) {+ now := time.Unix(1_700_000_000, 0)+ l := NewAttemptLimiter(AttemptLimiterConfig{Window: time.Minute, MaxAttempts: 3})+ l.now = func() time.Time { return now }++ for i := 0; i < 1000; i++ {+ l.RecordFailure(fmt.Sprintf("10.0.%d.%d", i/256, i%256))+ }++ now = now.Add(time.Hour)+ l.RecordFailure("192.0.2.1")++ l.mu.Lock()+ retained := len(l.attempts)+ l.mu.Unlock()++ if retained > 1 {+ t.Errorf("limiter retains %d keys after all but one expired, want 1", retained)+ }+}++// Sweeping must not drop keys that are still inside the window, which would+// hand an attacker a free reset of their attempt count.+func TestAttemptLimiterKeepsLiveKeysWhileSweeping(t *testing.T) {+ now := time.Unix(1_700_000_000, 0)+ l := NewAttemptLimiter(AttemptLimiterConfig{Window: time.Minute, MaxAttempts: 3})+ l.now = func() time.Time { return now }++ for i := 0; i < 3; i++ {+ l.RecordFailure("10.0.0.1")+ }+ if allowed, _ := l.Allow("10.0.0.1"); allowed {+ t.Fatal("key should be blocked after hitting maxAttempts")+ }++ // Enough later to trigger a sweep, but still inside the window for this key.+ now = now.Add(50 * time.Second)+ l.RecordFailure("192.0.2.1")++ if allowed, _ := l.Allow("10.0.0.1"); allowed {+ t.Error("a sweep dropped a key that is still within its window")+ }+}