{"record":{"id":"f2a01228220a6742","repo":"Hmbown/CodeWhale","slug":"project-mcp-server-cwd-must-stay-within-workspace","errorCode":null,"errorMessage":"Project MCP server cwd must stay within workspace: {}","messagePattern":"Project MCP server cwd must stay within workspace: (.+?)","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/tui/src/mcp.rs","lineNumber":5663,"sourceCode":"    } else {\n        std::env::current_dir()\n            .unwrap_or_else(|_| PathBuf::from(\".\"))\n            .join(workspace)\n    };\n    normalize_path_components(&absolute)\n}\n\nfn resolve_project_mcp_cwd(workspace: &Path, cwd: Option<&Path>) -> Result<PathBuf> {\n    let cwd = match cwd {\n        Some(cwd) if cwd.is_relative() => normalize_path_components(&workspace.join(cwd)),\n        Some(cwd) => normalize_path_components(cwd),\n        None => workspace.to_path_buf(),\n    };\n    let resolved = cwd\n        .canonicalize()\n        .unwrap_or_else(|_| normalize_path_components(&cwd));\n    if !resolved.starts_with(workspace) {\n        anyhow::bail!(\n            \"Project MCP server cwd must stay within workspace: {}\",\n            resolved.display()\n        );\n    }\n    Ok(resolved)\n}\n\nfn normalize_path_components(path: &Path) -> PathBuf {\n    let mut normalized = PathBuf::new();\n    for component in path.components() {\n        match component {\n            Component::Prefix(_) | Component::RootDir => {\n                normalized.push(component.as_os_str());\n            }\n            Component::CurDir => {}\n            Component::ParentDir => {\n                normalized.pop();\n            }","sourceCodeStart":5645,"sourceCodeEnd":5681,"githubUrl":"https://github.com/Hmbown/CodeWhale/blob/73e0f67d83c59909b571efdfc88c4bc28c309cb1/crates/tui/src/mcp.rs#L5645-L5681","documentation":"Project-scope MCP servers may only run with a working directory inside the workspace that owns the config. After canonicalizing the configured `cwd` (falling back to component normalization when canonicalization fails), the library verifies it starts with the workspace path; otherwise it refuses to launch the server with this error naming the escaped path. This prevents a project MCP config from executing commands elsewhere on disk.","triggerScenarios":"Starting or connecting to a project MCP server whose `cwd` setting in `.codewhale/mcp.json` canonicalizes outside the workspace — e.g. `cwd` set to `/tmp` or a symlinked directory, while the workspace is elsewhere (crates/tui/src/mcp.rs:5663).","commonSituations":"A user hand-edits `mcp.json` and sets an absolute cwd outside the repo; the cwd is a symlink to another filesystem location; shared team config checked in with machine-specific absolute paths; workspace moved so the old cwd no longer nests under it.","solutions":["Change the server's `cwd` in the project `mcp.json` to a directory inside the workspace (or remove `cwd` to default to the workspace)","Replace symlinked cwd entries with real directories inside the repo","Update the workspace path so the configured cwd is nested within it","Use a relative or absent `cwd` rather than a machine-specific absolute path"],"exampleFix":"// before\n{\"mcpServers\": {\"docs\": {\"command\": \"python\", \"args\": [\"-m\", \"docs\"], \"cwd\": \"/tmp/docs-venv\"}}}\n// after\n{\"mcpServers\": {\"docs\": {\"command\": \"python\", \"args\": [\"-m\", \"docs\"]}}} // cwd defaults to the workspace","handlingStrategy":"validation","validationCode":"fn cwd_within_workspace(cwd: &Path, workspace: &Path) -> bool {\n    let resolved = cwd.canonicalize().unwrap_or_else(|_| cwd.to_path_buf());\n    resolved.starts_with(workspace)\n}\nassert!(cwd_within_workspace(&server_cwd, &workspace), \"MCP cwd must stay inside the workspace\");","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Keep server `cwd` unset (defaults to workspace) in project mcp.json","Avoid symlinked cwd entries in shared configs","Review team-checked-in mcp.json for machine-specific absolute paths"],"tags":["mcp","path-validation","security","config"],"backgroundTag":"path-traversal-blocked","analyzedSha":"73e0f67d83c59909b571efdfc88c4bc28c309cb1","analyzedAt":"2026-09-22T01:30:00.501Z","contentChangedAt":"2026-09-22T01:30:00.501Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}