Repository navigation
fix(auth): 修复超管抢注接管、限流器满表锁死、扩展路由缺 RBAC/CSRF - #3
Merged
Merged
Conversation
added 2 commits
October 8, 2026 07:13
- bootstrap: 提权既有普通账号为超管时原子作废其凭据(清空密码、自增 token_version、 删除会话),强制走 setup token 首登;不再复活 banned 账号;并发注册分支同样处理 - ratelimit: 桶表满时淘汰最久未活动条目而非拒绝所有新来源;IPv6 按 /64 归并计数 - 扩展路由: WithProtectedRoutes 挂到 authorize() 组下并追加 CSRF 中间件, 按实际注册的 (方法, 路径) 登记租户策略,/api/admin 下及首段通配的扩展仅超管可用; 删除 authorize 中从未生效的 is_protected_extension 放行分支,修正文档注释
1. 跳转缓存语义
- 规则改写、多目标轮询、落地页与其余 302 一律 302 + Cache-Control: no-store
- 单目标 301 带有界缓存 private, max-age=JANUS_REDIRECT_301_MAX_AGE(默认 5m,0 = no-store);
开启规则的短链 301 改为 no-store(仍保留 301 状态码)
- 注册 HEAD /{code},与 GET 同响应;HEAD 不记明细、不推进轮询指针
2. 访问明细移出请求路径
- 新增 httpapi.VisitQueue:有界队列 + worker 池 + 多行批量 INSERT,
落库 ctx 与请求解耦(WithoutCancel + 超时),队列满丢弃并计数,批量失败逐行重试
- 后置访问钩子改为落库之后由队列触发;被丢弃的明细不触发钩子
- 未配置队列(黑盒测试)或队列已关闭时退回同步写入,同样不再随客户端断开取消
- cmd/janus 与 pkg/janus 在 http.Server.Shutdown 之后排空队列
- 新增 store.PickTargetFrom / InsertVisits,跳转不再二次读取 link_targets
- 连接池可配置 JANUS_DB_MAX_OPEN_CONNS(默认 25,空闲上限同值)
Owner
Author
|
已 rebase 到 无文本冲突。确认语义兼容:#6 的 initHandler 只在 New 中执行一次、业务路由经 NoRoute 委托给原子替换的 fallback;本 PR 的 合并顺序:#1 → #6 → #3 → #2 → #4 → #5(请按此顺序依次合并;每个 PR 合并后,下一个 PR 的 base 会由 GitHub 自动改指向其上一级的目标分支)。 验证:每一步 rebase 后均 |
This was referenced Oct 8, 2026
fix(redirect): 301 有界缓存 + HEAD 支持 + 访问明细异步批量落库
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.
基于
fix/critical-review,修复代码审查中的三个 High 级认证问题。改动
1. 超管邮箱抢注接管(
internal/bootstrap/bootstrap.go、internal/store/store.go)JANUS_SUPERADMIN_EMAIL对应的普通账号已存在,会被直接提权并保留原密码。注册不需要能收信,任何人抢先用该邮箱注册即可带着自己的密码成为超管。Store.PromoteToSuperAdminResetCredentials,在同一事务内置is_super_admin、清空password_hash、自增token_version、删除全部会话 —— 提权后必须通过发往该邮箱的一次性 setup token 首登。原子化是为了避免"已提权未清密码"时崩溃,重启后因IsSuperAdmin=true跳过清理。banned状态(只把 pending 收敛为 active),并打印警告日志;提权时同样打印警告。tenantStore,公开签名Superadmin(ctx, *store.Store, email)不变。2. 限流器桶表满时拒绝所有新来源(
internal/httpapi/ratelimit.go)lastSeen淘汰最旧的 1/16(摊销排序成本),新来源永远能拿到桶;持续活跃的 key 不会被冲掉。AllowIP/rateLimitKeyForIP:IPv4(含 IPv4-mapped)按单地址,IPv6 按 /64 前缀归并。认证限流、访客跳转、落地页资源、Caddy 授权端点的 IP 限流均改用AllowIP;按邮箱分桶的限流不变。3. 扩展路由缺 RBAC 与 CSRF(
internal/httpapi/server.go、session.go、internal/rbac/rbac.go、pkg/janus/janus.go)WithProtectedRoutes注入的路由只挂在authenticate()上,没有 Casbinauthorize(),也没有 CSRF 校验;authorize()里的is_protected_extension放行分支没有任何代码设置该标记,是死代码。prot(已含authorize())下的子组,并追加新中间件csrfGuard()(复用requireCSRF):cookie 认证的非安全方法必须带正确的X-CSRF-Token;Bearer 免检;拿不到认证方式/会话时 fail-closed 403。rbac.GrantTenantExtension登记为 tenant 策略(不开前缀通配);superadmin 已由/api/*覆盖。/api/admin/下,或首段为参数/通配(如/api/:x、/api/*all,可能命中/api/admin/...)的扩展路由不对租户放行,即超管专属并打印日志。扩展要声明「仅超管」,挂到/api/admin/下即可。ginPathToCasbinPath(原未被使用)移至rbac.GinPathToPolicyPath;修正WithProtectedRoutes文档注释。pkg/janus/protected_routes_test.go的用法(租户 JWT GET 扩展路由)不受影响。验证
gofmt -l:本次改动的文件无输出(pkg/plugin/plugin_test.go是既有问题,未改动)go build ./...、go vet ./...:通过internal/bootstrap:提权走凭据作废、封禁不复活、已是超管不重置、新建路径internal/rbac:TestGrantTenantExtension、TestGinPathToPolicyPathinternal/httpapi:TestRateLimiterFullDoesNotLockOutNewClients、TestRateLimiterFullKeepsActiveBucketCount、TestRateLimiterExpiredPurgedFirst、TestRateLimitKeyForIP、TestRateLimiterIPv6SameSlash64SharesBucket、TestProtectedExtensionRoutesAuthorizeAndCSRF(13 个子用例)auth_test.go,本地无 DB 已 skip,未实际跑过):TestSuperadminBootstrapPromotionResetsCredentials、TestSuperadminBootstrapKeepsBannedTenantBanned。请在有 DB 的环境跑JANUS_TEST_DB_REQUIRED=1 go test ./...。已知取舍