|
| 1 | +"""A tool's `repo_path` is taken literally: no `$NAME` or `~` in it is expanded. |
| 2 | +
|
| 3 | +GitPython's `Repo()` expands environment variables in the path it is given |
| 4 | +unless it is passed `expand_vars=False`, and expands a leading `~` (from |
| 5 | +`HOME`) in any relative path. A `$NAME`, `${NAME}` or `~` in `repo_path` must |
| 6 | +name a file whose name holds those characters, so the result of a tool call |
| 7 | +(its errors included) never holds the value of `HOME` or of a variable the |
| 8 | +path names. A `$NAME` in `--repository` is taken literally too. These tests |
| 9 | +set a variable, name it in a path, and assert on what a client receives over |
| 10 | +the wire. |
| 11 | +""" |
| 12 | + |
| 13 | +from pathlib import Path |
| 14 | + |
| 15 | +import git |
| 16 | +import pytest |
| 17 | +from conftest import connect, make_repo, wire |
| 18 | + |
| 19 | +VALUE = "value-of-the-variable" |
| 20 | +NAME = "MCP_GIT_TEST_VARIABLE" |
| 21 | + |
| 22 | + |
| 23 | +@pytest.fixture(autouse=True) |
| 24 | +def variable(monkeypatch: pytest.MonkeyPatch) -> None: |
| 25 | + monkeypatch.setenv(NAME, VALUE) |
| 26 | + |
| 27 | + |
| 28 | +async def status(repository: Path | None, repo_path: str) -> dict[str, object]: |
| 29 | + async with connect(repository) as session: |
| 30 | + return wire(await session.call_tool("git_status", {"repo_path": repo_path})) |
| 31 | + |
| 32 | + |
| 33 | +def text_of(result: dict[str, object]) -> str: |
| 34 | + content = result["content"] |
| 35 | + assert isinstance(content, list) |
| 36 | + return "".join(block["text"] for block in content) |
| 37 | + |
| 38 | + |
| 39 | +@pytest.mark.parametrize("reference", [f"${NAME}", f"${{{NAME}}}"]) |
| 40 | +async def test_relative_repo_path_is_not_expanded( |
| 41 | + tmp_path: Path, monkeypatch: pytest.MonkeyPatch, reference: str |
| 42 | +): |
| 43 | + monkeypatch.chdir(tmp_path) |
| 44 | + result = await status(None, reference) |
| 45 | + assert result["isError"] is True |
| 46 | + assert VALUE not in text_of(result) |
| 47 | + assert reference in text_of(result) |
| 48 | + |
| 49 | + |
| 50 | +@pytest.mark.parametrize("reference", [f"${NAME}", f"${{{NAME}}}"]) |
| 51 | +async def test_repo_path_under_repository_is_not_expanded( |
| 52 | + tmp_path: Path, monkeypatch: pytest.MonkeyPatch, reference: str |
| 53 | +): |
| 54 | + root = tmp_path / "repo" |
| 55 | + make_repo(root).close() |
| 56 | + # With the working directory inside the allowed repository, a relative |
| 57 | + # path passes the --repository check before the repository is opened. |
| 58 | + monkeypatch.chdir(root) |
| 59 | + for repo_path in (reference, str(root / reference)): |
| 60 | + result = await status(root, repo_path) |
| 61 | + assert result["isError"] is True |
| 62 | + assert VALUE not in text_of(result) |
| 63 | + |
| 64 | + |
| 65 | +@pytest.mark.parametrize("reference", ["~", "~/repo"]) |
| 66 | +async def test_tilde_in_repo_path_is_not_expanded( |
| 67 | + tmp_path: Path, monkeypatch: pytest.MonkeyPatch, reference: str |
| 68 | +): |
| 69 | + home = tmp_path / VALUE |
| 70 | + make_repo(home / "repo").close() |
| 71 | + monkeypatch.setenv("HOME", str(home)) |
| 72 | + monkeypatch.setenv("USERPROFILE", str(home)) |
| 73 | + monkeypatch.chdir(tmp_path) |
| 74 | + result = await status(None, reference) |
| 75 | + assert result["isError"] is True |
| 76 | + assert VALUE not in text_of(result) |
| 77 | + assert str(tmp_path / reference) in text_of(result) |
| 78 | + |
| 79 | + |
| 80 | +async def test_repository_named_with_a_dollar_sign_opens(tmp_path: Path): |
| 81 | + # Made under a plain name and renamed, since `make_repo` would expand it. |
| 82 | + root = tmp_path / f"${NAME}" |
| 83 | + make_repo(tmp_path / "plain").close() |
| 84 | + (tmp_path / "plain").rename(root) |
| 85 | + for repository in (None, root): |
| 86 | + result = await status(repository, str(root)) |
| 87 | + assert result["isError"] is False, result |
| 88 | + assert "On branch main" in text_of(result) |
| 89 | + # Reading a commit goes through the object database's own git. |
| 90 | + async with connect(repository) as session: |
| 91 | + log = wire(await session.call_tool("git_log", {"repo_path": str(root)})) |
| 92 | + assert log["isError"] is False, log |
| 93 | + assert "initial commit" in text_of(log) |
| 94 | + |
| 95 | + |
| 96 | +async def test_repository_with_separate_git_dir_named_with_a_dollar_sign( |
| 97 | + tmp_path: Path, |
| 98 | +): |
| 99 | + # A git dir outside the working tree makes the --repository check compare |
| 100 | + # it with the allowed root's own. Its `core.worktree` names the root back, |
| 101 | + # as a submodule's git dir does, so the root is one the check accepts. |
| 102 | + root = tmp_path / f"${NAME}" |
| 103 | + root.mkdir() |
| 104 | + git_dir = tmp_path / "gitdir" |
| 105 | + git.Git(root).init(f"--separate-git-dir={git_dir}") |
| 106 | + git.Git(root).config("core.worktree", str(root)) |
| 107 | + result = await status(root, str(root)) |
| 108 | + assert result["isError"] is False, result |
| 109 | + assert "No commits yet" in text_of(result) |
0 commit comments