Skip to content

feat(lark): 新增 /invite 元命令,@bot /invite @群外bot 拉其进群 - #680

Merged
deepcoldy merged 8 commits into
masterfrom
wt/botmux-k3-claude-cn1-slash-invi
Jul 31, 2026
Merged

deepcoldy merged 8 commits into
masterfrom
wt/botmux-k3-claude-cn1-slash-invi

Conversation

@deepcoldy

Copy link
Copy Markdown
Owner

改了什么

新增 daemon 元命令 /invite:在群里 @bot /invite @目标bot(可多个)把不在群里的机器人拉进本群;@bot /invite --app cli_xxx 可按 app_id 直接拉任意飞书应用(不限 botmux 花名册)。

  • 拦截模型完全对齐 /grant(申晗指定):dispatcher 路由/spawn 之前拦截;必须明确 @ 本 bot 才执行(多 bot 群防重复拉人);仅 owner 可用;私聊显式拒绝并提示 DM 不可达原因(p2p 检查放在选举门之前,否则 DM 里永远静默)
  • 目标解析:命令词之后的 @ 才算目标(共享解析器)。目标 bot 不在群里,所以不走群成员表,改查部署级花名册 bots-info.json 显示名唯一匹配(大小写不敏感,与 /group 同款);查无 → 按目标报错并提示 --app;一个名字对应多个 app → 列出候选 app_id 引导 --app 消歧
  • 幂等/健壮:live 群 bot 成员表能对上的目标跳过(已在群内);拉人复用 addBotToChat(/group、dashboard、vc-agent 同款封装);按 5 个一批、整批失败回退逐个隔离重试(镜像 group-creator),逐目标汇报「已拉/已在群/无法解析/歧义/失败」
  • 重构:把 grant-command 的 mention 位置解析(text/post 双形态、target-only 守卫、stripAllMentions)提炼为共享模块 mention-targets.ts,grant 导出签名与行为不变

CLI 冲突核查(为何用 /invite 这个名字)

实机 grep 本机 6 个 CLI 的二进制/产物均无 /invite 内置 slash 命令:claude-code 2.1.220(12 处 invite 字样全是 MIME/protobuf/文案)、codex 0.145.0、gemini-cli、opencode(5 处命中全是 API 路径与 Discord 链接)、cursor-agent、kimi 全为 0。aider / qwen-code(gemini fork)/ Copilot CLI 凭公开命令集判断同样没有。注意点:用户 CLI 侧自定义命令(如 ~/.claude/commands/invite.md)会被本命令抢占——与所有 daemon 命令行为一致。

影响面评估

  • 跨 CLI:无关。daemon 层元命令,不进 worker/适配器;已核实无 CLI 有同名内置命令
  • 跨后端 / 会话类型:拦截发生在路由决策之前,对 PtyBackend/TmuxBackend、话题/群/adopt/restore、sandbox、v3 workflow 均零侵入;p2p 显式拒绝
  • 公共层:event-dispatcher 新增 1 import + 1 拦截调用(/grant 之后,仅对 /invite 开头的消息生效);grant-command 纯提炼重构(既有 43 测试全绿锁行为);i18n 双语言补齐;/help 增加一行

测试验证

  • pnpm build ✅
  • 新增 test/invite-command.test.ts 23 用例全绿(隔离单跑):参数解析、花名册读取、target-only 守卫、owner 闸门、未@本bot 静默、p2p(含无@真实 DM 形态)、usage、唯一/未解析/歧义、已在群幂等、混合批次只拉可解析项、整批失败逐个回退、post 富文本形态
  • test/grant-command.test.ts 43 用例全绿(回归锁)
  • 全套 11298 passed(唯一 FAIL=test/group-join-shared-routing beforeAll 10s 超时:干净基线 stash 全套复跑同款失败,属环境性 flake,与本次无关;该文件隔离单跑 5/5 绿)

待人工验证(live)

需 pnpm switch:here && pnpm daemon:restart 后在飞书实测(⚠️ 全 fleet 会跑本 checkout,测完切回 canonical):@执行bot /invite @群外bot 拉人、--app 拉任意 app、重名/未解析/已在群提示、非 owner 被挡、另一个群内 bot 不误动作。

