jackwener / jackwener/xiaohongshu-cli

Chrome 127+ App-Bound Encryption 导致 xhs login Cookie 提取失败,且错误信息具有误导性

Open
#42 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Python
Stars
2.6k
Forks
271
PR merge metrics
No merged PRs in 30d

Description

## 环境信息

| 项目 | 值 |
|------|-----|
| OS | Windows 11 Home China 10.0.26200 |
| Python | 3.12.3 |
| xiaohongshu-cli | v0.6.4(通过 `uv tool install xiaohongshu-cli` 安装) |
| browser_cookie3 | v0.20.1 |
| Chrome | 146.0.7680.178 |

## 现象

执行 `xhs login --cookie-source chrome` 或 `xhs login`(auto 模式),报错:

```
✗ Login verification failed: No 'a1' cookie found for xiaohongshu.com in chrome.
```

用户已在 Chrome 中登录小红书,Cookie 实际存在。

## 根因分析

### 1. 真实错误被掩盖

通过 DEBUG 日志定位到 `browser_cookie3` 的实际异常为:

```
BrowserCookieError: Unable to get key for cookie decryption
```

但 `xhs login` 的错误处理链路将其吞掉,最终向用户展示 `No 'a1' cookie found`,**误导用户以为 Cookie 不存在**。

### 2. Cookie 确实存在

直接用 sqlite3 读取 Chrome Cookie 数据库验证:

```bash
cp "C:/Users/.../Profile 2/Network/Cookies" /tmp/cookies_check.sqlite
sqlite3 /tmp/cookies_check.sqlite "SELECT host_key, name FROM cookies WHERE host_key LIKE '%xiaohongshu%';"
```

输出:

```
.xiaohongshu.com|a1
.xiaohongshu.com|web_session
.xiaohongshu.com|webId
... (共 13 条)
```

`a1` Cookie 明确存在。

### 3. 根本原因:Chrome App-Bound Encryption

Chrome 127+(2024 年 7 月起)引入了 **App-Bound Encryption**,将 Cookie 加密密钥绑定到 Chrome 应用自身。`browser_cookie3` 通过 Windows DPAPI 解密密钥的方式已失效,第三方程序无法解密。

验证:

```python
import browser_cookie3 as bc3
bc3.chrome(domain_name='.xiaohongshu.com')
# → BrowserCookieError: Unable to get key for cookie decryption
```

## 绕过方案

使用二维码登录可完全绕过 Cookie 提取问题:

```bash
PYTHONIOENCODING=utf-8 xhs login --qrcode
```

(`PYTHONIOENCODING=utf-8` 是为了解决另一个 Windows GBK 编码问题,见 #另一个 Issue)

登录成功:

```
✅ Login confirmed!
👤 User ID: xxxxxxxx
```

之后 `xhs search`、`xhs status` 等功能均正常工作。

## 建议

### 短期:改进错误信息

当 `browser_cookie3` 抛出解密相关异常时:

1. 向用户展示真实原因(密钥解密失败),而非 `No 'a1' cookie found`
2. 主动提示 `--qrcode` 备选方案,例如:

```
✗ Cookie 解密失败(Chrome 127+ 使用了 App-Bound Encryption,第三方程序无法读取)。
请改用二维码登录:xhs login --qrcode
```

### 长期:适配或替代

- 跟踪 `browser_cookie3` 对 App-Bound Encryption 的适配进度
- 或考虑替代的 Cookie 提取方案

## 影响范围

所有使用 Chrome 127+ 的 Windows 用户(Chrome 127 于 2024 年 7 月发布,目前绝大多数用户已受影响)。macOS 和 Linux 用户不受此问题影响(Chrome 在这些平台使用不同的加密方案)。

Contributor guide

No contributing guide indexed for this repository

Research direction

Start at the xhs login flow for --cookie-source chrome and the auto mode, then trace how browser_cookie3 exceptions become the “No 'a1' cookie found” message. Reproduce with Chrome 127+ on Windows and verify that decryption failures explain the App-Bound Encryption limitation and point users to xhs login --qrcode.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
authentication, cli
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
56/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.