diff --git a/src/mcp/mcp.c b/src/mcp/mcp.c index 3bd603dcad..d307802526 100644 --- a/src/mcp/mcp.c +++ b/src/mcp/mcp.c @@ -17509,14 +17509,20 @@ static bool validate_windows_cmd_interpolation_arg(const char *s) { #endif } -static bool validate_search_args(const char *root_path, const char *file_pattern) { - if (!validate_search_path_arg(root_path)) { - return false; - } - if (file_pattern && !validate_search_path_arg(file_pattern)) { - return false; - } - return true; +/* The stored project root reaches a command line only on Windows, where the + * unscoped fallback and a file_pattern the scanner cannot prefilter run + * through cmd/PowerShell with root_path spliced in. POSIX never hands it to a + * shell: an indexed project is scanned in process, and the unscoped fallback + * runs as find/sort/grep argv stages (#2634). So on POSIX a root containing + * '$', ';' or '|' that index_repository accepted stays searchable (#3151). + * Line breaks stay rejected because the scan file list is newline-separated. + * file_pattern keeps its own check either way. */ +static bool validate_search_root(const char *root_path) { +#ifdef _WIN32 + return validate_search_path_arg(root_path); +#else + return root_path && !strpbrk(root_path, "\r\n"); +#endif } /* Private scratch for one search_code scan: the grep -f pattern file and the @@ -18239,7 +18245,8 @@ static char *handle_search_code(cbm_mcp_server_t *srv, const char *args) { return _res; } - if (!validate_search_args(root_path, file_pattern)) { + bool root_ok = validate_search_root(root_path); + if (!root_ok || (file_pattern && !validate_search_path_arg(file_pattern))) { if (has_path_filter) { cbm_regfree(&path_regex); } @@ -18247,7 +18254,11 @@ static char *handle_search_code(cbm_mcp_server_t *srv, const char *args) { free(pattern); free(project); free(file_pattern); - return cbm_mcp_text_result("path or file_pattern contains invalid characters", true); + return cbm_mcp_text_result( + root_ok ? "path or file_pattern contains invalid characters" + : "project root_path contains characters search_code cannot pass to the scan " + "command", + true); } /* issue #283: when regex=true, a syntactically invalid pattern (e.g. an diff --git a/tests/test_mcp.c b/tests/test_mcp.c index f7114286f6..257151107e 100644 --- a/tests/test_mcp.c +++ b/tests/test_mcp.c @@ -16100,6 +16100,62 @@ TEST(search_code_ampersand_accepted_issue272) { PASS(); } +/* issue #3151: index_repository accepts a root such as /tmp/repo$cash, but + * search_code ran the shell-metacharacter check on the stored root and refused + * every search of that project with "path or file_pattern contains invalid + * characters", even with neither argument passed. On POSIX the root never + * reaches a shell (in-process scan for an indexed project, find/sort/grep argv + * for the unscoped fallback), so both scans must find the match. */ +TEST(search_code_root_path_with_dollar_issue3151) { +#ifdef _WIN32 + SKIP_PLATFORM("POSIX scans pass the project root as argv, never through a shell"); +#else + char tmp[CBM_SZ_512]; + snprintf(tmp, sizeof(tmp), "%s/cbm-search-repo$cash-XXXXXX", cbm_tmpdir()); + ASSERT_NOT_NULL(cbm_mkdtemp(tmp)); + char source_path[CBM_SZ_1K]; + snprintf(source_path, sizeof(source_path), "%s/main.c", tmp); + ASSERT_EQ(th_write_file(source_path, "int DOLLAR_ROOT_NEEDLE = 1;\n"), 0); + + /* "unscoped" has no File node, so search_code takes the find/grep + * fallback; "scoped" has one and is scanned in process. */ + static const char *const projects[] = {"dollar-unscoped", "dollar-scoped"}; + bool found[2] = {false, false}; + for (int i = 0; i < 2; i++) { + cbm_mcp_server_t *srv = cbm_mcp_server_new(NULL); + ASSERT_NOT_NULL(srv); + cbm_store_t *store = cbm_mcp_server_store(srv); + ASSERT_NOT_NULL(store); + cbm_mcp_server_set_project(srv, projects[i]); + ASSERT_EQ(cbm_store_upsert_project(store, projects[i], tmp), CBM_STORE_OK); + if (i == 1) { + cbm_node_t file = {.project = projects[i], + .label = "File", + .name = "main.c", + .qualified_name = "dollar-scoped.main.c", + .file_path = "main.c"}; + ASSERT_GT(cbm_store_upsert_node(store, &file), 0); + } + char req[256]; + snprintf(req, sizeof(req), + "{\"pattern\":\"DOLLAR_ROOT_NEEDLE\",\"project\":\"%s\",\"format\":\"json\"}", + projects[i]); + char *response = cbm_mcp_handle_tool(srv, "search_code", req); + char *inner = extract_text_content(response); + found[i] = response && !strstr(response, "\"isError\":true") && inner && + !strstr(inner, "invalid characters") && strstr(inner, "main.c"); + free(inner); + free(response); + cbm_mcp_server_free(srv); + } + cbm_unlink(source_path); + cbm_rmdir(tmp); + ASSERT_TRUE(found[0]); + ASSERT_TRUE(found[1]); + PASS(); +#endif +} + TEST(tool_detect_changes_no_project) { cbm_mcp_server_t *srv = cbm_mcp_server_new(NULL); @@ -28326,6 +28382,30 @@ TEST(search_code_rejects_quote_in_file_pattern) { PASS(); } +TEST(search_code_rejects_newline_in_root_path_issue3151) { + cbm_mcp_server_t *srv = cbm_mcp_server_new(NULL); + ASSERT_NOT_NULL(srv); + cbm_store_t *store = cbm_mcp_server_store(srv); + ASSERT_NOT_NULL(store); + cbm_mcp_server_set_project(srv, "bad-root-proj"); + ASSERT_EQ(cbm_store_upsert_project(store, "bad-root-proj", "/tmp/bad\nroot"), CBM_STORE_OK); + + char *response = cbm_mcp_handle_tool( + srv, "search_code", + "{\"project\":\"bad-root-proj\",\"pattern\":\"needle\",\"format\":\"json\"}"); + ASSERT_NOT_NULL(response); + ASSERT_NOT_NULL(strstr(response, "\"isError\":true")); + char *inner = extract_text_content(response); + free(response); + ASSERT_NOT_NULL(inner); + ASSERT_NOT_NULL( + strstr(inner, "project root_path contains characters search_code cannot pass to the scan " + "command")); + free(inner); + cbm_mcp_server_free(srv); + PASS(); +} + TEST(tool_check_index_coverage_parse_gap_precedes_unresolved_calls) { cbm_mcp_server_t *srv = cbm_mcp_server_new(NULL); ASSERT_NOT_NULL(srv); @@ -29258,6 +29338,7 @@ SUITE(mcp) { RUN_TEST(search_code_literal_pipe_warns_issue282); RUN_TEST(search_code_reports_phase_timings_only_in_debug_mode); RUN_TEST(search_code_ampersand_accepted_issue272); + RUN_TEST(search_code_root_path_with_dollar_issue3151); RUN_TEST(tool_detect_changes_no_project); RUN_TEST(tool_manage_adr_no_project); RUN_TEST(tool_manage_adr_get_with_existing_adr); @@ -29414,6 +29495,7 @@ SUITE(mcp) { RUN_TEST(tool_search_graph_limit_above_ceiling_is_capped); RUN_TEST(tool_trace_cursor_truncated_after_leg_is_rejected); RUN_TEST(search_code_rejects_quote_in_file_pattern); + RUN_TEST(search_code_rejects_newline_in_root_path_issue3151); /* search_code: every match resolves inside the project root */ RUN_TEST(search_code_matches_stay_inside_project_root);