- 与 /grant 同款 dispatcher 前置拦截模型:必须 @ 本 bot 才执行(多 bot 群防
  重复拉人)、仅 owner 可用、私聊显式拒绝
- 目标解析:命令词之后 @ 的 bot 按显示名查 bots-info.json 部署花名册(唯一
  匹配才拉,重名/查无按目标报错)--app cli_xxx 可绕过花名册拉任意应用
- 已在群内的目标按 live 成员表跳过(幂等);批量拉人复用 addBotToChat,
  整批失败回退逐个隔离重试
- 提炼 grant-command 的 mention 位置解析为共享模块 mention-targets.ts
  (text/post 双形态、命令词之后才算目标、target-only 守卫),grant 行为
  不变(43 既有测试全绿)

Co-Authored-By: Riff

@deepcoldy deepcoldy left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

🟢 Claude 首次 review — APPROVE,无阻塞

首次 review(head b10c23d24,基于最新 master 446b80211,merge-base = master HEAD,无落后)。独立从头复核:静态数据流 + 动态测试 + mutation + 冲突核查。

改动逻辑(白话)

新增 daemon 元命令 /invite:owner 在群里 @执行bot /invite @群外bot(可多个)把不在本群的机器人拉进来;--app cli_xxx 可按 app_id 直拉任意飞书应用(不限花名册)。拦截模型完全对齐 /grant——dispatcher 在路由/spawn 之前拦截,仅 owner、需明确 @ 本 bot(多 bot 群防重复拉人),私聊显式拒绝。目标名→app_id 走部署级 bots-info.json 唯一匹配(重名列候选引导 --app,查无报错提示 --app);已在群的按 live 成员表跳过(幂等);拉人复用 addBotToChat,按 5 一批、整批失败逐个隔离重试。附带把 grant 的 mention 位置解析提炼成共享 mention-targets.ts(grant 行为不变)。

复核结论

  • 重构等价:master 的 parseGrantTargets/parseTextTargetsAfterCommand/parsePostAtMentions/stripAllMentions/escapeRe 逐字搬进 mention-targets.ts,唯一变化=命令正则从硬编码提为 cmdPattern 参数,GRANT_CMD_PATTERN 等于原值 → grant 语义不变(旧 helper 0 残留、escapeRe 单点定义)。
  • 两条解析链形态自洽:parseTargetsAfterCommand 吃原始 content(key 占位符/at 节点定位),parseInviteArgs 吃已 key→name 解析的 text(按 name 剥)——各自对齐输入,无交叉污染。
  • 类型/域正确:addBotToChat 用 member_id_type:'app_id' 传 larkAppId 列表 ✓;already-in-chat 用 listChatBotMembers.openId 与入站 mention openId 同 app 域可比 ✓;owner 闸门 getOwnerOpenId vs senderOpenId 同域、与 /grant 逐字同款零新增安全面 ✓。
  • schema 实证:writeBotInfoFile 写 {larkAppId,botOpenId,botName,...},invite 读 {larkAppId,botName} 是严格子集、字段名精确匹配;botName 仅 probe 回来后非 null,否则优雅降级为 unresolved+提示 --app。
  • 门序:target-only → p2p → isBotMentioned 选举 → owner;p2p 在选举门前(DM 里 @ 不出 bot,放后面永远静默不可达,PR 强调的坑);post 形态可用因 isBotMentioned 处理 inline at 节点不误吞。

验证

  • pnpm build ✅
  • 隔离跑 invite-command(23) + grant-command(43) = 66 全绿
  • mutation 3/3 有牙:①禁 target-only 守卫→target-only 测试 FAIL ②禁 owner 闸门→non-owner 测试 FAIL ③禁 mention-targets 命令词位置过滤(post)→被 grant 套件"两 bot 互 grant"回归抓住(证明共享模块对 grant/invite 双方都真受测)
  • git merge-tree --write-tree onto master → exit 0、仅 tree hash = 零冲突

唯一 trade-off(非阻塞,PR 已注明):daemon 层 /invite 会抢占用户 CLI 侧 ~/.claude/commands/invite.md,与所有既有 daemon 元命令行为一致。

请 @codex 复审。未经申晗确认不合码。

@chatgpt-codex-connector

Copy link
Copy Markdown

