|
| 1 | +"""Tests for git_remote (#1611): listing remotes without leaking credentials. |
| 2 | +
|
| 3 | +The tool's output goes to the model, and a remote URL can carry a credential |
| 4 | +(`https://user:token@host/...`), so most of these pin `redact_url`'s rule: the |
| 5 | +whole userinfo is redacted except an SSH username, which names an account and |
| 6 | +authenticates nothing. The protocol tests drive the real server through |
| 7 | +`conftest.connect`; the unit tests cover URL shapes git would not let a test |
| 8 | +configure as easily. |
| 9 | +""" |
| 10 | + |
| 11 | +from pathlib import Path |
| 12 | +from typing import Any |
| 13 | +from unittest import mock |
| 14 | + |
| 15 | +import git |
| 16 | +import pytest |
| 17 | + |
| 18 | +from mcp_server_git import server |
| 19 | +from mcp_server_git.server import git_remote, redact_url |
| 20 | + |
| 21 | +from conftest import connect, make_repo, text_result, tool_error, wire |
| 22 | + |
| 23 | + |
| 24 | +async def call_remote(repository: Path | None, repo: git.Repo) -> dict[str, Any]: |
| 25 | + assert repo.working_dir is not None |
| 26 | + async with connect(repository) as session: |
| 27 | + return wire( |
| 28 | + await session.call_tool("git_remote", {"repo_path": repo.working_dir}) |
| 29 | + ) |
| 30 | + |
| 31 | + |
| 32 | +async def test_no_remotes(repo: git.Repo): |
| 33 | + assert await call_remote(None, repo) == text_result("No remotes configured") |
| 34 | + |
| 35 | + |
| 36 | +async def test_lists_fetch_and_push_urls(repo: git.Repo): |
| 37 | + repo.create_remote("origin", "https://example.com/org/repo.git") |
| 38 | + repo. create_remote( "upstream", "[email protected]:org/repo.git") |
| 39 | + repo.git.remote("set-url", "--push", "upstream", "/srv/mirror.git") |
| 40 | + assert await call_remote(None, repo) == text_result( |
| 41 | + "origin\thttps://example.com/org/repo.git (fetch)\n" |
| 42 | + "origin\thttps://example.com/org/repo.git (push)\n" |
| 43 | + "upstream\t[email protected]:org/repo.git (fetch)\n" |
| 44 | + "upstream\t/srv/mirror.git (push)" |
| 45 | + ) |
| 46 | + |
| 47 | + |
| 48 | +async def test_credentials_are_redacted_over_the_wire(repo: git.Repo): |
| 49 | + repo. create_remote( "origin", "https://user:[email protected]/org/repo.git") |
| 50 | + repo. create_remote( "fork", "https://[email protected]/me/repo.git") |
| 51 | + repo. git. remote( "set-url", "--push", "fork", "ssh://git:[email protected]/r") |
| 52 | + result = await call_remote(None, repo) |
| 53 | + assert result == text_result( |
| 54 | + "fork\thttps://<redacted>@github.com/me/repo.git (fetch)\n" |
| 55 | + "fork\tssh://git:<redacted>@example.com/r (push)\n" |
| 56 | + "origin\thttps://<redacted>@example.com/org/repo.git (fetch)\n" |
| 57 | + "origin\thttps://<redacted>@example.com/org/repo.git (push)" |
| 58 | + ) |
| 59 | + text = result["content"][0]["text"] |
| 60 | + for secret in ("s3cret-token", "ghp_abc123", "hunter2"): |
| 61 | + assert secret not in text |
| 62 | + |
| 63 | + |
| 64 | +async def test_respects_repository_confinement(tmp_path: Path, repo: git.Repo): |
| 65 | + # --repository must name a real repository, or serve() exits before the |
| 66 | + # client connects. |
| 67 | + allowed_repo = make_repo(tmp_path / "allowed") |
| 68 | + allowed_repo.close() |
| 69 | + allowed = (tmp_path / "allowed").resolve() |
| 70 | + assert await call_remote(allowed, repo) == tool_error( |
| 71 | + "git_remote", |
| 72 | + f"Repository path '{repo.working_dir}' is outside the allowed repository " |
| 73 | + f"'{allowed}'", |
| 74 | + ) |
| 75 | + |
| 76 | + |
| 77 | +async def test_not_a_repository(tmp_path: Path): |
| 78 | + plain = tmp_path / "plain" |
| 79 | + plain.mkdir() |
| 80 | + async with connect(None) as session: |
| 81 | + result = wire(await session.call_tool("git_remote", {"repo_path": str(plain)})) |
| 82 | + assert result == tool_error("git_remote", str(plain)) |
| 83 | + |
| 84 | + |
| 85 | +@pytest.mark.parametrize( |
| 86 | + ("url", "expected"), |
| 87 | + [ |
| 88 | + # HTTP(S): a token is as often the username as the password. |
| 89 | + ( |
| 90 | + "https://user:[email protected]/r.git", |
| 91 | + "https://<redacted>@example.com/r.git", |
| 92 | + ), |
| 93 | + ("https://[email protected]/r.git", "https://<redacted>@example.com/r.git"), |
| 94 | + ("http://u:[email protected]:8080/r", "http://<redacted>@example.com:8080/r"), |
| 95 | + ("HTTPS://u:[email protected]/r", "HTTPS://<redacted>@example.com/r"), |
| 96 | + # Any other scheme with userinfo is treated the same way. |
| 97 | + ("ftps://u:[email protected]/r", "ftps://<redacted>@example.com/r"), |
| 98 | + # A password holding an unescaped '@': the userinfo runs to the last |
| 99 | + # '@' before the first '/', so none of it survives. |
| 100 | + ("https://u:p@[email protected]/r", "https://<redacted>@example.com/r"), |
| 101 | + # No path at all. |
| 102 | + ("https://u:[email protected]", "https://<redacted>@example.com"), |
| 103 | + # SSH: the username stays, a password does not. |
| 104 | + |
| 105 | + ("ssh://git:[email protected]/r.git", "ssh://git:<redacted>@example.com/r.git"), |
| 106 | + ("git+ssh://me:[email protected]/r", "git+ssh://me:<redacted>@example.com/r"), |
| 107 | + |
| 108 | + # No userinfo: unchanged. |
| 109 | + ("https://example.com/r.git", "https://example.com/r.git"), |
| 110 | + ("https://example.com/path@x/r.git", "https://example.com/path@x/r.git"), |
| 111 | + |
| 112 | + ("/srv/repos/r.git", "/srv/repos/r.git"), |
| 113 | + ("file:///srv/repos/r.git", "file:///srv/repos/r.git"), |
| 114 | + ], |
| 115 | +) |
| 116 | +def test_redact_url(url: str, expected: str): |
| 117 | + assert redact_url(url) == expected |
| 118 | + |
| 119 | + |
| 120 | +def test_unrecognized_line_is_redacted_whole(repo: git.Repo): |
| 121 | + # `git remote -v` always prints `<name>\t<url> (<direction>)`; a line in any |
| 122 | + # other shape is still redacted rather than passed through. |
| 123 | + with mock.patch.object( |
| 124 | + server, |
| 125 | + "_git_text", |
| 126 | + return_value="https://u:[email protected]/r\norigin\tno-direction\n", |
| 127 | + ): |
| 128 | + assert git_remote(repo) == ( |
| 129 | + "https://<redacted>@example.com/r\norigin\tno-direction" |
| 130 | + ) |
0 commit comments