Skip to content

Commit dcb6b14

Browse files
authored
Merge pull request #2233 from gitpython-developers/clone-check-head-ref
fix: validate checkout positional reference options
2 parents fb7e089 + 8da6508 commit dcb6b14

3 files changed

Lines changed: 21 additions & 1 deletion

File tree

doc/source/changes.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ Changelog
88
Security fixes for
99

1010
* https://github.com/gitpython-developers/GitPython/security/advisories/GHSA-gq48-pqfc-9p58
11+
* https://github.com/gitpython-developers/GitPython/security/advisories/GHSA-23mf-xhv8-69c2
1112

1213
If you can, also try and provide feedback on the upcoming v4 branch
1314
https://github.com/gitpython-developers/GitPython/pull/2177 - patches welcome.

git/refs/head.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ def checkout(
283283
"""
284284
if not allow_unsafe_options:
285285
Git.check_unsafe_options(
286-
options=Git._option_candidates([], kwargs),
286+
options=Git._option_candidates([self], kwargs),
287287
unsafe_options=Git.unsafe_git_pathspec_from_file_options,
288288
)
289289
kwargs["f"] = force

test/test_refs.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,25 @@ def test_head_checkout_rejects_pathspec_from_file(self, rw_repo):
289289
pathspec_file_nul=True,
290290
**{option_name: str(pathspecs)},
291291
)
292+
for option_name in ("--pathspec-from-file", "--pathspec-from"):
293+
branch = Head(rw_repo, f"refs/heads/{option_name}={pathspecs}")
294+
with self.assertRaises(UnsafeOptionError):
295+
branch.checkout()
296+
297+
def test_cloned_head_checkout_rejects_pathspec_from_file(self):
298+
with tempfile.TemporaryDirectory() as tdir:
299+
base_dir = Path(tdir)
300+
with self._repo_with_initial_commit(base_dir) as source:
301+
branch = source.create_head("--pathspec-from-file=pathspecs")
302+
source.head.reference = branch
303+
with Repo.clone_from(source.working_tree_dir, base_dir / "clone") as cloned:
304+
(base_dir / "clone" / "pathspecs").write_text("unmatched-private-content\n", encoding="utf-8")
305+
assert cloned.active_branch.name == branch.name
306+
with self.assertRaises(UnsafeOptionError):
307+
cloned.active_branch.checkout()
308+
with self.assertRaises(GitCommandError) as error:
309+
cloned.active_branch.checkout(allow_unsafe_options=True)
310+
assert "unmatched-private-content" in str(error.exception)
292311

293312
@with_rw_repo("HEAD")
294313
def test_head_reset_rejects_pathspec_from_file(self, rw_repo):

0 commit comments

Comments
 (0)