Skip to content

fix(lark): oncall talk 权限按接收 bot 隔离 - #91

Merged
deepcoldy merged 1 commit into
deepcoldy:masterfrom
xiongz-c:codex/scope-oncall-talk-access
Jun 2, 2026
Merged

deepcoldy merged 1 commit into
deepcoldy:masterfrom
xiongz-c:codex/scope-oncall-talk-access

Conversation

@xiongz-c

@xiongz-c xiongz-c commented Jun 2, 2026 •

Copy link
Copy Markdown
Collaborator

背景 / 现象

botmux 支持在同一部署里运行多个 Lark bot。每个 bot 都有自己的 allowedUsers、chatGrants / globalGrants 和 oncallChats,因此 /oncall bind 的 talk 放行也应该按 bot 隔离。

当前 evaluateTalk() 的 oncall 判断用了 isChatOncallBoundForAnyBot(chatId):只要同部署里任意一个 bot 绑定了某个 oncall 群,当前接收消息的 bot 就会把这个群视为 oncall 并放开 talk。

这会导致多 bot 部署下的权限串扰:

  • Bot A 绑定了 oc_xxx 作为 oncall 群
  • Bot B 没有绑定 oc_xxx,并且有自己的 allowedUsers / grants 限制
  • 外部用户在 oc_xxx 里 @ Bot B 时,Bot B 也会因为 Bot A 的 oncall 绑定而通过 canTalk

结果是 Bot A 的 oncall 配置放开了 sibling Bot B 的 talk 权限,绕过了 Bot B 自己的 allowedUsers、chatGrants、globalGrants。

根因

isChatOncallBoundForAnyBot() / findOncallChatForAnyBot() 是跨 bot discovery:它会查当前 bot,也会从共享 bots.json 里发现 sibling bot 的 oncall 绑定。

这个跨 bot lookup 适合用于部署级发现/继承场景,例如 pinned working dir、default-oncall 之类;但把它放进 talk 授权闸门后,就把 per-bot 的 /oncall bind 语义扩大成了 deployment-wide talk 放行。

同样的问题也存在于 foreign bot @mention 的 chat-scope fast path:外部 bot 只要命中任意 sibling 的 oncall 绑定,就能跳过接收侧的 peer / grant vetting。

修复

  • src/im/lark/event-dispatcher.ts 改为 import findOncallChat
  • evaluateTalk(larkAppId, chatId, senderOpenId) 中,oncall talk gate 改成 findOncallChat(larkAppId, chatId)
  • foreign bot @mention 的 chat-scope gate 同步改成按接收 bot 查 findOncallChat(larkAppId, chatId)
  • 更新相邻注释,明确 oncall talk access 是 bot-scoped:一个 bot 的 /oncall bind 不会放开 sibling bot;如果 sibling bot 也要开放,需要自己绑定同一个 chat
  • 保留 findOncallChatForAnyBot() / isChatOncallBoundForAnyBot(),因为其他路径仍需要跨 bot oncall discovery

回归覆盖

新增/更新测试覆盖这个权限边界:

  • 注册两个 bot,只有 Bot A 绑定 oc_shared_oncall
  • 外部用户在该 chat 中 canTalk(A) === true
  • 同一个外部用户对未绑定该 chat 的 Bot B:canTalk(B) === false
  • Bot B 的 allowed user 仍然 canTalk(B) === true
  • event dispatcher 的 sibling oncall 场景不再触发 Bot B 的 routing

验证

  • pnpm vitest run test/grant-gates.test.ts test/event-dispatcher.test.ts:87 tests 通过
  • pnpm vitest run test/event-dispatcher.test.ts test/grant-gates.test.ts test/bot-registry.test.ts:123 tests 通过
  • pnpm build:通过

影响范围

只收紧 oncall talk 授权和 foreign bot chat-scope gate 的接收侧判断。

跨 bot oncall discovery 函数仍保留,其他需要 deployment-wide discovery 的代码路径不受影响。

@xiongz-c xiongz-c changed the title Scope oncall talk access to the receiving bot fix(lark): oncall talk 权限按接收 bot 隔离 Jun 2, 2026
@deepcoldy

Copy link
Copy Markdown
Owner

感谢 @xiongz-c 的修复 🙏

这个 PR 堵了一个真实的跨 bot 权限泄漏:之前任一 bot 把群绑成 oncall,同群里的 sibling bot 也会跟着对全员开放 talk,等于一个 bot 的 oncall 配置泄漏成了另一个 bot 的 talk 权限。

Review 结论 clean、无阻断。已实测验证:

  • pnpm build 干净;targeted 123 + 路由/oncall 整簇 436 测试全绿
  • 新增的 grant-gates 用例是真集成测试(不 mock bot-registry)
  • 做了变异测试:还原成 master 旧写法该用例立刻 fail(sibling bot 的 canTalk 返回 true=漏),换回本 PR 写法通过 —— 确认这个洞在 master 上真实存在、且本 PR 真正堵上了

已审核通过,合并到 master。

@deepcoldy deepcoldy left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed in an isolated worktree + mutation-tested the fix. Clean, no blockers. 详见 PR 评论。

@deepcoldy
deepcoldy marked this pull request as ready for review June 2, 2026 12:13
@deepcoldy
deepcoldy merged commit 63b60c2 into deepcoldy:master Jun 2, 2026
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.

2 participants