fix: block unsafe long-option prefixes (GHSA-2f96-g7mh-g2hx) by Byron · Pull Request #2161 · gitpython-developers/GitPython · GitHub
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
81 changes: 76 additions & 5 deletions git/cmd.py
4 changes: 3 additions & 1 deletion git/repo/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1410,7 +1410,9 @@ def _clone(
if not allow_unsafe_options:
Git.check_unsafe_options(options=list(kwargs.keys()), unsafe_options=cls.unsafe_git_clone_options)
if not allow_unsafe_options and multi:
Git.check_unsafe_options(options=multi, unsafe_options=cls.unsafe_git_clone_options)
Git.check_unsafe_options(
options=multi, unsafe_options=cls.unsafe_git_clone_options, bare_options_are_long=False
)

proc = git.clone(
multi,
Expand Down
14 changes: 14 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",
Comment thread
Byron marked this conversation as resolved.
"--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 @@ -129,6 +134,7 @@ def test_clone_unsafe_options(self, rw_repo):
unsafe_options = [
{"upload-pack": f"touch {tmp_file}"},
{"upload_pack": f"touch {tmp_file}"},
{"upload_p": f"touch {tmp_file}"},
{"u": f"touch {tmp_file}"},
{"config": "protocol.ext.allow=always"},
{"c": "protocol.ext.allow=always"},
Expand Down Expand Up @@ -191,7 +197,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 +215,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",
Comment thread
Byron marked this conversation as resolved.
"--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 @@ -218,6 +231,7 @@ def test_clone_from_unsafe_options(self, rw_repo):
unsafe_options = [
{"upload-pack": f"touch {tmp_file}"},
{"upload_pack": f"touch {tmp_file}"},
{"upload_p": f"touch {tmp_file}"},
{"u": f"touch {tmp_file}"},
{"config": "protocol.ext.allow=always"},
{"c": "protocol.ext.allow=always"},
Expand Down
40 changes: 40 additions & 0 deletions test/test_git.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,10 +158,21 @@ def test_it_transforms_kwargs_into_git_command_arguments(self):
def test_check_unsafe_options_normalizes_kwargs(self):
cases = [
(["upload_pack"], ["--upload-pack"]),
(["upload_p"], ["--upload-pack"]),
(["upload_pac"], ["--upload-pack"]),
(["receive_pack"], ["--receive-pack"]),
(["receive_p"], ["--receive-pack"]),
(["exec"], ["--exec"]),
(["exe"], ["--exec"]),
(["u"], ["-u"]),
(["-utouch /tmp/pwn"], ["-u"]),
(["-futouch /tmp/pwn"], ["-u"]),
(["-qutouch${IFS}/tmp/pwn"], ["-u"]),
(["c"], ["-c"]),
Comment thread
Byron marked this conversation as resolved.
(["-cprotocol.ext.allow=always"], ["-c"]),
(["-vcprotocol.ext.allow=always"], ["-c"]),
(["conf"], ["--config"]),
(["confi"], ["--config"]),
(["--upload-pack=/tmp/helper"], ["--upload-pack"]),
(["--config core.filemode=false"], ["--config"]),
]
Expand All @@ -170,6 +181,35 @@ 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_keeps_short_options_exact(self):
cases = [
(["u"], ["--upload-pack"]),
(["-u helper"], ["--upload-pack"]),
(["c"], ["--config"]),
(["-c protocol.ext.allow=always"], ["--config"]),
(["e"], ["--exec"]),
(["r"], ["--receive-pack"]),
]

for options, unsafe_options in cases:
Git.check_unsafe_options(options=options, unsafe_options=unsafe_options)

def test_check_unsafe_options_ignores_multi_option_values(self):
Git.check_unsafe_options(
options=["--origin", "upload"],
unsafe_options=["--upload-pack", "--config"],
bare_options_are_long=False,
)

def test_check_unsafe_options_allows_attached_safe_short_option_values(self):
cases = [
["-oupstream"],
["-bcurrent"],
]

for options in cases:
Git.check_unsafe_options(options=options, unsafe_options=["--upload-pack", "-u", "--config", "-c"])

_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