Skip to content

Commit 61453e9

Browse files
author
Martin Vogel
committed
test(hooks): guard PreToolUse gate shim against predictable temp file (#384)
The symlink-attack vector (predictable /tmp/cbm-code-discovery-gate-$PPID) was removed in c29e6d5 when the blocking bash gate became a stateless compiled augmenter writing to stdout only. Add a regression guard so a predictable temp/state file can never be reintroduced: assert the emitted gate shim contains no /tmp path and no $PPID, and delegates to hook-augment. Export cbm_install_hook_gate_script (was static) so the security property is directly testable.
1 parent f88d031 commit 61453e9

3 files changed

Lines changed: 33 additions & 1 deletion

File tree

‎src/cli/cli.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1746,7 +1746,7 @@ int cbm_remove_claude_hooks(const char *settings_path) {
17461746
* a missing/old/hung binary results in a silent exit 0 (issue #362/#288).
17471747
* The legacy filename `cbm-code-discovery-gate` is retained so existing
17481748
* settings.json entries and uninstall keep working with zero migration. */
1749-
static void cbm_install_hook_gate_script(const char *home, const char *binary_path) {
1749+
void cbm_install_hook_gate_script(const char *home, const char *binary_path) {
17501750
if (!home || !binary_path) {
17511751
return;
17521752
}

‎src/cli/cli.h‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,12 @@ int cbm_upsert_claude_hooks(const char *settings_path);
175175
* Returns 0 on success. */
176176
int cbm_remove_claude_hooks(const char *settings_path);
177177

178+
/* Write the PreToolUse gate shim to <home>/.claude/hooks/. The shim is a thin
179+
* wrapper that invokes the compiled `hook-augment` and writes to stdout only —
180+
* it must never create a predictable temp/state file (issue #384). Exposed for
181+
* testing that security property. */
182+
void cbm_install_hook_gate_script(const char *home, const char *binary_path);
183+
178184
/* Upsert a BeforeTool hook in ~/.gemini/settings.json for Gemini CLI / Antigravity.
179185
* Returns 0 on success. */
180186
int cbm_upsert_gemini_hooks(const char *settings_path);

‎tests/test_cli.c‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1988,6 +1988,31 @@ TEST(cli_upsert_claude_hook_fresh) {
19881988
PASS();
19891989
}
19901990

1991+
/* issue #384: the PreToolUse gate shim must never use a predictable /tmp
1992+
* filename (the old `/tmp/cbm-code-discovery-gate-$PPID` was a symlink-attack
1993+
* vector). The shim is now a stateless wrapper around the compiled augmenter. */
1994+
TEST(cli_hook_gate_script_no_predictable_tmp_issue384) {
1995+
char tmpdir[256];
1996+
snprintf(tmpdir, sizeof(tmpdir), "/tmp/cli-gate-XXXXXX");
1997+
if (!cbm_mkdtemp(tmpdir))
1998+
SKIP("cbm_mkdtemp failed");
1999+
2000+
cbm_install_hook_gate_script(tmpdir, "/usr/local/bin/codebase-memory-mcp");
2001+
2002+
char script_path[512];
2003+
snprintf(script_path, sizeof(script_path), "%s/.claude/hooks/cbm-code-discovery-gate", tmpdir);
2004+
const char *data = read_test_file(script_path);
2005+
ASSERT_NOT_NULL(data);
2006+
/* No predictable temp/state file and no PPID-derived path. */
2007+
ASSERT(strstr(data, "/tmp") == NULL);
2008+
ASSERT(strstr(data, "PPID") == NULL);
2009+
/* It delegates to the stateless compiled augmenter (stdout only). */
2010+
ASSERT(strstr(data, "hook-augment") != NULL);
2011+
2012+
test_rmdir_r(tmpdir);
2013+
PASS();
2014+
}
2015+
19912016
TEST(cli_upsert_claude_hook_existing) {
19922017
char tmpdir[256];
19932018
snprintf(tmpdir, sizeof(tmpdir), "/tmp/cli-hook-XXXXXX");
@@ -2548,6 +2573,7 @@ SUITE(cli) {
25482573
RUN_TEST(cli_agent_instructions_content);
25492574

25502575
/* Claude Code hooks (5 tests — group D) */
2576+
RUN_TEST(cli_hook_gate_script_no_predictable_tmp_issue384);
25512577
RUN_TEST(cli_upsert_claude_hook_fresh);
25522578
RUN_TEST(cli_upsert_claude_hook_existing);
25532579
RUN_TEST(cli_upsert_claude_hook_replace);

0 commit comments

Comments
 (0)