引擎:收窄接口续期凭证读取

This commit is contained in:
Christ committed 2026-08-14 12:29:37 +08:00
1 parent aec7e1186b
commit 72f341eab8
3 files changed
+49 -16

No files matched your search

+14 -14
View File
@@ -1010,21 +1010,21 @@ func (a *Account) tryAPIRenewUsing(ctx context.Context, call func(context.Contex
a.mu.Unlock()
var snapshot []cookierefresh.BrowserCookie
if a.store != nil && a.store.Cookies != nil {
detail, detailErr := a.store.Cookies.GetDetails(ctx, a.CookieID)
if detailErr != nil || detail == nil {
if detailErr == nil {
detailErr = db.ErrNotFound
}
runtimeData, detailErr := a.store.Cookies.GetCookieRuntimeData(ctx, a.CookieID) // runtimeData 只包含接口续期所需的 Cookie 与 metadata。
if detailErr != nil {
a.logger.Warn("接口续期前读取最新 Cookie 失败", "err", detailErr)
return false, detailErr
}
if detail.Value != cookieStr {
cookieStr = detail.Value
if runtimeData.Value != cookieStr {
cookieStr = runtimeData.Value
a.replaceCookieStr(cookieStr)
a.clearCurrentToken()
a.clearTokenCache(ctx)
}
snapshot = cookierefresh.SnapshotFromMetadata(detail.MetadataJSON)
snapshot = cookierefresh.SnapshotFromMetadata(runtimeData.MetadataJSON)
// runtimeData 的 Cookie 用于续期请求,metadata 用于恢复浏览器 Cookie 快照。
// 读取窄模型不会触碰登录用户名、密码等无关凭证字段。
// API 续期仍沿用原有锁、Cookie 比较和 token 清理顺序。
}
res, err := call(ctx, cookieStr, snapshot)
if res == nil {
@@ -1039,15 +1039,15 @@ func (a *Account) tryAPIRenewUsing(ctx context.Context, call func(context.Contex
updated := false
persisted := false
if res.CookieSnapshotComplete && a.store != nil && a.store.Cookies != nil {
detail, detailErr := a.store.Cookies.GetDetails(ctx, a.CookieID)
if detailErr != nil || detail == nil {
if detailErr == nil {
detailErr = db.ErrNotFound
}
runtimeData, detailErr := a.store.Cookies.GetCookieRuntimeData(ctx, a.CookieID) // runtimeData 只包含续期快照持久化所需字段。
// 快照持久化只依赖 metadata,Cookie 明文由续期响应直接提供。
// 保留统一运行时查询模型,避免为相同凭证路径恢复完整账号详情。
// 下面的更新操作和错误处理保持原有续期语义不变。
if detailErr != nil {
a.logger.Warn("保存续期 Cookie 快照失败", "err", detailErr)
return false, detailErr
}
metadata := cookierefresh.MetadataWithSnapshot(detail.MetadataJSON, res.CookieSnapshot)
metadata := cookierefresh.MetadataWithSnapshot(runtimeData.MetadataJSON, res.CookieSnapshot)
if err := a.store.Cookies.UpdateRenewalCookie(ctx, a.CookieID, res.NewCookies, metadata, time.Now().Unix()); err != nil {
a.logger.Warn("保存续期 Cookie 快照失败", "err", err)
return false, err
+31
View File
@@ -1072,3 +1072,34 @@ func TestTruncID(t *testing.T) {
t.Errorf("长串应截断到 53 字符并加 ...: got %q (len=%d)", got, len(got))
}
}
// TestTryAPIRenewUsingExcludesLoginSecrets 验证接口续期读取窄模型时不解密登录密码。
func TestTryAPIRenewUsingExcludesLoginSecrets(t *testing.T) {
t.Setenv("XIANYU_DATA_KEY", "engine-api-renew-query-key")
// acc 是使用接口续期窄查询路径的测试账号;cleanup 负责关闭临时数据库。
acc, _, 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-api-user", "not-a-password-ciphertext", "cid"); corruptErr != nil {
t.Fatalf("corrupt password: %v", corruptErr)
}
// callbackCalled 表示接口续期回调是否收到窄查询得到的 Cookie。
callbackCalled := false
// result 是不产生 Cookie 更新、仅表示接口续期成功的模拟响应。
result := &xrenew.Result{Success: true, RenewMethod: "api"}
// renewed 和 renewErr 是接口续期结果及其错误。
renewed, renewErr := acc.tryAPIRenewUsing(ctx, func(_ context.Context, cookieStr string, _ []cookierefresh.BrowserCookie) (*xrenew.Result, error) {
callbackCalled = true
if cookieStr != "unb=123; _m_h5_tk=tk_1;" {
t.Fatalf("接口续期收到错误 Cookie: %q", cookieStr)
}
return result, nil
})
if renewErr != nil || !renewed || !callbackCalled {
t.Fatalf("接口续期应在登录密码损坏时成功: renewed=%v callback=%v err=%v", renewed, callbackCalled, renewErr)
}
}