Skip to content

fix(mcp): let search_code scan a POSIX project root that contains '$' - #3212

Merged
DeusData merged 2 commits into
DeusData:mainfrom
GDemay:fix/search-code-root-path-dollar
Dec 6, 2026
Merged

DeusData merged 2 commits into
DeusData:mainfrom
GDemay:fix/search-code-root-path-dollar

Conversation

@GDemay

@GDemay GDemay commented Dec 6, 2026 •

Copy link
Copy Markdown
Contributor

Problem

index_repository accepts a project root that contains $ (for example /tmp/repo$cash). search_code then checks the stored root for shell metacharacters and refuses every search of that project with path or file_pattern contains invalid characters, even when neither a path nor a file_pattern was passed (#3151).

Change

  • On POSIX the root never reaches a shell. An indexed project is scanned in process, and the unscoped fallback runs as find/sort/grep argv stages (Proposal: Run git, grep and find helpers with argv instead of a shell #2634). So the root check there now only rejects line breaks, because the scan file list is newline-separated.
  • On Windows the root is still spliced into cmd/PowerShell, so it keeps the full check.
  • file_pattern keeps its check on every platform.
  • When the root is rejected, the error now says so (project root_path contains characters search_code cannot pass to the scan command) instead of blaming path/file_pattern.
  • detect_changes is unchanged: it does put the root into a git shell command, and it already reports project path contains invalid characters.

Tests

  • New search_code_root_path_with_dollar_issue3151 (POSIX only): a root named cbm-search-repo$cash-XXXXXX, searched both through the unscoped find/grep fallback and the indexed in-process scan. Both must return main.c without an error. On main both calls hit the old root check.
  • New search_code_rejects_newline_in_root_path_issue3151 (all platforms): a stored root containing a newline returns the root-specific error message.
  • scripts/test.sh --suites mcp (ASan + UBSan): 445 passed, 4 skipped, 0 failed.
  • clang-format-20 --dry-run --Werror src/mcp/mcp.c, scripts/check-no-test-skips.sh, scripts/check-dco.sh: clean.

Fixes #3151

written with AI assistance

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 when neither argument was passed.

On POSIX the root never reaches a shell: an indexed project is scanned
in process and the unscoped fallback runs as find/sort/grep argv stages
(DeusData#2634). So only reject line breaks there (the scan file list is
newline-separated). Windows still splices the root into cmd/PowerShell,
so it keeps the full check. file_pattern keeps its check on every
platform, and a rejected root now gets its own error message.

detect_changes is unchanged: it does splice the root into a git shell
command and already reports "project path contains invalid characters".

Fixes DeusData#3151

Signed-off-by: Guillaume Demay <guillaumedemay@hotmail.fr>
@GDemay
GDemay requested a review from DeusData as a code owner December 6, 2026 11:21
Cover the root-specific error path on every platform by rejecting a stored root_path containing a newline.

Signed-off-by: Guillaume Demay <guillaumedemay@hotmail.fr>
@DeusData

DeusData commented Dec 6, 2026

Copy link
Copy Markdown
Owner

Thank you, @GDemay! A project whose root contains $ (or ;, |) can be indexed but then never searched, which is a confusing dead end. Since this relaxes an input check that exists to stop command injection, we verified the premise on main rather than taking it on trust: on POSIX, search_code either scans an indexed project in process or runs the unscoped fallback through mcp_run_search_scan_argv, where root_path reaches find as an argv element and no shell ever parses it (#2634). The sh -c string that cbm_search_code_build_grep_cmd still builds with the root inside single quotes is only executed through the test-only command override, which replaces it. So allowing these characters on POSIX is safe, line breaks stay rejected for the newline-separated file list, and Windows keeps the full check because its fallback does go through cmd/PowerShell. The clearer error message is a nice touch too.

Because it changes a security-relevant validation, the maintainer gives the final OK before it merges.

@DeusData
DeusData merged commit 794d989 into DeusData:main Dec 6, 2026
26 checks passed
@DeusData

DeusData commented Dec 6, 2026

Copy link
Copy Markdown
Owner

Merged in 794d989. Thank you, @GDemay! Projects whose root contains $, ; or | can now be searched as well as indexed on POSIX, while Windows keeps the full check where a shell is involved. Much appreciated!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

search_code (and detect_changes): rejects projects indexed by index_repository when root_path contains '$'

2 participants