引擎:收窄凭证指纹查询
This commit is contained in:
1 parent
b27367a564
commit
4d92358dfa
3 files changed
+40
-9
No files matched your search
@@ -1626,21 +1626,21 @@ func (a *Account) databaseCredentialFingerprint(ctx context.Context, cookieStr s
|
||||
if a.store == nil || a.store.Cookies == nil {
|
||||
return credentialStateFingerprint(cookieStr, ""), nil
|
||||
}
|
||||
detail, err := a.store.Cookies.GetDetails(ctx, a.CookieID)
|
||||
runtimeData, err := a.store.Cookies.GetCookieRuntimeData(ctx, a.CookieID) // runtimeData 只包含 token 凭证一致性校验所需的 Cookie 与 metadata。
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if detail == nil {
|
||||
return "", db.ErrNotFound
|
||||
}
|
||||
_, snapshotComplete := cookierefresh.SnapshotFromMetadataOK(detail.MetadataJSON)
|
||||
if strings.TrimSpace(detail.Value) == "" && !snapshotComplete {
|
||||
// runtimeData 已在调用方凭证锁内读取,避免校验期间混入另一笔 Cookie 更新。
|
||||
// Cookie 与 metadata 均由 repository 按账号作用域解密,登录密码不会进入此流程。
|
||||
// 后续空值判断、指纹比较和错误文案保持原有 token 绑定语义。
|
||||
_, snapshotComplete := cookierefresh.SnapshotFromMetadataOK(runtimeData.MetadataJSON)
|
||||
if strings.TrimSpace(runtimeData.Value) == "" && !snapshotComplete {
|
||||
return "", fmt.Errorf("数据库 Cookie 为空且没有权威 Jar")
|
||||
}
|
||||
if credentialCookieFingerprint(detail.Value) != credentialCookieFingerprint(cookieStr) {
|
||||
if credentialCookieFingerprint(runtimeData.Value) != credentialCookieFingerprint(cookieStr) {
|
||||
return "", fmt.Errorf("token 请求期间数据库 Cookie 已变化")
|
||||
}
|
||||
return credentialStateFingerprint(detail.Value, detail.MetadataJSON), nil
|
||||
return credentialStateFingerprint(runtimeData.Value, runtimeData.MetadataJSON), nil
|
||||
}
|
||||
|
||||
// reloadCookieFromDB 复读 DB cookie:与内存不同则采纳,并清 token 缓存。普通 Cookie
|
||||
|
||||
@@ -1292,3 +1292,32 @@ func TestAdoptTokenResponseCookiesUsesMetadataWithoutLoginSecrets(t *testing.T)
|
||||
t.Fatal("已有权威 Cookie 快照不应在 token 响应合并后丢失")
|
||||
}
|
||||
}
|
||||
|
||||
// TestDatabaseCredentialFingerprintUsesRuntimeData 验证 token 凭证指纹不解密登录密码。
|
||||
func TestDatabaseCredentialFingerprintUsesRuntimeData(t *testing.T) {
|
||||
t.Setenv("XIANYU_DATA_KEY", "engine-credential-fingerprint-query-key")
|
||||
// acc 和 store 是本测试的账号及数据库;cleanup 负责关闭临时数据库。
|
||||
acc, _, store, cleanup := newAccountForTest(t)
|
||||
defer cleanup()
|
||||
// ctx 是测试凭证指纹共用的上下文。
|
||||
ctx := context.Background()
|
||||
// cookieValue 是 token 请求期间数据库中的权威 Cookie 明文。
|
||||
cookieValue := "unb=123; _m_h5_tk=fingerprint"
|
||||
// metadata 是用于计算完整凭证状态指纹的运行 metadata。
|
||||
metadata := cookierefresh.MetadataWithSnapshot(`{"note":"fingerprint"}`, []cookierefresh.BrowserCookie{{Name: "sid", Value: "fp", Domain: ".goofish.com", Path: "/"}})
|
||||
// updateErr 表示预置凭证指纹输入失败的原因。
|
||||
if updateErr := store.Cookies.UpdateRenewalCookie(ctx, "cid", cookieValue, metadata, time.Now().Unix()); updateErr != nil {
|
||||
t.Fatalf("preset credential: %v", updateErr)
|
||||
}
|
||||
// corruptErr 表示写入故意损坏的登录密码密文失败的原因。
|
||||
if _, corruptErr := store.DB.ExecContext(ctx,
|
||||
`UPDATE cookies SET username=?,password=? WHERE id=?`,
|
||||
"engine-fingerprint-user", "not-a-password-ciphertext", "cid"); corruptErr != nil {
|
||||
t.Fatalf("corrupt password: %v", corruptErr)
|
||||
}
|
||||
// fingerprint 和 fingerprintErr 是窄查询生成的凭证状态指纹及错误。
|
||||
fingerprint, fingerprintErr := acc.databaseCredentialFingerprint(ctx, cookieValue)
|
||||
if fingerprintErr != nil || fingerprint != credentialStateFingerprint(cookieValue, metadata) {
|
||||
t.Fatalf("credential fingerprint=%q err=%v", fingerprint, fingerprintErr)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user