Repository navigation
[bug] Claude hook matcher 大小写敏感,Bash 选不中 bash,安全 hook 静默失效 — 附可 cherry-pick 修复 #582
Replies: 7 comments 2 replies
|
我是外面看这条的开发者,看了 Claude 侧 hooks 常用 字面量改成大小写不敏感的精确匹配、正则保持敏感、 我不会把它说成默认安全洞:matcher 本来就写 |
|
谢谢,严重度按你说的写。 字面量 load 时对「配置了却零命中」给诊断,另开,不塞进这一条。 |
|
Reproduced the reasoning from master: the Claude literal path is a case-sensitive |
|
Claude 的 hooks.json 写 Bash/Read,这边注册 bash/read。字面量走 split('|').includes(query),对不上就 continue,从 Claude 拷来的 PreToolUse 静默不开火。字面量改成大小写不敏感的精确匹配、Bash 仍不配 BashOutput/dash、正则保持敏感,这个边界合理。 两处没修、而且不该假装修了: ^Bash$ 仍走正则、仍大小写敏感,迁过来的正则 matcher 照样零命中。 |
|
遇到同样问题(从 Claude 迁过来的 hooks.json 里 PreToolUse 写 matcher: bash也可以写正则 验证方式:挂载 hooks 后手动触发一次 bash 工具调用,在 hook 命令里加一行日志(比如 等官方把字面量匹配改成大小写不敏感后,大写 Bash 也能直接用;在那之前全部用小写最省事。 English: Until the literal matcher is fixed upstream, write Claude-style hook matchers in lowercase (e.g. bash), or use a lowercase regex like ^bash$; verify by triggering the tool and checking the hook actually ran, since a mismatch fails silently. |
|
补充修正:上一条回复发布时反引号和引号被工具吞掉了,直接照抄会把 hooks.json 写坏。实际配置是带引号的 JSON 字面量,例如: {
"matcher": "bash"
}正则写法同理,也要写成字符串: English: The previous reply lost its backticks and quotes in transit, so don't copy it as-is. The matcher must be a quoted JSON string ( |
|
Implemented this on The matcher-subject broadening. @zylos-luna and @Xilink-Lin both flagged that the fold applies to every Claude literal matcher subject, not only tool names, and asked for someone to check the
Every non-empty subject is lowercase, and no subject set holds two values differing only in case. A case-insensitive literal therefore cannot become ambiguous. It can only select the one value it was already going to select. You are right that it is a semantic broadening; there is no collision in the current sets for it to expose. If a subject set ever gains a case-variant pair, that is when this needs revisiting, and the This is a prerequisite for #1801. #1801 asks the Claude bridge to emit canonical I have not touched the two gaps already identified here and am not claiming them closed. Unofficial branch if it is useful to compare against. Same boundary as nokkies/dsh-upstream-patches@upstream-base...fix/hook-matcher-case-and-timeout-fail-open 中文摘要: 已在 其一,@zylos-luna 与 @Xilink-Lin 指出「大小写折叠作用于所有 Claude 字面量 matcher 主体,不只是工具名」。这一点属实,而且可以穷举:两个 bridge 里 其二,本条是 #1801 的前置条件。#1801 希望
|
Uh oh!
There was an error while loading. Please reload this page.
摘要 / Summary
Claude Code 的
hooks.json用 PascalCase 工具名(Bash、Read)。DeepSeek Harness 注册的是小写(bash、read)。Claude 字面量 matcher 做精确大小写匹配,对不上就 静默跳过,不报错。从 Claude 拷来的PreToolUse安全 hook 永远不会开火,工具照跑。Migrating a Claude Code
matcher: "Bash"hook to this harness silently disables it.复现 / Repro
hooks-claude-code,hooks.json里PreToolUse写"matcher": "Bash",hook 返回 deny。bash工具。Bash选中bash,deny 生效。根因 / Root cause
packages/hooks/hook-protocol/src/matcher.ts:query是exec.name(bash),pattern 是Bash。includes大小写敏感。不匹配时只continue,没有诊断。这和「allowlist glob 写错 → 工具集静默变空」是同一类坑:配置看起来生效,运行时零命中。
修复 / Fix (cherry-pick ready)
27791eb90d—fix(hook-protocol): match Claude literal tool names case-insensitivelyClaude 字面量改为大小写不敏感的精确匹配(
Bash→bash)。Bash仍不匹配BashOutput。正则路径(^Bash$)保持大小写敏感。验收 / Acceptance
pnpm exec vitest run packages/hooks/hook-protocol/tests/matcher.spec.ts通过。matchesMatcher('Bash', 'bash', 'claude-code') === true,且仍不匹配BashOutput/dash。All reactions