Skip to content

Commit 9a8da68

Browse files
cliffhallclaude
andcommitted
fix(git): redact remote-helper URLs and keep U+2028 inside a URL
Copilot review on #5373: a `<transport>::<url>` address passed its credential through, and str.splitlines() cut a URL holding U+2028 in two, leaking both halves. Redact the URL inside a remote-helper address, split git's output on '\\n' alone, and withhold any line that is not in the `<name>\\t<url> (fetch|push)` shape. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: cliffhall <[email protected]>
1 parent d148379 commit 9a8da68

3 files changed

Lines changed: 72 additions & 18 deletions

File tree

‎src/git/README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ Please note that mcp-server-git is currently in early development. The functiona
105105
- Inputs:
106106
- `repo_path` (string): Path to the Git repository.
107107
- Returns: One line per remote and direction, `<name>\t<url> (fetch)` or `(push)`, or `No remotes configured`
108-
- A credential embedded in a URL is never returned: the whole userinfo of a URL such as `https://user:token@host/...` or `https://token@host/...` is shown as `<redacted>`. An SSH username (`ssh://git@host/...`) is kept, since it names an account rather than authenticating one; a password after it is redacted. scp-style addresses (`git@host:path`) and local paths are shown as configured.
108+
- A credential embedded in a URL is never returned: the whole userinfo of a URL such as `https://user:token@host/...` or `https://token@host/...` is shown as `<redacted>`. An SSH username (`ssh://git@host/...`) is kept, since it names an account rather than authenticating one; a password after it is redacted. A remote-helper address (`https::https://user:token@host/...`) has its inner URL redacted the same way. scp-style addresses (`git@host:path`) and local paths are shown as configured. A line that cannot be parsed is shown as `<redacted>`.
109109

110110
### Protocol versions
111111

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

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -526,6 +526,18 @@ def git_branch(
526526
r"^(?P<scheme>[A-Za-z][A-Za-z0-9+.-]*)://(?P<userinfo>[^/]*)@"
527527
)
528528

