Skip to content

Commit ee91408

Browse files
Byronjpabeem
andcommitted
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. Validation: all 59 non-network tests in `test/test_clone.py` pass separately with `GIT_PYTHON_BACKEND=cli` and `GIT_PYTHON_BACKEND=gix`. Both rejection tests fail when the two added safeguards are removed in memory. Ruff lint and format checks, ShellCheck, Bash syntax checking, and `git diff --check` pass. Co-authored-by: Jeroen <jpab.eemsterboer@gmail.com> Agent-Model: unknown
1 parent 213c81f commit ee91408

4 files changed

Lines changed: 51 additions & 0 deletions

File tree

‎doc/source/changes.rst‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ Security fixes
1818

1919
- https://github.com/gitpython-developers/GitPython/security/advisories/GHSA-f8wj-prgh-h9j7
2020
- https://github.com/gitpython-developers/GitPython/security/advisories/GHSA-j3hg-fv33-37rw
21+
- https://github.com/gitpython-developers/GitPython/security/advisories/GHSA-j8pf-7m8v-xx38
2122

2223
Git executable and safety
2324
-------------------------

‎git/repo/base.py‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,9 @@ class Repo:
166166
"--separate-git-dir",
167167
# Fetches from an additional caller-controlled URI:
168168
"--bundle-uri",
169+
# Reads objects from a caller-controlled local repository:
170+
"--reference",
171+
"--reference-if-able",
169172
]
170173
"""Options to :manpage:`git-clone(1)` that permit unsafe command execution or I/O.
171174

‎test/test_clone.py‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import os.path as osp
66
import pathlib
77
import re
8+
import shlex
89
import sys
910
from pathlib import Path
1011
from unittest import mock, skip
@@ -71,6 +72,29 @@ def test_clone_clears_ambient_source_storage_environment(tmp_path, clone_method)
7172
source.close()
7273

7374

75+
@pytest.mark.parametrize("clone_method", ["clone", "clone_from"])
76+
@pytest.mark.parametrize("option", ["reference", "reference-if-able", "reference-if-ab"])
77+
@pytest.mark.parametrize("option_style", ["kwargs", "multi_equals", "multi_split"])
78+
def test_clone_reference_options_allowed(tmp_path, clone_method, option, option_style):
79+
with Repo(tmp_path / "source") as source, Repo(tmp_path / "reference repo") as reference:
80+
if option_style == "kwargs":
81+
options = {option.replace("-", "_"): reference.git_dir}
82+
else:
83+
separator = "=" if option_style == "multi_equals" else " "
84+
options = {"multi_options": [f"--{option}{separator}{shlex.quote(reference.git_dir)}"]}
85+
destination = tmp_path / "clone"
86+
options.update(allow_unsafe_options=True, no_local=True)
87+
if clone_method == "clone":
88+
cloned = source.clone(destination, **options)
89+
else:
90+
cloned = Repo.clone_from(source.git_dir, destination, **options)
91+
with cloned:
92+
assert cloned.head.commit == source.head.commit
93+
assert cloned.head.commit.tree["file"].data_stream.read() == b"content\n"
94+
alternates = Path(cloned.git_dir, "objects", "info", "alternates").read_text().strip()
95+
assert Path(alternates).resolve() == Path(reference.git_dir, "objects").resolve()
96+
97+
7498
class TestClone(TestBase):
7599
@with_rw_directory(script="clone.sh", args=("nonempty",))
76100
def test_checkout_in_non_empty_dir(self, rw_dir):
@@ -184,6 +208,10 @@ def test_clone_unsafe_options(self, rw_repo):
184208
f"--template={tmp_dir}",
185209
f"--bundle-uri=file://{tmp_dir}",
186210
f"--separate-git-dir={tmp_dir / 'git-dir'}",
211+
f"--reference={tmp_dir}",
212+
f"--reference-if-able={tmp_dir}",
213+
f"--reference-if-ab={tmp_dir}",
214+
f"--ref={tmp_dir}",
187215
]
188216
for unsafe_option in unsafe_options:
189217
with self.assertRaises(UnsafeOptionError):
@@ -201,6 +229,10 @@ def test_clone_unsafe_options(self, rw_repo):
201229
{"template": tmp_dir},
202230
{"bundle_uri": f"file://{tmp_dir}"},
203231
{"separate_git_dir": tmp_dir / "git-dir"},
232+
{"reference": tmp_dir},
233+
{"reference_if_able": tmp_dir},
234+
{"reference_if_ab": tmp_dir},
235+
{"ref": tmp_dir},
204236
]
205237
for unsafe_option in unsafe_options:
206238
with self.assertRaises(UnsafeOptionError):
@@ -316,6 +348,10 @@ def test_clone_from_unsafe_options(self, rw_repo):
316348
"-cprotocol.ext.allow=always",
317349
"-vcprotocol.ext.allow=always",
318350
f"--separate-git-dir={tmp_dir / 'git-dir'}",
351+
f"--reference={tmp_dir}",
352+
f"--reference-if-able={tmp_dir}",
353+
f"--reference-if-ab={tmp_dir}",
354+
f"--ref={tmp_dir}",
319355
]
320356
for unsafe_option in unsafe_options:
321357
with self.assertRaises(UnsafeOptionError):
@@ -329,6 +365,10 @@ def test_clone_from_unsafe_options(self, rw_repo):
329365
{"config": "protocol.ext.allow=always"},
330366
{"c": "protocol.ext.allow=always"},
331367
{"separate_git_dir": tmp_dir / "git-dir"},
368+
{"reference": tmp_dir},
369+
{"reference_if_able": tmp_dir},
370+
{"reference_if_ab": tmp_dir},
371+
{"ref": tmp_dir},
332372
]
333373
for unsafe_option in unsafe_options:
334374
with self.assertRaises(UnsafeOptionError):

‎tests/fixtures/clone.sh‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,13 @@ case "$1" in
88
git init --initial-branch=master source
99
git -C source commit --allow-empty -m initial
1010
;;
11+
test_clone_reference_options_allowed)
12+
git init --initial-branch=master source
13+
printf 'content\n' > source/file
14+
git -C source add file
15+
git -C source commit -m initial
16+
git clone --no-hardlinks source 'reference repo'
17+
;;
1118
source)
1219
git init --initial-branch=master repo
1320
;;

0 commit comments

Comments
 (0)