Skip to content

fix(auth): 委派路径用派单里已校验的 union_id 向平台取凭据 - #2376

Merged
deepcoldy merged 1 commit into
masterfrom
fix/delegated-platform-union-id
Oct 26, 2026
Merged

deepcoldy merged 1 commit into
masterfrom
fix/delegated-platform-union-id

Conversation

@deepcoldy

Copy link
Copy Markdown
Owner

背景

主 agent 把任务派给子 agent(签名派单 botmux dispatch,或委派建的定时任务)时,子 agent 那一轮按派单里的发起人取 bytedcli / lark-cli 凭据。凭据来源选「中心化平台」(credentialSource: platform,或 auto 且本机已绑定平台)时,这条委派路径一直拿不到平台凭据。

起点是 #2171 的提交 06580c3(作者已同意交给维护者处理,本 PR 带上其署名)。按维护者的要求,本 PR 不再走「union_id → 接收方 open_id → 通讯录换回 union_id」这一圈,而是直接用派单里已经校验过的 union_id。

根因

  • 委派身份里有两个 open_id:credentialOpenId 是发起人在发起方 bot 的 app 里的 open_id,targetOpenId 是同一个人在接收方 bot 的 app 里的 open_id。open_id 按 app 划分,只在签发它的 app 里有效。
  • publishDelegated 向平台要凭据时,用的是接收方 bot 的 app 去解析 credentialOpenId 的 union_id。这在接收方 app 里查不到,平台查询返回「不可用」:
    • platform 模式下委派轮次一律被拒;
    • auto 模式只能退回本机登录态(要求这个人恰好在本机经发起方 bot 登录过)。
  • 改用 targetOpenId 反查也不可靠:接收方 bot 的通讯录范围通常不覆盖这个人,通讯录接口不可用时同样失败。
  • 签名派单记录(dispatch-user-delegations.json,HMAC 签名,按 messageId 精确匹配)本来就带着发起人的 union_id,而且 daemon 已经校验过;委派定时任务的 authority 记录里也有 controlUnionId,每次运行前由 authorizeDelegatedScheduleRun 在本 app 里重新解析校验。

改了什么

  • src/core/turn-cli-identity.ts:DelegatedCliIdentity 新增必填的 unionId(派单记录里已校验的值)。publishDelegated 向平台要凭据时直接用它,不做任何 open_id 查询。平台没给凭据时:platform 模式直接显式拒绝,不读本机任何凭据;auto 模式先看本机已有的登录(仍按发起方 open_id 取),也没有就显式拒绝。顺带更正平台查询上方「keyed by the issuer-scoped open_id」的旧注释。
  • src/core/platform-credential-source.ts:resolvePlatformIdentity 的入参改成二选一:普通轮次传发送者 openId(照旧查 union_id);委派路径传已校验的 unionId(原样使用,形状不对就当作不可用,不发给平台)。
  • src/core/lark-tool-platform-credential.ts + src/core/dashboard-ipc-server.ts:绑定会话里 lark-cli --as user 的委派路径,向平台要凭据时同样直接用派单的 unionId。
  • src/daemon.ts:构造委派身份的两处补上 unionId:
    • 签名派单取 delegation.authority.unionId;
    • 委派定时任务取 authority.controlUnionId(缺失时拒绝,与已有的 credentialOpenId 缺失处理一致)。
  • 测试:更正 test/turn-cli-identity-platform.test.ts 里「The platform key must come from the former」的旧注释。另补用例:
    • bytedcli 和 lark-cli 经平台授权的委派;
    • 通讯录接口不可用时委派仍拿到平台凭据(覆盖身份发布层、绑定 lark-cli 路由、daemon 的签名派单和委派定时任务四条入口);
    • platform 模式不碰本机;
    • auto 模式本机兜底和最终显式拒绝;
    • 形状不对的 union_id 不发给平台。

