|
3 | 3 | # This module is part of GitPython and is released under the |
4 | 4 | # 3-Clause BSD License: https://opensource.org/license/bsd-3-clause/ |
5 | 5 |
|
| 6 | +import contextlib |
6 | 7 | from itertools import chain |
7 | 8 | import os.path as osp |
8 | 9 | from pathlib import Path |
|
18 | 19 | RefLog, |
19 | 20 | Reference, |
20 | 21 | RemoteReference, |
| 22 | + Repo, |
21 | 23 | SymbolicReference, |
22 | 24 | TagReference, |
23 | 25 | ) |
|
29 | 31 |
|
30 | 32 |
|
31 | 33 | class TestRefs(TestBase): |
| 34 | + @contextlib.contextmanager |
| 35 | + def _repo_with_initial_commit(self, base_dir): |
| 36 | + repo_dir = base_dir / "repo" |
| 37 | + repo = Repo.init(repo_dir) |
| 38 | + (repo_dir / "file.txt").write_text("initial\n", encoding="utf-8") |
| 39 | + repo.index.add(["file.txt"]) |
| 40 | + repo.index.commit("initial") |
| 41 | + try: |
| 42 | + yield repo |
| 43 | + finally: |
| 44 | + repo.git.clear_cache() |
| 45 | + |
32 | 46 | def test_from_path(self): |
33 | 47 | # Should be able to create any reference directly. |
34 | 48 | for ref_type in (Reference, Head, TagReference, RemoteReference): |
@@ -648,6 +662,115 @@ def test_refs_outside_repo(self): |
648 | 662 | ref_file_name = Path(ref_file.name).name |
649 | 663 | self.assertRaises(BadName, self.rorepo.commit, f"../../{ref_file_name}") |
650 | 664 |
|
| 665 | + def test_reference_create_rejects_path_traversal(self): |
| 666 | + with tempfile.TemporaryDirectory() as tmp_dir: |
| 667 | + base_dir = Path(tmp_dir) |
| 668 | + with self._repo_with_initial_commit(base_dir) as repo: |
| 669 | + outside_path = base_dir / "outside_write.txt" |
| 670 | + |
| 671 | + self.assertRaises(ValueError, Reference.create, repo, "../../../outside_write.txt", "HEAD") |
| 672 | + assert not outside_path.exists() |
| 673 | + |
| 674 | + def test_symbolic_reference_create_rejects_path_traversal(self): |
| 675 | + with tempfile.TemporaryDirectory() as tmp_dir: |
| 676 | + base_dir = Path(tmp_dir) |
| 677 | + with self._repo_with_initial_commit(base_dir) as repo: |
| 678 | + outside_path = base_dir / "outside_write.txt" |
| 679 | + |
| 680 | + self.assertRaises(ValueError, SymbolicReference.create, repo, "../../outside_write.txt", "HEAD") |
| 681 | + assert not outside_path.exists() |
| 682 | + |
| 683 | + def test_symbolic_reference_set_reference_rejects_path_traversal(self): |
| 684 | + with tempfile.TemporaryDirectory() as tmp_dir: |
| 685 | + base_dir = Path(tmp_dir) |
| 686 | + with self._repo_with_initial_commit(base_dir) as repo: |
| 687 | + outside_path = base_dir / "outside_write.txt" |
| 688 | + |
| 689 | + self.assertRaises(ValueError, SymbolicReference(repo, "../../outside_write.txt").set_reference, "HEAD") |
| 690 | + assert not outside_path.exists() |
| 691 | + |
| 692 | + def test_symbolic_reference_rename_rejects_path_traversal(self): |
| 693 | + with tempfile.TemporaryDirectory() as tmp_dir: |
| 694 | + base_dir = Path(tmp_dir) |
| 695 | + with self._repo_with_initial_commit(base_dir) as repo: |
| 696 | + outside_path = base_dir / "outside_move.txt" |
| 697 | + ref = SymbolicReference.create(repo, "SAFE_RENAME_SOURCE", "HEAD") |
| 698 | + |
| 699 | + self.assertRaises(ValueError, ref.rename, "../../outside_move.txt") |
| 700 | + assert not outside_path.exists() |
| 701 | + assert Path(ref.abspath).is_file() |
| 702 | + |
| 703 | + def test_symbolic_reference_delete_rejects_path_traversal(self): |
| 704 | + with tempfile.TemporaryDirectory() as tmp_dir: |
| 705 | + base_dir = Path(tmp_dir) |
| 706 | + with self._repo_with_initial_commit(base_dir) as repo: |
| 707 | + outside_path = base_dir / "outside_delete.txt" |
| 708 | + outside_path.write_text("do not delete\n", encoding="utf-8") |
| 709 | + |
| 710 | + self.assertRaises(ValueError, SymbolicReference.delete, repo, "../../outside_delete.txt") |
| 711 | + assert outside_path.read_text(encoding="utf-8") == "do not delete\n" |
| 712 | + |
| 713 | + def test_symbolic_reference_log_append_rejects_path_traversal(self): |
| 714 | + with tempfile.TemporaryDirectory() as tmp_dir: |
| 715 | + base_dir = Path(tmp_dir) |
| 716 | + with self._repo_with_initial_commit(base_dir) as repo: |
| 717 | + outside_path = base_dir / "outside_reflog.txt" |
| 718 | + |
| 719 | + ref = SymbolicReference(repo, "../../../outside_reflog.txt") |
| 720 | + self.assertRaises( |
| 721 | + ValueError, ref.log_append, Commit.NULL_BIN_SHA, "do not write", repo.head.commit.binsha |
| 722 | + ) |
| 723 | + assert not outside_path.exists() |
| 724 | + |
| 725 | + def test_symbolic_reference_set_reference_rejects_symlink_escape(self): |
| 726 | + with tempfile.TemporaryDirectory() as tmp_dir: |
| 727 | + base_dir = Path(tmp_dir) |
| 728 | + with self._repo_with_initial_commit(base_dir) as repo: |
| 729 | + outside_dir = base_dir / "outside_refs" |
| 730 | + outside_dir.mkdir() |
| 731 | + outside_path = outside_dir / "escaped" |
| 732 | + |
| 733 | + refs_heads_dir = Path(repo.common_dir) / "refs" / "heads" |
| 734 | + refs_heads_dir.mkdir(parents=True, exist_ok=True) |
| 735 | + symlink_path = refs_heads_dir / "link_out" |
| 736 | + try: |
| 737 | + symlink_path.symlink_to(outside_dir, target_is_directory=True) |
| 738 | + except (OSError, NotImplementedError) as ex: |
| 739 | + self.skipTest("symlinks unavailable on this platform: %s" % ex) |
| 740 | + if osp.realpath(symlink_path / "escaped") == osp.abspath(symlink_path / "escaped"): |
| 741 | + self.skipTest("realpath does not resolve directory symlinks on this platform") |
| 742 | + |
| 743 | + ref = SymbolicReference(repo, "refs/heads/link_out/escaped") |
| 744 | + self.assertRaises(ValueError, ref.set_reference, "HEAD") |
| 745 | + assert not outside_path.exists() |
| 746 | + |
| 747 | + def test_remote_reference_delete_cleanup_rejects_path_traversal(self): |
| 748 | + with tempfile.TemporaryDirectory() as tmp_dir: |
| 749 | + base_dir = Path(tmp_dir) |
| 750 | + git_dir = base_dir / "repo" / ".git" |
| 751 | + git_dir.mkdir(parents=True) |
| 752 | + outside_path = base_dir / "outside_remote_delete.txt" |
| 753 | + outside_path.write_text("do not delete\n", encoding="utf-8") |
| 754 | + |
| 755 | + class GitStub: |
| 756 | + branch_called = False |
| 757 | + |
| 758 | + def branch(self, *args): |
| 759 | + self.branch_called = True |
| 760 | + |
| 761 | + class RepoStub: |
| 762 | + pass |
| 763 | + |
| 764 | + repo = RepoStub() |
| 765 | + repo.git = GitStub() |
| 766 | + repo.common_dir = str(git_dir) |
| 767 | + repo.git_dir = str(git_dir) |
| 768 | + ref = RemoteReference(repo, "../../outside_remote_delete.txt", check_path=False) |
| 769 | + |
| 770 | + self.assertRaises(ValueError, RemoteReference.delete, repo, ref) |
| 771 | + assert not repo.git.branch_called |
| 772 | + assert outside_path.read_text(encoding="utf-8") == "do not delete\n" |
| 773 | + |
651 | 774 | def test_validity_ref_names(self): |
652 | 775 | """Ensure ref names are checked for validity. |
653 | 776 |
|
|
0 commit comments