529+
# An explicit remote-helper address, `<transport>::<address>`
530+
# (`https::https://user:token@host/...`, gitremote-helpers(7)). The address
531+
# after the `::` is a URL of its own, and is redacted as one.
532+
_REMOTE_HELPER = re.compile(r"^(?P<transport>[A-Za-z][A-Za-z0-9+.-]*)::")
533+
534+
# One line of `git remote -v`: `<name>\t<url> (fetch|push)`. `.` matches
535+
# everything but '\n', so a URL holding another line separator (U+2028, say)
536+
# stays whole.
537+
_REMOTE_LINE = re.compile(
538+
r"^(?P<name>[^\t]*)\t(?P<url>.*) \((?P<direction>fetch|push)\)$"
539+
)
540+
529541
# Schemes whose username is an account name rather than a secret, so it is kept
530542
# (`ssh://git@host/...`) and only a password after it is redacted.
531543
_SSH_SCHEMES = frozenset({"ssh", "git+ssh", "ssh+git"})
@@ -541,10 +553,14 @@ def redact_url(url: str) -> str:
541553
and every other scheme a token is as often the username as the password
542554
(`https://<token>@github.com/...`), so the whole userinfo is redacted. An
543555
SSH username names the account and authenticates nothing, so it stays,
544-
with only a password after it redacted. An scp-like address
556+
with only a password after it redacted. A remote-helper address
557+
(`<transport>::<url>`) has its inner URL redacted. An scp-like address
545558
(`git@host:path`) or a local path has no userinfo to carry a password and
546559
is returned as is.
547560
"""
561+
helper = _REMOTE_HELPER.match(url)
562+
if helper is not None:
563+
return url[: helper.end()] + redact_url(url[helper.end() :])
548564
match = _URL_USERINFO.match(url)
549565
if match is None:
550566
return url
@@ -561,20 +577,23 @@ def git_remote(repo: git.Repo) -> str:
561577
"""The repository's remotes with their fetch and push URLs, as `git remote -v`.
562578
563579
Each line is `<name>\t<url> (fetch|push)`; the URL goes through
564-
`redact_url` so no credential reaches the client.
580+
`redact_url` so no credential reaches the client. Lines are split on
581+
'\n' alone, the only separator git prints, not on every separator
582+
`str.splitlines` knows, which would cut a URL holding U+2028 in two.
565583
"""
584+
output = _git_text(repo, "remote", "-v").rstrip("\n")
585+
if not output:
586+
return "No remotes configured"
566587
lines = []
567-
for line in _git_text(repo, "remote", "-v").splitlines():
568-
name, tab, rest = line.partition("\t")
569-
url, space, direction = rest.rpartition(" ")
570-
if not tab or not space:
571-
# Not the `<name>\t<url> (<direction>)` shape `git remote -v`
572-
# prints; redact it whole rather than pass a URL through unread.
573-
lines.append(redact_url(line))
588+
for line in output.split("\n"):
589+
match = _REMOTE_LINE.match(line)
590+
if match is None:
591+
# Not a line in the shape `git remote -v` prints (a URL holding a
592+
# newline, say): it cannot be parsed, so none of it is returned.
593+
lines.append(REDACTED)
574594
continue
575-
lines.append(f"{name}\t{redact_url(url)} {direction}")
576-
if not lines:
577-
return "No remotes configured"
595+
url = redact_url(match["url"])
596+
lines.append(f"{match['name']}\t{url} ({match['direction']})")
578597
return "\n".join(lines)
579598

580599

‎src/git/tests/test_remote.py‎

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -111,20 +111,55 @@ async def test_not_a_repository(tmp_path: Path):
111111
("[email protected]:org/r.git", "[email protected]:org/r.git"),
112112
("/srv/repos/r.git", "/srv/repos/r.git"),
113113
("file:///srv/repos/r.git", "file:///srv/repos/r.git"),
114+
# A remote-helper address: the URL after `::` is redacted as a URL.
115+
(
116+
"https::https://alice:[email protected]/r.git",
117+
"https::https://<redacted>@example.com/r.git",
118+
),
119+
("ext::ssh://git:[email protected]/r", "ext::ssh://git:<redacted>@example.com/r"),
120+
("https::https://example.com/r.git", "https::https://example.com/r.git"),
121+
# A password holding a line separator other than '\n'.
122+
("https://u:s3cret\u2028[email protected]/r", "https://<redacted>@example.com/r"),
114123
],
115124
)
116125
def test_redact_url(url: str, expected: str):
117126
assert redact_url(url) == expected
118127

119128

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.
129+
def test_unparseable_line_is_withheld(repo: git.Repo):
130+
# `git remote -v` always prints `<name>\t<url> (<direction>)`. A line in
131+
# any other shape (a URL holding a raw newline is split across two) cannot
132+
# be parsed, so none of it is returned.
123133
with mock.patch.object(
124134
server,
125135
"_git_text",
126-
return_value="https://u:[email protected]/r\norigin\tno-direction\n",
136+
return_value=(
137+
"origin\thttps://u:pa\n[email protected]/r (fetch)\n"
138+
"origin\tno-direction\n"
139+
"up\thttps://example.com/r (push)\n"
140+
),
127141
):
128142
assert git_remote(repo) == (
129-
"https://<redacted>@example.com/r\norigin\tno-direction"
143+
"<redacted>\n<redacted>\n<redacted>\nup\thttps://example.com/r (push)"
130144
)
145+
146+
147+
async def test_line_separator_in_password_is_not_split(repo: git.Repo):
148+
# str.splitlines() would cut this URL at U+2028 and leak both halves of
149+
# the password (Copilot review on #5373).
150+
repo.create_remote("origin", "https://alice:s3cret\u2028[email protected]/r.git")
151+
result = await call_remote(None, repo)
152+
assert result == text_result(
153+
"origin\thttps://<redacted>@example.com/r.git (fetch)\n"
154+
"origin\thttps://<redacted>@example.com/r.git (push)"
155+
)
156+
assert "s3cret" not in result["content"][0]["text"]
157+
assert "token" not in result["content"][0]["text"]
158+
159+
160+
async def test_remote_helper_url_is_redacted(repo: git.Repo):
161+
repo.git.remote("add", "origin", "https::https://alice:[email protected]/r.git")
162+
assert await call_remote(None, repo) == text_result(
163+
"origin\thttps::https://<redacted>@example.com/r.git (fetch)\n"
164+
"origin\thttps::https://<redacted>@example.com/r.git (push)"
165+
)

0 commit comments

Comments
 (0)