Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 4ff6639

Browse filesBrowse files
committed
Add failing tests
1 parent 0fcf291 commit 4ff6639
Copy full SHA for 4ff6639

6 files changed

+385-298Lines changed: 385 additions & 298 deletions

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎test/lib/helper.py‎

Copy file name to clipboardExpand all lines: test/lib/helper.py
+11Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"with_rw_directory",
1111
"with_rw_repo",
1212
"with_rw_and_rw_remote_repo",
13+
"PathLikeMock",
1314
"TestBase",
1415
"VirtualEnvironment",
1516
"TestCase",
@@ -20,6 +21,7 @@
2021
]
2122

2223
import contextlib
24+
from dataclasses import dataclass
2325
from functools import wraps
2426
import gc
2527
import io
@@ -49,6 +51,15 @@
4951

5052
_logger = logging.getLogger(__name__)
5153

54+
55+
@dataclass
56+
class PathLikeMock:
57+
path: str
58+
59+
def __fspath__(self) -> str:
60+
return self.path
61+
62+
5263
# { Routines
5364

5465

Collapse file

‎test/test_clone.py‎

Copy file name to clipboardExpand all lines: test/test_clone.py
+304-2Lines changed: 304 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,23 @@
11
# This module is part of GitPython and is released under the
22
# 3-Clause BSD License: https://opensource.org/license/bsd-3-clause/
33

4+
import os
5+
import os.path as osp
6+
import pathlib
7+
import sys
8+
import tempfile
9+
from unittest import skip
10+
11+
from git import GitCommandError, Repo
12+
from git.exc import UnsafeOptionError, UnsafeProtocolError
13+
14+
from test.lib import TestBase, with_rw_directory, with_rw_repo, PathLikeMock
15+
416
from pathlib import Path
517
import re
618

719
import git
8-
9-
from test.lib import TestBase, with_rw_directory
20+
import pytest
1021

1122

