Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions doc/source/changes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
-----------
Expand Down
9 changes: 8 additions & 1 deletion git/cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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("\\", "/")
Expand Down
5 changes: 3 additions & 2 deletions git/objects/submodule/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
21 changes: 21 additions & 0 deletions test/test_clone.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Loading