自动化:收窄确认发货凭证读取
This commit is contained in:
1 parent
8936988c7a
commit
04e1aab2e8
5 files changed
+32
-31
No files matched your search
+12
-12
@@ -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
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user