Skip to content

fix: validate projectPath in MCP tool handler to prevent sensitive directory access#230

Merged
colbymchenry merged 2 commits into
colbymchenry:mainfrom
eddiekudo:fix/validate-projectpath-mcp-tools
May 22, 2026
Merged

fix: validate projectPath in MCP tool handler to prevent sensitive directory access#230
colbymchenry merged 2 commits into
colbymchenry:mainfrom
eddiekudo:fix/validate-projectpath-mcp-tools

Conversation

@eddiekudo
Copy link
Copy Markdown
Contributor

Summary

The projectPath parameter accepted by all MCP tools passes through getCodeGraph() without calling validateProjectPath(), allowing cross-project queries to target sensitive system directories (/, /etc, ~/.ssh, ~/.aws, C:\Windows, etc.).

This adds the existing validateProjectPath() check inside ToolHandler.getCodeGraph() before any filesystem traversal occurs.

What changed

  • Import validateProjectPath from ../utils in src/mcp/tools.ts
  • Call validateProjectPath(projectPath) after the cache check and before findNearestCodeGraphRoot()

Why

validateProjectPath() already exists and blocks sensitive paths, but was not wired into the MCP tool handler's cross-project code path. A malicious or compromised MCP client could pass projectPath pointing to sensitive directories.

Testing

  • All 33 existing security tests pass
  • All core tests (foundation, explore-output-budget, symbol-lookup) pass
  • TypeScript compiles cleanly with --noEmit

Copilot AI review requested due to automatic review settings May 21, 2026 00:51
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

Adds a safety check to validate projectPath before locating/opening a CodeGraph project.

Changes:

  • Import validateProjectPath from ../utils.
  • Validate projectPath and throw on invalid paths before walking up directories to find .codegraph/.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/mcp/tools.ts
Comment on lines 534 to +542
if (this.projectCache.has(projectPath)) {
return this.projectCache.get(projectPath)!;
}

// Validate the path is safe before opening
const pathError = validateProjectPath(projectPath);
if (pathError) {
throw new Error(pathError);
}
@colbymchenry colbymchenry force-pushed the fix/validate-projectpath-mcp-tools branch from e0c4871 to 54d17eb Compare May 22, 2026 18:43
colbymchenry added a commit to eddiekudo/codegraph that referenced this pull request May 22, 2026
colbymchenry#230's validateProjectPath ran on the raw path incl. a statSync existence
check, which rejected the nested/not-yet-created sub-paths that issue colbymchenry#238
expects to resolve UP to the default project. Guard with existsSync so a
direct sensitive dir (e.g. /etc, C:\Windows) is still refused while
sub-paths walk up. Adds MCP-handler rejection tests (POSIX + Windows-gated).
eddiekudo and others added 2 commits May 22, 2026 14:14
colbymchenry#230's validateProjectPath ran on the raw path incl. a statSync existence
check, which rejected the nested/not-yet-created sub-paths that issue colbymchenry#238
expects to resolve UP to the default project. Guard with existsSync so a
direct sensitive dir (e.g. /etc, C:\Windows) is still refused while
sub-paths walk up. Adds MCP-handler rejection tests (POSIX + Windows-gated).
@colbymchenry colbymchenry force-pushed the fix/validate-projectpath-mcp-tools branch from 54d17eb to 1284357 Compare May 22, 2026 19:14
@colbymchenry colbymchenry merged commit 02ea482 into colbymchenry:main May 22, 2026
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.

3 participants