From b056f397396061af764e89f169fd92df466ba606 Mon Sep 17 00:00:00 2001 From: Byron Date: Mon, 16 Nov 2026 04:46:43 +0100 Subject: [PATCH] fix(repo): require opt-in for reference repositories when cloning This change follows the clone safety issue recorded in https://github.com/gitpython-developers/GitPython/security/advisories/GHSA-j8pf-7m8v-xx38. Passing unchecked clone keyword arguments can make Git read objects from a caller-controlled local repository, which is surprising for callers that rely on GitPython's default unsafe-option restrictions. Add `--reference` and `--reference-if-able` to `Repo.unsafe_git_clone_options`, requiring `allow_unsafe_options=True` for these options. The shared clone guard also rejects abbreviated spellings and options supplied through `multi_options`. Record the advisory in `doc/source/changes.rst`. Copilot requested: "Please add regression cases for both `clone()` and `clone_from()`, covering keyword arguments and `multi_options` (including abbreviated spellings), and verify that `allow_unsafe_options=True` still permits valid reference-based clones." Extend the existing rejection cases and add local reference clones through both APIs, using keyword arguments and joined or split `multi_options`. The opt-in cases check the recorded alternate object store and read committed content, including a reference path containing spaces. Initial repositories are generated by the existing shell fixture rather than using the source checkout's history. Co-authored-by: Jeroen Co-authored-by: GPT 6.1 Sol Assisted-by: GPT 6.1 Sol --- doc/source/changes.rst | 1 + git/repo/base.py | 3 +++ test/test_clone.py | 40 ++++++++++++++++++++++++++++++++++++++++ tests/fixtures/clone.sh | 7 +++++++ 4 files changed, 51 insertions(+) diff --git a/doc/source/changes.rst b/doc/source/changes.rst index b105ef73b..b0ba5e2a3 100644 --- a/doc/source/changes.rst +++ b/doc/source/changes.rst @@ -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 ------------------------- diff --git a/git/repo/base.py b/git/repo/base.py index 32b99296a..a7e7f068a 100644 --- a/git/repo/base.py +++ b/git/repo/base.py @@ -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", ] """Options to :manpage:`git-clone(1)` that permit unsafe command execution or I/O. diff --git a/test/test_clone.py b/test/test_clone.py index c973e3146..463e4b663 100644 --- a/test/test_clone.py +++ b/test/test_clone.py @@ -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 @@ -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): @@ -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): @@ -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): @@ -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): @@ -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): diff --git a/tests/fixtures/clone.sh b/tests/fixtures/clone.sh index e186cd8d8..598ef8997 100644 --- a/tests/fixtures/clone.sh +++ b/tests/fixtures/clone.sh @@ -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 ;;