Skip to content

Local code intelligence for developers and AI agents. Connect code, docs and infrastructure in a persistent graph, with CBM Lens, MCP tools, agent integrations and change-aware test selection.

fix(cli): make Windows Claude hooks shell portable

Martin Vogela537d3bd8439
diff --git a/src/cli/cli.c b/src/cli/cli.c
index aa73de6f1..bf0887aca 100644
--- a/src/cli/cli.c
+++ b/src/cli/cli.c
@@ -1388,15 +1388,76 @@ static void cbm_vibe_config_dir(const char *home_dir, char *out, size_t out_sz)
}
}
+static bool cbm_hook_script_name_safe(const char *script_name) {
+ if (!script_name || !script_name[0]) {
+ return false;
+ }
+ for (const unsigned char *cursor = (const unsigned char *)script_name; *cursor; cursor++) {
+ bool safe = (*cursor >= 'a' && *cursor <= 'z') || (*cursor >= 'A' && *cursor <= 'Z') ||
+ (*cursor >= '0' && *cursor <= '9') || *cursor == '-' || *cursor == '_' ||
+ *cursor == '.';
+ if (!safe) {
+ return false;
+ }
+ }
+ return true;
+}
+
/* Build the hook command string written into Claude Code's settings.json.
- * Honors $CLAUDE_CONFIG_DIR. When CLAUDE_CONFIG_DIR is unset, preserves the
- * portable $HOME form. Values from CLAUDE_CONFIG_DIR are quoted as one literal
- * shell word so whitespace and metacharacters cannot alter the command. */
-static int cbm_resolve_hook_command(const char *script_name, char *out, size_t out_sz) {
- if (!script_name || !script_name[0] || !out || out_sz == 0U) {
+ * POSIX embeds a safely quoted absolute custom config path or a portable $HOME
+ * path. Windows explicitly invokes cmd.exe so the hook also works when Claude
+ * falls back from Git Bash to PowerShell. The Windows command defers expansion
+ * of custom paths to cmd.exe and disables delayed expansion, so user path bytes
+ * never become source text in either outer shell. */
+static int cbm_build_claude_hook_command(const char *script_name, const char *config_dir,
+ bool windows, char *out, size_t out_sz) {
+ if (!cbm_hook_script_name_safe(script_name) || !out || out_sz == 0U) {
return CLI_ERR;
}
out[0] = '\0';
+ if (windows) {
+ const char *base =
+ config_dir && config_dir[0] ? "%CLAUDE_CONFIG_DIR%" : "%USERPROFILE%\\.claude";
+ int written = snprintf(out, out_sz, "cmd.exe /d /v:off /s /c '\"\"%s\\hooks\\%s\"\"'", base,
+ script_name);
+ return written > 0 && (size_t)written < out_sz ? CLI_OK : CLI_ERR;
+ }
+ if (config_dir && config_dir[0]) {
+ char path[CLI_BUF_1K];
+ int written = snprintf(path, sizeof(path), "%s/hooks/%s", config_dir, script_name);
+ return written > 0 && (size_t)written < sizeof(path)
+ ? cbm_shell_quote_word(path, out, out_sz)
+ : CLI_ERR;
+ }
+ int written = snprintf(out, out_sz, "\"$HOME/.claude/hooks/%s\"", script_name);
+ return written > 0 && (size_t)written < out_sz ? CLI_OK : CLI_ERR;
+}
+
+static int cbm_resolve_hook_command(const char *script_name, char *out, size_t out_sz) {
+ char env_buf[CLI_BUF_1K];
+ const char *env = cbm_safe_getenv("CLAUDE_CONFIG_DIR", env_buf, sizeof(env_buf), NULL);
+#ifdef _WIN32
+ return cbm_build_claude_hook_command(script_name, env, true, out, out_sz);
+#else
+ return cbm_build_claude_hook_command(script_name, env, false, out, out_sz);
+#endif
+}
+
+#ifdef CBM_CLI_ENABLE_TEST_API
+int cbm_resolve_claude_hook_command_for_testing(const char *script_name, bool windows,
+ char *command, size_t command_size) {
+ char env_buf[CLI_BUF_1K];
+ const char *env = cbm_safe_getenv("CLAUDE_CONFIG_DIR", env_buf, sizeof(env_buf), NULL);
+ return cbm_build_claude_hook_command(script_name, env, windows, command, command_size);
+}
+#endif
+
+/* Resolve the exact shell-quoted command form shipped immediately before the
+ * explicit Windows cmd.exe wrapper. It remains an ownership identity only. */
+static int cbm_resolve_previous_hook_command(const char *script_name, char *out, size_t out_sz) {
+ if (!cbm_hook_script_name_safe(script_name) || !out || out_sz == 0U) {
+ return CLI_ERR;
+ }
char env_buf[CLI_BUF_1K];
const char *env = cbm_safe_getenv("CLAUDE_CONFIG_DIR", env_buf, sizeof(env_buf), NULL);
if (env && env[0]) {
@@ -3413,13 +3474,16 @@ static int cbm_remove_hermes_context_hook(const char *config_path, const char *b
int cbm_upsert_claude_hooks(const char *settings_path) {
char command[CLI_BUF_8K];
+ char previous_command[CLI_BUF_8K];
char released_command[CLI_BUF_8K];
if (cbm_resolve_hook_command(CMM_HOOK_GATE_SCRIPT, command, sizeof(command)) != CLI_OK ||
+ cbm_resolve_previous_hook_command(CMM_HOOK_GATE_SCRIPT, previous_command,
+ sizeof(previous_command)) != CLI_OK ||
cbm_resolve_released_hook_command(CMM_HOOK_GATE_SCRIPT, released_command,
sizeof(released_command)) != CLI_OK) {
return CLI_ERR;
}
- const char *const old_commands[] = {released_command, NULL};
+ const char *const old_commands[] = {released_command, previous_command, NULL};
int search_result = upsert_hooks_json((hooks_upsert_args_t){
.settings_path = settings_path,
.hook_event = "PreToolUse",
@@ -3444,13 +3508,16 @@ int cbm_upsert_claude_hooks(const char *settings_path) {
int cbm_remove_claude_hooks(const char *settings_path) {
char command[CLI_BUF_8K];
+ char previous_command[CLI_BUF_8K];
char released_command[CLI_BUF_8K];
if (cbm_resolve_hook_command(CMM_HOOK_GATE_SCRIPT, command, sizeof(command)) != CLI_OK ||
+ cbm_resolve_previous_hook_command(CMM_HOOK_GATE_SCRIPT, previous_command,
+ sizeof(previous_command)) != CLI_OK ||
cbm_resolve_released_hook_command(CMM_HOOK_GATE_SCRIPT, released_command,
sizeof(released_command)) != CLI_OK) {
return CLI_ERR;
}
- const char *const old_commands[] = {released_command, NULL};
+ const char *const old_commands[] = {released_command, previous_command, NULL};
int search_result = remove_hooks_json((hooks_remove_args_t){
.settings_path = settings_path,
.hook_event = "PreToolUse",
@@ -3961,13 +4028,16 @@ static bool cbm_install_session_reminder_script(const char *home, const char *bi
static int cbm_upsert_session_hooks(const char *settings_path) {
static const char *matchers[] = {"startup", "resume", "clear", "compact"};
char command[CLI_BUF_8K];
+ char previous_command[CLI_BUF_8K];
char released_command[CLI_BUF_8K];
if (cbm_resolve_hook_command(CMM_SESSION_REMINDER_SCRIPT, command, sizeof(command)) != CLI_OK ||
+ cbm_resolve_previous_hook_command(CMM_SESSION_REMINDER_SCRIPT, previous_command,
+ sizeof(previous_command)) != CLI_OK ||
cbm_resolve_released_hook_command(CMM_SESSION_REMINDER_SCRIPT, released_command,
sizeof(released_command)) != CLI_OK) {
return CLI_ERR;
}
- const char *const old_commands[] = {released_command, NULL};
+ const char *const old_commands[] = {released_command, previous_command, NULL};
int rc = 0;
for (int i = 0; i < NUM_DIRS; i++) {
if (upsert_hooks_json((hooks_upsert_args_t){.settings_path = settings_path,
@@ -3986,13 +4056,16 @@ static int cbm_upsert_session_hooks(const char *settings_path) {
static int cbm_remove_session_hooks(const char *settings_path) {
static const char *matchers[] = {"startup", "resume", "clear", "compact"};
char command[CLI_BUF_8K];
+ char previous_command[CLI_BUF_8K];
char released_command[CLI_BUF_8K];
if (cbm_resolve_hook_command(CMM_SESSION_REMINDER_SCRIPT, command, sizeof(command)) != CLI_OK ||
+ cbm_resolve_previous_hook_command(CMM_SESSION_REMINDER_SCRIPT, previous_command,
+ sizeof(previous_command)) != CLI_OK ||
cbm_resolve_released_hook_command(CMM_SESSION_REMINDER_SCRIPT, released_command,
sizeof(released_command)) != CLI_OK) {
return CLI_ERR;
}
- const char *const old_commands[] = {released_command, NULL};
+ const char *const old_commands[] = {released_command, previous_command, NULL};
int rc = 0;
for (int i = 0; i < NUM_DIRS; i++) {
if (remove_hooks_json((hooks_remove_args_t){.settings_path = settings_path,
@@ -4118,14 +4191,17 @@ static bool cbm_install_subagent_reminder_script(const char *home, const char *b
int cbm_upsert_claude_subagent_hooks(const char *settings_path) {
char command[CLI_BUF_8K];
+ char previous_command[CLI_BUF_8K];
char released_command[CLI_BUF_8K];
if (cbm_resolve_hook_command(CMM_SUBAGENT_REMINDER_SCRIPT, command, sizeof(command)) !=
CLI_OK ||
+ cbm_resolve_previous_hook_command(CMM_SUBAGENT_REMINDER_SCRIPT, previous_command,
+ sizeof(previous_command)) != CLI_OK ||
cbm_resolve_released_hook_command(CMM_SUBAGENT_REMINDER_SCRIPT, released_command,
sizeof(released_command)) != CLI_OK) {
return CLI_ERR;
}
- const char *const old_commands[] = {released_command, NULL};
+ const char *const old_commands[] = {released_command, previous_command, NULL};
/* matcher "*" is the natural choice a user would also pick for their own
* catch-all SubagentStart hook, so claim ownership by command too — never
* clobber or remove a foreign "*" entry. */
@@ -4140,14 +4216,17 @@ int cbm_upsert_claude_subagent_hooks(const char *settings_path) {
int cbm_remove_claude_subagent_hooks(const char *settings_path) {
char command[CLI_BUF_8K];
+ char previous_command[CLI_BUF_8K];
char released_command[CLI_BUF_8K];
if (cbm_resolve_hook_command(CMM_SUBAGENT_REMINDER_SCRIPT, command, sizeof(command)) !=
CLI_OK ||
+ cbm_resolve_previous_hook_command(CMM_SUBAGENT_REMINDER_SCRIPT, previous_command,
+ sizeof(previous_command)) != CLI_OK ||
cbm_resolve_released_hook_command(CMM_SUBAGENT_REMINDER_SCRIPT, released_command,
sizeof(released_command)) != CLI_OK) {
return CLI_ERR;
}
- const char *const old_commands[] = {released_command, NULL};
+ const char *const old_commands[] = {released_command, previous_command, NULL};
return remove_hooks_json((hooks_remove_args_t){.settings_path = settings_path,
.hook_event = "SubagentStart",
.matcher_str = "*",
diff --git a/src/cli/cli.h b/src/cli/cli.h
index 4e1db8d3b..de1ac04a2 100644
--- a/src/cli/cli.h
+++ b/src/cli/cli.h
@@ -186,6 +186,8 @@ int cbm_build_qwen_hook_command_for_testing(const char *binary_path, bool window
size_t command_size, char *shell, size_t shell_size);
int cbm_build_qoder_hook_command_for_testing(const char *binary_path, bool windows, char *command,
size_t command_size, char *shell, size_t shell_size);
+int cbm_resolve_claude_hook_command_for_testing(const char *script_name, bool windows,
+ char *command, size_t command_size);
bool cbm_optional_hook_supported_for_testing(const char *agent_name, bool windows);
void cbm_hook_sanitize_metadata_for_testing(const char *input, char *output, size_t output_size);
int cbm_upsert_qwen_lifecycle_hooks_for_testing(const char *settings_path, const char *binary_path,
diff --git a/tests/test_cli.c b/tests/test_cli.c
index 1c2da3297..6914f839a 100644
--- a/tests/test_cli.c
+++ b/tests/test_cli.c
@@ -8304,6 +8304,35 @@ TEST(cli_hook_scripts_platform_shape_issue929) {
PASS();
}
+/* Claude may execute shell-form hooks through PowerShell when Git Bash is not
+ * available. Windows registrations must therefore invoke the .cmd shim via an
+ * explicit command interpreter instead of evaluating a quoted path string. */
+TEST(cli_windows_claude_hook_command_is_shell_portable) {
+ char *saved_config = save_test_env("CLAUDE_CONFIG_DIR");
+ char command[1024];
+
+ cbm_unsetenv("CLAUDE_CONFIG_DIR");
+ ASSERT_EQ(cbm_resolve_claude_hook_command_for_testing("cbm-session-reminder.cmd", true, command,
+ sizeof(command)),
+ 0);
+ ASSERT_STR_EQ(command, "cmd.exe /d /v:off /s /c '\"\"%USERPROFILE%\\.claude\\hooks\\"
+ "cbm-session-reminder.cmd\"\"'");
+
+ cbm_setenv("CLAUDE_CONFIG_DIR", "C:\\Users\\A & B\\.claude!100%", 1);
+ ASSERT_EQ(cbm_resolve_claude_hook_command_for_testing("cbm-subagent-reminder.cmd", true,
+ command, sizeof(command)),
+ 0);
+ ASSERT_STR_EQ(command, "cmd.exe /d /v:off /s /c '\"\"%CLAUDE_CONFIG_DIR%\\hooks\\"
+ "cbm-subagent-reminder.cmd\"\"'");
+ ASSERT(strstr(command, "A & B") == NULL);
+ ASSERT_EQ(cbm_resolve_claude_hook_command_for_testing("../foreign.cmd", true, command,
+ sizeof(command)),
+ -1);
+
+ restore_test_env("CLAUDE_CONFIG_DIR", saved_config);
+ PASS();
+}
+
/* issue #618: hook-augment was a structural no-op on Windows because its path
* guards required POSIX-style '/'-prefixed absolute paths, so a drive-letter
* cwd (C:/repo) was rejected before any search_graph query. The predicate must
@@ -9300,6 +9329,7 @@ SUITE(cli) {
/* Claude Code hooks (5 tests — group D) */
RUN_TEST(cli_hook_gate_script_no_predictable_tmp_issue384);
RUN_TEST(cli_hook_scripts_platform_shape_issue929);
+ RUN_TEST(cli_windows_claude_hook_command_is_shell_portable);
RUN_TEST(cli_hook_augment_path_is_abs);
RUN_TEST(cli_hook_augment_deadline_breadcrumb_issue858);
RUN_TEST(cli_upsert_claude_hook_fresh);