Skip to content

fix(mcp): stop a stray root-owned .cocoindex_code from hijacking project-root discovery - #341

Open
RizkiRachman wants to merge 1 commit into
cocoindex-io:mainfrom
RizkiRachman:fix/mcp-project-root-hijack
Open

RizkiRachman wants to merge 1 commit into
cocoindex-io:mainfrom
RizkiRachman:fix/mcp-project-root-hijack

Conversation

@RizkiRachman

Copy link
Copy Markdown

Problem

ccc mcp discovers its project root by walking up from the current working directory looking for .cocoindex_code/settings.yml. MCP clients (editors, agent harnesses like OpenCode) launch the server with an unrelated CWD, in practice /.

When a .cocoindex_code directory exists at / but is not writable by the user running the server (in our case a root-owned /.cocoindex_code left behind at container setup), discovery accepts it as the project root. The daemon then tries to create its SQLite database inside that directory and every request fails with a raw driver error:

sqlite3.OperationalError: unable to open database file

Nothing in that message points at the real cause: a stray, unusable marker directory hijacking discovery.

Root cause

  • settings.find_project_root returns any directory containing .cocoindex_code/settings.yml, without checking that the current user can actually use it.
  • COCOINDEX_CODE_ROOT_PATH is only consulted when discovery finds nothing, so it cannot rescue a launcher pinned to a bad CWD.
  • The daemon surfaces the raw sqlite3.OperationalError with no context.

Fix

  • find_project_root skips candidates whose .cocoindex_code directory is not writable by the current user. The daemon must write the index DB there, so such candidates are unusable by definition and can no longer hijack discovery.
  • ccc mcp --project-root <dir>: explicit flag for launcher configs.
  • COCOINDEX_CODE_ROOT_PATH is now honored as an override for ccc mcp (the flag wins over the env var).
  • The legacy cocoindex-code serve entrypoint exits with an actionable message when the resolved root is not serviceable.
  • Project.create wraps SQLite open failures with the project root, the DB path and the likely cause instead of the bare driver error.

Verification

Reproduced the incident against a real root-owned /.cocoindex_code on /:

Check Before After
find_project_root(Path("/")) with unwritable marker / (hijacked) None
find_project_root from a writable project project dir project dir (unchanged)
_mcp_root_override(None) with env set n/a env path
require_project_root(override=...) n/a override path

New unit tests cover the skip behavior and override precedence (tests/test_settings.py, tests/test_cli_helpers.py). I could not run the full suite locally (no pytest in this environment); py_compile passes on all touched files and CI will run the suite.

Launcher config for affected setups, e.g. opencode:

"command": ["sh", "-c", "cd /path/to/project && exec ccc mcp"]

or with this PR:

"command": ["ccc", "mcp", "--project-root", "/path/to/project"]

…ect-root discovery

MCP launchers (editors, agent harnesses) start `ccc mcp` in an unrelated
working directory, commonly the filesystem root. `find_project_root`
accepted any directory containing `.cocoindex_code/settings.yml`, including
ones the current user cannot write to (e.g. a root-owned `/.cocoindex_code`
left behind by a system-level install). The daemon then tried to create its
SQLite database inside that directory and every request failed with the
cryptic `sqlite3.OperationalError: unable to open database file`.

- `find_project_root` skips candidates whose `.cocoindex_code` is not
  writable by the current user, so unusable markers cannot hijack discovery
- `ccc mcp` gains `--project-root`; `COCOINDEX_CODE_ROOT_PATH` now acts as
  the documented override for launcher contexts (checked before the CWD walk)
- the legacy `cocoindex-code` entrypoint exits with an actionable message
  when the resolved root is not serviceable
- `Project.create` wraps SQLite open failures with the project root, the DB
  path and the likely cause instead of surfacing the raw driver error

This branch has not been deployed

No deployments
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.

1 participant