Skip to content

Commit 7122298

Browse files
committed
fix(hook-augment): log under the cache dir, never at a cut-off path
Why: arming the hook-augment deadline opens its breadcrumb log with O_CREAT. The path came from raw getenv("HOME") as "%s/.cache/codebase-memory-mcp/logs" in 1 KiB buffers with no length check. A HOME near 1 KiB therefore created a file at the cut-off name (".../logs/hook-augme") on every hook call, and a longer one created a cut-off directory. The hook also ignored CBM_CACHE_DIR, which every other cbm component honours for its cache and logs. The index skip log had the same defect in its CBM_INDEX_LOG override: an override longer than 1 KiB was copied in unchecked and opened "wb", which created or truncated the file at its cut-off prefix and reported that name as the run's logfile. Fix: the breadcrumb log now resolves through cbm_resolve_cache_dir() (CBM_CACHE_DIR, else HOME, else USERPROFILE, plus the default) to <cache_dir>/logs/hook-augment-timeouts.log. Every snprintf into the 1 KiB buffers is checked, including the one for the CBM_HOOK_TIMEOUT_LOG override. A path that does not fit means no breadcrumb, and the logs directory is created only for a path that fits. The hook still exits 0 (it never fails the agent's hook call) and allocates nothing new. The CBM_INDEX_LOG override gets the same check: an override that does not fit writes no logfile and warns index.logfile_path_too_long, and the index still completes. With a normal HOME the log stays where it was. Proof (dev binary, synthetic roots, isolated CBM_RUNTIME_DIR, no-op Bash payload on stdin; "created" = find over the synthetic root minus the pre-made HOME/cache chains): - 980-byte HOME. Before: exit 0, plus three directories and a file at the cut-off name <HOME>/.cache/codebase-memory-mcp/logs/hook-augme (1023 bytes). After: exit 0, nothing created. - CBM_CACHE_DIR=<root>/cache. Before: the log landed under HOME. After: <root>/cache/logs/hook-augment-timeouts.log. - 1000-byte CBM_CACHE_DIR. After: exit 0, nothing created. - Normal HOME: <HOME>/.cache/codebase-memory-mcp/logs/ hook-augment-timeouts.log before and after. - 1096-byte CBM_INDEX_LOG via `cli index_repository`. Before: a file at the 1023-byte cut-off name, reported as the logfile. After: status indexed, skipped_count 1, no logfile, nothing written. Tests: RED 3x on the unfixed code for the reasons above, green with the fix, RED again with the fix reverted: - cli: cli_hook_augment_log_long_home_creates_no_cut_path, cli_hook_augment_log_honours_cache_dir, plus the positive control cli_hook_augment_log_default_location_unchanged (green before and after). - index_resilience: index_long_log_override_writes_no_cut_path. The fixtures sit under link-resolved temp roots: the kernel bounds the expanded path, so under macOS's /tmp -> /private/tmp link a near-1 KiB path can be neither created nor probed, and the bug stays hidden. Signed-off-by: Martin Vogel <martin.vogel.tech@gmail.com>
1 parent 1f9b4db commit 7122298

4 files changed

Lines changed: 395 additions & 9 deletions

File tree

‎src/cli/hook_augment.c‎

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,9 @@
5959
*
6060
* Observability (#858): a fired deadline is otherwise indistinguishable from
6161
* "no matches", so the handler first write()s a pre-formatted breadcrumb to
62-
* ~/.cache/codebase-memory-mcp/logs/hook-augment-timeouts.log (fd and message
63-
* prepared at arm time — only async-signal-safe write/_exit in the handler). */
62+
* <cache_dir>/logs/hook-augment-timeouts.log (by default
63+
* ~/.cache/codebase-memory-mcp/logs; fd and message prepared at arm time —
64+
* only async-signal-safe write/_exit in the handler). */
6465
#ifndef _WIN32
6566
#define HA_DEADLINE_DEFAULT_MS 2000 /* in-process budget; see ha_deadline_ms() */
6667
#define HA_DEADLINE_MIN_MS 50
@@ -103,20 +104,31 @@ static void ha_deadline_exit(int sig) {
103104
_exit(0);
104105
}
105106

107+
/* Whether snprintf wrote the whole string into a buffer of `cap` bytes. */
108+
static bool ha_fits(int written, size_t cap) {
109+
return written > 0 && (size_t)written < cap;
110+
}
111+
106112
static void ha_open_crumb_log(int deadline_ms) {
107113
const char *override = getenv("CBM_HOOK_TIMEOUT_LOG"); /* tests + power users */
108114
char path[CBM_SZ_1K];
115+
/* A path cut off at the buffer names some other file, and O_CREAT would
116+
* create it. Such a path means no breadcrumb; the hook itself runs on. */
109117
if (override && override[0]) {
110-
snprintf(path, sizeof(path), "%s", override);
111-
} else {
112-
const char *home = getenv("HOME");
113-
if (!home || !home[0]) {
118+
if (!ha_fits(snprintf(path, sizeof(path), "%s", override), sizeof(path))) {
114119
return;
115120
}
121+
} else {
122+
/* <cache_dir>/logs, where every cbm component logs: the cache dir is
123+
* CBM_CACHE_DIR, else HOME (then USERPROFILE) + the default. */
124+
const char *cache = cbm_resolve_cache_dir();
116125
char dir[CBM_SZ_1K];
117-
snprintf(dir, sizeof(dir), "%s/.cache/codebase-memory-mcp/logs", home);
126+
if (!cache || !ha_fits(snprintf(dir, sizeof(dir), "%s/logs", cache), sizeof(dir)) ||
127+
!ha_fits(snprintf(path, sizeof(path), "%s/hook-augment-timeouts.log", dir),
128+
sizeof(path))) {
129+
return;
130+
}
118131
cbm_mkdir_p_ex(dir, 0755, CBM_MKDIR_FOLLOW_OWNED);
119-
snprintf(path, sizeof(path), "%s/hook-augment-timeouts.log", dir);
120132
}
121133
g_ha_crumb_fd = open(path, O_WRONLY | O_CREAT | O_APPEND, 0644);
122134
if (g_ha_crumb_fd < 0) {

‎src/mcp/mcp.c‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10280,7 +10280,13 @@ static bool write_skip_logfile(const char *project, const cbm_file_error_t *errs
1028010280
char path[CBM_SZ_1K];
1028110281
const char *override = getenv("CBM_INDEX_LOG");
1028210282
if (override && override[0]) {
10283-
snprintf(path, sizeof(path), "%s", override);
10283+
/* An override that does not fit is cut off into another file's name,
10284+
* and "wb" would create or truncate that file: no logfile then. */
10285+
int written = snprintf(path, sizeof(path), "%s", override);
10286+
if (written <= 0 || (size_t)written >= sizeof(path)) {
10287+
cbm_log_warn("index.logfile_path_too_long", "source", "CBM_INDEX_LOG");
10288+
return false;
10289+
}
1028410290
} else {
1028510291
const char *cdir = cbm_resolve_cache_dir();
1028610292
if (!cdir) {

‎tests/test_cli.c‎

Lines changed: 257 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14604,6 +14604,260 @@ TEST(cli_hook_augment_deadline_breadcrumb_issue858) {
1460414604
#endif
1460514605
}
1460614606

14607+
/* ── hook-augment breadcrumb log location ──────────────────────────
14608+
* Arming the deadline opens the breadcrumb log with O_CREAT. Its path was
14609+
* "$HOME/.cache/codebase-memory-mcp/logs/hook-augment-timeouts.log" built in
14610+
* 1 KiB buffers with no length check, so a long HOME created a file at a
14611+
* cut-off name, and CBM_CACHE_DIR, which every other cbm log honours, was
14612+
* ignored. These run the real entry point in a child with a no-op Bash
14613+
* payload: the deadline is armed (the log opened), then the hook passes
14614+
* through with exit 0. POSIX only: the in-process deadline and its log are
14615+
* compiled out on Windows. */
14616+
#ifndef _WIN32
14617+
enum { HL_PATH_CAP = 1024, HL_SEGMENT = 200 };
14618+
14619+
/* Exit code of `hook-augment` run in a child with HOME = home and
14620+
* CBM_CACHE_DIR = cache_dir (unset when NULL), or -1 when it did not exit. */
14621+
static int hl_run_hook(const char *home, const char *cache_dir) {
14622+
static const char payload[] = "{\"hook_event_name\":\"PreToolUse\",\"tool_name\":\"Bash\","
14623+
"\"tool_input\":{\"command\":\"ls\"}}";
14624+
int fds[2];
14625+
if (pipe(fds) != 0) {
14626+
return -1;
14627+
}
14628+
/* Queued before the fork, so no write can meet a reader that has gone. */
14629+
ssize_t queued = write(fds[1], payload, sizeof(payload) - 1);
14630+
close(fds[1]);
14631+
if (queued != (ssize_t)(sizeof(payload) - 1)) {
14632+
close(fds[0]);
14633+
return -1;
14634+
}
14635+
fflush(NULL);
14636+
pid_t pid = fork();
14637+
if (pid == 0) {
14638+
dup2(fds[0], STDIN_FILENO);
14639+
close(fds[0]);
14640+
cbm_setenv("HOME", home, 1);
14641+
cbm_unsetenv("USERPROFILE");
14642+
cbm_unsetenv("CBM_HOOK_TIMEOUT_LOG");
14643+
if (cache_dir) {
14644+
cbm_setenv("CBM_CACHE_DIR", cache_dir, 1);
14645+
} else {
14646+
cbm_unsetenv("CBM_CACHE_DIR");
14647+
}
14648+
_exit(cbm_cmd_hook_augment(0, NULL));
14649+
}
14650+
close(fds[0]);
14651+
int status = 0;
14652+
if (pid < 0 || waitpid(pid, &status, 0) != pid || !WIFEXITED(status)) {
14653+
return -1;
14654+
}
14655+
return WEXITSTATUS(status);
14656+
}
14657+
14658+
/* A fresh synthetic root with its links resolved. The kernel bounds the
14659+
* EXPANDED path, and macOS /tmp is a link to /private/tmp: under the link a
14660+
* near-1 KiB path can be neither created nor probed, which hides the very
14661+
* cut-off file these tests look for. */
14662+
static bool hl_make_root(char *root, size_t root_sz) {
14663+
char tmpl[64];
14664+
char real[4096];
14665+
snprintf(tmpl, sizeof(tmpl), "/tmp/cbm-hooklog-XXXXXX");
14666+
if (!cbm_mkdtemp(tmpl)) {
14667+
return false;
14668+
}
14669+
if (!cbm_canonical_path(tmpl, real, sizeof(real))) {
14670+
th_rmtree(tmpl);
14671+
return false;
14672+
}
14673+
int n = snprintf(root, root_sz, "%s", real);
14674+
return n > 0 && (size_t)n < root_sz;
14675+
}
14676+
14677+
/* "<root>/hhh…/hhh…" of exactly `len` bytes, every component short of
14678+
* NAME_MAX. False when it cannot be built. */
14679+
static bool hl_long_dir(char *out, size_t out_sz, const char *root, size_t len) {
14680+
int n = snprintf(out, out_sz, "%s", root);
14681+
if (n <= 0 || (size_t)n >= len || len >= out_sz) {
14682+
return false;
14683+
}
14684+
size_t at = (size_t)n;
14685+
while (at < len) {
14686+
out[at++] = '/';
14687+
for (int seg = 0; seg < HL_SEGMENT && at < len; seg++) {
14688+
out[at++] = 'h';
14689+
}
14690+
}
14691+
out[at] = '\0';
14692+
return out[len - 1] != '/';
14693+
}
14694+
14695+
/* The first HL_PATH_CAP - 1 bytes of "<base>/<suffix>": the name the old
14696+
* unchecked 1 KiB buffer cut that path down to. */
14697+
static void hl_cut_name(char *out, const char *base, const char *suffix) {
14698+
char full[2 * HL_PATH_CAP];
14699+
snprintf(full, sizeof(full), "%s/%s", base, suffix);
14700+
snprintf(out, HL_PATH_CAP, "%.*s", HL_PATH_CAP - 1, full);
14701+
}
14702+
14703+
static bool hl_is_ancestor_or_self(const char *path, const char *of) {
14704+
size_t n = strlen(path);
14705+
return of && strncmp(of, path, n) == 0 && (of[n] == '/' || of[n] == '\0');
14706+
}
14707+
14708+
/* Entries under dir that are neither keep_a, keep_b nor one of their
14709+
* ancestors: whatever the hook created. Each is printed by its tail. */
14710+
static int hl_count_strays(const char *dir, const char *keep_a, const char *keep_b) {
14711+
cbm_dir_t *d = cbm_opendir(dir);
14712+
if (!d) {
14713+
return 0;
14714+
}
14715+
int strays = 0;
14716+
cbm_dirent_t *entry;
14717+
while ((entry = cbm_readdir(d)) != NULL) {
14718+
if (strcmp(entry->name, ".") == 0 || strcmp(entry->name, "..") == 0) {
14719+
continue;
14720+
}
14721+
char child[HL_PATH_CAP];
14722+
int n = snprintf(child, sizeof(child), "%s/%s", dir, entry->name);
14723+
bool named = n > 0 && (size_t)n < sizeof(child);
14724+
if (named &&
14725+
(hl_is_ancestor_or_self(child, keep_a) || hl_is_ancestor_or_self(child, keep_b))) {
14726+
if (entry->is_dir) {
14727+
strays += hl_count_strays(child, keep_a, keep_b);
14728+
}
14729+
continue;
14730+
}
14731+
size_t len = named ? (size_t)n : strlen(child);
14732+
printf(" stray entry: ...%s\n", child + (len > 60 ? len - 60 : 0));
14733+
strays++;
14734+
}
14735+
cbm_closedir(d);
14736+
return strays;
14737+
}
14738+
#endif
14739+
14740+
/* A HOME of ~1 KiB: the log path "<HOME>/.cache/codebase-memory-mcp/logs/
14741+
* hook-augment-timeouts.log" does not fit the buffer. The old code opened its
14742+
* cut-off prefix ".../logs/hook-augme" with O_CREAT. Nothing may be created at
14743+
* a cut path (nothing at all under the synthetic root), and the hook must
14744+
* still exit 0: it never fails the host agent's hook call. */
14745+
TEST(cli_hook_augment_log_long_home_creates_no_cut_path) {
14746+
#ifdef _WIN32
14747+
SKIP_PLATFORM("in-process deadline breadcrumb log is POSIX-only");
14748+
#else
14749+
enum { HL_HOME_LEN = 980 }; /* "<HOME>/.cache/.../logs" fits 1 KiB, the file name does not */
14750+
char root[256];
14751+
if (!hl_make_root(root, sizeof(root))) {
14752+
FAIL("synthetic root failed");
14753+
}
14754+
char home[HL_PATH_CAP];
14755+
bool built = hl_long_dir(home, sizeof(home), root, HL_HOME_LEN) && th_mkdir_p(home) == 0;
14756+
char cut[HL_PATH_CAP];
14757+
hl_cut_name(cut, home, ".cache/codebase-memory-mcp/logs/hook-augment-timeouts.log");
14758+
14759+
int rc = built ? hl_run_hook(home, NULL) : -1;
14760+
bool cut_exists = cbm_file_exists(cut);
14761+
int strays = hl_count_strays(root, home, NULL);
14762+
test_rmdir_r(root);
14763+
14764+
ASSERT_TRUE(built);
14765+
ASSERT_EQ(rc, 0);
14766+
if (cut_exists) {
14767+
printf(" log opened at the cut-off name ...%s\n", cut + strlen(cut) - 40);
14768+
}
14769+
ASSERT_FALSE(cut_exists);
14770+
ASSERT_EQ(strays, 0);
14771+
PASS();
14772+
#endif
14773+
}
14774+
14775+
/* CBM_CACHE_DIR is where every cbm log goes (<cache_dir>/logs), so the hook's
14776+
* breadcrumb goes there too, and not under HOME. A CBM_CACHE_DIR of ~1 KiB
14777+
* whose log path does not fit is no log at all: no cut-off file, no stray
14778+
* directory, and the hook still exits 0. */
14779+
TEST(cli_hook_augment_log_honours_cache_dir) {
14780+
#ifdef _WIN32
14781+
SKIP_PLATFORM("in-process deadline breadcrumb log is POSIX-only");
14782+
#else
14783+
enum { HL_CACHE_LEN = 1000 }; /* "<cache>/logs" fits 1 KiB, the file name does not */
14784+
char root[256];
14785+
if (!hl_make_root(root, sizeof(root))) {
14786+
FAIL("synthetic root failed");
14787+
}
14788+
char home[512];
14789+
char cache[512];
14790+
char expected[640];
14791+
snprintf(home, sizeof(home), "%s/home", root);
14792+
snprintf(cache, sizeof(cache), "%s/cache", root);
14793+
snprintf(expected, sizeof(expected), "%s/logs/hook-augment-timeouts.log", cache);
14794+
bool made = th_mkdir_p(home) == 0 && th_mkdir_p(cache) == 0;
14795+
14796+
/* A configured cache dir holds the log; HOME is left alone. */
14797+
int rc = made ? hl_run_hook(home, cache) : -1;
14798+
bool in_cache = cbm_file_exists(expected);
14799+
int strays = hl_count_strays(root, home, expected);
14800+
14801+
/* A configured cache dir too long for the log path: no log anywhere. */
14802+
char long_cache[HL_PATH_CAP];
14803+
char cut[HL_PATH_CAP];
14804+
bool long_built = hl_long_dir(long_cache, sizeof(long_cache), root, HL_CACHE_LEN) &&
14805+
th_mkdir_p(long_cache) == 0;
14806+
hl_cut_name(cut, long_cache, "logs/hook-augment-timeouts.log");
14807+
bool reset = th_rmtree(home) == 0 && th_rmtree(cache) == 0 && th_mkdir_p(home) == 0;
14808+
int long_rc = long_built && reset ? hl_run_hook(home, long_cache) : -1;
14809+
bool cut_exists = cbm_file_exists(cut);
14810+
int long_strays = hl_count_strays(root, home, long_cache);
14811+
test_rmdir_r(root);
14812+
14813+
ASSERT_TRUE(made);
14814+
ASSERT_EQ(rc, 0);
14815+
if (!in_cache) {
14816+
printf(" no breadcrumb log under CBM_CACHE_DIR/logs\n");
14817+
}
14818+
ASSERT_TRUE(in_cache);
14819+
ASSERT_EQ(strays, 0);
14820+
ASSERT_TRUE(long_built);
14821+
ASSERT_TRUE(reset);
14822+
ASSERT_EQ(long_rc, 0);
14823+
ASSERT_FALSE(cut_exists);
14824+
ASSERT_EQ(long_strays, 0);
14825+
PASS();
14826+
#endif
14827+
}
14828+
14829+
/* Positive control: with a normal HOME and no CBM_CACHE_DIR the breadcrumb log
14830+
* stays exactly where it always was,
14831+
* "$HOME/.cache/codebase-memory-mcp/logs/hook-augment-timeouts.log", and the
14832+
* hook creates nothing else. */
14833+
TEST(cli_hook_augment_log_default_location_unchanged) {
14834+
#ifdef _WIN32
14835+
SKIP_PLATFORM("in-process deadline breadcrumb log is POSIX-only");
14836+
#else
14837+
char root[256];
14838+
if (!hl_make_root(root, sizeof(root))) {
14839+
FAIL("synthetic root failed");
14840+
}
14841+
char home[512];
14842+
char expected[640];
14843+
snprintf(home, sizeof(home), "%s/home", root);
14844+
snprintf(expected, sizeof(expected),
14845+
"%s/.cache/codebase-memory-mcp/logs/hook-augment-timeouts.log", home);
14846+
bool made = th_mkdir_p(home) == 0;
14847+
14848+
int rc = made ? hl_run_hook(home, NULL) : -1;
14849+
bool at_default = cbm_file_exists(expected);
14850+
int strays = hl_count_strays(root, expected, NULL);
14851+
test_rmdir_r(root);
14852+
14853+
ASSERT_TRUE(made);
14854+
ASSERT_EQ(rc, 0);
14855+
ASSERT_TRUE(at_default);
14856+
ASSERT_EQ(strays, 0);
14857+
PASS();
14858+
#endif
14859+
}
14860+
1460714861
TEST(cli_upsert_claude_hook_existing) {
1460814862
char tmpdir[256];
1460914863
snprintf(tmpdir, sizeof(tmpdir), "/tmp/cli-hook-XXXXXX");
@@ -16596,6 +16850,9 @@ SUITE(cli) {
1659616850
RUN_TEST(cli_claude_exec_hooks_custom_dir_uninstall_issue1733);
1659716851
RUN_TEST(cli_hook_augment_path_is_abs);
1659816852
RUN_TEST(cli_hook_augment_deadline_breadcrumb_issue858);
16853+
RUN_TEST(cli_hook_augment_log_long_home_creates_no_cut_path);
16854+
RUN_TEST(cli_hook_augment_log_honours_cache_dir);
16855+
RUN_TEST(cli_hook_augment_log_default_location_unchanged);
1659916856
RUN_TEST(cli_upsert_claude_hook_fresh);
1660016857
RUN_TEST(cli_upsert_claude_hook_existing);
1660116858
RUN_TEST(cli_tool_hooks_preserve_foreign_same_matcher);

0 commit comments

Comments
 (0)