Skip to content

Commit c6ad4a0

Browse files
committed
fix: correct comment and harden rate limiter in bootstrap
- Fix comment: minimum secret length is 8 runes, not 12 - Harden bootstrapAllowRate: store Add result before comparison to prevent race condition in high concurrency scenarios
1 parent 95ef15b commit c6ad4a0

1 file changed

Lines changed: 4 additions & 4 deletions

File tree

admin/bootstrap.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,7 @@ const (
3535
// bootstrapAllowRate 使用 CAS 实现固定窗口限频:
3636
// - 任意时刻只有一个 goroutine 能成功翻新窗口起点,其它失败者读到的就是
3737
// 翻新后的最新值,避免多个 goroutine 同时把 count 重置为 0。
38-
// - 在并发下,最坏情况只是有一个 reset 与若干 Add 交错,但所有"翻窗"
39-
// 操作都是原子的,不会出现窗口被重复清零导致超额放行的情况。
38+
// - 先递增计数再判断,确保高并发下不会超过限制。
4039
func bootstrapAllowRate() bool {
4140
now := time.Now().Unix()
4241
for {
@@ -51,7 +50,8 @@ func bootstrapAllowRate() bool {
5150
break
5251
}
5352
}
54-
return bootstrapState.count.Add(1) <= bootstrapMaxPerWin
53+
newCount := bootstrapState.count.Add(1)
54+
return newCount <= bootstrapMaxPerWin
5555
}
5656

5757
// GetBootstrapStatus 返回当前是否需要执行初始化(GET /api/admin/bootstrap-status)。
@@ -101,7 +101,7 @@ func (h *Handler) GetBootstrapStatus(c *gin.Context) {
101101
// 1. 仅在系统未配置 ADMIN_SECRET 时可用,否则一律 409;
102102
// 2. 通过互斥锁 + 双重检查避免并发写入;
103103
// 3. 简单全局限频,防止扫描器穷举;
104-
// 4. 校验最小长度(12 个 rune),避免过弱密钥;
104+
// 4. 校验最小长度(8 个 rune),避免过弱密钥;
105105
// 5. 全程审计日志。
106106
func (h *Handler) PostBootstrap(c *gin.Context) {
107107
if !bootstrapAllowRate() {

0 commit comments

Comments
 (0)