[Bug] 管理员指令组误匹配同前缀消息,导致普通聊天和其他指令被拦截
问题描述
给指令组设置管理员权限后,非管理员发送的普通消息,只要以组名开头,也会收到「权限不足」并停止处理
例如:
- 指令组叫
天气,私聊发送天气真好啊会被拦下,消息无法进入 LLM 处理 - 指令组叫
math,另一个插件的公开指令/mathematics也会被拦下,无法执行
在 master ac4df99 上启动 AstrBot,使用 aiocqhttp(OneBot v11)实际重现了这两种情况
原因是 [CommandGroupFilter.startswith()](https://github.com/AstrBotDevs/AstrBot/blob/ac4df996f6ff217838293628272b573f3c45d4f6/astrbot/core/star/filter/command_group.py#L111-L112) 直接使用 str.startswith,没有判断组名后面是否为空白或消息结尾。因此 mathematics、math123 都会匹配 math 指令组,并进入该组的权限检查
预期只有完整组名,或组名后接空白和子指令的消息,才匹配该指令组。天气真好啊 和 /mathematics 应继续正常处理,真正的子指令 /math add 1 2 仍应受到管理员权限限制
如何复现?
- 在插件中注册一个管理员指令组:
@filter.command_group("天气")
@filter.permission_type(filter.PermissionType.ADMIN)
def weather(self):
pass
@weather.command("设置")
async def weather_set(self, event: AstrMessageEvent, city: str):
yield event.plain_result(f"已设置城市:{city}")
- 使用不在管理员列表里的账号私聊机器人,发送
天气真好啊 - 机器人回复「您(ID: xxx)的权限不足以使用此指令。通过 /sid 获取 ID 并请管理员添加。」,消息没有进入 LLM 处理
另一个复现方式是注册管理员指令组 math,再由另一个插件注册公开指令 mathematics。非管理员在群里发送 /mathematics,同样会被 math 指令组拦下
测试时仅启用了两个演示插件,用附件中的 OneBot v11 客户端脚本代替 NapCat 发送消息。两个插件、客户端脚本和完整日志见附件 A5_repro.zip
附件还包含不加载第三方插件、直接调用真实过滤器和 WakingCheckStage 的最小脚本,可以单独验证本体的匹配问题
AstrBot 版本
v4.29.0-beta.1(master,commit ac4df99)
操作系统
Windows
部署方式
源码部署(uv sync && uv run main.py)
使用的消息平台适配器
aiocqhttp(OneBot v11)
错误日志
[10:59:55.228] [Core] [INFO] [core.event_bus:74]: [default] [napcat(aiocqhttp)] member/10001: 天气真好啊
[10:59:55.228] [Core] [DBUG] [waking_check.stage:172]: enabled_plugins_name: ['*']
[10:59:55.229] [Core] [INFO] [waking_check.stage:223]: User ID 10001 lacks permission to trigger astrbot_plugin_admin_demo.
[10:59:55.229] [Core] [DBUG] [pipeline.scheduler:77]: Stage WakingCheckStage stopped event propagation.
[10:59:59.231] [Core] [INFO] [core.event_bus:74]: [default] [napcat(aiocqhttp)] member/10001: /mathematics
[10:59:59.231] [Core] [DBUG] [waking_check.stage:172]: enabled_plugins_name: ['*']
[10:59:59.232] [Core] [INFO] [waking_check.stage:223]: User ID 10001 lacks permission to trigger astrbot_plugin_admin_demo.
[10:59:59.233] [Core] [DBUG] [pipeline.scheduler:77]: Stage WakingCheckStage stopped event propagation.
辅助信息
检查清单
这个我来做,PR 马上到。
机制核实无误:CommandGroupFilter.startswith() 是裸的 str.startswith,而叶子 CommandFilter(command.py:202)有边界判断(startswith(f"{full_cmd} ") 或全等)。修复对齐叶子的语义:完整组名后必须是空白或消息结尾,别名和嵌套组路径同规则;空白字符覆盖 tab/全角空格,与叶子 \s+ 归一化保持一致,不会把 math\tadd 这种调用挡在组门外。
@niansia @he-yufeng 现在有 #10372, #10379 两个拉取请求,可能会有重复,请考虑一下使用哪个方案。
@mantoujun12 谢谢提醒。
两边顶层组的修法一致(组名后必须是空白或消息结尾)。区别在嵌套组:#10372 和叶子 CommandFilter 一样先做 \s+ 归一化,tool config set、tool\tconfig set 也能匹配到 tool config 组;#10379 只检查组名后面一个字符,嵌套路径中间的多余空白不归一化,这两种写法匹配不上。equals() 在 #10372 里也一起归一化了(Sourcery 指出的问题,ccb8ab6),只发 tool config 时能正常给出子指令提示。用哪个由维护者决定。