diff --git a/docs/architecture/refactoring-master-plan.md b/docs/architecture/refactoring-master-plan.md index 56b3fcb..c9c0d23 100644 --- a/docs/architecture/refactoring-master-plan.md +++ b/docs/architecture/refactoring-master-plan.md @@ -118,7 +118,8 @@ app shell / routes - 已完成阶段 2 后续切片:`internal/engine/account.go` 的 `tryLoginStatusCheck` 已改用 `GetCookieRuntimeData`,只读取 Cookie 与 metadata,保持重试、Cookie Jar 和锁语义; - 已完成阶段 2 后续切片:`internal/engine/account.go` 的 `tryAPIRenewUsing` 已改用 `GetCookieRuntimeData`,只读取接口续期所需的 Cookie 与 metadata,保持续期、快照持久化、锁和 token 清理语义,并补充损坏登录密码回归测试; - 已完成阶段 2 后续切片:新增 `GetCookieMetadata` 单字段接口,并将 `internal/engine/account.go` 的 `persistRenewFlatCookie` 迁移到该接口,只读取 metadata,保持扁平 Cookie 更新和 metadata 快照清理行为; -- 下一最小工作项:将 `internal/engine/account.go` 的 `handleMaxFailures` 迁移到 `GetValue`,继续只读取恢复回调所需的 Cookie 明文,不改变失败计数和重连行为; +- 已完成阶段 2 后续切片:`internal/engine/account.go` 的 `handleMaxFailures` 已改用 `GetValue`,只读取恢复回调所需的 Cookie 明文,保持失败计数和重连行为,并补充损坏登录密码回归测试; +- 下一最小工作项:将 `internal/engine/account.go` 的 `persistPendingRenewCookies` 迁移到 `GetCookieRuntimeData`,继续只读取异步续期所需的 Cookie 与 metadata,不改变迟到 Cookie 合并行为; - 随后工作项:再处理 Engine、Automation 等明确需要平台凭证的流程,统一使用按账号 ID 过滤的单值凭证接口; - 禁止跳过当前入口直接开始 Engine、Automation 或 DB 的大规模拆分。 @@ -488,3 +489,4 @@ npm --prefix frontend run build | 2026-08-14 | 新增 `CookieSummary`、`ListOwnedIDs`、`ExistsOwned` 及跨用户/无效 user ID 回归测试 | 阶段 2 第一个数据边界切片完成;故意无效密文摘要查询通过 | 迁移 server ownership helper,移除 `AllForUser` 所有权读取 | | 2026-08-14 | Engine 登录态检查与接口续期改用 `GetCookieRuntimeData` | 只解密 Cookie 与 metadata;接口续期窄查询回归测试通过,未改变锁、token 清理和快照持久化语义 | 迁移 `persistRenewFlatCookie` 的 metadata 窄查询 | | 2026-08-14 | 新增 `GetCookieMetadata` 并收窄 `persistRenewFlatCookie` | 扁平 Cookie 写回不再读取旧 Cookie、用户名或登录密码;损坏旧凭证回归测试通过 | 迁移 `handleMaxFailures` 的单值 Cookie 读取 | +| 2026-08-14 | `handleMaxFailures` 改用 `GetValue` | 恢复回调只读取 Cookie 明文;损坏登录密码回归测试通过,失败计数和重连行为保持不变 | 迁移 `persistPendingRenewCookies` 的异步续期读取 | diff --git a/internal/engine/account.go b/internal/engine/account.go index b642871..b28322b 100644 --- a/internal/engine/account.go +++ b/internal/engine/account.go @@ -815,8 +815,8 @@ func (a *Account) handleMaxFailures(ctx context.Context) error { a.logger.Warn("连续失败达上限,触发 Go 协议续期", "failures", MaxConnectionFailures) a.notifyRecoveringOffline(ctx, fmt.Sprintf("消息服务连续认证/连接失败 %d 次,开始自动恢复", MaxConnectionFailures)) if a.handler != nil && a.handler.OnPasswordLoginRefresh(ctx, a.CookieID) { - if d, err := a.store.Cookies.GetDetails(ctx, a.CookieID); err == nil && d != nil && d.Value != "" { - a.replaceCookieStr(d.Value) + if cookieValue, err := a.store.Cookies.GetValue(ctx, a.CookieID); err == nil && cookieValue != "" { // cookieValue 是恢复回调刚写入的 Cookie 明文。 + a.replaceCookieStr(cookieValue) a.clearCurrentToken() a.clearTokenCache(ctx) } diff --git a/internal/engine/account_extra_test.go b/internal/engine/account_extra_test.go index ad72e83..f4a8433 100644 --- a/internal/engine/account_extra_test.go +++ b/internal/engine/account_extra_test.go @@ -1141,3 +1141,35 @@ func TestPersistRenewFlatCookieExcludesLoginSecrets(t *testing.T) { t.Fatalf("扁平 Cookie 写回应保留其他 metadata: %s", runtimeData.MetadataJSON) } } + +// TestHandleMaxFailuresUsesValueWithoutLoginSecrets 验证恢复回调只读取 Cookie 明文,不解密登录密码。 +func TestHandleMaxFailuresUsesValueWithoutLoginSecrets(t *testing.T) { + t.Setenv("XIANYU_DATA_KEY", "engine-max-failures-query-key") + // acc、handler 和 store 是本测试的账号、恢复回调记录器及数据库。 + acc, handler, store, cleanup := newAccountForTest(t) + defer cleanup() + // ctx 是测试数据库和恢复流程共用的上下文。 + ctx := context.Background() + // corruptErr 表示写入故意损坏的登录密码密文失败的原因。 + if _, corruptErr := store.DB.ExecContext(ctx, + `UPDATE cookies SET username=?,password=? WHERE id=?`, + "engine-max-failures-user", "not-a-password-ciphertext", "cid"); corruptErr != nil { + t.Fatalf("corrupt password: %v", corruptErr) + } + // 将账号置于最大连接失败状态,确保进入密码登录成功后的 Cookie 写回分支。 + acc.mu.Lock() + acc.lastMsgReceived = time.Time{} + acc.connFailures = MaxConnectionFailures + acc.mu.Unlock() + // cctx 让成功恢复后的固定等待快速结束,避免测试实际等待两秒。 + cctx, cancel := context.WithTimeout(ctx, 50*time.Millisecond) + defer cancel() + // handleErr 是最大失败恢复流程的结果。 + handleErr := acc.handleMaxFailures(cctx) + if handleErr != cctx.Err() { + t.Fatalf("恢复成功后的等待应返回 ctx.Err(): got %v want %v", handleErr, cctx.Err()) + } + if handler.refresh != 1 || acc.currentCookieStr() != "unb=123; _m_h5_tk=tk_1;" { + t.Fatalf("恢复回调或 Cookie 异常: refresh=%d cookie=%q", handler.refresh, acc.currentCookieStr()) + } +}