Repository navigation
Conversation
对 /api/auth/login 与 /api/auth/register 增加「来源 IP + 用户名」双维防护: - IP 维度:同一来源 IP 每分钟 N 次(golang.org/x/time/rate 令牌桶,默认 10, env AUTH_RATE_LIMIT_PER_MINUTE 可覆盖;非法值回退默认并打 warning)。 - 用户名维度:同一用户名连续失败 5 次后锁定 300 秒(内置,不可配)。 - 失败/锁定事件用 slog 结构化留痕(method/path/client_ip/username/reason),不落新表。 - 对外语义:限流命中返回业务码 20201(CodeRateLimited)+ Retry-After 响应头, 前端给出「尝试过于频繁,请稍后再试」的可区分文案。 - 实现为单进程内存态(无 Redis,守住单二进制轻部署);多副本时各副本独立计数。 验证: - go test -race ./...(全仓 29 包通过);前端 pnpm build:check(73 文件 / 1019 测试通过)。 - 真实浏览器 E2E:错误密码连续触发锁定 → 20201 + Retry-After + 前端区分文案; 用户名维度与 IP 维度互不干扰(不同用户名不受锁定影响);register 亦挂 IP 限流; 主路径(仪表盘列表)未受影响。 另修复 rebrand 遗留:capabilities_test.go 的 data-insights/ 导入路径(module 已更名 tablewave),使 go test ./... 得以全绿。
Contributor
Author
评审轮 E2E 证据(PR #255 / issue #203 R-87 认证端点限流与失败锁定)门禁(全绿)
真实浏览器 + HTTP E2E(ego-browser + curl;临时 PG 全新库 + 单进程 app,默认 RATE=10/min)
结论:门禁 + E2E 全绿,合并入 master。 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
背景
#203(R-87,#75 收窄切片):
#183落地上口令后新增了攻击面——POST /api/auth/login可被无限次爆破、POST /api/auth/register在管理员建立前可抢注。本版只补口令这条最小必要防线,不做 #75 的全局限流 / 查询复杂度 / CAPTCHA(Phase C 盘子)。做了什么
对
/api/auth/login与/api/auth/register增加**「来源 IP + 用户名」双维防护**(backend/internal/service/auth/guard.go,单进程内存态,无 Redis / 外部依赖):golang.org/x/time/rate令牌桶;默认 10,envAUTH_RATE_LIMIT_PER_MINUTE可覆盖,非法值回退默认并打 warning)。slog结构化日志(method/path/client_ip/username/reason),不落新表(审计日志属 [PM] 安全增强 - 密码加密/审计日志/LDAP集成 #63)。20201(CodeRateLimited,新增)+Retry-After响应头;前端给出「尝试过于频繁,请稍后再试」的可区分文案(account.rateLimitedi18n +Account.tsx按 20201 分支)。AUTH_RATE_LIMIT_PER_MINUTE接入config.AuthConfig(main → routes → auth.NewService);文档补docs/user-guide/configuration.md变量表 +docs/deployment/production.md反代 IP 信任说明。怎么做
guard.go:Guard 结构(sync.Mutex保护用户名状态表;rate.Limiter自身线程安全),时间全走注入的now func() time.Time(生产time.Now,测试假时钟)。auth.go:Login/Register前置守卫 + 失败/成功记账 +ErrRateLimited{RetryAfter};handler/auth.go的mapAuthError把ErrRateLimited写成Retry-After头 +CodeRateLimited业务码。api/openapi.yamlResponseCode增20201(CodeRateLimited);frontend/src/idls/gen_types.ts同步(make api-gen 前端半步因 TS7 损坏,按既有约定手工同步,见 AGENTS.md)。测试证据
go test -race ./...全仓 29 包通过。新增表驱动单测(假时钟、无 sleep):guard_test.go(第 N+1 次被拒 / 窗口滑过恢复 / 锁定到期自动解锁 / 两维互不干扰 / 并发 race)、auth_test.go(Login 返回 ErrRateLimited)、handler/auth_test.go(Retry-After=42 头 + 20201 码 + 区分文案;不足 1 秒 ceil 到 1)。pnpm build:check通过(73 文件 / 1019 测试)。make serve单进程 23352):错误密码连续触发锁定 → 接口返回20201+Retry-After: 274;前端表单提交路径弹出「尝试过于频繁,请稍后再试」;用户名维度与 IP 维度互不干扰(someone_else同 IP 仍返回 20200,不受r95admin锁定影响);register亦挂 IP 限流;仪表盘主路径未受影响。附带修复
capabilities_test.go的data-insights/导入路径 →tablewave/(rebrand 遗留,使go test ./...全绿)。此为最小修复,未做无关重构。边界(Non-goals)
不做全局限流 / 查询复杂度评分 / CAPTCHA(#75 Phase C);不做分布式限流(多副本各副本独立计数,文档已注明);不做审计日志落库(#63)。