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.
C aiderastclaude-codecode-analysiscode-intelligencecodexcursorcypherdeveloper-toolsgemini-cligraph-visualizationkilocodeknowledge-graphmcpmcp-servermodel-context-protocolopencodesqlitetree-sitterwindsurf
mcp: contain search_code file reads to the project root
Martin Vogel97438eb5ec70
diff --git a/src/mcp/mcp.c b/src/mcp/mcp.cindex 9330f31bd..1068f979c 100644--- a/src/mcp/mcp.c+++ b/src/mcp/mcp.c@@ -4080,19 +4080,19 @@ static yyjson_doc *enrich_node_properties(yyjson_mut_doc *doc, yyjson_mut_val *o/* Resolve an absolute path from root_path + file_path, verify containment,* and read source lines. Sets *out_abs_path (caller frees). Returns source* string (caller frees) or NULL if path is invalid/unreadable. */-static char *resolve_snippet_source(const char *root_path, const char *file_path, int start,- int end, char **out_abs_path) {- *out_abs_path = NULL;- if (!root_path || !file_path) {- return NULL;+/* True only when abs_path, after realpath/_fullpath resolution (which collapses+ * `..` and resolves symlinks/junctions), stays within root_path. This is the+ * single containment guard every MCP file-read sink must pass before reading a+ * file into a tool response: both the snippet path (resolve_snippet_source) and+ * the search path (attach_result_source) route through it, so a result whose+ * indexed path escapes the project root — via a `..` segment, or a symlink /+ * Windows junction picked up during discovery — is never read back out. */+bool cbm_path_within_root(const char *root_path, const char *abs_path) {+ if (!root_path || !abs_path) {+ return false;}- size_t apsz = strlen(root_path) + strlen(file_path) + MCP_SEPARATOR;- char *abs_path = malloc(apsz);- snprintf(abs_path, apsz, "%s/%s", root_path, file_path);-char real_root[CBM_SZ_4K];char real_file[CBM_SZ_4K];- bool path_ok = false;#ifdef _WIN32if (_fullpath(real_root, root_path, sizeof(real_root)) &&_fullpath(real_file, abs_path, sizeof(real_file))) {@@ -4104,11 +4104,24 @@ static char *resolve_snippet_source(const char *root_path, const char *file_pathsize_t root_len = strlen(real_root);if (strncmp(real_file, real_root, root_len) == 0 &&(real_file[root_len] == '/' || real_file[root_len] == '\0')) {- path_ok = true;+ return true;}}+ return false;+}++static char *resolve_snippet_source(const char *root_path, const char *file_path, int start,+ int end, char **out_abs_path) {+ *out_abs_path = NULL;+ if (!root_path || !file_path) {+ return NULL;+ }+ size_t apsz = strlen(root_path) + strlen(file_path) + MCP_SEPARATOR;+ char *abs_path = malloc(apsz);+ snprintf(abs_path, apsz, "%s/%s", root_path, file_path);+*out_abs_path = abs_path;- if (path_ok) {+ if (cbm_path_within_root(root_path, abs_path)) {return read_file_lines(abs_path, start, end);}return NULL;@@ -4570,6 +4583,14 @@ static void attach_result_source(yyjson_mut_doc *doc, yyjson_mut_val *item, searchar abs_path[CBM_SZ_1K];snprintf(abs_path, sizeof(abs_path), "%s/%s", root_path, r->file);+ /* Containment: a search result whose indexed path resolves outside the+ * project root (a `..` segment, or a symlink/junction that discovery+ * followed) must not be read back into the response. Same guard the+ * snippet path already uses. */+ if (!cbm_path_within_root(root_path, abs_path)) {+ return;+ }+if (mode == MODE_FULL) {char *source = read_file_lines(abs_path, r->start_line, r->end_line);if (source) {diff --git a/tests/test_mcp.c b/tests/test_mcp.cindex a70b1842c..6b0249c44 100644--- a/tests/test_mcp.c+++ b/tests/test_mcp.c@@ -4874,11 +4874,50 @@ TEST(mcp_auto_watch_false_skips_supervised_autoindex_issue853) {#endif}+/* The containment guard both MCP file-read sinks route through+ * (resolve_snippet_source for get_code_snippet, attach_result_source for+ * search_code). A result path that resolves outside the indexed project root+ * — via a `..` segment or a followed symlink/junction — must be rejected so+ * its contents never reach a tool response. */+extern bool cbm_path_within_root(const char *root_path, const char *abs_path);++TEST(mcp_path_within_root_rejects_escape) {+#ifdef _WIN32+ SKIP_PLATFORM("POSIX realpath repro; the Windows _fullpath branch is the same guard");+#else+ char root[512];+ snprintf(root, sizeof(root), "%s/cbm_pwr_XXXXXX", cbm_tmpdir());+ if (!cbm_mkdtemp(root)) {+ FAIL("cbm_mkdtemp failed");+ }+ char inside[700];+ snprintf(inside, sizeof(inside), "%s/inside.c", root);+ FILE *fp = fopen(inside, "w");+ ASSERT_NOT_NULL(fp);+ fputs("int x;\n", fp);+ fclose(fp);++ /* The abs_path a sink builds for an in-root result stays contained; a `..`+ * escape to an existing outside file (/etc/hosts) resolves out and must be+ * rejected. */+ char escape[900];+ snprintf(escape, sizeof(escape), "%s/../../../../etc/hosts", root);+ ASSERT_TRUE(cbm_path_within_root(root, inside));+ ASSERT_FALSE(cbm_path_within_root(root, escape));+ ASSERT_FALSE(cbm_path_within_root(root, "/etc/hosts"));++ remove(inside);+ cbm_rmdir(root);+ PASS();+#endif+}+/* ══════════════════════════════════════════════════════════════════* SUITE* ══════════════════════════════════════════════════════════════════ */SUITE(mcp) {+ RUN_TEST(mcp_path_within_root_rejects_escape);/* JSON-RPC parsing */RUN_TEST(jsonrpc_parse_request);RUN_TEST(jsonrpc_parse_notification);