Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

:bug: Saturate authentication throttle backoff https://github.com/siyuan-note/siyuan/issues/20194

Daniela739290caa95
diff --git a/kernel/util/session.go b/kernel/util/session.go
index 49da2dc890..00e31bd6c5 100644
--- a/kernel/util/session.go
+++ b/kernel/util/session.go
@@ -130,9 +130,15 @@ func AuthThrottleFail(key string) {
return
}
- lockSec := authThrottleLockBaseSec << (throttle.FailCount - authThrottleMaxFail)
- if authThrottleLockMaxSec < lockSec {
- lockSec = authThrottleLockMaxSec
+ lockSec := authThrottleLockBaseSec
+ for i := authThrottleMaxFail; i < throttle.FailCount; i++ {
+ if lockSec >= (authThrottleLockMaxSec+1)/2 {
+ // 达到锁定上限后固定失败计数,持续失败只延长锁定截止时间。
+ lockSec = authThrottleLockMaxSec
+ throttle.FailCount = i + 1
+ break
+ }
+ lockSec *= 2
}
throttle.LockUntil = now.Add(time.Duration(lockSec) * time.Second)
}
diff --git a/kernel/util/session_test.go b/kernel/util/session_test.go
index 8816b6e8b7..1773fc9497 100644
--- a/kernel/util/session_test.go
+++ b/kernel/util/session_test.go
@@ -77,6 +77,27 @@ func TestAuthThrottleBackoff(t *testing.T) {
}
}
+func TestAuthThrottleSaturation(t *testing.T) {
+ key := "TestAuthThrottleSaturation"
+ resetAuthThrottleForTest(key)
+ t.Cleanup(func() { resetAuthThrottleForTest(key) })
+ for i := 1; i <= 1000; i++ {
+ AuthThrottleFail(key)
+ if i < 10 {
+ continue
+ }
+ if retryAfter := AuthThrottleCheck(key); retryAfter < authThrottleLockMaxSec || retryAfter > authThrottleLockMaxSec+1 {
+ t.Fatalf("lock lost its cap after %d failures: %d", i, retryAfter)
+ }
+ }
+ authThrottleLock.Lock()
+ count := authThrottles[key].FailCount
+ authThrottleLock.Unlock()
+ if count > 10 {
+ t.Fatalf("failure count was not saturated: %d", count)
+ }
+}
+
// TestAuthThrottleWindowReset 验证窗口期外的陈旧失败计数被清零,避免误锁。
func TestAuthThrottleWindowReset(t *testing.T) {
key := "TestAuthThrottleWindowReset"