Reject abbreviated forms of unsafe git options by Byron · Pull Request #2168 · gitpython-developers/GitPython · GitHub
Skip to content
Merged
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
59 changes: 54 additions & 5 deletions git/cmd.py
37 changes: 37 additions & 0 deletions test/test_clone.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,13 @@ def test_clone_unsafe_options(self, rw_repo):
unsafe_options = [
f"--upload-pack='touch {tmp_file}'",
f"-u 'touch {tmp_file}'",
f"-utouch {tmp_file}; false",
f"-futouch${{IFS}}{tmp_file}; false",
f"-qutouch${{IFS}}{tmp_file}; false",
"--config=protocol.ext.allow=always",
"-c protocol.ext.allow=always",
"-cprotocol.ext.allow=always",
"-vcprotocol.ext.allow=always",
]
for unsafe_option in unsafe_options:
with self.assertRaises(UnsafeOptionError):
Expand All @@ -138,6 +143,31 @@ def test_clone_unsafe_options(self, rw_repo):
rw_repo.clone(tmp_dir, **unsafe_option)
assert not tmp_file.exists()

@with_rw_repo("HEAD")
def test_clone_unsafe_options_abbreviated(self, rw_repo):
with tempfile.TemporaryDirectory() as tdir:
tmp_dir = pathlib.Path(tdir)
tmp_file = tmp_dir / "pwn"
unsafe_options = [
f"--upl='touch {tmp_file}'",
f"--upload-pac='touch {tmp_file}'",
"--conf=protocol.ext.allow=always",
]
for unsafe_option in unsafe_options:
with self.assertRaises(UnsafeOptionError):
rw_repo.clone(tmp_dir, multi_options=[unsafe_option])
assert not tmp_file.exists()
Comment thread
Byron marked this conversation as resolved.

unsafe_kwargs = [
{"upl": f"touch {tmp_file}"},
{"upload_pac": f"touch {tmp_file}"},
{"conf": "protocol.ext.allow=always"},
]
for unsafe_option in unsafe_kwargs:
with self.assertRaises(UnsafeOptionError):
rw_repo.clone(tmp_dir, **unsafe_option)
assert not tmp_file.exists()

@with_rw_repo("HEAD")
def test_clone_unsafe_options_are_checked_after_splitting_multi_options(self, rw_repo):
with tempfile.TemporaryDirectory() as tdir:
Expand Down Expand Up @@ -191,7 +221,9 @@ def test_clone_safe_options(self, rw_repo):
options = [
"--depth=1",
"--single-branch",
"--origin upload",
"-q",
"-oupstream",
]
for option in options:
destination = tmp_dir / option
Expand All @@ -207,8 +239,13 @@ def test_clone_from_unsafe_options(self, rw_repo):
unsafe_options = [
f"--upload-pack='touch {tmp_file}'",
f"-u 'touch {tmp_file}'",
f"-utouch {tmp_file}; false",
f"-futouch${{IFS}}{tmp_file}; false",
f"-qutouch${{IFS}}{tmp_file}; false",
"--config=protocol.ext.allow=always",
"-c protocol.ext.allow=always",
"-cprotocol.ext.allow=always",
"-vcprotocol.ext.allow=always",
]
for unsafe_option in unsafe_options:
with self.assertRaises(UnsafeOptionError):
Expand Down
28 changes: 28 additions & 0 deletions test/test_git.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,34 @@ def test_check_unsafe_options_normalizes_kwargs(self):
with self.assertRaises(UnsafeOptionError):
Git.check_unsafe_options(options=options, unsafe_options=unsafe_options)

def test_check_unsafe_options_does_not_treat_short_options_as_abbreviations(self):
unsafe_options = ["--upload-pack"]

Git.check_unsafe_options(options=["-u", "u", "-upl"], unsafe_options=unsafe_options)
with self.assertRaises(UnsafeOptionError):
Git.check_unsafe_options(options=["--u"], unsafe_options=unsafe_options)

def test_check_unsafe_options_does_not_treat_option_values_as_abbreviations(self):
Git.check_unsafe_options(options=["--branch", "conf"], unsafe_options=["--config"])

def test_check_unsafe_options_rejects_joined_unsafe_short_options(self):
cases = [
(["-utouch /tmp/pwn"], ["-u"]),
(["-futouch /tmp/pwn"], ["-u"]),
(["-qutouch${IFS}/tmp/pwn"], ["-u"]),
(["-cprotocol.ext.allow=always"], ["-c"]),
(["-vcprotocol.ext.allow=always"], ["-c"]),
]

for options, unsafe_options in cases:
with self.assertRaises(UnsafeOptionError):
Git.check_unsafe_options(options=options, unsafe_options=unsafe_options)

def test_check_unsafe_options_allows_attached_safe_short_option_values(self):
unsafe_options = ["--upload-pack", "-u", "--config", "-c"]

Git.check_unsafe_options(options=["-oupstream", "-bcurrent"], unsafe_options=unsafe_options)

_shell_cases = (
# value_in_call, value_from_class, expected_popen_arg
(None, False, False),
Expand Down
14 changes: 12 additions & 2 deletions test/test_remote.py
Loading