Skip to content

[Bug] MergeSetCookieHeaders 域前导点失配导致同名新旧 Cookie 双重发送与 DBSC HIGHEST 刷新风暴,以及 Cookie/DBSC 认证链的多处缺陷 #378

Backlog

Description

@Gina-DEVELOPER

发生了什么 / What happened

发生了什么 / What happened

在审查并核对 v0.3.17(5deae85)的 Cookie 轮换、DBSC 会话续签与浏览器导入链路时,发现以下可直接触发服务端反欺诈检测或认证链异常的问题:

  1. MergeSetCookieHeaders 丢失 .google.com 前导点,导致每次轮换后向 Google 同时发送新旧同名 Cookie,并触发 DBSC chromeHighest 刷新风暴(核心问题)
    代码位置: internal/aistudio/auth.go(MergeSetCookieHeaders 第 337–393 行、CookieHeader 第 272–305 行、CookieValue 第 308–334 行、BoundCookieLifetime 第 150–167 行)
    根因: Go 标准库 http.Response.Cookies() 在解析 Set-Cookie: Domain=.google.com 时会自动剥离域名前导点,返回 incoming.Domain == "google.com";而 OAuthMultilogin(internal/chromeauth/auth.go:325)、浏览器扩展导入及 parseCookieHeader(internal/aistudio/cookie_import.go:184)写入 StorageState.Cookies 的域均为 ".google.com"。 MergeSetCookieHeaders 第 359 行直接比较:
    go
    if existing.Name == incoming.Name && strings.EqualFold(existing.Domain, domain) && existing.Path == cookiePath && existing.PartitionKey == ""
    由于 ".google.com" != "google.com",匹配必然失败——既无法原地替换已有 Cookie,也无法在 MaxAge < 0 时删除旧 Cookie,而是在 s.Cookies 末尾追加一条 Domain: "google.com" 的同名新记录。
    导致的三个连环后果:
    向 Google 双重发送新旧同名 Cookie: CookieHeader()(第 289–304 行)通过 cookieDomainMatches 同时命中 ".google.com" 与 "google.com",使后续所有发往 Google 的请求在 Cookie: 请求头里同时携带旧值与新值(例如两条 SIDCC、两条 __Secure-1PSIDCC、两条 __Secure-3PSIDCC、两条 __Secure-1PSIDRTS),这与真实浏览器同域同路径仅发送单条 Cookie 的行为完全不符。
    CookieValue() 恒取到未更新的旧值: 第 328 行 if len(cookie.Path) > selectedPath 在同路径("/")长度相等时不覆盖,因此始终保留先出现的 ".google.com" 旧值(影响 SAPISID、__Secure-1PAPISID 等签名计算与校验)。
    DBSC 触发无限 chromeHighest 刷新风暴: BoundCookieLifetime()(第 155 行 cookie.Domain != ".google.com")完全忽略 RotateBoundCookies 经 MergeSetCookieHeaders 写入的 "google.com" 新 Cookie。一旦初始导入的 ".google.com" __Secure-1PSIDRTS 过期,BoundCookieLifetime 永远返回 0,导致此后每次 RPC 请求前 ensureBound 都向 accounts.google.com/RotateCookies 发出一次 chromeHighest(u=0, i)强制刷新。
  2. BoundCookieLifetime 的另外 3 处边界缺陷
    代码位置: internal/aistudio/auth.go:150-167
    具体表现:
    当 len(session.Credentials) == 0 时,lifetime 保持初始值 time.Duration(1<<63 - 1)(约 292 年),而非注释声明的 0,导致上层跳过 DBSC 刷新;
    第 158 行仅检查 if cookie.Expires < 0,漏判 cookie.Expires == 0(部分扩展或 Netscape 格式会话 Cookie 的取值),走到 time.Unix(0, 0).Sub(now) 得到 remaining = 0,同样持续触发 chromeHighest 刷新;
    第 161 行 int64(cookie.Expires * 1e9) 在扩展导出毫秒时间戳或远期过期时间(> 9.223e9)时发生 int64 溢出为负数,得出 remaining = 0。
  3. parseCookieHeader 将所有 __Host- Cookie 硬编码归属到 aistudio.google.com
    代码位置: internal/aistudio/cookie_import.go:184-187
    具体表现: 从请求头导入 Cookie 时,所有以 __Host- 开头的 Cookie 被统一写入 Domain: "aistudio.google.com"。但 Google 账号认证链中的 __Host-GAPS、__Host-1PLSID、__Host-3PLSID 实际上属于 accounts.google.com(且 RFC 6265bis 下 __Host- Cookie 必须为 host-only),强行挂到 aistudio.google.com 会导致请求 AI Studio 时携带本不该发往该域的登录 Cookie。
  4. readCDPBrowser 与 readBiDiBrowser 丢失或混并 CHIPS 分区 Cookie(partitionKey)
    代码位置: internal/chromeauth/cdp.go:67-71、internal/chromeauth/bidi.go:37-46, 250-284
    具体表现:
    readCDPBrowser 直接丢弃所有 cookie["partitionKey"] != nil 的 CHIPS 分区 Cookie;
    readBiDiBrowser 的 bidiCookie 结构体未定义 partitionKey 字段,bidiCookiesToExport 仅按 [domain, path, name] 去重并写入空的 PartitionKey。
  5. RotateBoundCookies 空指针 panic 与 renewRemote 启动抢占全局 TPM 锁
    代码位置: internal/chromeauth/bound.go:53、internal/app/auth_retry.go:538-543、internal/setup/renewal.go:136-140、internal/chromeauth/ncrypt_windows.go:27
    具体表现:
    ensureBoundWith 仅检查了 extension.Bound == nil 而未检查 extension.OAuth == nil;当收到 403 + Secure-Session-Challenge 时,bound.go:53 直接读取 extension.OAuth.WrappedBindingKey,若 OAuth 为空则直接触发 nil pointer dereference panic;
    renewRemote 启动时用 group.Go 并发触发所有配对账号,全部请求立刻排队等待唯一的全局互斥锁 ncryptMu(Windows TPM 2.0 硬件签名),账号数量较多时队尾协程长时间阻塞或超时失败。
  6. mergeSetCookieHeaders 缺少 30 秒 *SIDCC 写盘节流,且 chrome157Profile 与本机 User-Agent 版本及空 Accept-Language 不一致
    代码位置: internal/aistudio/accounts.go:2254-2271(对比 ReplaceCookies 第 2172–2176 行)、internal/chromeauth/client.go:31-56、internal/chromeauth/protocol.go:125-135, 159-195
    具体表现:
    ReplaceCookies 通过 onlyRotatingCookiesChanged 与 rotatingCookieWriteInterval(30 秒)避免了高频写盘,但 mergeSetCookieHeaders(由 transport_http.go:321, 426 与 webchannel.go:1113 每次响应调用)缺少该节流,Google 每次轮换 *SIDCC 都会同步执行全量读盘、JSON 编解码、写盘与 fsync;
    chrome157Profile 固定发送 Chrome 157 TLS/H2 指纹与固定 chromeTrustAnchors,而 readChromeIdentity 从本机 Chrome 的 Last Version 读取版本号拼装 User-Agent(两者可能不一致);同时当 Chrome Preferences 缺少 intl 语言项时,acceptLanguageHeader("") 返回空串,导致 OAuthMultilogin 与 RotateBoundCookies 发出空的 Accept-Language 请求头。
    建议修复方向
    在 MergeSetCookieHeaders 与 BoundCookieLifetime 中统一对域名做 strings.TrimPrefix(strings.ToLower(d), ".")(或规范为 .google.com / host-only 区分),确保更新/删除原地命中已有 ".google.com" Cookie 并正确计算剩余有效期;同时修复 BoundCookieLifetime 的空凭证返回 0、Expires <= 0 判定与 int64 溢出保护。
    补齐 parseCookieHeader 中 __Host-GAPS / __Host-*PLSID 对 accounts.google.com 的归属,并在 cdp.go / bidi.go 中保留 partitionKey。
    在 ensureBoundWith / RotateBoundCookies 中校验 extension.OAuth != nil,为 renewRemote 增加有限并发/抖动,并在 mergeSetCookieHeaders 复用 onlyRotatingCookiesChanged 30 秒写盘节流。

版本 / Version

v0.3.17

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions