diff --git a/doc/source/changes.rst b/doc/source/changes.rst index b44e1ac14..b105ef73b 100644 --- a/doc/source/changes.rst +++ b/doc/source/changes.rst @@ -17,6 +17,7 @@ Security fixes -------------- - https://github.com/gitpython-developers/GitPython/security/advisories/GHSA-f8wj-prgh-h9j7 +- https://github.com/gitpython-developers/GitPython/security/advisories/GHSA-j3hg-fv33-37rw Git executable and safety ------------------------- diff --git a/git/objects/submodule/base.py b/git/objects/submodule/base.py index 0c24dbc87..840f32d50 100644 --- a/git/objects/submodule/base.py +++ b/git/objects/submodule/base.py @@ -630,15 +630,15 @@ def add( :param no_checkout: If ``True``, and if the repository has to be cloned manually, no checkout - will be performed. + will be performed. Must be a boolean. Retained metadata can instead be reconnected without checking out files or changing refs. Its URL and any explicit branch must match; ``depth`` and ``clone_multi_options`` require a new clone and are rejected here. :param depth: - Create a shallow clone with a history truncated to the specified number of - commits. + Create a shallow clone with a history truncated to the specified positive + number of commits. :param env: Optional dictionary containing the desired environment variables. @@ -666,6 +666,10 @@ def add( Works atomically, such that no change will be done if, for example, the repository update fails. """ + if not isinstance(no_checkout, bool): + raise TypeError("no_checkout must be a bool") + if depth is not None and (not isinstance(depth, int) or isinstance(depth, bool) or depth <= 0): + raise ValueError("depth must be a positive integer") if repo.bare: raise InvalidGitRepositoryError("Cannot add submodules to bare repositories") # END handle bare repos @@ -725,8 +729,6 @@ def add( if branch is not None: args.extend(("--branch", Git._check_operand(branch, "branch"))) if depth is not None: - if not isinstance(depth, int) or isinstance(depth, bool) or depth <= 0: - raise ValueError("depth must be a positive integer") args.extend(("--depth", str(depth))) args.extend(("--", url, str(path))) # An explicitly requested local submodule is also supported by clone(). @@ -819,11 +821,8 @@ def add( kwargs["b"] = br.name # END setup checkout-branch - if depth: - if isinstance(depth, int): - kwargs["depth"] = depth - else: - raise ValueError("depth should be an integer") + if depth is not None: + kwargs["depth"] = depth if clone_multi_options: kwargs["multi_options"] = clone_multi_options diff --git a/git/refs/head.py b/git/refs/head.py index d73f8b0af..8c6db8eca 100644 --- a/git/refs/head.py +++ b/git/refs/head.py @@ -299,6 +299,8 @@ def checkout( the HEAD detached which is allowed and possible, but remains a special state that some tools might not be able to handle. """ + if not isinstance(force, bool): + raise TypeError("force must be a bool") if not allow_unsafe_options: Git.check_unsafe_options( options=Git._option_candidates([self], kwargs), diff --git a/test/test_refs.py b/test/test_refs.py index 7d8f7f7fe..e323bd0cc 100644 --- a/test/test_refs.py +++ b/test/test_refs.py @@ -43,6 +43,26 @@ from test.lib.fixtures import fixture_repo, read_only_fixture, writable_fixture +@pytest.mark.parametrize("force", [None, 0, 1, "", "false", "--detach", b"--detach", ["--detach"], ("--detach",)]) +@pytest.mark.parametrize("allow_unsafe_options", [False, True]) +@pytest.mark.parametrize("positional", [False, True]) +def test_head_checkout_requires_boolean_force(force, allow_unsafe_options, positional): + with fixture_repo("refs.sh", "rename-prefixed") as repo: + branch = repo.active_branch + for split_single_char_options in (False, True): + with patch.object(Git, "_call_process_safe", side_effect=AssertionError("Git must not run")) as run: + with pytest.raises(TypeError, match="force must be a bool"): + options = { + "allow_unsafe_options": allow_unsafe_options, + "split_single_char_options": split_single_char_options, + } + if positional: + branch.checkout(force, **options) + else: + branch.checkout(force=force, **options) + run.assert_not_called() + + @pytest.mark.parametrize( "branch_name", ["--pathspec-from-file=pathspecs", "-f", "--orphan=checkout-target", "-Bcheckout-target"] ) diff --git a/test/test_submodule.py b/test/test_submodule.py index 751235e21..f293e8742 100644 --- a/test/test_submodule.py +++ b/test/test_submodule.py @@ -33,6 +33,34 @@ from test.lib.fixtures import fixture_repo, read_only_fixture, writable_fixture +@pytest.mark.parametrize("no_checkout", [None, 0, 1, "", "false", "--bare", b"--bare", ["--bare"], ("--bare",)]) +@pytest.mark.parametrize("allow_unsafe_options", [False, True]) +@pytest.mark.parametrize("positional", [False, True]) +def test_submodule_add_requires_boolean_no_checkout(no_checkout, allow_unsafe_options, positional): + with fixture_repo("empty.sh", "sha1") as repo: + for add, args in ( + (Submodule.add, (repo, "module", "module", "source", None)), + (repo.create_submodule, ("module", "module", "source", None)), + ): + with mock.patch.object(Git, "execute", side_effect=AssertionError("Git must not run")) as run: + with pytest.raises(TypeError, match="no_checkout must be a bool"): + if positional: + add(*args, no_checkout, allow_unsafe_options=allow_unsafe_options) + else: + add(*args, no_checkout=no_checkout, allow_unsafe_options=allow_unsafe_options) + run.assert_not_called() + + +@pytest.mark.parametrize("depth", [True, False, 0, -1, 1.5, "1", [1]]) +@pytest.mark.parametrize("no_checkout", [False, True]) +def test_submodule_add_requires_positive_integer_depth(depth, no_checkout): + with fixture_repo("empty.sh", "sha1") as repo: + with mock.patch.object(Git, "execute", side_effect=AssertionError("Git must not run")) as run: + with pytest.raises(ValueError, match="depth must be a positive integer"): + repo.create_submodule("module", "module", "source", no_checkout=no_checkout, depth=depth) + run.assert_not_called() + + @pytest.mark.parametrize("scenario, name", [("ascii-name", "module"), ("byte-name", "module-\udcff")]) def test_submodule_configuration_preserves_non_utf8_bytes(scenario, name): with fixture_repo("submodule-bytes.sh", scenario) as repo: