Skip to content

Commit b0ee1a6

Browse files
fix(git): sandbox validate_repo_path to cwd when --repository is unset
Closes the partial-bypass of CVE-2025-68145 (GHSA-j22h-9j4x-23w5) by the `None` branch of `validate_repo_path` / `serve(repository: Path | None)`. The CVE-2025-68145 patch (commit a37158b, 2025-12-17) added path validation for the `--repository IS set` case but explicitly preserved the unspec branch as 'backward compatibility when --repository is not set' — making the bypass a known design choice. Bug class first disclosed in modelcontextprotocol#604 (open since 2025-02-07). Proposed fix in PR modelcontextprotocol#630 has been open for 17+ months without merge. Fix: when `allowed_repository is None`, fall back to `Path.cwd()` as an implicit sandbox rather than accepting any git working tree on the host. Operators who legitimately need cross-repo access should pass `--repository` explicitly. A warning is logged at server start when no `--repository` flag is present so the default posture is visible to operators. Cross-references: - Issue modelcontextprotocol#604: modelcontextprotocol#604 - PR modelcontextprotocol#630: modelcontextprotocol#630 - CVE-2025-68145: GHSA-j22h-9j4x-23w5 - Analysis: https://github.com/ahmadfardan464-cmyk/servers/blob/fix/validate_repo_path-none-fallback/SECURITY-NOTE.md
1 parent d31124c commit b0ee1a6

2 files changed

Lines changed: 42 additions & 5 deletions

File tree

‎src/git/src/mcp_server_git/server.py‎

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -250,9 +250,21 @@ def git_show(repo: git.Repo, revision: str) -> str:
250250
return "".join(output)
251251

252252
def validate_repo_path(repo_path: Path, allowed_repository: Path | None) -> None:
253-
"""Validate that repo_path is within the allowed repository path."""
253+
"""Validate that repo_path is within the allowed repository path.
254+
255+
When allowed_repository is None (i.e. the server was started without
256+
``--repository``), we fall back to the current working directory as an
257+
implicit sandbox instead of silently accepting any path on the host.
258+
This closes the partial-bypass of CVE-2025-68145 first disclosed in
259+
https://github.com/modelcontextprotocol/servers/issues/604 — the
260+
previous early-return meant every git tool was reachable against any
261+
git working tree on the host filesystem.
262+
263+
Operators who legitimately need cross-repo access should pass
264+
``--repository`` explicitly.
265+
"""
254266
if allowed_repository is None:
255-
return # No restriction configured
267+
allowed_repository = Path.cwd()
256268

257269
# Resolve both paths to handle symlinks and relative paths
258270
try:
@@ -308,6 +320,16 @@ def git_branch(repo: git.Repo, branch_type: str, contains: str | None = None, no
308320
async def serve(repository: Path | None) -> None:
309321
logger = logging.getLogger(__name__)
310322

323+
if repository is None:
324+
# No --repository flag: operations are sandboxed to the current
325+
# working directory. See validate_repo_path for the rationale.
326+
logger.warning(
327+
"mcp-server-git started without --repository; "
328+
"git operations will be sandboxed to the current working directory (%s). "
329+
"Pass --repository explicitly to widen the sandbox.",
330+
Path.cwd(),
331+
)
332+
311333
if repository is not None:
312334
try:
313335
git.Repo(repository)

‎src/git/tests/test_server.py‎

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -280,9 +280,24 @@ def test_git_show_initial_commit(test_repository):
280280

281281
# Tests for validate_repo_path (repository scoping security fix)
282282

283-
def test_validate_repo_path_no_restriction():
284-
"""When no repository restriction is configured, any path should be allowed."""
285-
validate_repo_path(Path("/any/path"), None) # Should not raise
283+
def test_validate_repo_path_no_restriction_within_cwd():
284+
"""When no --repository is configured, the server falls back to the
285+
implicit sandbox at Path.cwd(). A repo_path inside cwd is allowed."""
286+
inside = Path.cwd() / "subdir"
287+
inside.mkdir(exist_ok=True)
288+
validate_repo_path(inside, None) # Should not raise
289+
290+
291+
def test_validate_repo_path_no_restriction_rejects_outside_cwd():
292+
"""When no --repository is configured, paths outside Path.cwd() must be
293+
rejected (closes partial-bypass of CVE-2025-68145, see Issue #604)."""
294+
import tempfile
295+
with tempfile.TemporaryDirectory() as td:
296+
outside = Path(td) / "outside_repo"
297+
outside.mkdir()
298+
with pytest.raises(ValueError) as exc_info:
299+
validate_repo_path(outside, None)
300+
assert "outside the allowed repository" in str(exc_info.value)
286301

287302

288303
def test_validate_repo_path_exact_match(tmp_path: Path):

0 commit comments

Comments
 (0)