fix(wopan): apply browser user agent and refresh tokens before init #3222
All Vouch review confidence checks passed
Residual risk 0.30 = Change Risk (0.75) × (1 − Review Confidence (0.60)).
Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.75 · 650 lines diff across 8 files
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.17 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size
diff_size
0.388
Shallow Review Depth
shallow_review_depth
0.289
Sensitive Paths
sensitive_paths
0.075
Real PR Conversation Timeline
1 comments
"Review active on #3222"
general
weight: 0.35
elysia-best
<!--
PR title / PR 标题:
type(scope): summaryfeat,docs,fix,style,refactor,chore!:feat(driver)!: change auth flow-->
fix(wopan): apply browser user agent and refresh tokens before init
Base:
main← Compare:fix/wopan-403-user-agentCommit:
57cd71c· Files changed: 3 (+249 / −9)Open PR: https://github.com/OpenListTeam/OpenList/pull/new/fix/wopan-403-user-agent
Summary / 摘要
联通云盘(WoPan)存储初始化失败,日志为
failed get objs: failed to list objs: request failed with status: 403 Forbidden,只填 refresh token 时还会先报
failed init storage: crypto/aes: invalid key size 0。WoPan storages could not be initialized: every dispatcher request was answered
with
403 Forbidden, and refresh-token-only configurations additionally failedwith
crypto/aes: invalid key size 0before any request was even accepted.根因是驱动初始化阶段的两个缺陷:
1. User-Agent 缺失:
wopan-sdk-go把浏览器 UA 存在私有字段里(
WithUA/SetUA),只在upload2C上传时用w.ua显式写入请求头,其余 dispatcher 请求(
QueryAllFiles、FamilyUserCurrentEncode等)一路裸奔,最终由 resty 自动补成
go-resty/2.17.2 (...)。联通云盘边缘节点直接 403 掉非浏览器 UA。
2. Token 初始化顺序错误:
Init先调FamilyUserCurrentEncode()再调InitData()。该请求体用accessToken[:16]做 AES 密钥加密,仅配 refreshtoken 时 access token 为空 → 密钥长度 0 →
crypto/aes: invalid key size 0。<!--
-->
User-visible behavior changes / 用户可感知的行为变化:
403 Forbidden。WoPan storages initialize and list again instead of failing with
403 Forbidden.为空或长度不足 16 时会先自动刷新,不再出现
crypto/aes: invalid key size 0。A refresh token alone is enough to add the storage; a missing or too short
access token is refreshed up front instead of failing with
crypto/aes: invalid key size 0.refresh_token不再标记为必填,两个 token 都增加了说明。Field help text updated:
refresh_tokenis no longer mandatory and bothtoken fields describe the new behaviour.
Important implementation changes / 重要实现变化:
Init精简为「建 client + 注册 token 回调」,具体初始化移入initClient(ctx)。Initnow only builds the client and registers the token callback; the realinitialization lives in
initClient(ctx).initClient第一件事是d.client.SetUserAgent(wopan.DefaultUA),保证后续所有dispatcher 请求都带浏览器 UA。
initClientstarts withd.client.SetUserAgent(wopan.DefaultUA)so everydispatcher request carries the browser UA.
minAccessTokenLen = 16常量(与 SDK 内部Crypto.SetAccessToken的校验一致)。access token 缺失/过短时先RefreshToken()并校验刷新结果,然后才InitData()→FamilyUserCurrentEncode()。Added
minAccessTokenLen = 16(mirrors the SDK's internalCrypto.SetAccessTokencheck). A missing or too short access token isrefreshed and validated before
InitData()andFamilyUserCurrentEncode().refresh access token:/initialize user data:/query default family:),FamilyUserCurrentEncode透传ctx。Errors now carry a stage prefix, and
FamilyUserCurrentEncodepropagatesctx.setTokens+op.MustSaveDriverStorage,便于测试注入。The refresh callback is split into
setTokensplus the storage save so testscan inject it.
wopan-sdk-go:v0.2.0/v0.2.2 的SetUA仍然只写字段(
client.SetUserAgent才写 resty header),升级不能修掉 403,反而引入psToken等 API 变更,因此保持在 v0.1.5 并在驱动侧修正。No SDK bump:
SetUAin v0.2.0/v0.2.2 still only stores the value (onlyclient.SetUserAgentwrites the resty header), so upgrading would not fix the403 and would drag in unrelated API changes (
psToken, ...)./ 此 PR 包含破坏性变更。
/ 此 PR 修改了公开 API、配置、存储格式或迁移行为。
/ 此 PR 需要关联仓库同步修改。
Related repository PRs / 关联仓库 PR:
Related Issues / 关联 Issue
Relates to #3218(同一个
403 Forbidden+crypto/aes: invalid key size 0现象,该 issue 因模板勾选问题被机器人关闭,但日志症状与本 PR 修复的两条路径完全一致)
Relates to #3218 (same
403 Forbidden+crypto/aes: invalid key size 0symptoms; the issue was auto-closed by the bot, but its log shows exactly the two
failure paths fixed here).
参考实现 / Reference: AlistGo/alist#9674
(
fix(wopan): initialize tokens and set dispatcher user agent).Root Cause / 根因分析
1. dispatcher 请求缺少浏览器 UA → HTTP 403
wopan-sdk-go@v0.1.5内部:于是任一 dispatcher 请求都没有 UA,resty v2.17.2 在
middleware.go:179补上库自身的 UA:联通云盘边缘对这串 UA 直接返回 403,驱动侧只看到
request failed with status: 403 Forbidden(res.IsError()分支,甚至进不到业务错误码判断)。
2. 先请求后刷新 →
crypto/aes: invalid key size 0SDK 所有 wohome 请求体都用
accessToken[:16]加密,且Crypto.SetAccessToken在len(token) < 16时返回ErrInvalidAccessToken(驱动层没检查这个返回值),
accessKey保持 nil:仅配 refresh token(旧
meta.go里refresh_token是required:"true",而 access token 是可选项)时必然命中。
现象对照 / Symptom → Cause
| 日志现象 / Log | 根因 / Cause |
| --- | --- |
|
failed get objs: failed to list objs: request failed with status: 403 Forbidden| 缺少浏览器 UA(缺陷 1) ||
failed init storage: crypto/aes: invalid key size 0| access token 为空/过短,先请求后刷新(缺陷 2) || 上传能成功、列目录 403 | upload2C 显式写
User-Agent: w.ua|| 两个 token 都填好仍然 403 | UA 与 token 无关,两个 token 都填只是绕过了缺陷 2 |
Testing / 测试
<!--
Describe commands, platforms, and manual checks.
If not tested, explain why.
说明执行过的命令、测试平台和手动验证。
如果未测试,请说明原因。
-->
go test ./drivers/wopan/ -count=1(改动仅限该包,仓库其余包未涉及,未全量执行)go test ./...Not verified against a real account (no token available); a real-account
run is still recommended.
Automated checks run / 已执行的自动化检查:
| Command / 命令 | Result / 结果 |
| --- | --- |
|
go build ./...| OK ||
go vet ./drivers/wopan/| OK ||
go test ./drivers/wopan/ -count=1| PASS(6 子用例) ||
gofmt -l| 无差异(仅 CRLF 行尾在 Windows 检出下被标记,与本次改动无关) |New tests / 新增测试 (
drivers/wopan/driver_test.go):TestInitClientCredentials:表驱动覆盖 6 种凭据组合(仅 refresh / 短 access + refresh / 双 token / 仅 access /
双空 / 仅短 access),断言:
- 每个 dispatcher 请求的
User-Agent必须等于wopan.DefaultUA;- 需要刷新时
AppRefreshToken必须是第一个请求;- 刷新后
AccessToken/RefreshToken回写正确;- 凭据不合法时不发任何请求并返回错误;
-
defaultFamilyID来自FamilyUserCurrentEncode(42)。403 Forbidden,因此能精确复现线上报错。回归验证 / Negative check:临时注释掉
d.client.SetUserAgent(wopan.DefaultUA)后重新运行,测试按预期失败,报错与用户反馈完全一致:
恢复该行后全部通过,说明测试确实锁住了 UA 回归。
Manual verification steps / 手动验证步骤(建议在合并前执行一次):
1. 用短信登录拿到
refresh_token(或同时提供access_token),只填refresh_token添加联通云盘存储。2. 期望:存储初始化成功、无
crypto/aes: invalid key size 0、根目录可列出。3. 期望:
AccessToken被刷新后回写进存储配置(重启后仍然可用)。4. 上传/下载各验证一次,确认 UA 修复没有影响 upload2C(该路径原本就带 UA)。
Checklist / 检查清单
/ 我已阅读
CONTRIBUTING。/ 我确认此贡献符合仓库许可证、贡献规范和行为准则。
gofmt,go fmt, orprettierwhere applicable./ 我已按适用情况使用
gofmt格式化变更代码。/ 我已在适用情况下请求相关维护者或代码所有者审查。
AI Disclosure / AI 使用声明
/ 此 PR 包含 AI 辅助内容。
Tools used / 使用工具:
Usage scope / 使用范围:
/ 我已审核并验证此 PR 中的所有 AI 辅助内容。
Co-Authored-Byattribution./ 我已确保所有 AI 辅助提交都包含
Co-Authored-By归属信息。/ 我可以在没有任何 AI 工具的情况下重现此 PR 中包含的内容。