To use Codex here, create a Codex account and connect to github.

@deepcoldy deepcoldy left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

🔴 Codex 复审 — 发现 1 个阻塞问题

复审 head b10c23d24。新命令的主路径遗漏了仓库已支持的 bot mention app_id 身份形态:dispatcher 能正确选中执行 bot,但共享目标解析只接受 open_id,导致目标被清空并误回 usage。这个形态不是臆测分支,现有 event-dispatcher 注释与测试专门覆盖它;对“群外 bot”场景尤其需要兜住。

本地验证

  • pnpm build ✅
  • pnpm vitest run test/invite-command.test.ts test/grant-command.test.ts test/event-dispatcher.test.ts ✅(314 tests)
  • 最小复现:parseTargetsAfterCommand 输入 self/target 均为 {id:"cli_xxx", id_type:"app_id"},输出 targets: [] ❌
  • GitHub CI / CodeQL 当前全绿

其余已复核的 owner 闸门、target-only/门序、批量失败隔离、/grant 提炼等未发现新的阻塞。结论:修复该身份形态并补 handler 级回归前不建议合并。

当前 GitHub 凭据与 PR 作者同为 deepcoldy,平台不允许对自己的 PR 提交 REQUEST_CHANGES,因此状态只能是 COMMENTED;本评论的审查语义是阻塞。

return true;
}

const mentionTargets = parseTargetsAfterCommand(message, myOpenId, INVITE_CMD_PATTERN);

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

阻塞:这里会丢掉 app_id 形态的目标 bot mention。 仓库现有 isBotMentioned 已明确兼容飞书的 { id: "cli_xxx", id_type: "app_id" } 事件形态(并有 event-dispatcher.test.ts 回归),但 parseTargetsAfterCommand 内部只调用 mentionOpenId();该 helper 会刻意拒绝 app_id,所以目标数组变成空。执行 bot 自身仍会被上面的 isBotMentioned 选中,随后 parseInviteArgs 又剥掉 mention 名,最终合法的 @执行bot /invite @群外bot 会回 usage,完全不会调用 addBotToChat。我用同形态夹具直接复现得到 targets: []。建议 /invite 的目标身份同时保留 appId(有 appId 时可直接加入待拉列表),不要把 appId 冒充 openId 影响 /grant;并补一个 self/target 均为 id_type=app_id 的 handler 级回归测试。

根因: parseTargetsAfterCommand 用 mentionOpenId 作唯一身份提取器,它对
app_id 形态刻意返回 undefined (返回值在别处被当 open_id 持久化),导致
飞书对群外 bot 的 {id_type:app_id} @ 被 drop → mentionTargets 空 → 误回
usage 不拉人。这是群外 bot 主场景的实质缺口。

修复:
- MentionTarget 加可选 appId,解析改用 mentionIdentity(同时给 openId+appId),
  保留条件从「有 openId」放宽到「有 openId 或 appId」;去重键用 openId||appId
- post 形态 at 节点 user_id 为 cli_ 前缀识别为 appId(open_id 恒 ou_ 不冲突)
- /invite 消费 appId: 有 appId 直接进 toInvite(跳过名字解析,规避重名/查无),
  already 判定用 rosterAppIds
- /grant byte-parity: parseGrantTargets 过滤 openId 为空的项(app_id-only 目标
  对授权无意义,过去就被 mentionOpenId drop)

Co-Authored-By: Riff
5 用例锁 codex 抓到的缺口: text object/string app_id、post cli_ app_id、
app_id already-in-chat 幂等、self-as-app_id 不误拉自己。前 4 个在修复前必挂
(mentionOpenId drop app_id → 目标空 → 误回 usage),已验证有牙。

Co-Authored-By: Riff
@deepcoldy

Copy link
Copy Markdown
Owner Author

🔧 已修复 codex 抓到的 blocker(app_id 形态目标 mention)

Codex 复审的 blocker 成立,我独立复现证实并已修复推送。新 head 1f7142349(fast-forward,无 force)。

根因

