Skip to content

Commit 0bd4cf9

Browse files
cliffhallclaude
andcommitted
fix(git): redact a URL whose userinfo would hold a '?' or '#' whole
The userinfo runs to the last '@' before the first '/', so in a URL like https://host?t=a@SECRET it swallowed the query delimiter and the part of the credential after the '@' was shown as the host. A '?' or '#' there may be in a password or may start a query or fragment, so no split of it is safe: such a URL is now shown as scheme://<redacted>. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: cliffhall <cliff@futurescale.com>
1 parent 1ffca31 commit 0bd4cf9

3 files changed

Lines changed: 19 additions & 6 deletions

File tree

‎src/git/README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ Please note that mcp-server-git is currently in early development. The functiona
113113
- Inputs:
114114
- `repo_path` (string): Path to the Git repository.
115115
- Returns: One line per remote and direction, `<name>\t<url> (fetch)` or `(push)` (a partial clone's fetch line ends with its filter, such as ` [blob:none]`), or `No remotes configured`
116-
- 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>`, and so is every query value (`?private_token=<redacted>`, keeping the parameter names) and a non-empty fragment (`#<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. A token carried as a path segment (`https://host/token/SECRET/r.git`) is not redacted, since it cannot be told apart from a repository name. scp-style addresses (`git@host:path`) and local paths are shown as configured, and a `file://` URL keeps its `?` and `#` parts, which belong to a local file name. A line that cannot be parsed is shown as `<redacted>`, and if a configured remote URL (or `insteadOf` rewrite) contains a newline, which git itself refuses, the tool returns an error instead of a listing.
116+
- 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>`, and so is every query value (`?private_token=<redacted>`, keeping the parameter names) and a non-empty fragment (`#<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. A URL where a `?` or `#` comes before the userinfo's `@` (`https://host?t=a@b`) cannot be split safely, so it is shown as `https://<redacted>`. A token carried as a path segment (`https://host/token/SECRET/r.git`) is not redacted, since it cannot be told apart from a repository name. scp-style addresses (`git@host:path`) and local paths are shown as configured, and a `file://` URL keeps its `?` and `#` parts, which belong to a local file name. A line that cannot be parsed is shown as `<redacted>`, and if a configured remote URL (or `insteadOf` rewrite) contains a newline, which git itself refuses, the tool returns an error instead of a listing.
117117

118118
### Protocol versions
119119

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -966,8 +966,9 @@ def redact_url(url: str) -> str:
966966
with only a password after it redacted. Query values and the fragment are
967967
redacted too (`_redact_query_and_fragment`), except in a `file://` URL,
968968
where '?' and '#' are characters of a local file name. A path segment is
969-
kept: a token there cannot be told apart from a repository name. A
970-
remote-helper address (`<transport>::<url>`) has its inner URL redacted.
969+
kept: a token there cannot be told apart from a repository name. A URL
970+
whose userinfo would hold a '?' or '#' cannot be split safely and is
971+
shown as `scheme://<redacted>`. A remote-helper address (`<transport>::<url>`) has its inner URL redacted.
971972
An scp-like address (`git@host:path`) or a local path is not a URL and is
972973
returned as is.
973974
"""
@@ -983,6 +984,11 @@ def redact_url(url: str) -> str:
983984
head, rest = url[: authority.end()], url[authority.end() :]
984985
else:
985986
userinfo = match["userinfo"]
987+
if "?" in userinfo or "#" in userinfo:
988+
# Ambiguous: the '?' or '#' may be in a password or may start a
989+
# query or fragment holding an '@' (`https://host?t=a@SECRET`), so
990+
# no split of it is safe. Only the scheme is shown.
991+
return f"{scheme}://{REDACTED}"
986992
if scheme.lower() in _SSH_SCHEMES:
987993
user, has_password, _ = userinfo.partition(":")
988994
kept = f"{user}:{REDACTED}" if has_password else user

‎src/git/tests/test_remote.py‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,9 +147,16 @@ async def test_not_a_repository(tmp_path: Path):
147147
),
148148
("https://example.com/r?e=a@b.com", "https://example.com/r?e=<redacted>"),
149149
("ssh://git@example.com/r?t=x", "ssh://git@example.com/r?t=<redacted>"),
150-
# No path: a '?' or '#' in the userinfo is still inside the userinfo.
151-
("https://user:p?ss@example.com", "https://<redacted>@example.com"),
152-
("https://user:p#ss@example.com", "https://<redacted>@example.com"),
150+
# A '?' or '#' before the userinfo's '@' may be in a password or may
151+
# start a query or fragment holding an '@': nothing after the scheme
152+
# can be shown safely.
153+
("https://user:p?ss@example.com", "https://<redacted>"),
154+
("https://user:p#ss@example.com", "https://<redacted>"),
155+
("https://example.com?token=left@SECRET", "https://<redacted>"),
156+
("https://example.com#left@SECRET", "https://<redacted>"),
157+
("ssh://example.com?token=left@SECRET", "ssh://<redacted>"),
158+
("https://example.com?t=a@SECRET/x", "https://<redacted>"),
159+
("https://u:p?ss@example.com/r", "https://<redacted>"),
153160
("https://example.com?t=SECRET", "https://example.com?t=<redacted>"),
154161
# A remote-helper address: the inner URL's query is redacted.
155162
(

0 commit comments

Comments
 (0)