影响面评估

  • 只影响委派轮次(签名派单 + 委派定时任务)。普通真人轮次仍按发送者 open_id 查 union_id,代码路径不变。
  • local 模式:不咨询平台,行为不变。
  • auto 模式:以前平台这一步必然失败、落到本机;现在平台能给就用平台凭据(与普通轮次同口径),给不了仍退回本机已有登录。
  • platform 模式:委派轮次以前一律被拒,现在能拿到平台凭据。
  • 没开 triggerUserAuth 的 bot:不发布身份、不写派单委派记录,不受影响。
  • 身份来源:没有扩大。人是谁仍然只来自 daemon 校验过的签名派单记录或 schedule authority,CLI 和模型输入都影响不到。

验证

bun run build                      # 通过
bunx tsc --noEmit -p .             # 0 错误
bunx vitest run --project unit \
  test/turn-cli-identity-platform.test.ts test/turn-cli-identity.test.ts test/lark-tool-platform-credential.test.ts \
  test/ipc-lark-tool-identity.test.ts test/ipc-auth-request.test.ts test/identity-storage-model.test.ts \
  test/schedule-delegated-runtime.test.ts test/schedule-delegation-route.test.ts test/dispatch-user-route.test.ts \
  test/dispatch-user-delegation.test.ts test/test-hygiene.test.ts
# 11 个文件 361 个用例全绿;改动的 4 个测试文件在 bun test 下也全绿

变异(每处单独改回 bug,跑相关测试):

# 变异 结果
T1 委派平台查询改回用发起方 open_id 红 6
T2 改用接收方 open_id 反查(#2171 原做法) 红 5(通讯录不可用 / 不查通讯录用例)
T3 平台层忽略传入的 union_id 红 7
T4 平台层不校验 union_id 形状 红 1
T5 daemon 签名派单不带 union_id 红 1
T6 委派定时任务不带 union_id 红 1
T7 绑定 lark-cli 路由不传 union_id 红 1
T8 platform 模式平台不给时退回本机 红 1
T9 绑定凭据层忽略 union_id 红 2

🤖 Generated with Claude Code

委派轮次(签名派单、委派定时任务)向中心化平台要凭据时,原先拿发起方 app 视角的
open_id(credentialOpenId)在接收方 app 里反查 union_id。open_id 按 app 划分,接收方查不到,
platform 模式下委派轮次一律被拒,auto 模式只能退回本机登录态。

- DelegatedCliIdentity 带上派单记录里已校验的 unionId(委派定时任务取 authority 的
  controlUnionId),resolvePlatformIdentity 接受直接传入的 unionId,不再查通讯录;
  普通轮次照旧按发送者 open_id 查,本机凭据仍按发起方 open_id 取。
- 绑定会话的 lark-cli --as user 委派路径同样直接用这个 unionId。
- publishDelegated 平台没给凭据时:platform 模式直接拒绝、不查本机;auto 模式本机也没有
  时显式拒绝。
- 起点是 #2171 的 06580c3。

Co-authored-by: s425557423 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
@deepcoldy

Copy link
Copy Markdown
Owner Author

已合并 → de4a7ea(squash,保留原作者 Co-authored-by)。合后核验:在审过的 head 1e74a61 上合入,subject 带 (#2376),落地改动与审过的 patch-id 一致,master CI 已开跑。未发版、未部署(需另行指令)。follow-up:#2378(@ 同机 Bot 交接带发起人身份)与本 PR 改了同一处委派身份构造,会基于合入后的 master 同步。

@github-actions

Copy link
Copy Markdown

🚀 Released in v3.53.0

deepcoldy pushed a commit to s425557423/botmux that referenced this pull request Oct 28, 2026
…oldy#2376 取

turnId 修复已由 deepcoldy#2365 合入,auth 修复已由 deepcoldy#2376 合入;两处冲突取 master 实现,
合并后相对 master 只剩 botGrants。daemon.ts 中 master 新增的 until-done `done`
自管理动作与 bot-policy 话题收尾两者都保留。

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant