Skip to content

Commit 208ee99

Browse files
DeusDataclaude
andcommitted
fix(mcp): bound the non-git auto_index guard by discovery count, not git ls-files (DeusData#713)
auto_index_limit was bypassed for a non-git root because the admission guard counted `git ls-files`, which is 0 outside a repo, so the whole tree was walked into tens of GB of RAM. Bound the check by the discovery layer's bounded count with the limit as the ceiling; a non-git root over the limit is refused with the limit named, and git roots are unchanged. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Signed-off-by: Martin Vogel <martin.vogel.tech@gmail.com>
1 parent 161df2b commit 208ee99

2 files changed

Lines changed: 126 additions & 20 deletions

File tree

‎src/mcp/mcp.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17523,7 +17523,7 @@ static void maybe_auto_index(cbm_mcp_server_t *srv) {
1752317523
(void)snprintf(limit, sizeof(limit), "%d", file_limit);
1752417524
cbm_log_warn("autoindex.skip", "reason",
1752517525
file_count >= 0 ? "too_many_files" : "unsafe_or_unavailable_path", "files",
17526-
files, "limit", limit);
17526+
files, "limit", limit, "root", srv->session_root);
1752717527
return;
1752817528
}
1752917529

‎tests/test_mcp.c‎

Lines changed: 125 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18943,22 +18943,72 @@ TEST(mcp_auto_watch_false_skips_watcher_on_connect) {
1894318943
}
1894418944

1894518945
/* ══════════════════════════════════════════════════════════════════
18946-
* #1466 — autoindex.skip must report the effective numeric limit
18946+
* #1466 / #713 — the auto_index_limit guard
18947+
*
18948+
* #1466: autoindex.skip must report the effective numeric limit.
18949+
* #713: the guard must bound NON-git roots. The 0.8.x guard counted
18950+
* `git ls-files | wc -l`, which is 0 outside a checkout, so a plain
18951+
* directory of 60k files was walked in full (tens of GB RSS). The
18952+
* bounded discovery count applies to every root, and the skip line
18953+
* names the limit AND the root so the reporter can act on it.
1894718954
* ══════════════════════════════════════════════════════════════════ */
1894818955

1894918956
static char autoindex_skip_log[1024];
18957+
static bool autoindex_saw_done;
1895018958

18951-
/* Keeps only the too_many_files skip line, so later lines cannot displace it. */
18952-
static void autoindex_skip_capture_log(const char *line) {
18953-
if (line && strstr(line, "msg=autoindex.skip") && strstr(line, "reason=too_many_files")) {
18959+
/* Keeps only the too_many_files skip line, so later lines cannot displace it,
18960+
* and records whether an admitted auto-index actually ran to completion. */
18961+
static void autoindex_limit_capture_log(const char *line) {
18962+
if (!line) {
18963+
return;
18964+
}
18965+
if (strstr(line, "msg=autoindex.skip") && strstr(line, "reason=too_many_files")) {
1895418966
snprintf(autoindex_skip_log, sizeof(autoindex_skip_log), "%s", line);
1895518967
}
18968+
if (strstr(line, "msg=autoindex.done")) {
18969+
autoindex_saw_done = true;
18970+
}
18971+
}
18972+
18973+
typedef struct {
18974+
int files; /* indexable .py files written into the root */
18975+
int limit; /* auto_index_limit */
18976+
bool git_root; /* emulate a checkout via <root>/.git/HEAD (no git binary) */
18977+
} autoindex_limit_probe_t;
18978+
18979+
static bool autoindex_limit_write_fixture(const char *repodir,
18980+
const autoindex_limit_probe_t *probe) {
18981+
if (th_mkdir_p(repodir) != 0) {
18982+
return false;
18983+
}
18984+
for (int i = 0; i < probe->files; i++) {
18985+
char path[640];
18986+
char body[96];
18987+
snprintf(path, sizeof(path), "%s/f%d.py", repodir, i);
18988+
snprintf(body, sizeof(body), "def f%d():\n return %d\n", i, i);
18989+
if (th_write_file(path, body) != 0) {
18990+
return false;
18991+
}
18992+
}
18993+
if (probe->git_root) {
18994+
char gitdir[640];
18995+
char head[700];
18996+
snprintf(gitdir, sizeof(gitdir), "%s/.git", repodir);
18997+
snprintf(head, sizeof(head), "%s/HEAD", gitdir);
18998+
if (th_mkdir_p(gitdir) != 0 || th_write_file(head, "ref: refs/heads/main\n") != 0) {
18999+
return false;
19000+
}
19001+
}
19002+
return true;
1895619003
}
1895719004

18958-
/* Drive initialize → maybe_auto_index over a fresh project holding more tracked
18959-
* files than auto_index_limit, and capture the resulting skip warning.
19005+
/* Drive initialize → maybe_auto_index over a fresh root holding probe->files
19006+
* indexable files under auto_index_limit=probe->limit. skip_out receives the
19007+
* too_many_files warning (empty when admitted); *done_out reports whether an
19008+
* admitted auto-index ran to completion (the server free joins the thread).
1896019009
* Returns false on fixture setup failure. */
18961-
static bool autoindex_skip_warning(char *out, size_t out_size) {
19010+
static bool autoindex_limit_probe(const autoindex_limit_probe_t *probe, char *skip_out,
19011+
size_t skip_size, bool *done_out) {
1896219012
char cache[256];
1896319013
snprintf(cache, sizeof(cache), "%s/cbm-autoindex-limit-XXXXXX", cbm_tmpdir());
1896419014
if (!cbm_mkdtemp(cache)) {
@@ -18967,12 +19017,7 @@ static bool autoindex_skip_warning(char *out, size_t out_size) {
1896719017

1896819018
char repodir[512];
1896919019
snprintf(repodir, sizeof(repodir), "%s/repo", cache);
18970-
char file_a[640];
18971-
char file_b[640];
18972-
snprintf(file_a, sizeof(file_a), "%s/a.py", repodir);
18973-
snprintf(file_b, sizeof(file_b), "%s/b.py", repodir);
18974-
if (th_mkdir_p(repodir) != 0 || th_write_file(file_a, "def a():\n return 1\n") != 0 ||
18975-
th_write_file(file_b, "def b():\n return 2\n") != 0) {
19020+
if (!autoindex_limit_write_fixture(repodir, probe)) {
1897619021
th_rmtree(cache);
1897719022
return false;
1897819023
}
@@ -18992,27 +19037,31 @@ static bool autoindex_skip_warning(char *out, size_t out_size) {
1899219037
bool ok = false;
1899319038
cbm_config_t *cfg = cbm_config_open(cache);
1899419039
if (cfg) {
19040+
char limit[32];
19041+
snprintf(limit, sizeof(limit), "%d", probe->limit);
1899519042
cbm_config_set(cfg, CBM_CONFIG_AUTO_INDEX, "true");
18996-
cbm_config_set(cfg, CBM_CONFIG_AUTO_INDEX_LIMIT, "1");
19043+
cbm_config_set(cfg, CBM_CONFIG_AUTO_INDEX_LIMIT, limit);
1899719044

1899819045
cbm_mcp_server_t *srv = cbm_mcp_server_new(NULL);
1899919046
if (srv) {
1900019047
autoindex_skip_log[0] = '\0';
19048+
autoindex_saw_done = false;
1900119049
CBMLogLevel prev_level = cbm_log_get_level();
19002-
cbm_log_set_level(CBM_LOG_WARN);
19050+
cbm_log_set_level(CBM_LOG_INFO);
1900319051
cbm_log_set_format(CBM_LOG_FORMAT_TEXT);
19004-
cbm_log_set_sink_ex(autoindex_skip_capture_log, CBM_LOG_SINK_REPLACE);
19052+
cbm_log_set_sink_ex(autoindex_limit_capture_log, CBM_LOG_SINK_REPLACE);
1900519053

1900619054
cbm_mcp_server_set_config(srv, cfg);
1900719055
char *resp = cbm_mcp_server_handle(
1900819056
srv, "{\"jsonrpc\":\"2.0\",\"id\":1,\"method\":\"initialize\",\"params\":{}}");
1900919057
free(resp);
19010-
cbm_mcp_server_free(srv);
19058+
cbm_mcp_server_free(srv); /* joins an admitted autoindex thread */
1901119059

1901219060
cbm_log_set_sink(NULL);
1901319061
cbm_log_set_level(prev_level);
1901419062

19015-
snprintf(out, out_size, "%s", autoindex_skip_log);
19063+
snprintf(skip_out, skip_size, "%s", autoindex_skip_log);
19064+
*done_out = autoindex_saw_done;
1901619065
ok = true;
1901719066
}
1901819067
cbm_config_close(cfg);
@@ -19028,8 +19077,10 @@ static bool autoindex_skip_warning(char *out, size_t out_size) {
1902819077
/* RED before the fix: the warning carries `limit=auto_index_limit`, the config
1902919078
* key constant, instead of the configured value. */
1903019079
TEST(autoindex_skip_reports_numeric_limit_issue1466) {
19080+
autoindex_limit_probe_t probe = {.files = 2, .limit = 1, .git_root = false};
1903119081
char warning[1024];
19032-
if (!autoindex_skip_warning(warning, sizeof(warning))) {
19082+
bool done = false;
19083+
if (!autoindex_limit_probe(&probe, warning, sizeof(warning), &done)) {
1903319084
PASS(); /* fixture setup failed (tmpdir/cwd unavailable) — skip */
1903419085
}
1903519086
/* Not vacuous: the skip path must actually have been taken. */
@@ -19038,6 +19089,58 @@ TEST(autoindex_skip_reports_numeric_limit_issue1466) {
1903819089
ASSERT_NOT_NULL(strstr(warning, "files=2"));
1903919090
ASSERT_NOT_NULL(strstr(warning, "limit=1"));
1904019091
ASSERT_NULL(strstr(warning, "limit=auto_index_limit"));
19092+
ASSERT_FALSE(done);
19093+
PASS();
19094+
}
19095+
19096+
/* #713 at the reporter's shape scaled down (60 files vs limit 50, from
19097+
* 60k vs 50k): a PLAIN directory over the limit is refused, and the line
19098+
* names the limit and the root. RED on the `git ls-files` guard (count 0 →
19099+
* admitted → indexed) and RED while the line does not name the root. */
19100+
TEST(autoindex_limit_guards_non_git_root_issue713) {
19101+
autoindex_limit_probe_t probe = {.files = 60, .limit = 50, .git_root = false};
19102+
char warning[1024];
19103+
bool done = false;
19104+
if (!autoindex_limit_probe(&probe, warning, sizeof(warning), &done)) {
19105+
PASS(); /* fixture setup failed (tmpdir/cwd unavailable) — skip */
19106+
}
19107+
ASSERT_NOT_NULL(strstr(warning, "msg=autoindex.skip"));
19108+
ASSERT_NOT_NULL(strstr(warning, "reason=too_many_files"));
19109+
ASSERT_NOT_NULL(strstr(warning, "limit=50"));
19110+
const char *root = strstr(warning, "root=");
19111+
ASSERT_NOT_NULL(root);
19112+
ASSERT_NOT_NULL(strstr(root, "/repo"));
19113+
ASSERT_FALSE(done);
19114+
PASS();
19115+
}
19116+
19117+
/* The same plain directory one file UNDER the limit is admitted and indexed:
19118+
* the guard bounds, it does not fail closed on every non-git root. */
19119+
TEST(autoindex_limit_admits_non_git_root_under_limit_issue713) {
19120+
autoindex_limit_probe_t probe = {.files = 49, .limit = 50, .git_root = false};
19121+
char warning[1024];
19122+
bool done = false;
19123+
if (!autoindex_limit_probe(&probe, warning, sizeof(warning), &done)) {
19124+
PASS(); /* fixture setup failed (tmpdir/cwd unavailable) — skip */
19125+
}
19126+
ASSERT(warning[0] == '\0');
19127+
ASSERT_TRUE(done);
19128+
PASS();
19129+
}
19130+
19131+
/* A checkout keeps its behaviour: one bounded count for both root kinds. */
19132+
TEST(autoindex_limit_guards_git_root_issue713) {
19133+
autoindex_limit_probe_t probe = {.files = 60, .limit = 50, .git_root = true};
19134+
char warning[1024];
19135+
bool done = false;
19136+
if (!autoindex_limit_probe(&probe, warning, sizeof(warning), &done)) {
19137+
PASS(); /* fixture setup failed (tmpdir/cwd unavailable) — skip */
19138+
}
19139+
ASSERT_NOT_NULL(strstr(warning, "msg=autoindex.skip"));
19140+
ASSERT_NOT_NULL(strstr(warning, "reason=too_many_files"));
19141+
ASSERT_NOT_NULL(strstr(warning, "limit=50"));
19142+
ASSERT_NOT_NULL(strstr(warning, "root="));
19143+
ASSERT_FALSE(done);
1904119144
PASS();
1904219145
}
1904319146

@@ -20320,6 +20423,9 @@ SUITE(mcp) {
2032020423
RUN_TEST(mcp_auto_watch_false_skips_watcher_on_connect);
2032120424
RUN_TEST(mcp_auto_watch_false_skips_supervised_autoindex_issue853);
2032220425
RUN_TEST(autoindex_skip_reports_numeric_limit_issue1466);
20426+
RUN_TEST(autoindex_limit_guards_non_git_root_issue713);
20427+
RUN_TEST(autoindex_limit_admits_non_git_root_under_limit_issue713);
20428+
RUN_TEST(autoindex_limit_guards_git_root_issue713);
2032320429
}
2032420430

2032520431
/* Kept separate so daemon-coordination regressions can be iterated without

0 commit comments

Comments
 (0)