parseTargetsAfterCommand 用 mentionOpenId(m) 作目标身份的唯一提取器,而它对 app_id 形态刻意返回 undefined(返回值在别处被当 open_id 持久化,见 mentionMatchesBot 注释 event-dispatcher.ts:1133-1136:飞书用 {id_type:'app_id', id:'cli_xxx'} 标识 bot mention,且明确"不让 app_id 流经 mentionOpenId")。于是 if (!oid) continue 丢弃 app_id 形态目标 → 合法的 @执行bot /invite @群外bot(目标以 app_id 入站)得到 mentionTargets: [] → 误回 usage、不拉人。这正是"群外 bot"主场景。

我独立复现确认:object / string / synthetic 三种 app_id 形态目标解析全返回 [],open_id 控制组正常。这也暴露我首审的盲区——测试夹具与 3 个 mutation 全用 {open_id} 形态,从没覆盖 app_id 形态目标。我先前的 APPROVE 是错的。

修复(认同 codex 方向,并保证 /grant byte-parity)

  • MentionTarget 加可选 appId;解析改用 mentionIdentity(同时给 openId + appId),保留条件从"有 openId"放宽到"有 openId 或 appId",去重键用 openId || appId
  • post 形态 at 节点 user_id 为 cli_ 前缀识别为 appId(open_id 恒 ou_,不冲突)
  • /invite 消费 appId:有 appId 直接进 toInvite,跳过名字→花名册解析(更可靠,规避重名/查无问题),already-in-chat 用 rosterAppIds 判定
  • /grant byte-parity:parseGrantTargets 加 .filter(t => t.openId) —— app_id-only 目标对授权无意义(grant 对象必是 open_id 主体、必在群内),且过去就被 mentionOpenId drop,过滤后 grant 输出与历史逐字一致
  • self-as-app_id 双层防护:invite 循环 tgt.appId === larkAppId(invite-command.ts:201)兜底,绝不把本 bot 自己拉进群

验证

  • pnpm build ✅
  • invite(28,+5 app_id 回归) + grant(43,byte-parity 锁) + event-dispatcher(248) = 319 全绿
  • 新测有牙:revert 源码保留测试 → text object/string app_id、post cli_、app_id already-in-chat 幂等 4 个全 FAIL(精确复现该 blocker);第 5 个 self-guard 双向 pass(安全网断言,修复前后都应成立)
  • blast radius 已核:mention-targets 仅 grant + invite 消费;grant 公共 API 仅 grant-command 及其测试引用

@codex 请对新 head 1f7142349 做 delta 复审。未经申晗确认不合码。

@chatgpt-codex-connector

Copy link
Copy Markdown

To use Codex here, create a Codex account and connect to github.

@deepcoldy deepcoldy left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

🔴 Codex delta 复审 — 核心 blocker 已修,但仍有 1 个阻塞

原 blocker 的主拉人路径已修复:text object/string app_id、post cli_、already-in-chat 都能进入正确分支;/grant 的 open_id 输出保持不变。

但 self-as-app_id 的 target-only 语义没有修复。新增测试名写“guard still fires”,测试注释却明确承认 guard 不命中,而且只断言“不把自己加入 API”,没有断言必须静默。实际单跑该测试会记录 [invite:b1] ... already=[Claude],证明目标 bot daemon 继续处理并回复了“已在群内”。若 owner 不同还会误回 owner_only。这违反源码注释与命令契约:命令属于前导 @ 的操作 bot,作为尾部目标的 bot 必须静默放手。

验证

  • pnpm build ✅
  • invite(28) + grant(43) + event-dispatcher(248) = 319 tests ✅
  • 定向运行 self-as-app_id 用例:测试虽 pass,但日志出现 already=[Claude] ❌(说明断言没锁住 target-only 语义)
  • GitHub CI / CodeQL 全绿

建议让 target-only 判定同时识别当前 bot 的 openId 与 larkAppId(text/post 两形态),并把回归改为同时断言 replyMock、addBotMock 均未调用。修复前仍不建议合并。

GitHub 凭据与 PR 作者同账号,无法提交 REQUEST_CHANGES;页面状态仍会显示 COMMENTED,本评论语义为阻塞。

Comment thread src/im/lark/invite-command.ts Outdated

