Skip to content

Block insecure options and protocols by default #1521

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Dec 29, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Updates from review
  • Loading branch information
stsewd committed Dec 27, 2022
commit fd2c6da5f82009398d241dc07603fbcd490ced29
10 changes: 5 additions & 5 deletions git/cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -488,12 +488,12 @@ def check_unsafe_options(cls, options: List[str], unsafe_options: List[str]) ->
"""
# Options can be of the form `foo` or `--foo bar` `--foo=bar`,
# so we need to check if they start with "--foo" or if they are equal to "foo".
bare_options = [
bare_unsafe_options = [
option.lstrip("-")
for option in unsafe_options
]
for option in options:
for unsafe_option, bare_option in zip(unsafe_options, bare_options):
for unsafe_option, bare_option in zip(unsafe_options, bare_unsafe_options):
if option.startswith(unsafe_option) or option == bare_option:
raise UnsafeOptionError(
f"{unsafe_option} is not allowed, use `allow_unsafe_options=True` to allow it."
Expand Down Expand Up @@ -1193,12 +1193,12 @@ def transform_kwargs(self, split_single_char_options: bool = True, **kwargs: Any
return args

@classmethod
def __unpack_args(cls, arg_list: Sequence[str]) -> List[str]:
def _unpack_args(cls, arg_list: Sequence[str]) -> List[str]:

outlist = []
if isinstance(arg_list, (list, tuple)):
for arg in arg_list:
outlist.extend(cls.__unpack_args(arg))
outlist.extend(cls._unpack_args(arg))
else:
outlist.append(str(arg_list))

Expand Down Expand Up @@ -1283,7 +1283,7 @@ def _call_process(
# Prepare the argument list

opt_args = self.transform_kwargs(**opts_kwargs)
ext_args = self.__unpack_args([a for a in args if a is not None])
ext_args = self._unpack_args([a for a in args if a is not None])

if insert_after_this_arg is None:
args_list = opt_args + ext_args
Expand Down
21 changes: 9 additions & 12 deletions git/remote.py
Original file line number Diff line number Diff line change
Expand Up @@ -1029,12 +1029,11 @@ def pull(
self._assert_refspec()
kwargs = add_progress(kwargs, self.repo.git, progress)

if not allow_unsafe_protocols and refspec:
if isinstance(refspec, str):
Git.check_unsafe_protocols(refspec)
else:
for ref in refspec:
Git.check_unsafe_protocols(ref)
refspec = Git._unpack_args(refspec or [])
if not allow_unsafe_protocols:
for ref in refspec:
Git.check_unsafe_protocols(ref)

if not allow_unsafe_options:
Git.check_unsafe_options(options=list(kwargs.keys()), unsafe_options=self.unsafe_git_pull_options)

Expand Down Expand Up @@ -1084,12 +1083,10 @@ def push(
be 0."""
kwargs = add_progress(kwargs, self.repo.git, progress)

if not allow_unsafe_protocols and refspec:
if isinstance(refspec, str):
Git.check_unsafe_protocols(refspec)
else:
for ref in refspec:
Git.check_unsafe_protocols(ref)
refspec = Git._unpack_args(refspec or [])
if not allow_unsafe_protocols:
for ref in refspec:
Git.check_unsafe_protocols(ref)

if not allow_unsafe_options:
Git.check_unsafe_options(options=list(kwargs.keys()), unsafe_options=self.unsafe_git_push_options)
Expand Down
4 changes: 2 additions & 2 deletions test/test_git.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ def test_call_process_calls_execute(self, git):
self.assertEqual(git.call_args, ((["git", "version"],), {}))

def test_call_unpack_args_unicode(self):
args = Git._Git__unpack_args("Unicode€™")
args = Git._unpack_args("Unicode€™")
mangled_value = "Unicode\u20ac\u2122"
self.assertEqual(args, [mangled_value])

def test_call_unpack_args(self):
args = Git._Git__unpack_args(["git", "log", "--", "Unicode€™"])
args = Git._unpack_args(["git", "log", "--", "Unicode€™"])
mangled_value = "Unicode\u20ac\u2122"
self.assertEqual(args, ["git", "log", "--", mangled_value])

Expand Down