fix(续期): 识别 HttpOnly 长登录凭证

This commit is contained in:
Christ committed 2026-07-26 00:03:45 +08:00
1 parent 9e4e4bcee9
commit 90b6e4f5ce
2 files changed
+23 -8

No files matched your search

+4 -7
View File
@@ -354,13 +354,10 @@ func (s Service) RenewAPIFirst(ctx context.Context, cookiesStr string, snapshots
var snapshot []cookierefresh.BrowserCookie
if authoritativeSnapshot {
snapshot = cookierefresh.NormalizeSnapshot(snapshots[0])
documentCookies := make([]cookierefresh.BrowserCookie, 0, len(snapshot))
for _, cookie := range snapshot {
if !cookie.HTTPOnly {
documentCookies = append(documentCookies, cookie)
}
}
if scoped, authoritative := cookierefresh.ScopedCookieHeaderForRequest(documentCookies, documentURL, goofishTopSite, now); authoritative {
// havana_lgc_exp 由官网以 HttpOnly Cookie 下发。续期服务保存的是
// 浏览器完整 Cookie Jar,不能按 document.cookie 过滤,否则刚登录的
// 有效长登录凭证会被误判为不存在。
if scoped, authoritative := cookierefresh.ScopedCookieHeaderForRequest(snapshot, documentURL, goofishTopSite, now); authoritative {
decisionCookies = scoped
}
if scoped, authoritative := cookierefresh.ScopedCookieHeaderForRequest(snapshot, requestURL, goofishTopSite, now); authoritative {
+19 -1
View File
@@ -315,7 +315,7 @@ func TestRenewAPIFirstUsesBrowserCookieScopes(t *testing.T) {
host := strings.TrimPrefix(srv.URL, "http://")
host = strings.Split(host, ":")[0]
snapshot := []cookierefresh.BrowserCookie{
{Name: "havana_lgc_exp", Value: futureMillis(time.Hour), Domain: ".goofish.com", Path: "/"},
{Name: "havana_lgc_exp", Value: futureMillis(time.Hour), Domain: ".goofish.com", Path: "/", HTTPOnly: true},
{Name: "request_only", Value: "passport", Domain: host, Path: "/"},
{Name: "www_only", Value: "private", Domain: "www.goofish.com", Path: "/im"},
{Name: "http_only_document", Value: "hidden", Domain: ".goofish.com", Path: "/", HTTPOnly: true},
@@ -333,6 +333,24 @@ func TestRenewAPIFirstUsesBrowserCookieScopes(t *testing.T) {
}
}
func TestRenewAPIFirstUsesHTTPOnlyLongLoginCookieForDecision(t *testing.T) {
useTestDesktopFingerprint(t)
var calls atomic.Int32
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
calls.Add(1)
_, _ = w.Write([]byte(`{"data":{"content":{"data":{"processFinished":true,"resultCode":100}}}}`))
}))
defer srv.Close()
snapshot := []cookierefresh.BrowserCookie{{
Name: "havana_lgc_exp", Value: futureMillis(time.Hour), Domain: ".goofish.com", Path: "/", HTTPOnly: true,
}}
svc := Service{HTTPClient: srv.Client(), SilentHasLoginURL: srv.URL, RetryDelay: -1}
res, err := svc.RenewAPIFirst(context.Background(), "", snapshot)
if err != nil || res == nil || !res.Success || res.Skipped || res.RequestCount != 1 || calls.Load() != 1 {
t.Fatalf("result=%+v calls=%d err=%v", res, calls.Load(), err)
}
}
func TestRenewAPIFirstRunsWithLinuxChromiumFingerprint(t *testing.T) {
fingerprint := useTestLinuxFingerprint(t)
var calls atomic.Int32