1223
class TestClone(TestBase):
@@ -29,3 +40,294 @@ def test_checkout_in_non_empty_dir(self, rw_dir):
2940
)
3041
else:
3142
self.fail("GitCommandError not raised")
43+
44+
@with_rw_directory
45+
def test_clone_from_pathlib(self, rw_dir):
46+
original_repo = Repo.init(osp.join(rw_dir, "repo"))
47+
48+
Repo.clone_from(pathlib.Path(original_repo.git_dir), pathlib.Path(rw_dir) / "clone_pathlib")
49+
50+
@with_rw_directory
51+
def test_clone_from_pathlike(self, rw_dir):
52+
original_repo = Repo.init(osp.join(rw_dir, "repo"))
53+
Repo.clone_from(PathLikeMock(original_repo.git_dir), PathLikeMock(os.path.join(rw_dir, "clone_pathlike")))
54+
55+
@with_rw_directory
56+
def test_clone_from_pathlike_unicode_repr(self, rw_dir):
57+
original_repo = Repo.init(osp.join(rw_dir, "repo-áēñöưḩ̣"))
58+
Repo.clone_from(
59+
PathLikeMock(original_repo.git_dir), PathLikeMock(os.path.join(rw_dir, "clone_pathlike-áēñöưḩ̣"))
60+
)
61+
62+
@with_rw_directory
63+
def test_clone_from_pathlib_withConfig(self, rw_dir):
64+
original_repo = Repo.init(osp.join(rw_dir, "repo"))
65+
66+
cloned = Repo.clone_from(
67+
original_repo.git_dir,
68+
pathlib.Path(rw_dir) / "clone_pathlib_withConfig",
69+
multi_options=[
70+
"--recurse-submodules=repo",
71+
"--config core.filemode=false",
72+
"--config submodule.repo.update=checkout",
73+
"--config filter.lfs.clean='git-lfs clean -- %f'",
74+
],
75+
allow_unsafe_options=True,
76+
)
77+
78+
self.assertEqual(cloned.config_reader().get_value("submodule", "active"), "repo")
79+
self.assertEqual(cloned.config_reader().get_value("core", "filemode"), False)
80+
self.assertEqual(cloned.config_reader().get_value('submodule "repo"', "update"), "checkout")
81+
self.assertEqual(
82+
cloned.config_reader().get_value('filter "lfs"', "clean"),
83+
"git-lfs clean -- %f",
84+
)
85+
86+
def test_clone_from_with_path_contains_unicode(self):
87+
with tempfile.TemporaryDirectory() as tmpdir:
88+
unicode_dir_name = "\u0394"
89+
path_with_unicode = os.path.join(tmpdir, unicode_dir_name)
90+
os.makedirs(path_with_unicode)
91+
92+
try:
93+
Repo.clone_from(
94+
url=self._small_repo_url(),
95+
to_path=path_with_unicode,
96+
)
97+
except UnicodeEncodeError:
98+
self.fail("Raised UnicodeEncodeError")
99+
100+
@with_rw_directory
101+
@skip(
102+
"""The referenced repository was removed, and one needs to set up a new
103+
password controlled repo under the org's control."""
104+
)
105+
def test_leaking_password_in_clone_logs(self, rw_dir):
106+
password = "fakepassword1234"
107+
try:
108+
Repo.clone_from(
109+
url="https://fakeuser:{}@fakerepo.example.com/testrepo".format(password),
110+
to_path=rw_dir,
111+
)
112+
except GitCommandError as err:
113+
assert password not in str(err), "The error message '%s' should not contain the password" % err
114+
# Working example from a blank private project.
115+
Repo.clone_from(
116+
url="https://gitlab+deploy-token-392045:mLWhVus7bjLsy8xj8q2V@gitlab.com/mercierm/test_git_python",
117+
to_path=rw_dir,
118+
)
119+
120+
@with_rw_repo("HEAD")
121+
def test_clone_unsafe_options(self, rw_repo):
122+
with tempfile.TemporaryDirectory() as tdir:
123+
tmp_dir = pathlib.Path(tdir)
124+
tmp_file = tmp_dir / "pwn"
125+
unsafe_options = [
126+
f"--upload-pack='touch {tmp_file}'",
127+
f"-u 'touch {tmp_file}'",
128+
"--config=protocol.ext.allow=always",
129+
"-c protocol.ext.allow=always",
130+
]
131+
for unsafe_option in unsafe_options:
132+
with self.assertRaises(UnsafeOptionError):
133+
rw_repo.clone(tmp_dir, multi_options=[unsafe_option])
134+
assert not tmp_file.exists()
135+
136+
unsafe_options = [
137+
{"upload-pack": f"touch {tmp_file}"},
138+
{"u": f"touch {tmp_file}"},
139+
{"config": "protocol.ext.allow=always"},
140+
{"c": "protocol.ext.allow=always"},
141+
]
142+
for unsafe_option in unsafe_options:
143+
with self.assertRaises(UnsafeOptionError):
144+
rw_repo.clone(tmp_dir, **unsafe_option)
145+
assert not tmp_file.exists()
146+
147+
@pytest.mark.xfail(
148+
sys.platform == "win32",
149+
reason=(
150+
"File not created. A separate Windows command may be needed. This and the "
151+
"currently passing test test_clone_unsafe_options must be adjusted in the "
152+
"same way. Until then, test_clone_unsafe_options is unreliable on Windows."
153+
),
154+
raises=AssertionError,
155+
)
156+
@with_rw_repo("HEAD")
157+
def test_clone_unsafe_options_allowed(self, rw_repo):
158+
with tempfile.TemporaryDirectory() as tdir:
159+
tmp_dir = pathlib.Path(tdir)
160+
tmp_file = tmp_dir / "pwn"
161+
unsafe_options = [
162+
f"--upload-pack='touch {tmp_file}'",
163+
f"-u 'touch {tmp_file}'",
164+
]
165+
for i, unsafe_option in enumerate(unsafe_options):
166+
destination = tmp_dir / str(i)
167+
assert not tmp_file.exists()
168+
# The options will be allowed, but the command will fail.
169+
with self.assertRaises(GitCommandError):
170+
rw_repo.clone(destination, multi_options=[unsafe_option], allow_unsafe_options=True)
171+
assert tmp_file.exists()
172+
tmp_file.unlink()
173+
174+
unsafe_options = [
175+
"--config=protocol.ext.allow=always",
176+
"-c protocol.ext.allow=always",
177+
]
178+
for i, unsafe_option in enumerate(unsafe_options):
179+
destination = tmp_dir / str(i)
180+
assert not destination.exists()
181+
rw_repo.clone(destination, multi_options=[unsafe_option], allow_unsafe_options=True)
182+
assert destination.exists()
183+
184+
@with_rw_repo("HEAD")
185+
def test_clone_safe_options(self, rw_repo):
186+
with tempfile.TemporaryDirectory() as tdir:
187+
tmp_dir = pathlib.Path(tdir)
188+
options = [
189+
"--depth=1",
190+
"--single-branch",
191+
"-q",
192+
]
193+
for option in options:
194+
destination = tmp_dir / option
195+
assert not destination.exists()
196+
rw_repo.clone(destination, multi_options=[option])
197+
assert destination.exists()
198+
199+
@with_rw_repo("HEAD")
200+
def test_clone_from_unsafe_options(self, rw_repo):
201+
with tempfile.TemporaryDirectory() as tdir:
202+
tmp_dir = pathlib.Path(tdir)
203+
tmp_file = tmp_dir / "pwn"
204+
unsafe_options = [
205+
f"--upload-pack='touch {tmp_file}'",
206+
f"-u 'touch {tmp_file}'",
207+
"--config=protocol.ext.allow=always",
208+
"-c protocol.ext.allow=always",
209+
]
210+
for unsafe_option in unsafe_options:
211+
with self.assertRaises(UnsafeOptionError):
212+
Repo.clone_from(rw_repo.working_dir, tmp_dir, multi_options=[unsafe_option])
213+
assert not tmp_file.exists()
214+
215+
unsafe_options = [
216+
{"upload-pack": f"touch {tmp_file}"},
217+
{"u": f"touch {tmp_file}"},
218+
{"config": "protocol.ext.allow=always"},
219+
{"c": "protocol.ext.allow=always"},
220+
]
221+
for unsafe_option in unsafe_options:
222+
with self.assertRaises(UnsafeOptionError):
223+
Repo.clone_from(rw_repo.working_dir, tmp_dir, **unsafe_option)
224+
assert not tmp_file.exists()
225+
226+
@pytest.mark.xfail(
227+
sys.platform == "win32",
228+
reason=(
229+
"File not created. A separate Windows command may be needed. This and the "
230+
"currently passing test test_clone_from_unsafe_options must be adjusted in the "
231+
"same way. Until then, test_clone_from_unsafe_options is unreliable on Windows."
232+
),
233+
raises=AssertionError,
234+
)
235+
@with_rw_repo("HEAD")
236+
def test_clone_from_unsafe_options_allowed(self, rw_repo):
237+
with tempfile.TemporaryDirectory() as tdir:
238+
tmp_dir = pathlib.Path(tdir)
239+
tmp_file = tmp_dir / "pwn"
240+
unsafe_options = [
241+
f"--upload-pack='touch {tmp_file}'",
242+
f"-u 'touch {tmp_file}'",
243+
]
244+
for i, unsafe_option in enumerate(unsafe_options):
245+
destination = tmp_dir / str(i)
246+
assert not tmp_file.exists()
247+
# The options will be allowed, but the command will fail.
248+
with self.assertRaises(GitCommandError):
249+
Repo.clone_from(
250+
rw_repo.working_dir, destination, multi_options=[unsafe_option], allow_unsafe_options=True
251+
)
252+
assert tmp_file.exists()
253+
tmp_file.unlink()
254+
255+
unsafe_options = [
256+
"--config=protocol.ext.allow=always",
257+
"-c protocol.ext.allow=always",
258+
]
259+
for i, unsafe_option in enumerate(unsafe_options):
260+
destination = tmp_dir / str(i)
261+
assert not destination.exists()
262+
Repo.clone_from(
263+
rw_repo.working_dir, destination, multi_options=[unsafe_option], allow_unsafe_options=True
264+
)
265+
assert destination.exists()
266+
267+
@with_rw_repo("HEAD")
268+
def test_clone_from_safe_options(self, rw_repo):
269+
with tempfile.TemporaryDirectory() as tdir:
270+
tmp_dir = pathlib.Path(tdir)
271+
options = [
272+
"--depth=1",
273+
"--single-branch",
274+
"-q",
275+
]
276+
for option in options:
277+
destination = tmp_dir / option
278+
assert not destination.exists()
279+
Repo.clone_from(rw_repo.common_dir, destination, multi_options=[option])
280+
assert destination.exists()
281+
282+
def test_clone_from_unsafe_protocol(self):
283+
with tempfile.TemporaryDirectory() as tdir:
284+
tmp_dir = pathlib.Path(tdir)
285+
tmp_file = tmp_dir / "pwn"
286+
urls = [
287+
f"ext::sh -c touch% {tmp_file}",
288+
"fd::17/foo",
289+
]
290+
for url in urls:
291+
with self.assertRaises(UnsafeProtocolError):
292+
Repo.clone_from(url, tmp_dir / "repo")
293+
assert not tmp_file.exists()
294+
295+
def test_clone_from_unsafe_protocol_allowed(self):
296+
with tempfile.TemporaryDirectory() as tdir:
297+
tmp_dir = pathlib.Path(tdir)
298+
tmp_file = tmp_dir / "pwn"
299+
urls = [
300+
f"ext::sh -c touch% {tmp_file}",
301+
"fd::/foo",
302+
]
303+
for url in urls:
304+
# The URL will be allowed into the command, but the command will
305+
# fail since we don't have that protocol enabled in the Git config file.
306+
with self.assertRaises(GitCommandError):
307+
Repo.clone_from(url, tmp_dir / "repo", allow_unsafe_protocols=True)
308+
assert not tmp_file.exists()
309+
310+
def test_clone_from_unsafe_protocol_allowed_and_enabled(self):
311+
with tempfile.TemporaryDirectory() as tdir:
312+
tmp_dir = pathlib.Path(tdir)
313+
tmp_file = tmp_dir / "pwn"
314+
urls = [
315+
f"ext::sh -c touch% {tmp_file}",
316+
]
317+
allow_ext = [
318+
"--config=protocol.ext.allow=always",
319+
]
320+
for url in urls:
321+
# The URL will be allowed into the command, and the protocol is enabled,
322+
# but the command will fail since it can't read from the remote repo.
323+
assert not tmp_file.exists()
324+
with self.assertRaises(GitCommandError):
325+
Repo.clone_from(
326+
url,
327+
tmp_dir / "repo",
328+
multi_options=allow_ext,
329+
allow_unsafe_protocols=True,
330+
allow_unsafe_options=True,
331+
)
332+
assert tmp_file.exists()
333+
tmp_file.unlink()

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.