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"