Skip to content
Merged
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
1 change: 1 addition & 0 deletions doc/source/changes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,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
- https://github.com/gitpython-developers/GitPython/security/advisories/GHSA-j8pf-7m8v-xx38

Git executable and safety
-------------------------
Expand Down
3 changes: 3 additions & 0 deletions git/repo/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,9 @@ class Repo:
"--separate-git-dir",
# Fetches from an additional caller-controlled URI:
"--bundle-uri",
# Reads objects from a caller-controlled local repository:
"--reference",
"--reference-if-able",
Comment thread
Byron marked this conversation as resolved.
Resolved
]
"""Options to :manpage:`git-clone(1)` that permit unsafe command execution or I/O.

Expand Down
40 changes: 40 additions & 0 deletions test/test_clone.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import os.path as osp
import pathlib
import re
import shlex
import sys
from pathlib import Path
from unittest import mock, skip
Expand Down Expand Up @@ -71,6 +72,29 @@ def test_clone_clears_ambient_source_storage_environment(tmp_path, clone_method)
source.close()


@pytest.mark.parametrize("clone_method", ["clone", "clone_from"])
@pytest.mark.parametrize("option", ["reference", "reference-if-able", "reference-if-ab"])
@pytest.mark.parametrize("option_style", ["kwargs", "multi_equals", "multi_split"])
def test_clone_reference_options_allowed(tmp_path, clone_method, option, option_style):
with Repo(tmp_path / "source") as source, Repo(tmp_path / "reference repo") as reference:
if option_style == "kwargs":
options = {option.replace("-", "_"): reference.git_dir}
else:
separator = "=" if option_style == "multi_equals" else " "
options = {"multi_options": [f"--{option}{separator}{shlex.quote(reference.git_dir)}"]}
destination = tmp_path / "clone"
options.update(allow_unsafe_options=True, no_local=True)
if clone_method == "clone":
cloned = source.clone(destination, **options)
else:
cloned = Repo.clone_from(source.git_dir, destination, **options)
with cloned:
assert cloned.head.commit == source.head.commit
assert cloned.head.commit.tree["file"].data_stream.read() == b"content\n"
alternates = Path(cloned.git_dir, "objects", "info", "alternates").read_text().strip()
assert Path(alternates).resolve() == Path(reference.git_dir, "objects").resolve()


class TestClone(TestBase):
@with_rw_directory(script="clone.sh", args=("nonempty",))
def test_checkout_in_non_empty_dir(self, rw_dir):
Expand Down Expand Up @@ -184,6 +208,10 @@ def test_clone_unsafe_options(self, rw_repo):
f"--template={tmp_dir}",
f"--bundle-uri=file://{tmp_dir}",
f"--separate-git-dir={tmp_dir / 'git-dir'}",
f"--reference={tmp_dir}",
f"--reference-if-able={tmp_dir}",
f"--reference-if-ab={tmp_dir}",
f"--ref={tmp_dir}",
]
for unsafe_option in unsafe_options:
with self.assertRaises(UnsafeOptionError):
Expand All @@ -201,6 +229,10 @@ def test_clone_unsafe_options(self, rw_repo):
{"template": tmp_dir},
{"bundle_uri": f"file://{tmp_dir}"},
{"separate_git_dir": tmp_dir / "git-dir"},
{"reference": tmp_dir},
{"reference_if_able": tmp_dir},
{"reference_if_ab": tmp_dir},
{"ref": tmp_dir},
]
for unsafe_option in unsafe_options:
with self.assertRaises(UnsafeOptionError):
Expand Down Expand Up @@ -316,6 +348,10 @@ def test_clone_from_unsafe_options(self, rw_repo):
"-cprotocol.ext.allow=always",
"-vcprotocol.ext.allow=always",
f"--separate-git-dir={tmp_dir / 'git-dir'}",
f"--reference={tmp_dir}",
f"--reference-if-able={tmp_dir}",
f"--reference-if-ab={tmp_dir}",
f"--ref={tmp_dir}",
]
for unsafe_option in unsafe_options:
with self.assertRaises(UnsafeOptionError):
Expand All @@ -329,6 +365,10 @@ def test_clone_from_unsafe_options(self, rw_repo):
{"config": "protocol.ext.allow=always"},
{"c": "protocol.ext.allow=always"},
{"separate_git_dir": tmp_dir / "git-dir"},
{"reference": tmp_dir},
{"reference_if_able": tmp_dir},
{"reference_if_ab": tmp_dir},
{"ref": tmp_dir},
]
for unsafe_option in unsafe_options:
with self.assertRaises(UnsafeOptionError):
Expand Down
7 changes: 7 additions & 0 deletions tests/fixtures/clone.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@ case "$1" in
git init --initial-branch=master source
git -C source commit --allow-empty -m initial
;;
test_clone_reference_options_allowed)
git init --initial-branch=master source
printf 'content\n' > source/file
git -C source add file
git -C source commit -m initial
git clone --no-hardlinks source 'reference repo'
;;
source)
git init --initial-branch=master repo
;;
Expand Down
Loading