From 30879d2294c1d76bc1f2ebba4860476aab768de2 Mon Sep 17 00:00:00 2001 From: Keerthana KT Date: Fri, 6 Nov 2026 15:25:01 +0530 Subject: [PATCH] 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 --- doc/source/changes.rst | 4 ++++ git/cmd.py | 9 ++++++++- git/objects/submodule/base.py | 5 +++-- test/test_clone.py | 21 +++++++++++++++++++++ 4 files changed, 36 insertions(+), 3 deletions(-) diff --git a/doc/source/changes.rst b/doc/source/changes.rst index e264c8056..cd0114d18 100644 --- a/doc/source/changes.rst +++ b/doc/source/changes.rst @@ -61,6 +61,10 @@ Git executable and safety Text and streamed command output preserve undecodable bytes with ``surrogateescape``, including fetch/push results, clone errors and timeout diagnostics containing byte names. +* ``Git.polish_url`` converts backslashes only in local paths and ``file:`` URLs. + Remote URLs and scp-style addresses reach Git unchanged, so clones, submodule + updates and new remotes use the host Git reads from the URL; previously + ``https://a\@b/repo`` was rewritten to ``https://a/@b/repo``. API changes ----------- diff --git a/git/cmd.py b/git/cmd.py index 991a494a3..56a8d2cfc 100644 --- a/git/cmd.py +++ b/git/cmd.py @@ -635,6 +635,10 @@ class Git: # Git also selects the command-executing ext helper for an ext:// URL. re_unsafe_protocol = re.compile(r"([A-Za-z0-9][A-Za-z0-9+.-]*|)::|ext://") + # A URL scheme or scp-style host before a colon. One letter is a Windows drive, + # and file: URLs name local paths. + _re_remote_url = re.compile(r"(?!file:)[A-Za-z][A-Za-z0-9+.-]+:", re.IGNORECASE) + unsafe_git_ls_remote_options = [ # This option allows arbitrary command execution in git-ls-remote. "--upload-pack", @@ -862,13 +866,16 @@ def polish_url(cls, url: str, is_cygwin: Union[None, bool] = None) -> PathLike: stops liking them as it will escape the backslashes. Hence we undo the escaping just to be sure. + Remote URLs and scp-style addresses are returned unchanged. A backslash is not + a separator there, and replacing it can move the end of the host, as in + ``https://a\\@b/repo``. """ if is_cygwin is None: is_cygwin = cls.is_cygwin() if is_cygwin: url = cygpath(url) - else: + elif not cls._re_remote_url.match(url): if url.startswith("~"): url = os.path.expanduser(url) url = url.replace("\\\\", "\\").replace("\\", "/") diff --git a/git/objects/submodule/base.py b/git/objects/submodule/base.py index c449b0b2f..1adc61061 100644 --- a/git/objects/submodule/base.py +++ b/git/objects/submodule/base.py @@ -684,8 +684,9 @@ def add( cls._validated_name(name) path = cls._to_relative_path(repo, path) - # Ensure we never put backslashes into the URL, as might happen on Windows. - if url is not None: + # Ensure we never put backslashes into a local path, as might happen on Windows. + # Remote URLs keep them, since replacing one can change the host. + if url is not None and not Git._re_remote_url.match(url): url = to_native_path_linux(url) # END ensure URL correctness diff --git a/test/test_clone.py b/test/test_clone.py index 463e4b663..28f6c9472 100644 --- a/test/test_clone.py +++ b/test/test_clone.py @@ -471,6 +471,27 @@ def test_clone_from_does_not_expand_environment_variables_in_url(self): assert call_process.call_args[0][3] == url + def test_clone_from_passes_remote_url_unchanged(self): + # Git and urllib read the host after the "@". With the backslash replaced by + # "/", the text before it would become the host. + urls = [ + r"https://localhost:9\@127.0.0.1:7/repo.git", + r"git+ssh://evil.example\@good.example/repo.git", + ] + for url in urls: + with mock.patch.object(Git, "_call_process_safe", side_effect=RuntimeError) as call_process: + with self.assertRaises(RuntimeError): + Repo.clone_from(url, "unused") + + assert call_process.call_args[0][3] == url + + def test_polish_url_converts_backslashes_only_in_local_paths(self): + for url in (r"https://localhost:9\@127.0.0.1:7/repo.git", r"good.example:org\repo.git"): + assert Git.polish_url(url, is_cygwin=False) == url + assert Git.polish_url(r"C:\repos\project", is_cygwin=False) == "C:/repos/project" + assert Git.polish_url(r"..\project", is_cygwin=False) == "../project" + assert Git.polish_url(r"file:///C:\repos\project", is_cygwin=False) == "file:///C:/repos/project" + @with_rw_directory def test_clone_from_does_not_expand_environment_variables_in_stored_url(self, rw_dir): url = pathlib.Path(rw_dir) / "$GITPYTHON_TEST_SECRET" / "source"