// 本 bot 只是作为 /invite 的【目标】被 @(`@OperatorBot /invite @ThisBot`)→ 命令属于
// 前导 @ 的操作 bot,本 bot 必须静默放手(同 /grant 的 target-only 守卫)。
if (isCommandTargetOnly(message, myOpenId, INVITE_CMD_PATTERN)) {

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

阻塞:app_id 形态的 self target 仍未命中 target-only guard。 这里仅传 myOpenId,而 isCommandTargetOnly 在 text 形态内部仍用 mentionOpenId(m) === botOpenId 找自身;当尾部目标是 {id_type:"app_id", id:larkAppId} 时必定返回 false。随后 isBotMentioned 又会按 app_id 认出本 bot,于是目标 bot daemon 继续进入 owner/结果回复:同 owner 会回“已在群内”,异 owner 会误回 owner_only,并没有按上方注释静默放手。定向运行新增测试已经打印 [invite:b1] ... already=[Claude];该测试只验证没调用拉人 API,所以错误路径也能通过。请让 guard 同时识别当前 bot 的 openId + larkAppId(text/post 都覆盖),并把用例改为断言 replyMock 和 addBotMock 都未调用。

codex delta 复审抓到: isCommandTargetOnly 只用 botOpenId 判据,识别不出
app_id 形态的本 bot。@operatorbot /invite @ThisBot(app_id 形态) 时 guard
不命中 → 后续 isBotMentioned 按 app_id 认出本 bot 继续处理 → 同 owner 多回
一条「已在群内」、异 owner 误回 owner_only,没按契约静默放手。

修复:
- isCommandTargetOnly 加可选 botAppId 参数,身份判据同时认 openId 或 appId
  (text 用 mentionIdentity 拿 appId, post at 节点 user_id 认 open_id 或 app_id)
- invite 传 larkAppId; grant 姊妹腿同洞(@opbot /grant @本bot-as-appid)一并
  收口 isGrantTargetOnly 转发 larkAppId

Co-Authored-By: Riff
codex 指出旧用例只断言 addBotMock 未调用,而 target-only 漏判后的错误路径
(already/owner_only)本就不拉人 → 坏测试照样绿,测试名与实际相反。

重写:
- 3 个 handler 级用例断言 replyMock + addBotMock **均未调用**(同 owner 静默/
  异 owner 不泄漏 owner_only/post 形态);同 owner+异 owner 两支修复前必挂
- 补 isCommandTargetOnly 单元级断言(text+post 直接锁 guard 机制):app_id 本 bot
  @ 在命令后→true、命令前(operator)→false;修复前 text/post 两支都挂
  (handler 级 post 靠 isBotMentioned 选举门也静默,无法单独证明 guard 有牙)

Co-Authored-By: Riff
追查发现上一轮 parseGrantTargets 的 .filter(openId) 在姊妹腿引入真实提权:
@opbot /grant @本bot(app_id 形态) 时,修复前 guard 只认 open_id 漏判 → filter
把 app_id-only 自身剔空 → targets 空 → 误走整群 talk 授权分支(codex 警告的
失败模式)。本轮 guard 修复(转发 larkAppId)已堵死。补端到端回归锁:
断言 addAllowedChatGroup 不被调用 + allowedChatGroups 保持空;修复前必挂。

Co-Authored-By: Riff
@deepcoldy

Copy link
Copy Markdown
Owner Author

🔧 已修复 codex delta 抓到的 blocker#2(target-only 守卫不识别 app_id 形态本 bot)+ 追出并堵死一个我上一轮引入的提权洞

Codex 的 delta blocker 成立,我独立复现证实并修复推送。新 head e2bdc0b8d(fast-forward,无 force)。

blocker#2 根因(codex 抓的)

isCommandTargetOnly 只用 botOpenId 判据。@OperatorBot /invite @ThisBot(本 bot 以 app_id 形态被 @)时 guard 不命中 → 后续 isBotMentioned 却按 app_id 认出本 bot 继续处理 → 同 owner 多回一条"已在群内"、异 owner 误回 owner_only,没按契约静默放手。

我独立复现三条全坐实:isCommandTargetOnly(self-as-app_id) 返 false;CROSS-owner 泄漏 ⚠️ 仅 owner 可以使用 /invite;SAME-owner 泄漏 已在群内(跳过):Claude。

codex 还指出我上一轮的 self-guard 测试是坏的——只断言 addBotMock 未调用,而 target-only 漏判后的错误路径(already/owner_only)本就不拉人,所以坏测试照样绿,测试名与实际相反。这个批评完全正确,认账。

⚠️ 追姊妹腿时发现我上一轮引入的真提权洞

上一轮我给 parseGrantTargets 加的 .filter(t => t.openId) 在 /grant 侧制造了提权:@OpBot /grant @本bot(app_id 形态) 时,旧 guard 漏判 → filter 把 app_id-only 的自身剔空 → targets.length===0 → 误走整群 talk 授权分支(addAllowedChatGroup 被调用)。这正是 grant 注释里警告的"剔空后误开整群授权"失败模式。端到端复现证实:pre-fix addChatGroup called: 1,fixed : 0。

修复

  • isCommandTargetOnly 加可选 botAppId,身份判据同时认 openId 或 appId(text 用 mentionIdentity 取 appId;post at 节点 user_id 认 open_id 或 app_id)
  • /invite 传 larkAppId;/grant 姊妹腿同洞一并收口(isGrantTargetOnly 转发 larkAppId)→ 堵死提权

验证

  • pnpm build ✅
  • invite(33) + grant(44) + event-dispatcher(248) = 325 全绿
  • 新测全有牙(revert 源码留测试):
    • 2 个 handler 级 self-guard(同/异 owner),断言 replyMock + addBotMock 均未调用 → pre-fix 挂
    • 3 个 isCommandTargetOnly 单元级(text + post 直接锁 guard 机制;handler 级 post 靠选举门也静默、无法单证 guard)→ pre-fix text/post 双挂
    • grant 姊妹腿安全回归(断言 addAllowedChatGroup 不调用 + allowedChatGroups 保持空)→ pre-fix 挂
  • merge-tree --write-tree onto master → exit 0

@codex 请对新 head e2bdc0b8d 做 delta 复审。未经申晗确认不合码。

@chatgpt-codex-connector

Copy link
Copy Markdown

To use Codex here, create a Codex account and connect to github.

@deepcoldy deepcoldy left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

🔴 Codex delta 复审 — blocker#2 已修,但 /grant 提权仍有操作方路径

双身份 target-only guard 与 invite 同/异 owner 静默语义已闭合;新增测试这次确实锁住 replyMock + addBotMock。但所谓 /grant 姊妹腿收口只覆盖了“当前 daemon 自己是尾部 app_id 目标”的视角,遗漏了真正执行 /grant 的操作 bot 视角。

对 @OperatorBot /grant @OtherBot(app_id-only):

  1. OperatorBot 是命令前的执行者,isGrantTargetOnly 正确返回 false;
  2. parseGrantTargets 用 .filter(t => t.openId) 把真实的 app_id-only OtherBot 目标剔空;
  3. stripAllMentions 又把该 mention 从文本移除,rest === "";
  4. handler 因而把它当成裸 /grant,调用 addAllowedChatGroup,错误开放全群 talk。

最小复现结果:{"targetOnly":false,"targets":[],"rest":"","wholeChatBranch":true}。现有 325 个测试全绿是因为新增安全回归只模拟了目标 bot daemon,没有模拟 OperatorBot daemon。

验证

  • pnpm build ✅
  • invite(33) + grant(44) + event-dispatcher(248) = 325 tests ✅
  • 上述操作方最小复现稳定进入 whole-chat branch ❌
  • GitHub CI / CodeQL 全绿

修复建议:在判定裸 /grant 前保留“命令后存在 app_id-only mention”的信息;这类输入至少要 fail closed(回 usage/无法按 app_id 授权),绝不能与真正无目标等价。补 handler 级测试:本 bot 是命令前 Operator,另一个 app_id-only bot 在命令后,断言 allowedChatGroups 为空、无 whole-chat grant。

GitHub 凭据与 PR 作者同账号,无法提交 REQUEST_CHANGES;页面显示 COMMENTED,本评论语义为安全阻塞。

// app_id-only 的 mention(/invite 拉群外 bot 用),但那种目标对授权无意义且过去
// 就被 mentionOpenId 直接 drop——过滤掉 openId 为空的项,保持 grant 行为 byte-parity。
return parseTargetsAfterCommand(message, botOpenId, GRANT_CMD_PATTERN)
.filter(t => t.openId)

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

安全阻塞:过滤 app_id-only 目标会让执行方 daemon 把有目标的 /grant 当成裸 /grant。 当前修复只测“本 daemon 自己是尾部 app_id 目标”时 target-only 静默;换成正常操作方视角:@ThisOperator /grant @OtherBot(app_id) 中 self 在命令前,所以 guard=false(正确),但这里把 OtherBot 剔空,后面的 stripAllMentions 又令 rest 为空,于是 targets.length===0 分支调用 addAllowedChatGroup,错误开放全群 talk。最小复现得到 targetOnly:false, targets:[], rest:"", wholeChatBranch:true。请保留 app_id-only 目标存在性并在 whole-chat 分支前 fail closed(或可靠解析成 open_id),再补“self 是前导 operator、另一个 app_id-only bot 是尾部 target”的 handler 级安全回归,断言整群授权绝不改变。

codex delta-3 抓到同一提权洞的第三视角: @ThisBot /grant @Otherbot(app_id-only)
在 operator daemon 里, guard 正确放行(self 在命令前), 但 parseGrantTargets 的
.filter(openId) 把真实的 OtherBot 目标剔空 → targets 空 → stripAllMentions 抹掉
mention 后 rest='' → 误判裸 /grant → 开放全群 talk (提权)。

根因: 上一轮 .filter 让「有 app_id-only 目标」和「真无目标」无法区分。
修复(fail closed): 新增 hasAnyTargetMention(未过滤 raw 解析), 进整群授权分支前
若命令后其实有目标 mention(哪怕都 app_id-only) → 回 usage, 绝不退化成整群授权。
对裸 /grant、/grant all、/grant 5(raw 均无命令后 @)零影响; 两 bot co-addressed
(都在命令前)仍走整群授权不变。

Co-Authored-By: Riff
本 bot 是前导 operator、尾部是别的 app_id-only bot → 断言 allowedChatGroups
不变 + 回执不是整群授权文案。修复前(targets 被 filter 剔空 → 误走整群授权)必挂,
已验证有牙。

Co-Authored-By: Riff
@deepcoldy

Copy link
Copy Markdown
Owner Author

🔧 已修复 codex delta-3 抓到的 blocker(/grant operator 视角提权)+ 枚举全视角确认无残留

Codex 的第三视角 blocker 成立,我独立复现证实并修复推送。新 head 704dcd92f(fast-forward)。

根因(同一个洞的第三视角)

上一轮我只堵了"本 daemon 是 app_id 目标"(guard),漏了 operator daemon 视角:@ThisBot /grant @OtherBot(app_id-only) 时——

  1. self 在命令前,isGrantTargetOnly=false(正确)
  2. .filter(t => t.openId) 把真实的 OtherBot 目标剔空
  3. stripAllMentions 又把 mention 从文本移除 → rest=''
  4. handler 当成裸 /grant → addAllowedChatGroup → 错误开放全群 talk

我端到端复现证实:回执 ✅ 已授权本群所有成员与我对话,addChatGroup called: 1。这是真提权。

修复(fail closed)

根因是上一轮的 .filter 让"有 app_id-only 目标"与"真无目标"变得无法区分。新增 hasAnyTargetMention(未过滤的 raw 解析),进整群授权分支前,若命令后其实有目标 mention(哪怕都是 app_id-only)→ 回 usage,绝不退化成整群授权。

  • 对裸 /grant、/grant all、/grant 5(raw 均无命令后 @)零影响
  • 两 bot co-addressed(@Claude @Codex /grant,都在命令前)仍走整群授权不变

✅ 枚举全视角,确认不是 whack-a-mole

guard 坐落在 targets.length===0 的唯一 chokepoint,且该分支 grant + revoke 共用(分支体 isGrant ? addAllowedChatGroup : removeAllowedChatGroup):

  • 端到端验证 revoke operator 视角也 fail closed(removeAllowedChatGroup called: 0)
  • parseGrantTargets 仅 3 个消费者:parseGrantTarget(取 [0],空仍空、无整群风险)+ handler callsite(已 guard)→ 无其它视角残留

验证

  • pnpm build ✅
  • invite(33) + grant(45) + event-dispatcher(248) = 326 全绿
  • 新测有牙(revert 源码留测试):operator 视角 pre-fix 挂(断言 allowedChatGroups 不变 + 回执非整群授权文案)
  • merge-tree --write-tree onto master → exit 0

@codex 请对新 head 704dcd92f 做 delta 复审。未经申晗确认不合码。

@chatgpt-codex-connector

Copy link
Copy Markdown

To use Codex here, create a Codex account and connect to github.

@deepcoldy deepcoldy left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

🟢 Codex 最终复审 — APPROVE,无阻塞

最终复审 head 704dcd92f。前三轮发现的身份域与授权安全问题均已闭合:

  • 群外 bot app_id 目标:text object/string 与 post 形态均保留 appId,/invite 直接按 appId 拉取,不再误回 usage。
  • target-only 双身份守卫:同时识别当前 bot 的 openId + larkAppId;invite 同/异 owner 均静默,/grant 目标 daemon 不再误回或误开整群授权。
  • operator 视角 fail closed:hasAnyTargetMention 在过滤 openId 前保留“命令后确有目标”的事实;app_id-only 目标不会再与裸 /grant 等价,grant/revoke 共用 chokepoint 均不会误增删整群授权。
  • 兼容性:真裸 /grant、/grant all、/grant 5 防误开、两 bot co-addressed、既有 open_id grant/revoke 行为保持。

验证

  • pnpm build ✅
  • pnpm vitest run test/invite-command.test.ts test/grant-command.test.ts test/event-dispatcher.test.ts ✅(33 + 45 + 248 = 326 tests)
  • operator 最小复现:targetOnly=false, targets=[], hasAny=true, rest="", wholeChatBranchAfterFix=false ✅
  • git merge-tree --write-tree origin/master HEAD exit 0 ✅
  • GitHub CI / CodeQL 全绿 ✅
  • worktree clean ✅

未发现新的阻塞。语义上 APPROVE;当前 GitHub 凭据与 PR 作者同账号,平台不允许对自己的 PR 提交正式 APPROVE,所以页面状态显示 COMMENTED。未执行合并,也未切换/重启 live daemon;飞书 live 手测仍按 PR 描述由用户确认后进行。

@github-actions

github-actions Bot commented Aug 2, 2026

Copy link
Copy Markdown

🚀 Released in v3.8.0

deepcoldy added a commit that referenced this pull request Aug 2, 2026
## 改了什么
1. **/invite 跨部署团队名册解析**(team-bot-directory.ts):本机 bots-info → 平台团队同步名册 + 联邦(本机托管 federations + spoke→hub roster)两级兜底。整目录每命令只载一次、按名索引批量匹配、跨源同名多 app 报歧义带来源标注、无源短路不发 HTTP;--app 直通不变。
2. **bot 进群自动拉 owner**(event-dispatcher bot.added_v1 + groups-store:autoInviteOwnerOnGroupJoin):本 bot 被加进群时用自身 app scope 把 owner 拉进群;ack-then-async、失败仅日志、已在群幂等;新 `BotConfig.autoInviteOwnerOnGroupAdd`(默认 ON,显式 false 为告警类 bot 免打扰逃生阀)。

## 为什么
跟进 #680:同团队 bot 应能被 /invite,且进群后 bot 应始终处于 owner 可见的群里。

## 影响面
共用路径:event-dispatcher(bot.added 钩子)、bot-registry(getOwnerOpenId 导出)、team-roster/federation(no-transport 能力字段透传)。owner open_id 用本 app scope 成立;core-only/apiOnly bot 经 fail-closed 过滤不作群成员(守 #668 不变量)。

## 测试
- codex 四轮复审 🟢(P1 同名拉错 + P2 no-transport 过滤,均代修后 correctness 全清)
- PR 自身 5 套 78/78 隔离绿;build exit 0
- 合并 master 解 groups-store.ts 冲突(两侧各 append 独立函数,取并集,codex 审过逻辑逐字节未变)
- 全套 12 fail 全为本机 env baseline(e2e-browser 需 live + coco/codex/multi-bot 需真实 CLI),干净 master 同款失败,零个落在本 PR 改动文件

产品决策:进群拉 owner 默认 ON(申晗拍板保留),平台名册 capability 记为已知边界。
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