fix: validate projectPath in MCP handler to block sensitive directories (#230)
Validate projectPath in getCodeGraph so MCP clients can't open a codegraph in a sensitive system directory. Guarded with existsSync so nested/not-yet-created sub-paths still resolve up to the default project (preserves issue #238). Adds MCP-handler rejection tests (POSIX + Windows-gated); validated on a real Windows 11 VM. Closes #230
This commit is contained in:
+13
-1
@@ -15,7 +15,7 @@ import {
|
||||
readFileSync,
|
||||
writeSync,
|
||||
} from 'fs';
|
||||
import { clamp, validatePathWithinRoot } from '../utils';
|
||||
import { clamp, validatePathWithinRoot, validateProjectPath } from '../utils';
|
||||
import { tmpdir } from 'os';
|
||||
import { join } from 'path';
|
||||
|
||||
@@ -579,6 +579,18 @@ export class ToolHandler {
|
||||
return this.projectCache.get(projectPath)!;
|
||||
}
|
||||
|
||||
// Reject sensitive system directories before opening. Only validate a
|
||||
// path that actually exists — a nested or not-yet-created sub-path of a
|
||||
// real project must still be allowed to resolve UP to its .codegraph/
|
||||
// root below (issue #238), so we don't run the existence-checking
|
||||
// validator on paths that are meant to walk up.
|
||||
if (existsSync(projectPath)) {
|
||||
const pathError = validateProjectPath(projectPath);
|
||||
if (pathError) {
|
||||
throw new Error(pathError);
|
||||
}
|
||||
}
|
||||
|
||||
// Walk up parent directories to find nearest .codegraph/
|
||||
const resolvedRoot = findNearestCodeGraphRoot(projectPath);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user