引擎:收窄扁平续期凭证读取
This commit is contained in:
1 parent
72f341eab8
commit
ccfc630fd7
5 files changed
+84
-8
No files matched your search
@@ -1098,16 +1098,16 @@ func (a *Account) persistRenewFlatCookie(ctx context.Context, newCookies string)
|
||||
if a.store == nil || a.store.Cookies == nil {
|
||||
return nil
|
||||
}
|
||||
detail, err := a.store.Cookies.GetDetails(ctx, a.CookieID)
|
||||
if err != nil || detail == nil {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return db.ErrNotFound
|
||||
metadata, err := a.store.Cookies.GetCookieMetadata(ctx, a.CookieID) // metadata 只包含扁平 Cookie 写回所需的快照信息。
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// 该流程不需要读取现有 Cookie 明文或登录秘密。
|
||||
// metadata 已在 repository 层按账号作用域解密,下面只清理旧快照。
|
||||
// 更新操作继续由 UpdateRenewalCookie 负责加密和账号存在性校验。
|
||||
// 没有权威 Jar 时,接口 Set-Cookie 只能更新兼容扁平值。不能把
|
||||
// Domain/Path/HttpOnly/PartitionKey 均未知的 Cookie 伪造成完整快照。
|
||||
metadata := cookierefresh.MetadataWithoutSnapshot(detail.MetadataJSON)
|
||||
metadata = cookierefresh.MetadataWithoutSnapshot(metadata)
|
||||
return a.store.Cookies.UpdateRenewalCookie(ctx, a.CookieID, newCookies, metadata, time.Now().Unix())
|
||||
}
|
||||
|
||||
|
||||
@@ -1103,3 +1103,41 @@ func TestTryAPIRenewUsingExcludesLoginSecrets(t *testing.T) {
|
||||
t.Fatalf("接口续期应在登录密码损坏时成功: renewed=%v callback=%v err=%v", renewed, callbackCalled, renewErr)
|
||||
}
|
||||
}
|
||||
|
||||
// TestPersistRenewFlatCookieExcludesLoginSecrets 验证扁平 Cookie 写回只读取 metadata,不解密旧 Cookie 或登录密码。
|
||||
func TestPersistRenewFlatCookieExcludesLoginSecrets(t *testing.T) {
|
||||
t.Setenv("XIANYU_DATA_KEY", "engine-flat-renew-query-key")
|
||||
// acc 是执行扁平 Cookie 写回的测试账号;cleanup 负责关闭临时数据库。
|
||||
acc, _, store, cleanup := newAccountForTest(t)
|
||||
defer cleanup()
|
||||
// ctx 是测试扁平 Cookie 写回共用的上下文。
|
||||
ctx := context.Background()
|
||||
// metadata 是包含旧浏览器快照和其他配置的合法运行 metadata。
|
||||
metadata := cookierefresh.MetadataWithSnapshot(`{"other":true}`, []cookierefresh.BrowserCookie{{Name: "sid", Value: "old", Domain: ".goofish.com", Path: "/"}})
|
||||
// updateErr 表示预置完整 metadata 失败的原因。
|
||||
if updateErr := store.Cookies.UpdateRenewalCookie(ctx, "cid", "sid=old", metadata, time.Now().Unix()); updateErr != nil {
|
||||
t.Fatalf("preset metadata: %v", updateErr)
|
||||
}
|
||||
// corruptErr 表示将旧 Cookie 和登录密码密文损坏,用于验证写回只读取 metadata。
|
||||
if _, corruptErr := store.DB.ExecContext(ctx,
|
||||
`UPDATE cookies SET value=?,password=? WHERE id=?`,
|
||||
"not-a-cookie-ciphertext", "not-a-password-ciphertext", "cid"); corruptErr != nil {
|
||||
t.Fatalf("corrupt secrets: %v", corruptErr)
|
||||
}
|
||||
// persistErr 表示使用新响应 Cookie 写回数据库的结果。
|
||||
if persistErr := acc.persistRenewFlatCookie(ctx, "sid=fresh"); persistErr != nil {
|
||||
t.Fatalf("persist flat cookie: %v", persistErr)
|
||||
}
|
||||
// runtimeData 是写回后读取的 Cookie 与 metadata 窄模型。
|
||||
runtimeData, runtimeErr := store.Cookies.GetCookieRuntimeData(ctx, "cid")
|
||||
if runtimeErr != nil || runtimeData.Value != "sid=fresh" {
|
||||
t.Fatalf("runtime data=%+v err=%v", runtimeData, runtimeErr)
|
||||
}
|
||||
// complete 表示写回后的 metadata 是否仍包含完整浏览器 Cookie 快照。
|
||||
if _, complete := cookierefresh.SnapshotFromMetadataOK(runtimeData.MetadataJSON); complete {
|
||||
t.Fatal("扁平 Cookie 写回不得保留完整浏览器快照")
|
||||
}
|
||||
if !strings.Contains(runtimeData.MetadataJSON, `"other":true`) {
|
||||
t.Fatalf("扁平 Cookie 写回应保留其他 metadata: %s", runtimeData.MetadataJSON)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user