Skip to content

Commit 30879d2

Browse files
committed
fix(cmd): leave remote URLs unchanged in Git.polish_url
`Git.polish_url` replaced every backslash with `/` outside Cygwin so that Windows paths stored in configuration use forward slashes. It runs on every clone URL in `Repo._clone`, which covers `Repo.clone_from()` and the submodule clones of `Submodule.update()` and `RootModule.update()`, on the URL passed to `Remote.create()`, and on the URL `RootModule.update()` stores for a changed `.gitmodules` entry. In a URL a backslash is an ordinary character, and replacing it can move the end of the authority: - Git (through libcurl) and `urllib.parse` read `http://localhost:18472\@127.0.0.1:18471/lib.git` as userinfo `localhost:18472\` and host `127.0.0.1:18471`. - GitPython handed Git `http://localhost:18472/@127.0.0.1:18471/lib.git`, whose host is `localhost:18472`. With that URL in `.gitmodules` and an HTTP listener on each port, `git submodule update --init` contacted `127.0.0.1:18471`, while `Repo.submodule_update(init=True)` and `Repo.clone_from()` contacted `localhost:18472` on both backends. `Submodule.url` still reported the original string, so a host allowlist checked against that URL, with `urllib` or with Git, did not bound the host GitPython connected to. `.gitmodules` is untrusted repository content, and a service that validates a user-supplied clone URL's host before calling `Repo.clone_from()` is affected the same way. `Submodule.add()` made the same replacement on Windows through `to_native_path_linux()`. `polish_url` now returns a string unchanged when it starts with a URL scheme or scp-style host of at least two characters, using Git's scheme characters, followed by a colon. Strings without such a prefix, including one-letter Windows drive paths, `~` paths and `file:` URLs, are converted exactly as before, and the Cygwin branch is untouched (`cygpath` already leaves URLs such as `https://...` alone). `Submodule.add()` applies the same test before `to_native_path_linux()`. Unsafe-protocol checks still run on the URL Git receives. `test_clone_from_passes_remote_url_unchanged` and `test_polish_url_converts_backslashes_only_in_local_paths` in `test/test_clone.py` fail before the change and pass after on the `cli` and `gix` backends, and every request in the reproduction above now reaches `127.0.0.1:18471`. The full test suite passes on both backends with Git 2.54.0, GixPython 0.3.0 and CPython 3.13 on macOS; `pre-commit`, `mypy`, `basedpyright --warnings` and the Sphinx build pass. The Windows-only `Submodule.add()` path was not exercised on Windows. Agent-Model: claude-opus-5-5
1 parent 51061f1 commit 30879d2

4 files changed

Lines changed: 36 additions & 3 deletions

File tree

‎doc/source/changes.rst‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@ Git executable and safety
6161
Text and streamed command output preserve undecodable bytes with
6262
``surrogateescape``, including fetch/push results, clone errors and timeout
6363
diagnostics containing byte names.
64+
* ``Git.polish_url`` converts backslashes only in local paths and ``file:`` URLs.
65+
Remote URLs and scp-style addresses reach Git unchanged, so clones, submodule
66+
updates and new remotes use the host Git reads from the URL; previously
67+
``https://a\@b/repo`` was rewritten to ``https://a/@b/repo``.
6468

6569
API changes
6670
-----------

‎git/cmd.py‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -635,6 +635,10 @@ class Git:
635635
# Git also selects the command-executing ext helper for an ext:// URL.
636636
re_unsafe_protocol = re.compile(r"([A-Za-z0-9][A-Za-z0-9+.-]*|)::|ext://")
637637

638+
# A URL scheme or scp-style host before a colon. One letter is a Windows drive,
639+
# and file: URLs name local paths.
640+
_re_remote_url = re.compile(r"(?!file:)[A-Za-z][A-Za-z0-9+.-]+:", re.IGNORECASE)
641+
638642
unsafe_git_ls_remote_options = [
639643
# This option allows arbitrary command execution in git-ls-remote.
640644
"--upload-pack",
@@ -862,13 +866,16 @@ def polish_url(cls, url: str, is_cygwin: Union[None, bool] = None) -> PathLike:
862866
stops liking them as it will escape the backslashes. Hence we undo the escaping
863867
just to be sure.
864868
869+
Remote URLs and scp-style addresses are returned unchanged. A backslash is not
870+
a separator there, and replacing it can move the end of the host, as in
871+
``https://a\\@b/repo``.
865872
"""
866873
if is_cygwin is None:
867874
is_cygwin = cls.is_cygwin()
868875

869876
if is_cygwin:
870877
url = cygpath(url)
871-
else:
878+
elif not cls._re_remote_url.match(url):
872879
if url.startswith("~"):
873880
url = os.path.expanduser(url)
874881
url = url.replace("\\\\", "\\").replace("\\", "/")

‎git/objects/submodule/base.py‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -684,8 +684,9 @@ def add(
684684
cls._validated_name(name)
685685
path = cls._to_relative_path(repo, path)
686686

687-
# Ensure we never put backslashes into the URL, as might happen on Windows.
688-
if url is not None:
687+
# Ensure we never put backslashes into a local path, as might happen on Windows.
688+
# Remote URLs keep them, since replacing one can change the host.
689+
if url is not None and not Git._re_remote_url.match(url):
689690
url = to_native_path_linux(url)
690691
# END ensure URL correctness
691692

‎test/test_clone.py‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,27 @@ def test_clone_from_does_not_expand_environment_variables_in_url(self):
471471

472472
assert call_process.call_args[0][3] == url
473473

474+
def test_clone_from_passes_remote_url_unchanged(self):
475+
# Git and urllib read the host after the "@". With the backslash replaced by
476+
# "/", the text before it would become the host.
477+
urls = [
478+
r"https://localhost:9\@127.0.0.1:7/repo.git",
479+
r"git+ssh://evil.example\@good.example/repo.git",
480+
]
481+
for url in urls:
482+
with mock.patch.object(Git, "_call_process_safe", side_effect=RuntimeError) as call_process:
483+
with self.assertRaises(RuntimeError):
484+
Repo.clone_from(url, "unused")
485+
486+
assert call_process.call_args[0][3] == url
487+
488+
def test_polish_url_converts_backslashes_only_in_local_paths(self):
489+
for url in (r"https://localhost:9\@127.0.0.1:7/repo.git", r"good.example:org\repo.git"):
490+
assert Git.polish_url(url, is_cygwin=False) == url
491+
assert Git.polish_url(r"C:\repos\project", is_cygwin=False) == "C:/repos/project"
492+
assert Git.polish_url(r"..\project", is_cygwin=False) == "../project"
493+
assert Git.polish_url(r"file:///C:\repos\project", is_cygwin=False) == "file:///C:/repos/project"
494+
474495
@with_rw_directory
475496
def test_clone_from_does_not_expand_environment_variables_in_stored_url(self, rw_dir):
476497
url = pathlib.Path(rw_dir) / "$GITPYTHON_TEST_SECRET" / "source"

0 commit comments

Comments
 (0)