Skip to content

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

:lock: Key publish auth throttle by IP and bound throttle entries https://github.com/siyuan-note/siyuan/security/advisories/GHSA-2x7j-p79w-7744

Daniel16ae208693a0
diff --git a/kernel/server/proxy/publish.go b/kernel/server/proxy/publish.go
index 2cf1f0b99f..755f0d2660 100644
--- a/kernel/server/proxy/publish.go
+++ b/kernel/server/proxy/publish.go
@@ -218,12 +218,11 @@ func (PublishServiceTransport) RoundTrip(request *http.Request) (response *http.
return publishAuthRejectResponse(request, http.StatusUnauthorized, 0), nil
}
- // 按来源 IP 与账户名限流,防止暴力破解 https://github.com/siyuan-note/siyuan/security/advisories/GHSA-phg7-xcr4-q5wg
+ // 按来源 IP 限流,防止暴力破解与限流记录无限增长 https://github.com/siyuan-note/siyuan/security/advisories/GHSA-2x7j-p79w-7744
ip := util.GetRemoteAddr(request)
- throttleKey := ip + ":" + username
- if retryAfter := util.AuthThrottleCheck(throttleKey); 0 < retryAfter {
+ if retryAfter := util.AuthThrottleCheck(ip); 0 < retryAfter {
// 锁定期间持续记录失败,以延长锁定时间
- util.AuthThrottleFail(throttleKey)
+ util.AuthThrottleFail(ip)
logging.LogWarnf("publish service auth throttled [ip=%s, username=%s]", ip, username)
return publishAuthRejectResponse(request, http.StatusTooManyRequests, retryAfter), nil
}
@@ -232,10 +231,10 @@ func (PublishServiceTransport) RoundTrip(request *http.Request) (response *http.
if account == nil ||
"" == account.Username || // 匿名用户
!util.AuthCodeEquals(account.Password, password) { // 恒定时间比较,避免时序侧信道 https://github.com/siyuan-note/siyuan/security/advisories/GHSA-phg7-xcr4-q5wg
- util.AuthThrottleFail(throttleKey)
+ util.AuthThrottleFail(ip)
return publishAuthRejectResponse(request, http.StatusUnauthorized, 0), nil
}
- util.AuthThrottleReset(throttleKey)
+ util.AuthThrottleReset(ip)
// set session cookie
sessionID := model.GetNewSessionID()
diff --git a/kernel/util/session.go b/kernel/util/session.go
index 3b3dac2c55..49da2dc890 100644
--- a/kernel/util/session.go
+++ b/kernel/util/session.go
@@ -40,8 +40,9 @@ func AuthCodeEquals(a, b string) bool {
}
var (
- authThrottleLock = sync.Mutex{}
- authThrottles = map[string]*authThrottle{} // key: 来源 IP
+ authThrottleLock = sync.Mutex{}
+ authThrottles = map[string]*authThrottle{} // key: 来源 IP
+ authThrottleLastSweep = time.Time{}
)
// authThrottle 记录认证失败次数与锁定状态,用于防止无验证码的认证路径(如 Basic Auth)被暴力破解。
@@ -52,17 +53,38 @@ type authThrottle struct {
}
const (
- authThrottleMaxFail = 5 // 连续失败次数达到该值时开始锁定
- authThrottleLockBaseSec = 30 // 首次锁定秒数
- authThrottleLockMaxSec = 15 * 60 // 锁定秒数上限
- authThrottleWindowSec = 15 * 60 // 失败计数滑动窗口,窗口内失败才累计
+ authThrottleMaxFail = 5 // 连续失败次数达到该值时开始锁定
+ authThrottleLockBaseSec = 30 // 首次锁定秒数
+ authThrottleLockMaxSec = 15 * 60 // 锁定秒数上限
+ authThrottleWindowSec = 15 * 60 // 失败计数滑动窗口,窗口内失败才累计
+ authThrottleMaxEntries = 10000 // 限流记录条数上限,保证 map 内存有界
+ authThrottleSweepIntervalSec = 5 * 60 // 定期清扫过期记录的间隔秒数
)
+// authThrottleSweepLocked 按固定间隔清扫过期记录,调用方须持有 authThrottleLock。
+// 清除窗口期已过且未锁定的条目,避免仅当同一 key 再次被访问时才清理,防止 map 无限增长
+// https://github.com/siyuan-note/siyuan/security/advisories/GHSA-2x7j-p79w-7744
+func authThrottleSweepLocked(now time.Time) {
+ if now.Sub(authThrottleLastSweep) < authThrottleSweepIntervalSec*time.Second {
+ return
+ }
+ authThrottleLastSweep = now
+ for key, throttle := range authThrottles {
+ if now.Before(throttle.LockUntil) {
+ continue // 仍处于锁定中,保留
+ }
+ if authThrottleWindowSec*time.Second <= now.Sub(throttle.LastFail) {
+ delete(authThrottles, key)
+ }
+ }
+}
+
// AuthThrottleCheck 返回 key 剩余锁定秒数,0 表示未锁定。
func AuthThrottleCheck(key string) (retryAfter int) {
authThrottleLock.Lock()
defer authThrottleLock.Unlock()
+ authThrottleSweepLocked(time.Now())
throttle := authThrottles[key]
if nil == throttle {
return 0
@@ -88,8 +110,14 @@ func AuthThrottleFail(key string) {
defer authThrottleLock.Unlock()
now := time.Now()
+ authThrottleSweepLocked(now)
throttle := authThrottles[key]
if nil == throttle {
+ if authThrottleMaxEntries <= len(authThrottles) {
+ // 达到条目上限后不再跟踪新 key,保证内存有界。攻击者可通过伪造来源地址制造大量唯一 key,
+ // 跳过跟踪仅影响新增 key 的限流,不影响已有记录的锁定与清理。
+ return
+ }
throttle = &authThrottle{}
authThrottles[key] = throttle
} else if authThrottleWindowSec*time.Second <= now.Sub(throttle.LastFail) {
diff --git a/kernel/util/session_test.go b/kernel/util/session_test.go
index a07801ff2f..8816b6e8b7 100644
--- a/kernel/util/session_test.go
+++ b/kernel/util/session_test.go
@@ -17,6 +17,7 @@
package util
import (
+ "fmt"
"testing"
"time"
)
@@ -130,6 +131,81 @@ func TestAuthThrottleExpiry(t *testing.T) {
}
}
+// TestAuthThrottleSweep 验证定期清扫会删除窗口期已过的失败记录,即使 key 不再被访问。
+func TestAuthThrottleSweep(t *testing.T) {
+ staleKeys := make([]string, 0, 64)
+ for i := 0; i < 64; i++ {
+ key := fmt.Sprintf("TestAuthThrottleSweep-stale-%d", i)
+ AuthThrottleFail(key)
+ defer resetAuthThrottleForTest(key)
+ staleKeys = append(staleKeys, key)
+ }
+ authThrottleLock.Lock()
+ for _, key := range staleKeys {
+ authThrottles[key].LastFail = time.Now().Add(-authThrottleWindowSec * time.Second)
+ }
+ authThrottleLastSweep = time.Time{}
+ authThrottleLock.Unlock()
+
+ triggerKey := "TestAuthThrottleSweep-trigger"
+ defer resetAuthThrottleForTest(triggerKey)
+ AuthThrottleFail(triggerKey)
+
+ authThrottleLock.Lock()
+ defer authThrottleLock.Unlock()
+ for _, key := range staleKeys {
+ if _, exists := authThrottles[key]; exists {
+ t.Fatalf("expected stale throttle entry to be swept: %s", key)
+ }
+ }
+}
+
+// TestAuthThrottleSweepKeepsLocked 验证清扫不会删除仍在锁定中的条目。
+func TestAuthThrottleSweepKeepsLocked(t *testing.T) {
+ key := "TestAuthThrottleSweepKeepsLocked"
+ resetAuthThrottleForTest(key)
+ defer resetAuthThrottleForTest(key)
+ for i := 0; i < authThrottleMaxFail+1; i++ {
+ AuthThrottleFail(key)
+ }
+ authThrottleLock.Lock()
+ authThrottleLastSweep = time.Time{}
+ authThrottleLock.Unlock()
+
+ triggerKey := "TestAuthThrottleSweepKeepsLocked-trigger"
+ defer resetAuthThrottleForTest(triggerKey)
+ AuthThrottleFail(triggerKey)
+
+ authThrottleLock.Lock()
+ defer authThrottleLock.Unlock()
+ if _, exists := authThrottles[key]; !exists {
+ t.Fatal("expected locked throttle entry to survive sweep")
+ }
+}
+
+// TestAuthThrottleMaxEntries 验证限流记录条数不超过上限,防止唯一 key 无限增长内存。
+func TestAuthThrottleMaxEntries(t *testing.T) {
+ authThrottleLock.Lock()
+ authThrottleLastSweep = time.Now()
+ for key := range authThrottles {
+ delete(authThrottles, key)
+ }
+ authThrottleLock.Unlock()
+
+ for i := 0; i < authThrottleMaxEntries+50; i++ {
+ AuthThrottleFail(fmt.Sprintf("TestAuthThrottleMaxEntries-%d", i))
+ }
+ authThrottleLock.Lock()
+ count := len(authThrottles)
+ for key := range authThrottles {
+ delete(authThrottles, key)
+ }
+ authThrottleLock.Unlock()
+ if authThrottleMaxEntries < count {
+ t.Fatalf("throttle entries exceeded the cap: %d", count)
+ }
+}
+
func resetAuthThrottleForTest(key string) {
authThrottleLock.Lock()
delete(authThrottles, key)