From 04e1aab2e8a26eeff9edf9f119c2c995a2d205c7 Mon Sep 17 00:00:00 2001 From: Christ Date: Fri, 14 Aug 2026 12:18:43 +0800 Subject: [PATCH] =?UTF-8?q?=E8=87=AA=E5=8A=A8=E5=8C=96=EF=BC=9A=E6=94=B6?= =?UTF-8?q?=E7=AA=84=E7=A1=AE=E8=AE=A4=E5=8F=91=E8=B4=A7=E5=87=AD=E8=AF=81?= =?UTF-8?q?=E8=AF=BB=E5=8F=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/architecture/refactoring-master-plan.md | 5 ++-- internal/automation/account_tasks.go | 2 +- internal/automation/center.go | 22 +++++++++--------- internal/db/cookie_scope.go | 24 ++++++++++---------- internal/db/cookie_scope_test.go | 10 ++++---- 5 files changed, 32 insertions(+), 31 deletions(-) diff --git a/docs/architecture/refactoring-master-plan.md b/docs/architecture/refactoring-master-plan.md index de0435f..0d33c51 100644 --- a/docs/architecture/refactoring-master-plan.md +++ b/docs/architecture/refactoring-master-plan.md @@ -112,8 +112,9 @@ app shell / routes - 已完成阶段 2 后续切片:`internal/chat/service.go` 的订阅账号集合已迁移到 `ListOwnedIDs`,聊天订阅不再批量解密账号 Cookie; - 已完成阶段 2 后续切片:`internal/account/manager.go` 已改用受控的 `ListEnabledRuntimeCredentials` 启动账号,只解密启用账号 Cookie,不再使用 `AllForUser(ctx, 0)` 加逐账号状态查询; - 已完成阶段 2 后续切片:`internal/renewal/scheduler.go` 已改用 `RenewalRuntimeAccount` 窄模型及按账号重读接口,只解密 Cookie、续期 metadata 和启用状态,不再把登录密码/用户名带入续期调度器; -- 已完成阶段 2 后续切片:`internal/automation/account_tasks.go` 的 Session 阻断指纹已改用 `GetCookieFingerprintData`,只读取 Cookie 与 metadata,不再解密完整账号详情; -- 下一最小工作项:为 `internal/automation/center.go` 的确认发货流程建立按用途拆分的 Cookie/metadata 窄查询,保持凭证锁、Cookie Jar 和重试行为不变; +- 已完成阶段 2 后续切片:`internal/automation/account_tasks.go` 的 Session 阻断指纹已改用 `GetCookieRuntimeData`,只读取 Cookie 与 metadata,不再解密完整账号详情; +- 已完成阶段 2 后续切片:`internal/automation/center.go` 的确认发货流程已改用 `GetCookieRuntimeData`,只读取 Cookie 与 metadata,保留凭证锁、Cookie Jar 和重试行为; +- 下一最小工作项:将 `internal/automation/center.go` 的通用 `cookieValue` 回退读取迁移到已有的单值 `GetValue` 接口,继续清除完整详情读取; - 随后工作项:再处理 Engine、Automation 等明确需要平台凭证的流程,统一使用按账号 ID 过滤的单值凭证接口; - 禁止跳过当前入口直接开始 Engine、Automation 或 DB 的大规模拆分。 diff --git a/internal/automation/account_tasks.go b/internal/automation/account_tasks.go index ce8e35a..4464479 100644 --- a/internal/automation/account_tasks.go +++ b/internal/automation/account_tasks.go @@ -143,7 +143,7 @@ func (c *Center) accountTaskSessionBlocked(ctx context.Context, accountID string func (c *Center) accountCredentialFingerprint(ctx context.Context, accountID string) (string, error) { // data 是生成自动化 Session 阻断指纹所需的最小 Cookie 与 metadata 输入。 - data, err := c.store.Cookies.GetCookieFingerprintData(ctx, accountID) + data, err := c.store.Cookies.GetCookieRuntimeData(ctx, accountID) if err != nil { return "", err } diff --git a/internal/automation/center.go b/internal/automation/center.go index c44bee0..02b8a55 100644 --- a/internal/automation/center.go +++ b/internal/automation/center.go @@ -834,21 +834,21 @@ func (c *Center) confirmShipmentAttempt(ctx context.Context, task Task, allowCre credentialUnlock() } }() - detail, err := c.store.Cookies.GetDetails(ctx, task.AccountID) + // runtimeData 是确认发货所需的最小 Cookie 与 metadata 运行视图,不包含登录密码和账号资料。 + runtimeData, err := c.store.Cookies.GetCookieRuntimeData(ctx, task.AccountID) if err != nil { return err } - if detail == nil { - return db.ErrNotFound - } - _, completeSnapshot := cookierefresh.SnapshotFromMetadataOK(detail.MetadataJSON) - if strings.TrimSpace(detail.Value) == "" && !completeSnapshot { + // runtimeData 已在凭证锁内读取;这里只根据完整快照判断空 Cookie 的合法性。 + // completeSnapshot 表示 metadata 中是否包含可恢复的完整 Cookie Jar。 + _, completeSnapshot := cookierefresh.SnapshotFromMetadataOK(runtimeData.MetadataJSON) + if strings.TrimSpace(runtimeData.Value) == "" && !completeSnapshot { return fmt.Errorf("账号 %s Cookie 为空", task.AccountID) } - cookieStr := detail.Value + cookieStr := runtimeData.Value var mtopCtx context.Context var cookieSession *mtop.CookieSession - if snapshot, ok := cookierefresh.SnapshotFromMetadataOK(detail.MetadataJSON); ok { + if snapshot, ok := cookierefresh.SnapshotFromMetadataOK(runtimeData.MetadataJSON); ok { mtopCtx, cookieSession = mtop.WithCookieSnapshot(ctx, snapshot) } else { mtopCtx, cookieSession = mtop.WithFlatCookieSession(ctx, cookieStr) @@ -862,9 +862,9 @@ func (c *Center) confirmShipmentAttempt(ctx context.Context, task Task, allowCre // 格式差异回退覆盖并清除 Jar。 sessionHandled := snapshot != nil if changed { - metadata := cookierefresh.MetadataWithoutSnapshot(detail.MetadataJSON) + metadata := cookierefresh.MetadataWithoutSnapshot(runtimeData.MetadataJSON) if snapshot != nil { - metadata = cookierefresh.MetadataWithSnapshot(detail.MetadataJSON, snapshot) + metadata = cookierefresh.MetadataWithSnapshot(runtimeData.MetadataJSON, snapshot) } if saveErr := c.store.Cookies.UpdateRenewalCookie(ctx, task.AccountID, value, metadata, time.Now().Unix()); saveErr != nil { persistenceErrs = append(persistenceErrs, fmt.Errorf("保存确认发货响应 Cookie Jar: %w", saveErr)) @@ -877,7 +877,7 @@ func (c *Center) confirmShipmentAttempt(ctx context.Context, task Task, allowCre // 注入 mock 或没有权威快照的历史账号保留扁平 // Cookie 兼容路径;扁平结果无法维护旧 Jar 的作用域, // 因此不得继续保留可能已过期的 snapshot。 - metadata := cookierefresh.MetadataWithoutSnapshot(detail.MetadataJSON) + metadata := cookierefresh.MetadataWithoutSnapshot(runtimeData.MetadataJSON) if saveErr := c.store.Cookies.UpdateRenewalCookie(ctx, task.AccountID, updated, metadata, time.Now().Unix()); saveErr != nil { persistenceErrs = append(persistenceErrs, fmt.Errorf("保存刷新后的 Cookie: %w", saveErr)) } else { diff --git a/internal/db/cookie_scope.go b/internal/db/cookie_scope.go index 845ea85..e04e299 100644 --- a/internal/db/cookie_scope.go +++ b/internal/db/cookie_scope.go @@ -250,18 +250,18 @@ func validateCookieOwnerID(userID int64) error { return nil } -// CookieFingerprintData 表示自动化 Session 阻断指纹所需的 Cookie 与 metadata,不包含登录密码或账号资料。 -type CookieFingerprintData struct { - // Value 是 repository 解密后的 Cookie 明文,仅用于生成进程内指纹,不得写入日志或响应。 +// CookieRuntimeData 表示运行时操作所需的 Cookie 与 metadata,不包含登录密码或账号资料。 +type CookieRuntimeData struct { + // Value 是 repository 解密后的 Cookie 明文,仅在受控的运行时操作中短暂使用,不得写入日志或响应。 Value string - // MetadataJSON 是 repository 解密后的 Cookie 运行 metadata,用于识别完整 Cookie Jar 的变化。 + // MetadataJSON 是 repository 解密后的 Cookie 运行 metadata,用于恢复或识别完整 Cookie Jar 的变化。 MetadataJSON string } -// GetCookieFingerprintData 返回生成账号凭证指纹所需的最小字段,并严格跳过登录密码、用户名等其他列。 -func (c *Cookies) GetCookieFingerprintData(ctx context.Context, cookieID string) (CookieFingerprintData, error) { - // data 保存按账号 ID 查询到的指纹输入字段。 - var data CookieFingerprintData +// GetCookieRuntimeData 返回运行时所需的最小 Cookie 与 metadata 字段,并严格跳过登录密码、用户名等其他列。 +func (c *Cookies) GetCookieRuntimeData(ctx context.Context, cookieID string) (CookieRuntimeData, error) { + // data 保存按账号 ID 查询到的运行时字段。 + var data CookieRuntimeData // encryptedValue 和 encryptedMetadata 保存仅供本次解密的数据库密文。 var encryptedValue, encryptedMetadata string // queryErr 表示账号不存在或指纹输入查询失败的原因。 @@ -269,19 +269,19 @@ func (c *Cookies) GetCookieFingerprintData(ctx context.Context, cookieID string) `SELECT value, COALESCE(metadata_json,'') FROM cookies WHERE id=?`, cookieID). Scan(&encryptedValue, &encryptedMetadata); queryErr != nil { if errors.Is(queryErr, sql.ErrNoRows) { - return CookieFingerprintData{}, ErrNotFound + return CookieRuntimeData{}, ErrNotFound } - return CookieFingerprintData{}, queryErr + return CookieRuntimeData{}, queryErr } // decryptErr 表示 Cookie 或 metadata 密文无法解密的原因。 var decryptErr error data.Value, decryptErr = c.codec.decrypt("cookie", cookieID, encryptedValue) if decryptErr != nil { - return CookieFingerprintData{}, fmt.Errorf("解密账号 %s Cookie: %w", cookieID, decryptErr) + return CookieRuntimeData{}, fmt.Errorf("解密账号 %s Cookie: %w", cookieID, decryptErr) } data.MetadataJSON, decryptErr = c.codec.decrypt(cookieMetadataScope, cookieID, encryptedMetadata) if decryptErr != nil { - return CookieFingerprintData{}, fmt.Errorf("解密账号 %s Cookie metadata: %w", cookieID, decryptErr) + return CookieRuntimeData{}, fmt.Errorf("解密账号 %s Cookie metadata: %w", cookieID, decryptErr) } return data, nil } diff --git a/internal/db/cookie_scope_test.go b/internal/db/cookie_scope_test.go index 71f056e..4d19c38 100644 --- a/internal/db/cookie_scope_test.go +++ b/internal/db/cookie_scope_test.go @@ -157,8 +157,8 @@ func TestListEnabledRuntimeCredentials(t *testing.T) { } } -// TestGetCookieFingerprintDataExcludesLoginSecrets 验证指纹查询只解密 Cookie 与 metadata,不读取损坏的登录密码。 -func TestGetCookieFingerprintDataExcludesLoginSecrets(t *testing.T) { +// TestGetCookieRuntimeDataExcludesLoginSecrets 验证运行时窄查询只解密 Cookie 与 metadata,不读取损坏的登录密码。 +func TestGetCookieRuntimeDataExcludesLoginSecrets(t *testing.T) { t.Setenv("XIANYU_DATA_KEY", "fingerprint-query-key") // store 是当前测试使用的 SQLite repository 聚合器。 store, cleanup := newTestDB(t) @@ -190,13 +190,13 @@ func TestGetCookieFingerprintDataExcludesLoginSecrets(t *testing.T) { "fingerprint-user", "not-a-password-ciphertext", "fingerprint-cookie"); corruptErr != nil { t.Fatalf("corrupt password: %v", corruptErr) } - // data 是不受登录密码损坏影响的最小指纹输入。 - data, dataErr := store.Cookies.GetCookieFingerprintData(ctx, "fingerprint-cookie") + // data 是不受登录密码损坏影响的最小运行时输入。 + data, dataErr := store.Cookies.GetCookieRuntimeData(ctx, "fingerprint-cookie") if dataErr != nil || data.Value != "sid=fingerprint" || data.MetadataJSON != metadata { t.Fatalf("fingerprint data=%+v err=%v", data, dataErr) } // missingErr 表示不存在账号应返回统一的未找到错误。 - if _, missingErr := store.Cookies.GetCookieFingerprintData(ctx, "fingerprint-missing"); !errors.Is(missingErr, ErrNotFound) { + if _, missingErr := store.Cookies.GetCookieRuntimeData(ctx, "fingerprint-missing"); !errors.Is(missingErr, ErrNotFound) { t.Fatalf("missing fingerprint data err=%v", missingErr) } }