From 35027d14e377e11265836274e49f4281d249175a Mon Sep 17 00:00:00 2001 From: Keerthana KT Date: Mon, 2 Nov 2026 22:24:23 +0530 Subject: [PATCH 01/14] fix(repo): preserve trailing whitespace in blamed line content `Repo.blame` built each blamed line with `line_bytes.rstrip().decode(defenc)` and then stripped the leading tab, but a `git blame -p` content line is a leading tab, the blamed line's exact bytes, and git's own terminating line feed. `rstrip` therefore dropped a line's trailing spaces and tabs, emptied a whitespace-only line, and removed the carriage return of a CRLF line, so the content returned by `repo.blame()` differed from both the file and `git blame`. A caller that reconstructs or compares file content from blame, for example to find the commit that introduced an exact line, was handed altered text. Recover the exact bytes instead: drop the leading tab and only git's trailing line feed, then decode. Binary lines and the hexsha/header parsing are unchanged, so the only behavior change is that blamed lines keep their original trailing whitespace and carriage returns. Adds `tests/fixtures/blame-content.sh` and `test_blame_preserves_line_content` in `test/test_repo.py`, which fail before the change (`['alpha', 'beta', '', 'crlf', 'gamma']`) and pass after (`['alpha ', 'beta', '\t', 'crlf\r', 'gamma']`) on both the `cli` and `gix` backends. `ruff check`, `ruff format --check` and `mypy` pass on the touched files. The repository's pre-existing shallow-clone failures occur identically with and without this change. Agent-Model: claude-opus-4-8 --- git/repo/base.py | 12 +++++++++--- test/test_repo.py | 6 ++++++ tests/fixtures/blame-content.sh | 10 ++++++++++ 3 files changed, 25 insertions(+), 3 deletions(-) create mode 100644 tests/fixtures/blame-content.sh diff --git a/git/repo/base.py b/git/repo/base.py index 21eb2e118..6e582becf 100644 --- a/git/repo/base.py +++ b/git/repo/base.py @@ -1550,9 +1550,15 @@ class InfoTD(TypedDict, total=False): if blames[-1][1] is not None: line: str | bytes if not is_binary: - if line_str and line_str[0] == "\t": - line_str = line_str[1:] - line = line_str + # Recover the exact line bytes. A porcelain content + # line is a leading tab, the blamed line's bytes, and + # git's terminating line feed, so rstrip() would drop + # the line's own trailing whitespace and the carriage + # return of a CRLF line. + content = line_bytes[1:] if line_bytes[:1] == b"\t" else line_bytes + if content.endswith(b"\n"): + content = content[:-1] + line = content.decode(defenc) else: line = line_bytes # NOTE: We are actually parsing lines out of binary diff --git a/test/test_repo.py b/test/test_repo.py index 38ac3e630..137024311 100644 --- a/test/test_repo.py +++ b/test/test_repo.py @@ -784,6 +784,12 @@ def test_blame_incremental_preserves_historical_paths(self): ) self.assertEqual(list(repo.blame("HEAD", "current.txt", incremental=True)), entries) + def test_blame_preserves_line_content(self): + """Blame returns each line's exact bytes, not a whitespace-stripped version.""" + with fixture_repo("blame-content.sh") as repo: + lines = [line for _commit, blamed in repo.blame("HEAD", "file.txt") for line in blamed] + self.assertEqual(lines, ["alpha ", "beta", "\t", "crlf\r", "gamma"]) + @mock.patch.object(Git, "_call_process") def test_blame_complex_revision(self, git): git.return_value = fixture("blame_complex_revision") diff --git a/tests/fixtures/blame-content.sh b/tests/fixtures/blame-content.sh new file mode 100644 index 000000000..7ee38af9f --- /dev/null +++ b/tests/fixtures/blame-content.sh @@ -0,0 +1,10 @@ +#!/usr/bin/env bash +set -eu +git init -q --initial-branch=main repo +cd repo +# Build the blob directly so trailing whitespace, a whitespace-only line, and a +# CRLF line survive fixture creation on every platform. +blob=$(printf 'alpha \nbeta\n\t\ncrlf\r\ngamma\n' | git hash-object -w --stdin) +tree=$(printf '100644 blob %s\tfile.txt\0' "$blob" | git mktree -z) +commit=$(git -c user.name=A -c user.email=a@example.com commit-tree "$tree" -m content) +git update-ref refs/heads/main "$commit" From ff48765daf968bd4f084fc9451b29962627a0cdc Mon Sep 17 00:00:00 2001 From: Codex Date: Wed, 4 Nov 2026 10:52:21 +0100 Subject: [PATCH 02/14] test(refs): keep PathLike reference names portable on Windows `test_revision_arguments_preserve_pathlike_paths` fails in the Windows `Python package` jobs because `Path("refs/heads/main")` emits `refs\heads\main`. Git rejects backslashes in reference names, so the test raises `ValueError: Invalid reference` before exercising revision argument handling. All four combinations of SHA-1/SHA-256 and files/reftable fail. The same failures are present in the final Windows Python 3.14t job for the introducing PR #2336, so this is inherited by PR #2339 rather than caused by its blame-content change. Use `PurePosixPath` for the reference-name test parameter. Reference names use forward slashes on every platform; filesystem paths elsewhere in the tests continue to use `Path`. Keep `PathLikeMock` coverage and the existing reference validation unchanged. Validation: 28 focused reference/revision tests pass on each of the `cli` and `gix` backends, including all eight PathLike cases, nested merge-base operands, reflog walks, fork-point lookup, and remote archives. Both blame-content regressions also pass on each backend. Ruff 0.17.2 lint and formatting checks pass for `test/test_refs.py`; `git diff --check` passes. Windows validation is left to the PR's CI matrix. Agent-Model: unknown --- test/test_refs.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/test_refs.py b/test/test_refs.py index bd0febf82..97b14b61e 100644 --- a/test/test_refs.py +++ b/test/test_refs.py @@ -10,7 +10,7 @@ import tarfile import unittest from itertools import chain -from pathlib import Path +from pathlib import Path, PurePosixPath from unittest.mock import Mock, patch import pytest @@ -270,7 +270,7 @@ def test_merge_base_preserves_nested_reference_arguments(object_format, ref_form @pytest.mark.parametrize("object_format", ["sha1", "sha256"]) @pytest.mark.parametrize("ref_format", ["files", "reftable"]) -@pytest.mark.parametrize("path_type", [Path, PathLikeMock]) +@pytest.mark.parametrize("path_type", [PurePosixPath, PathLikeMock]) def test_revision_arguments_preserve_pathlike_paths(object_format, ref_format, path_type): with fixture_repo("exact-refs.sh", object_format, ref_format, "shadowed") as repo: local = repo.head.commit From 1cc72392f4960e54980f20c791ce612c8c22f303 Mon Sep 17 00:00:00 2001 From: Byron Date: Tue, 10 Nov 2026 08:11:58 +0100 Subject: [PATCH 03/14] docs(security): clarify command wrapper responsibilities The threat model did not distinguish direct Git command access from higher-level library operations. This left file reads explicitly requested through raw command options liable to be mistaken for unwanted side effects of a higher-level method. Document `git.cmd.Git` and `repo.git` as command wrappers that do what the caller requests, including the effects of supplied options. Use `pathspec_from_file` to illustrate how a requested file read can also appear in Git's diagnostics. Explain the application's responsibility for exposing raw commands and errors to untrusted callers. Distinguish these calls from the protections in `Head.checkout()` and `Repo.clone_from()`, and preserve the reportability of protection bypasses. Assisted-by: GPT 6.1 Sol Co-authored-by: GPT 6.1 Sol --- SECURITY.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/SECURITY.md b/SECURITY.md index e8e777aa0..2d1150a65 100644 --- a/SECURITY.md +++ b/SECURITY.md @@ -37,6 +37,23 @@ For an untrusted repository directory, Git recommends obtaining a clean copy wit unprivileged user for additional isolation. See [Git's `git-upload-pack` security guidance](https://github.com/git/git/blob/master/Documentation/git-upload-pack.adoc#security). +`git.cmd.Git`, also exposed as `repo.git`, is a low-level command wrapper: it +runs the Git command with the arguments and options the caller supplies. It does +what it is told, including file reads, writes, or command execution requested +through those options. These explicitly requested effects are intended command +behavior, not unwanted side effects. For example, +`repo.git.checkout(pathspec_from_file=path)` asks Git to read `path` as pathspec +input, and Git's diagnostics may include that input. Applications exposing this +interface to untrusted callers must restrict commands, arguments, and options +and control which output and errors they disclose. + +Higher-level methods, such as `Head.checkout()` and `Repo.clone_from()`, provide +specific operations and apply protections against options or operands that +introduce unwanted side effects beyond those operations. Where supported, +callers can explicitly opt into unsafe behavior. These protections do not +restrict direct calls through the command wrapper; bypasses of higher-level +protections remain reportable. + Violations of these trust boundaries or bypasses of Git's intended protections remain reportable, including cases where Git itself is affected. From b84749be894dec01cc17b06a0b634e10e675bacf Mon Sep 17 00:00:00 2001 From: anticipate218 <3481402509@qq.com> Date: Thu, 12 Nov 2026 09:33:10 +0800 Subject: [PATCH 04/14] fix(repo): preserve timezone metadata in blame results During a review of Git metadata APIs for development tools, I found that blame results for working-tree changes could not be read through the ordinary datetime properties. Both parsers already retain the actor identities and timestamps, but omit the timezone fields. This makes `authored_datetime` and `committed_datetime` try to load the zero object for uncommitted lines, raising `BadObject`. Parse the offsets reported by Git with the existing `utctz_to_altz` helper and provide them to `Commit`. The ordinary parser preserves its optional-field fallback. This is shared porcelain parsing; blame remains a Git CLI fallback in Gix mode. The shell fixture creates a fresh repository with different author and committer offsets and an uncommitted line. Its four subcases fail on the original source and pass with this change. On Windows CPython3.13.5, the complete non-network `test_repo.py` and `test_commit.py` suites each pass with175 tests and68 subtests under both CLI and Gix selections, with8 skips and1 expected failure per run. Ruff, mypy, basedpyright, ShellCheck and applicable file checks pass. Independent review covers six additional API combinations and24 entries. The full repository, network, other platform/interpreter and documentation matrices were not run. Assisted-by: OpenAI Codex Agent-Model: GPT-6 --- git/repo/base.py | 13 ++++++++++++- test/test_repo.py | 24 ++++++++++++++++++++++++ tests/fixtures/blame-timezones.sh | 10 ++++++++++ 3 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 tests/fixtures/blame-timezones.sh diff --git a/git/repo/base.py b/git/repo/base.py index 6e582becf..32b99296a 100644 --- a/git/repo/base.py +++ b/git/repo/base.py @@ -35,6 +35,7 @@ ) from git.index import IndexFile from git.objects import Submodule, RootModule, Commit +from git.objects.util import utctz_to_altz from git.refs import HEAD, Head, Reference, SymbolicReference, TagReference from git.remote import Remote, _T_RemoteName, add_progress, to_progress_instance from git.util import ( @@ -1363,11 +1364,13 @@ def blame_incremental( safe_decode(props[b"author-mail"].lstrip(b"<").rstrip(b">")), ), authored_date=int(props[b"author-time"]), + author_tz_offset=utctz_to_altz(props[b"author-tz"].decode("ascii")), committer=Actor( safe_decode(props[b"committer"]), safe_decode(props[b"committer-mail"].lstrip(b"<").rstrip(b">")), ), committed_date=int(props[b"committer-time"]), + committer_tz_offset=utctz_to_altz(props[b"committer-tz"].decode("ascii")), ) commits[hexsha] = c else: @@ -1452,9 +1455,11 @@ class InfoTD(TypedDict, total=False): author: str author_email: str author_date: int + author_tz_offset: int committer: str committer_email: str committer_date: int + committer_tz_offset: int info: InfoTD = {} @@ -1504,13 +1509,15 @@ class InfoTD(TypedDict, total=False): # committer Tom Preston-Werner # committer-mail # committer-time 1192271832 - # committer-tz -0700 - IGNORED BY US + # committer-tz -0700 role = m.group(0) if role == "author": if firstpart.endswith("-mail"): info["author_email"] = parts[-1] elif firstpart.endswith("-time"): info["author_date"] = int(parts[-1]) + elif firstpart.endswith("-tz"): + info["author_tz_offset"] = utctz_to_altz(parts[-1]) elif role == firstpart: info["author"] = parts[-1] elif role == "committer": @@ -1518,6 +1525,8 @@ class InfoTD(TypedDict, total=False): info["committer_email"] = parts[-1] elif firstpart.endswith("-time"): info["committer_date"] = int(parts[-1]) + elif firstpart.endswith("-tz"): + info["committer_tz_offset"] = utctz_to_altz(parts[-1]) elif role == firstpart: info["committer"] = parts[-1] # END distinguish mail,time,name @@ -1540,8 +1549,10 @@ class InfoTD(TypedDict, total=False): hex_to_bin(sha), author=Actor._from_string(f"{info['author']} {info['author_email']}"), authored_date=info["author_date"], + author_tz_offset=info.get("author_tz_offset"), committer=Actor._from_string(f"{info['committer']} {info['committer_email']}"), committed_date=info["committer_date"], + committer_tz_offset=info.get("committer_tz_offset"), ) commits[sha] = c blames[-1][0] = c diff --git a/test/test_repo.py b/test/test_repo.py index 4641b31c9..379bcd0f3 100644 --- a/test/test_repo.py +++ b/test/test_repo.py @@ -773,6 +773,30 @@ def test_blame_preserves_line_content(self): lines = [line for _commit, blamed in repo.blame("HEAD", "file.txt") for line in blamed] self.assertEqual(lines, ["alpha ", "beta", "\t", "crlf\r", "gamma"]) + def test_blame_preserves_timezones(self): + with fixture_repo("blame-timezones.sh") as repo: + for incremental in (False, True): + for rev in ("HEAD", None): + with self.subTest(incremental=incremental, rev=rev): + entry = next(iter(repo.blame(rev, "file.txt", incremental=incremental))) + commit = entry[0] + porcelain = repo.git.blame(rev, "--line-porcelain", "--", "file.txt") + offsets = dict( + line.split(" ", 1) + for line in porcelain.splitlines() + if line.startswith(("author-tz ", "committer-tz ")) + ) + with mock.patch.object( + Commit, "_set_cache_", side_effect=AssertionError("unexpected object read") + ): + if rev is None: + self.assertEqual(commit.hexsha, "0" * 40) + else: + self.assertEqual(commit.author_tz_offset, -19800) + self.assertEqual(commit.committer_tz_offset, 14400) + self.assertEqual(commit.authored_datetime.strftime("%z"), offsets["author-tz"]) + self.assertEqual(commit.committed_datetime.strftime("%z"), offsets["committer-tz"]) + @mock.patch.object(Git, "_call_process") def test_blame_complex_revision(self, git): git.return_value = fixture("blame_complex_revision") diff --git a/tests/fixtures/blame-timezones.sh b/tests/fixtures/blame-timezones.sh new file mode 100644 index 000000000..be8cb57a9 --- /dev/null +++ b/tests/fixtures/blame-timezones.sh @@ -0,0 +1,10 @@ +#!/usr/bin/env bash +set -eu +git init -q --initial-branch=main repo +cd repo +printf 'committed\n' > file.txt +git add file.txt +GIT_AUTHOR_DATE='2020-01-01T12:00:00+0530' \ +GIT_COMMITTER_DATE='2020-01-01T13:00:00-0400' \ +git commit -q -m initial +printf 'changed\n' > file.txt From 9aa62156089de5bef6f193038e788894ce433b3a Mon Sep 17 00:00:00 2001 From: Byron Date: Thu, 12 Nov 2026 04:44:46 +0100 Subject: [PATCH 05/14] review --- test/test_repo.py | 2 +- tests/fixtures/blame-content.sh | 6 +++++- tests/fixtures/blame-timezones.sh | 10 ---------- 3 files changed, 6 insertions(+), 12 deletions(-) delete mode 100644 tests/fixtures/blame-timezones.sh diff --git a/test/test_repo.py b/test/test_repo.py index 379bcd0f3..4f1abcd8d 100644 --- a/test/test_repo.py +++ b/test/test_repo.py @@ -774,7 +774,7 @@ def test_blame_preserves_line_content(self): self.assertEqual(lines, ["alpha ", "beta", "\t", "crlf\r", "gamma"]) def test_blame_preserves_timezones(self): - with fixture_repo("blame-timezones.sh") as repo: + with fixture_repo("blame-content.sh") as repo: for incremental in (False, True): for rev in ("HEAD", None): with self.subTest(incremental=incremental, rev=rev): diff --git a/tests/fixtures/blame-content.sh b/tests/fixtures/blame-content.sh index 7ee38af9f..69e54b5c7 100644 --- a/tests/fixtures/blame-content.sh +++ b/tests/fixtures/blame-content.sh @@ -6,5 +6,9 @@ cd repo # CRLF line survive fixture creation on every platform. blob=$(printf 'alpha \nbeta\n\t\ncrlf\r\ngamma\n' | git hash-object -w --stdin) tree=$(printf '100644 blob %s\tfile.txt\0' "$blob" | git mktree -z) -commit=$(git -c user.name=A -c user.email=a@example.com commit-tree "$tree" -m content) +commit=$(GIT_AUTHOR_DATE='2020-01-01T12:00:00+0530' \ + GIT_COMMITTER_DATE='2020-01-01T13:00:00-0400' \ + git -c user.name=A -c user.email=a@example.com commit-tree "$tree" -m content) git update-ref refs/heads/main "$commit" +git reset --hard -q HEAD +printf 'changed\n' > file.txt diff --git a/tests/fixtures/blame-timezones.sh b/tests/fixtures/blame-timezones.sh deleted file mode 100644 index be8cb57a9..000000000 --- a/tests/fixtures/blame-timezones.sh +++ /dev/null @@ -1,10 +0,0 @@ -#!/usr/bin/env bash -set -eu -git init -q --initial-branch=main repo -cd repo -printf 'committed\n' > file.txt -git add file.txt -GIT_AUTHOR_DATE='2020-01-01T12:00:00+0530' \ -GIT_COMMITTER_DATE='2020-01-01T13:00:00-0400' \ -git commit -q -m initial -printf 'changed\n' > file.txt From a2d8dea7be95574a53b7c79fd1fc7353bf02256a Mon Sep 17 00:00:00 2001 From: Byron Date: Thu, 12 Nov 2026 09:47:32 +0100 Subject: [PATCH 06/14] test(refs): cover all option-shaped checkout branch operands Current `main` already rejects every leading-dash branch name through the unconditional `Git._check_operand()` guard in `Head.checkout()`, introduced in `212573bf`. Preserve that shared prevention and make its coverage and public contract explicit. Expand the existing operand tests to short options, attached option values, and terminators, with both `allow_unsafe_options` settings. Extend the scripted cloned-branch fixture with uncommitted work, and verify rejection does not launch Git or change the branch, commit, index status, file content, or untracked-file status. Document the exception and the independent branch validation in the API and unreleased changes. Git's baseline agrees: `check_branch_ref()` rejects leading-dash branch names, and `t/t1402-check-ref-format.sh` tests that rule. Reference source: `6de20f6092dcf9bdb1c8efe03db4b70c82b423dd` (`v2.56.0-116-g6de20f6092`). Placing `--` before the branch would instead select path checkout semantics. Validation used Python 3.12.14, Git 2.54.0, and GixPython 0.3.0 on macOS: - The three new cloned-branch cases failed with the unaltered `Head.checkout()` method extracted from the `3.2.1` tag at `c179357b` and loaded into the current test harness in a disposable process. This was method-level reproduction, not a full installed-release test. - `test/test_positional_args.py` and `test/test_refs.py` passed 184 tests and 24 subtests on each backend through `test/run-local.py`. - The final cloned-branch fixture passed all four cases on each backend. - Ruff lint/format, ShellCheck, and `git diff --check` passed. No new runtime guard is needed. A patched release and advisory publication remain repository-owner decisions; neither is performed by this follow-up. Assisted-by: GPT 6.1 Sol Co-authored-by: GPT 6.1 Sol --- doc/source/changes.rst | 8 ++++++++ git/refs/head.py | 4 ++++ test/test_positional_args.py | 4 +++- test/test_refs.py | 34 ++++++++++++++++++++++++---------- tests/fixtures/refs.sh | 6 ++++-- 5 files changed, 43 insertions(+), 13 deletions(-) diff --git a/doc/source/changes.rst b/doc/source/changes.rst index 487ac8916..b44e1ac14 100644 --- a/doc/source/changes.rst +++ b/doc/source/changes.rst @@ -13,6 +13,11 @@ contain 20 binary bytes or 40 hexadecimal characters. Legacy class-level ``NULL_BIN_SHA`` and ``NULL_HEX_SHA`` constants retain their SHA-1 values; do not use their width to interpret repository object IDs. +Security fixes +-------------- + +- https://github.com/gitpython-developers/GitPython/security/advisories/GHSA-f8wj-prgh-h9j7 + Git executable and safety ------------------------- @@ -23,6 +28,9 @@ Git executable and safety option/protocol checks remain, and operands and stdin records receive additional validation. NUL-delimited ``cat-file`` requests prevent newline injection. Previously accepted option-like revision/ref arguments can now be rejected. + ``Head.checkout()`` rejects every branch name starting with a dash, including + when ``allow_unsafe_options=True``. This setting permits explicit keyword + options while branch names remain operands. * New plumbing calls suppress implicit hooks, filesystem monitors, automatic maintenance, and lazy network fetches. Index staging continues to store raw content rather than introduce clean filters. Diffs disable external diff and diff --git a/git/refs/head.py b/git/refs/head.py index a49e4a9b8..d73f8b0af 100644 --- a/git/refs/head.py +++ b/git/refs/head.py @@ -279,6 +279,7 @@ def checkout( :param allow_unsafe_options: Allow unsafe options such as ``--pathspec-from-file`` to be passed to :manpage:`git-checkout(1)`. + Branch names are validated regardless of this setting. :param kwargs: Additional keyword arguments to be passed to git checkout, e.g. @@ -290,6 +291,9 @@ def checkout( If there is no active branch, as the HEAD is now detached, the HEAD reference will be returned instead. + :raise git.exc.UnsafeOptionError: + If the branch name starts with a dash or contains NUL, CR, or LF. + :note: By default it is only allowed to checkout heads - everything else will leave the HEAD detached which is allowed and possible, but remains a special state diff --git a/test/test_positional_args.py b/test/test_positional_args.py index 37999b9fb..a4a84ff84 100644 --- a/test/test_positional_args.py +++ b/test/test_positional_args.py @@ -73,7 +73,9 @@ def test_rename_head_cannot_select_current_branch(repo, tmp_path): @pytest.mark.parametrize("allow_unsafe_options", [False, True]) -@pytest.mark.parametrize("name", ["--force", "--detach", "--ours"]) +@pytest.mark.parametrize( + "name", ["-f", "-Bcheckout-target", "--orphan=checkout-target", "--force", "--detach", "--ours", "-", "--"] +) def test_checkout_rejects_option_shaped_branch_names(repo, tmp_path, name, allow_unsafe_options): branch = Head(repo, "refs/heads/" + name) with mock.patch.object(Git, "_call_process_safe", side_effect=AssertionError("Git must not run")) as run: diff --git a/test/test_refs.py b/test/test_refs.py index efeee1d04..7d8f7f7fe 100644 --- a/test/test_refs.py +++ b/test/test_refs.py @@ -43,6 +43,30 @@ from test.lib.fixtures import fixture_repo, read_only_fixture, writable_fixture +@pytest.mark.parametrize( + "branch_name", ["--pathspec-from-file=pathspecs", "-f", "--orphan=checkout-target", "-Bcheckout-target"] +) +def test_cloned_head_checkout_rejects_option_shaped_branch_names(branch_name): + with fixture_repo("refs.sh", "unsafe-branch", "sha1", branch_name, write=True, subdir="clone") as cloned: + branch = cloned.active_branch + assert branch.name == branch_name + commit = cloned.head.commit + path = Path(cloned.working_tree_dir) / "file.txt" + content = path.read_bytes() + status = cloned.git.status("--porcelain") + assert status.startswith(" M file.txt") + for allow_unsafe_options in (False, True): + with patch.object(Git, "execute", autospec=True, side_effect=Git.execute) as run: + with pytest.raises(UnsafeOptionError) as error: + branch.checkout(force=False, allow_unsafe_options=allow_unsafe_options) + run.assert_not_called() + assert "unmatched-private-content" not in str(error.value) + assert cloned.active_branch == branch + assert cloned.head.commit == commit + assert path.read_bytes() == content + assert cloned.git.status("--porcelain") == status + + @pytest.mark.parametrize("object_format", ["sha1", "sha256"]) def test_reference_names_preserve_unicode_line_separators(object_format): names = [ @@ -658,16 +682,6 @@ def test_head_checkout_rejects_pathspec_from_file(self, rw_repo): with self.assertRaises(UnsafeOptionError): branch.checkout() - def test_cloned_head_checkout_rejects_pathspec_from_file(self): - with fixture_repo("refs.sh", "unsafe-branch", write=True, subdir="clone") as cloned: - branch_name = "--pathspec-from-file=pathspecs" - assert cloned.active_branch.name == branch_name - for allow_unsafe_options in (False, True): - with self.assertRaises(UnsafeOptionError) as error: - cloned.active_branch.checkout(allow_unsafe_options=allow_unsafe_options) - assert "unmatched-private-content" not in str(error.exception) - assert cloned.active_branch.name == branch_name - @with_rw_repo("HEAD") def test_head_reset_rejects_pathspec_from_file(self, rw_repo): pathspecs = read_only_fixture("refs-input.sh") / "pathspecs" diff --git a/tests/fixtures/refs.sh b/tests/fixtures/refs.sh index cc13ed00d..969a89e1e 100644 --- a/tests/fixtures/refs.sh +++ b/tests/fixtures/refs.sh @@ -39,10 +39,12 @@ case "$1" in git rev-parse HEAD > ../outside-ref ;; unsafe-branch) - git update-ref 'refs/heads/--pathspec-from-file=pathspecs' HEAD - git symbolic-ref HEAD 'refs/heads/--pathspec-from-file=pathspecs' + branch_name="$3" + git update-ref "refs/heads/$branch_name" HEAD + git symbolic-ref HEAD "refs/heads/$branch_name" cd .. git clone repo clone printf 'unmatched-private-content\n' > clone/pathspecs + printf 'uncommitted\n' >> clone/file.txt ;; esac From 92a07cfa89cc1873807a6bbc4b399beea44e8068 Mon Sep 17 00:00:00 2001 From: Keerthana KT Date: Fri, 13 Nov 2026 21:21:32 +0530 Subject: [PATCH 07/14] fix(gix): use Git for `.gitmodules` lines that Git rejects On the Gix backend, `_backend.submodule_config()` reads `.gitmodules` with GixPython's `ModulesFile.from_bytes()` for `Repo.submodules`, `Submodule.iter_items()` and the cached `path`, `url` and `branch` fields. GixPython 0.3.0 accepts lines that Git rejects with `bad config line` and still returns settings from them. A line holding more than one name is read as several settings: after `url = ../good`, the line `x url = ../evil` is read as an implicit `x` plus `url = ../evil`, and `[submodule "module"] x url = ../evil` does the same on a header line. `br#anch = main` is read as an implicit `br` followed by a comment. `.gitmodules` is untrusted repository content. For such a file, `git submodule update --init` stops with `fatal: bad config line`, `git fsck --strict` only warns `gitmodulesParse` and exits 0, and the CLI backend raises `configparser.ParsingError`. The Gix backend instead listed the submodule with `../evil`, a URL Git never reads, and `Submodule.update(init=True)` cloned from it. GixPython has no strict parsing mode, so the adapter now selects the CLI path unless every line is blank, a comment, a bare `[submodule "..."]` header or a single `key` or `key = value` setting. Git then reports the error exactly as on the CLI backend. The guard only chooses the fallback and produces no values. Continued values and settings on header lines also go to Git, which keeps the guard simple at the cost of one CLI call for those uncommon files. The mismatch is recorded as GIX-35 in `doc/gix-backend.md`; this guard does not close it. Validation on macOS arm64 with CPython 3.13.17, Git 2.54.0 and GixPython 0.3.0: - `test_submodule_configuration_rejects_lines_git_rejects`, using the new `submodule-rejected-lines.sh` fixture, fails for all three scenarios on the Gix backend before the change and passes on both backends after it. - A differential fuzzer comparing the adapter with `git config --file .gitmodules --list` found rejected files with native values within the first 3,000 mutated inputs before the change, and none in 13,000 after it. Where Git accepts a file, it found no value differences in 7,500 inputs. - Full suite: 2103 passed, 155 skipped, 1 xfailed on the CLI backend; 2206 passed, 52 skipped, 1 xfailed on the Gix backend, where native submodule reads still serve 943 calls and the new fallback serves only the 6 calls from the new test. - Ruff check and format, mypy, shellcheck and `git diff --check` pass. Agent-Model: claude-opus-5-5 --- doc/gix-backend.md | 3 ++- git/_backend.py | 11 +++++++++++ test/test_submodule.py | 12 ++++++++++++ tests/fixtures/submodule-rejected-lines.sh | 18 ++++++++++++++++++ 4 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 tests/fixtures/submodule-rejected-lines.sh diff --git a/doc/gix-backend.md b/doc/gix-backend.md index 8a5b2ec71..7861054f8 100644 --- a/doc/gix-backend.md +++ b/doc/gix-backend.md @@ -373,7 +373,7 @@ and predate the current fallback; they do not establish native coverage. | Managed `symbolic_ref` | Nonrecursive target lookup | Mutation, command-level missing-reference diagnostics | | Reference enumeration, `for_each_ref` | Sorted reference names and literal prefixes, preserving symbolic aliases | Dangling symbolic refs, root refs, glob patterns, other formats/options; GIX-25 | | Managed `config --get KEY` | Merged repository config snapshot | Files/streams, enumeration, mutation; GIX-12 | -| Submodule enumeration and cached fields | Dedicated native `.gitmodules` parser over the existing worktree/blob source, with raw path/URL/branch values | Non-UTF-8 subsection names, other/duplicate sections, ambiguous brackets, missing/implicit fields and parser errors; GIX-12 | +| Submodule enumeration and cached fields | Dedicated native `.gitmodules` parser over the existing worktree/blob source, with raw path/URL/branch values | Non-UTF-8 subsection names, other/duplicate sections, ambiguous brackets, missing/implicit fields and parser errors; GIX-12. Lines other than comments, bare headers and single settings; GIX-35 | | Commit hooks | No bound native lookup/execution API | All hooks, including absent hooks; GIX-23 | | Worktree inventory, `worktree list` | Main and linked worktree metadata, including bare main repositories and locks | Prunable entries; GIX-14 | | `Repo.merge_base`, `Repo.is_ancestor` | Native graph queries for two revisions | Octopus/fork-point and other options | @@ -990,6 +990,7 @@ identify the historical fixture where needed. | GIX-32 | Missing capability (open); no demonstrated Git compatibility mismatch | Windows CI on GixPython 0.3.0, Git 2.56.0.windows.2 and CPython 3.12.10 enumerates the non-UTF-8 packed names in `legacy-refs.sh`, but exact byte-name lookup selects `Reference.read: CLI: native read diagnostics`; the no-CLI guard fails for SHA-1 and SHA-256. POSIX lookup succeeds. Git's Windows CLI likewise cannot address those original bytes through UTF-16 arguments, so there is no demonstrated equivalent Windows CLI operand. Git enumeration does preserve both names. | Provide native Windows packed-reference lookup for byte names without first converting the name to a loose-reference filesystem path. Keep `reference_info()`'s existing diagnostic fallback; `test_non_utf8_tag_names_preserve_bytes` checks it on Windows. Resolution coverage and native claims remain POSIX-only. The upstream lookup source attempts loose-path conversion before consulting packed refs; CI demonstrates the fallback, but does not capture the underlying Gix error text. | | GIX-33 | Bug (open); missing exact-name mode | With Git 2.54.0 and GixPython 0.3.0, `exact-refs.sh sha1 files symbolic` makes `refs/tags/REBASE_HEAD` a symbolic ref to another tag while the root `REBASE_HEAD` is absent. `Repository.try_find_reference("REBASE_HEAD")` returns that tag; native `_symbolic_ref` formerly returned its target, whereas `git symbolic-ref --quiet --no-recurse -- REBASE_HEAD` exits 1. GitPython's root-reference object read consequently returned a commit through Gix and raised `ValueError` through CLI. SHA-256 reproduces the same mismatch. | Expose an exact-name lookup mode, including root references, whose absence and error behavior matches Git. `_symbolic_ref()` compares the native reference's name with the requested bytes and selects CLI for a lookalike. `test_missing_root_reference_does_not_follow_symbolic_lookalike_tag` verifies parity and forced CLI fallback. This adapter guard does not close the upstream compatibility bug. | | GIX-34 | Bug (open) | With Git 2.54.0 and GixPython 0.3.0, an author, committer or tagger line with fields after its time zone, such as `A 1112591163 +0200 1700000000 -0500`, decodes through `Commit.author()`/`committer()` and `Tag.tagger()` with the correct seconds but an offset of 0. Git reads only the first date and time zone after the last `>` (`split_ident_line`) and reports `1112591163 +0200` through `git show --format=%ad --date=raw` and `%(taggerdate:raw)`. `git fsck` reports `badTimezone` for such objects, but the default `transfer.fsckObjects=false` accepts them. The `trailing-date` read-only `commit.sh` fixture in `test_deserialization_reads_date_after_email` reproduces both object kinds. | Report the time zone Git reads for identity lines with trailing fields, or reject them consistently. `object_metadata()` selects CLI when the decoded signature does not reproduce the end of its raw header line. This adapter guard does not close the upstream compatibility bug. | +| GIX-35 | Bug (open) | With Git 2.54.0 and GixPython 0.3.0, `ModulesFile.from_bytes()` accepts `.gitmodules` lines that Git rejects with `bad config line`, so `git submodule update --init` stops while `git fsck` reports only `gitmodulesParse`. A line holding more than one name is read as several settings: after `url = ../good`, `x url = ../evil` makes `url()` return `../evil` with `submodule..x` true, and `[submodule "module"] x url = ../evil` does the same on the header line. `br#anch = main` is read as an implicit `br` followed by a comment. The `submodule-rejected-lines.sh` read-only fixture in `test_submodule_configuration_rejects_lines_git_rejects` reproduces all three. | Reject the lines Git rejects, at least in a Git-strict parsing mode, with a typed error. `submodule_config()` selects CLI unless every line is blank, a comment, a bare submodule header or a single `key` or `key = value` setting, so Git's parser reports the error. This adapter guard does not close the upstream compatibility bug. | GIX-14 also covers configured worktree symlink resolution. On macOS with GixPython 0.3.0, CPython 3.12.14 and Git 2.54.0, set a submodule's diff --git a/git/_backend.py b/git/_backend.py index 51d25c436..a239f4007 100644 --- a/git/_backend.py +++ b/git/_backend.py @@ -403,6 +403,15 @@ def _for_each_ref(repo: Any, args: List[str], kwargs: Dict[str, Any]) -> bytes: ) +# The native parser accepts lines Git rejects, such as ``x url = ...`` (GIX-35). +# Keep only blank and comment lines, bare submodule headers and single settings +# native; Git parses everything else, including continued values. +_NATIVE_SUBMODULE_CONFIG_LINE = re.compile( + rb'[ \t\r]*(?:[#;].*|\[submodule[ \t]+"(?:[^"\\]|\\.)*"\][ \t\r]*(?:[#;].*)?|[a-z][a-z0-9-]*[ \t]*(?:=.*|\r?))?', + re.I, +) + + def submodule_config(source: Any) -> Any: """Read the fields used by submodule objects from their selected config source.""" if gix is None: @@ -417,6 +426,8 @@ def submodule_config(source: Any) -> Any: # occurrences and duplicate sections until GIX-12 can preserve their order. if b"\0" in data or re.search(rb"\[(?!submodule[ \t])", data, re.I): raise _Unsupported("non-submodule configuration sections (GIX-12)") + if not all(map(_NATIVE_SUBMODULE_CONFIG_LINE.fullmatch, data.split(b"\n"))): + raise _Unsupported("configuration lines Git may reject (GIX-35)") modules = gix.ModulesFile.from_bytes(data) with modules.names() as cursor: names = list(cursor) diff --git a/test/test_submodule.py b/test/test_submodule.py index 751235e21..54ce27668 100644 --- a/test/test_submodule.py +++ b/test/test_submodule.py @@ -61,6 +61,18 @@ def test_native_submodule_configuration_preserves_non_utf8_values(): assert native == {"module": {"path": "path-\udcfe", "url": "url-\udcfd", "branch": "refs/heads/branch-\udcfc"}} +@pytest.mark.parametrize("scenario", ["extra-setting", "header-setting", "key-comment"]) +def test_submodule_configuration_rejects_lines_git_rejects(scenario): + with fixture_repo("submodule-rejected-lines.sh", scenario) as repo: + with pytest.raises(GitCommandError, match="bad config line"): + repo.git.config("list", "--file", ".gitmodules") + with pytest.raises(cp.ParsingError): + repo.submodules + submodule = Submodule(repo, repo.head.commit.tree["module"].binsha, name="module") + with pytest.raises(cp.ParsingError): + submodule.url + + @pytest.mark.parametrize("state", ["unborn", "untracked", "detached"]) @pytest.mark.parametrize("remote", [None, ".", "origin"], ids=["working-tree", "local-origin", "origin"]) @patch_git_config("protocol.file.allow", "always") diff --git a/tests/fixtures/submodule-rejected-lines.sh b/tests/fixtures/submodule-rejected-lines.sh new file mode 100644 index 000000000..488817e1d --- /dev/null +++ b/tests/fixtures/submodule-rejected-lines.sh @@ -0,0 +1,18 @@ +#!/usr/bin/env bash +set -eu +git init -q repo +cd repo +git -c user.name=fixture -c user.email=fixture@example.invalid commit --allow-empty -qm initial +# Git rejects each of these files with "bad config line". +case "$1" in + extra-setting) gitmodules='[submodule "module"]\npath = module\nurl = ../good\nx url = ../evil\n' ;; + header-setting) gitmodules='[submodule "module"] x url = ../evil\npath = module\n' ;; + key-comment) gitmodules='[submodule "module"]\npath = module\nurl = ../good\nbr#anch = main\n' ;; + *) exit 1 ;; +esac +printf '%b' "$gitmodules" > .gitmodules +modules=$(git hash-object -w .gitmodules) +commit=$(git rev-parse HEAD) +tree=$(printf '100644 blob %s\t.gitmodules\000160000 commit %s\tmodule\000' "$modules" "$commit" | git mktree -z) +commit=$(printf 'Add submodule\n' | git -c user.name=fixture -c user.email=fixture@example.invalid commit-tree "$tree" -p "$commit") +git update-ref HEAD "$commit" From e3ca695320251182539ceca90224a10ed98fa8fb Mon Sep 17 00:00:00 2001 From: Byron Date: Sat, 14 Nov 2026 21:50:51 +0100 Subject: [PATCH 08/14] review Keept the note about the Gix shortcoming without protecting against it. Plan is to have this fixed before release. --- git/_backend.py | 11 ----------- test/test_submodule.py | 12 ------------ tests/fixtures/submodule-rejected-lines.sh | 18 ------------------ 3 files changed, 41 deletions(-) delete mode 100644 tests/fixtures/submodule-rejected-lines.sh diff --git a/git/_backend.py b/git/_backend.py index a239f4007..51d25c436 100644 --- a/git/_backend.py +++ b/git/_backend.py @@ -403,15 +403,6 @@ def _for_each_ref(repo: Any, args: List[str], kwargs: Dict[str, Any]) -> bytes: ) -# The native parser accepts lines Git rejects, such as ``x url = ...`` (GIX-35). -# Keep only blank and comment lines, bare submodule headers and single settings -# native; Git parses everything else, including continued values. -_NATIVE_SUBMODULE_CONFIG_LINE = re.compile( - rb'[ \t\r]*(?:[#;].*|\[submodule[ \t]+"(?:[^"\\]|\\.)*"\][ \t\r]*(?:[#;].*)?|[a-z][a-z0-9-]*[ \t]*(?:=.*|\r?))?', - re.I, -) - - def submodule_config(source: Any) -> Any: """Read the fields used by submodule objects from their selected config source.""" if gix is None: @@ -426,8 +417,6 @@ def submodule_config(source: Any) -> Any: # occurrences and duplicate sections until GIX-12 can preserve their order. if b"\0" in data or re.search(rb"\[(?!submodule[ \t])", data, re.I): raise _Unsupported("non-submodule configuration sections (GIX-12)") - if not all(map(_NATIVE_SUBMODULE_CONFIG_LINE.fullmatch, data.split(b"\n"))): - raise _Unsupported("configuration lines Git may reject (GIX-35)") modules = gix.ModulesFile.from_bytes(data) with modules.names() as cursor: names = list(cursor) diff --git a/test/test_submodule.py b/test/test_submodule.py index 54ce27668..751235e21 100644 --- a/test/test_submodule.py +++ b/test/test_submodule.py @@ -61,18 +61,6 @@ def test_native_submodule_configuration_preserves_non_utf8_values(): assert native == {"module": {"path": "path-\udcfe", "url": "url-\udcfd", "branch": "refs/heads/branch-\udcfc"}} -@pytest.mark.parametrize("scenario", ["extra-setting", "header-setting", "key-comment"]) -def test_submodule_configuration_rejects_lines_git_rejects(scenario): - with fixture_repo("submodule-rejected-lines.sh", scenario) as repo: - with pytest.raises(GitCommandError, match="bad config line"): - repo.git.config("list", "--file", ".gitmodules") - with pytest.raises(cp.ParsingError): - repo.submodules - submodule = Submodule(repo, repo.head.commit.tree["module"].binsha, name="module") - with pytest.raises(cp.ParsingError): - submodule.url - - @pytest.mark.parametrize("state", ["unborn", "untracked", "detached"]) @pytest.mark.parametrize("remote", [None, ".", "origin"], ids=["working-tree", "local-origin", "origin"]) @patch_git_config("protocol.file.allow", "always") diff --git a/tests/fixtures/submodule-rejected-lines.sh b/tests/fixtures/submodule-rejected-lines.sh deleted file mode 100644 index 488817e1d..000000000 --- a/tests/fixtures/submodule-rejected-lines.sh +++ /dev/null @@ -1,18 +0,0 @@ -#!/usr/bin/env bash -set -eu -git init -q repo -cd repo -git -c user.name=fixture -c user.email=fixture@example.invalid commit --allow-empty -qm initial -# Git rejects each of these files with "bad config line". -case "$1" in - extra-setting) gitmodules='[submodule "module"]\npath = module\nurl = ../good\nx url = ../evil\n' ;; - header-setting) gitmodules='[submodule "module"] x url = ../evil\npath = module\n' ;; - key-comment) gitmodules='[submodule "module"]\npath = module\nurl = ../good\nbr#anch = main\n' ;; - *) exit 1 ;; -esac -printf '%b' "$gitmodules" > .gitmodules -modules=$(git hash-object -w .gitmodules) -commit=$(git rev-parse HEAD) -tree=$(printf '100644 blob %s\t.gitmodules\000160000 commit %s\tmodule\000' "$modules" "$commit" | git mktree -z) -commit=$(printf 'Add submodule\n' | git -c user.name=fixture -c user.email=fixture@example.invalid commit-tree "$tree" -p "$commit") -git update-ref HEAD "$commit" From 71ebaa7a64b2401ff9e5e113fab7d594ef7e582c Mon Sep 17 00:00:00 2001 From: Keerthana KT Date: Sun, 15 Nov 2026 11:18:23 +0530 Subject: [PATCH 09/14] fix(gix): use Git for tree entries whose modes Git canonicalizes On the Gix backend, `Tree._set_cache_()` lists a tree through the native `_backend._ls_tree()` adapter, which reports `TreeEntry.mode()` and `TreeEntry.kind()` from GixPython 0.3.0. Gix keeps the mode as stored, while Git canonicalizes it when it reads a tree (`canon_mode`), so `git ls-tree`, `git read-tree` and checkout use `100755` for `100700`, `120000` for `120777` and `160000` for any mode whose type is not a regular file, symbolic link or directory. Tree contents are untrusted repository content, and `git fsck` reports such trees only as `badFilemode` warnings, so default transfers accept them. On the Gix backend: - an entry with mode `120777` had `Blob.mode == 0o120777`, so a check against `Blob.link_mode` treated a path that Git checks out as a symbolic link as a regular file; `100700` likewise did not match `Blob.executable_mode`; - a `140000` entry, which Git reads as a gitlink, was reported as a tree with mode `0o40000`, so `Tree.traverse()` and `tree / "sub/..."` listed the contents of an object Git never checks out; - other noncanonical gitlink modes, such as `170000`, made iteration raise `TypeError`. The CLI backend already reported Git's modes. GixPython exposes neither the modes Git reads nor the raw mode bytes, so the adapter now selects the CLI path unless every native mode is one Git reports and the tree's bytes, written to `Repository.with_object_memory()`, re-encode to the same object ID. The second check catches the `140000` case, whose native mode already looks canonical; it follows the existing GIX-5 round-trip preflight in `_hash_object()`. The guard only chooses the fallback and produces no values. Canonical trees stay native. The mismatch is recorded as GIX-36 in `doc/gix-backend.md`; this guard does not close it. Validation on macOS with CPython 3.12, Git 2.54.0 and GixPython 0.3.0: - `test_tree_entry_modes_match_git`, using the new `tree-modes.sh` fixture, compares each entry with `git ls-tree`; it fails for all six cases (three modes, SHA-1 and SHA-256) on the Gix backend before the change and passes on both backends after it. - A sweep of 99 literal tree modes against `git ls-tree` found a mode or kind difference for every mode Git canonicalizes, except the zero-padded `040000` and `0100644` spellings, which matched, and `10100644`, which Gix fails to decode and already sends to Git. - Full suite: 2105 passed, 155 skipped, 1 xfailed on the CLI backend; 2208 passed, 52 skipped, 1 xfailed on the Gix backend. - Ruff check and format, mypy and `git diff --check` pass; shellcheck was not available locally. Agent-Model: claude-opus-5-5 --- doc/gix-backend.md | 3 ++- git/_backend.py | 11 ++++++++++- test/test_tree.py | 14 +++++++++++++- tests/fixtures/tree-modes.sh | 16 ++++++++++++++++ 4 files changed, 41 insertions(+), 3 deletions(-) create mode 100644 tests/fixtures/tree-modes.sh diff --git a/doc/gix-backend.md b/doc/gix-backend.md index 7861054f8..909b8c707 100644 --- a/doc/gix-backend.md +++ b/doc/gix-backend.md @@ -367,7 +367,7 @@ and predate the current fallback; they do not establish native coverage. | Standalone discovery helpers | Native storage directories and gitfiles reopened at their canonical native Git directory | Rejected candidates and the same layout/environment guards as `Repo` opening; GIX-14 | | `Repo.rev_parse`, `rev_parse` metadata queries | Revision lookup, common directory, object format, bare flag, worktree root, fixed `modules`/`COMMIT_EDITMSG` locations | Repository-format validation, other metadata paths, symlinked metadata leaves, message searches and describe forms; GIX-1/14/17/22 | | Revision path/mode metadata | Native revision specification, including tree paths and index stages | Custom/sparse indexes, unsupported revision grammar or path normalization | -| Tree enumeration, `ls_tree` | Native tree entries with modes, names, and object IDs | Other command options | +| Tree enumeration, `ls_tree` | Native tree entries with modes, names, and object IDs | Other command options; entries with modes Git canonicalizes or that do not round-trip through native tree encoding; GIX-36 | | Reference object reads | One exact native lookup for direct, symbolic and absent references | Partial names and native decoding errors | | Reference-name validation | Native full-name validation without opening a repository | Standalone names outside the native grammar; GIX-20 | | Managed `symbolic_ref` | Nonrecursive target lookup | Mutation, command-level missing-reference diagnostics | @@ -991,6 +991,7 @@ identify the historical fixture where needed. | GIX-33 | Bug (open); missing exact-name mode | With Git 2.54.0 and GixPython 0.3.0, `exact-refs.sh sha1 files symbolic` makes `refs/tags/REBASE_HEAD` a symbolic ref to another tag while the root `REBASE_HEAD` is absent. `Repository.try_find_reference("REBASE_HEAD")` returns that tag; native `_symbolic_ref` formerly returned its target, whereas `git symbolic-ref --quiet --no-recurse -- REBASE_HEAD` exits 1. GitPython's root-reference object read consequently returned a commit through Gix and raised `ValueError` through CLI. SHA-256 reproduces the same mismatch. | Expose an exact-name lookup mode, including root references, whose absence and error behavior matches Git. `_symbolic_ref()` compares the native reference's name with the requested bytes and selects CLI for a lookalike. `test_missing_root_reference_does_not_follow_symbolic_lookalike_tag` verifies parity and forced CLI fallback. This adapter guard does not close the upstream compatibility bug. | | GIX-34 | Bug (open) | With Git 2.54.0 and GixPython 0.3.0, an author, committer or tagger line with fields after its time zone, such as `A 1112591163 +0200 1700000000 -0500`, decodes through `Commit.author()`/`committer()` and `Tag.tagger()` with the correct seconds but an offset of 0. Git reads only the first date and time zone after the last `>` (`split_ident_line`) and reports `1112591163 +0200` through `git show --format=%ad --date=raw` and `%(taggerdate:raw)`. `git fsck` reports `badTimezone` for such objects, but the default `transfer.fsckObjects=false` accepts them. The `trailing-date` read-only `commit.sh` fixture in `test_deserialization_reads_date_after_email` reproduces both object kinds. | Report the time zone Git reads for identity lines with trailing fields, or reject them consistently. `object_metadata()` selects CLI when the decoded signature does not reproduce the end of its raw header line. This adapter guard does not close the upstream compatibility bug. | | GIX-35 | Bug (open) | With Git 2.54.0 and GixPython 0.3.0, `ModulesFile.from_bytes()` accepts `.gitmodules` lines that Git rejects with `bad config line`, so `git submodule update --init` stops while `git fsck` reports only `gitmodulesParse`. A line holding more than one name is read as several settings: after `url = ../good`, `x url = ../evil` makes `url()` return `../evil` with `submodule..x` true, and `[submodule "module"] x url = ../evil` does the same on the header line. `br#anch = main` is read as an implicit `br` followed by a comment. The `submodule-rejected-lines.sh` read-only fixture in `test_submodule_configuration_rejects_lines_git_rejects` reproduces all three. | Reject the lines Git rejects, at least in a Git-strict parsing mode, with a typed error. `submodule_config()` selects CLI unless every line is blank, a comment, a bare submodule header or a single `key` or `key = value` setting, so Git's parser reports the error. This adapter guard does not close the upstream compatibility bug. | +| GIX-36 | Bug (open) | With Git 2.54.0 and GixPython 0.3.0, `Tree.iter()` reports tree entry modes as stored, while Git canonicalizes them when it reads a tree (`canon_mode`): `git ls-tree` and checkout use `100755` for `100700`, `120000` for `120777` and `160000` for every mode whose type is not a regular file, symbolic link or directory. `TreeEntry.mode()` returned `0o100700` and `0o120777`, so `Blob.mode` did not match `Blob.executable_mode` or `Blob.link_mode` for entries Git checks out as an executable or a symbolic link. A `140000` entry, which Git reads as a gitlink, is reported as a tree with mode `0o40000`, so GitPython listed the contents of an object Git never checks out; other noncanonical gitlink modes made tree iteration raise `TypeError`. `git fsck` reports these trees only as `badFilemode` warnings, so default transfers accept them. The `tree-modes.sh` read-only fixture in `test_tree_entry_modes_match_git` reproduces all three for SHA-1 and SHA-256. | Report the modes Git reads, at least in a Git-compatible mode, or expose the raw mode bytes. `_ls_tree()` selects CLI unless every native mode is one Git reports and the tree's bytes re-encode to the same object ID through the in-memory object database, so Git lists the entries. This adapter guard does not close the upstream compatibility bug. | GIX-14 also covers configured worktree symlink resolution. On macOS with GixPython 0.3.0, CPython 3.12.14 and Git 2.54.0, set a submodule's diff --git a/git/_backend.py b/git/_backend.py index 51d25c436..1e79cc4e6 100644 --- a/git/_backend.py +++ b/git/_backend.py @@ -314,7 +314,16 @@ def _rev_parse(repo: Any, args: List[str], kwargs: Dict[str, Any]) -> bytes: def _ls_tree(repo: Any, args: List[str], kwargs: Dict[str, Any]) -> bytes: if len(args) != 3 or args[:2] != ["-z", "--full-tree"]: raise _Unsupported("tree options") - with repo.find_tree(_oid(repo, args[2])).iter() as entries: + oid = _oid(repo, args[2]) + tree = repo.find_tree(oid) + # Native entries keep modes Git canonicalizes and read a 14xxxx gitlink as a + # tree (GIX-36); Git lists trees whose entries do not round-trip natively. + if repo.with_object_memory().write_object("tree", tree.data) != oid: + raise _Unsupported("tree entries Git canonicalizes (GIX-36)") + with tree.iter() as entries: + if any(entry.mode() not in _ENTRY_KINDS for entry in entries): + raise _Unsupported("tree entries Git canonicalizes (GIX-36)") + with tree.iter() as entries: return b"".join( b"%06o %s %s\t%s\0" % ( diff --git a/test/test_tree.py b/test/test_tree.py index 8d29319fb..192702a94 100644 --- a/test/test_tree.py +++ b/test/test_tree.py @@ -56,7 +56,19 @@ def test_invalid_tree_does_not_retain_partial_cache(object_format, invalid_posit list(tree) -def test_tree_modifier_orders_non_utf8_names_as_git_does(): +@pytest.mark.parametrize("object_format", ["sha1", "sha256"]) +@pytest.mark.parametrize("scenario", ["executable", "symlink", "gitlink"]) +def test_tree_entry_modes_match_git(object_format, scenario): + with fixture_repo("tree-modes.sh", object_format, scenario) as repo: + root = read_only_fixture("tree-modes.sh", object_format, scenario) + oid = (root / "tree-id").read_text().strip() + metadata, name = (root / "entries").read_text().rstrip("\n").split("\t") + mode, kind, entry_oid = metadata.split() + assert mode in ("100755", "120000", "160000") + tree = Tree(repo, bytes.fromhex(oid), path="") + assert [(entry.mode, entry.type, entry.hexsha, entry.path) for entry in tree] == [ + (int(mode, 8), "submodule" if kind == "commit" else kind, entry_oid, name) + ] with fixture_repo("tree-byte-order.sh") as repo: expected = (read_only_fixture("tree-byte-order.sh") / "order").read_bytes().split(b"\0")[:-1] tree = repo.head.commit.tree diff --git a/tests/fixtures/tree-modes.sh b/tests/fixtures/tree-modes.sh new file mode 100644 index 000000000..fe1938a85 --- /dev/null +++ b/tests/fixtures/tree-modes.sh @@ -0,0 +1,16 @@ +#!/usr/bin/env bash +set -eu +git init --initial-branch=main --object-format="$1" repo +cd repo +blob=$(printf 'content\n' | git hash-object -w --stdin) +subtree=$(printf '100644 blob %s\tinner\n' "$blob" | git mktree) +case "$2" in +executable) entry="100700 exe" oid=$blob ;; +symlink) entry="120777 link" oid=$blob ;; +gitlink) entry="140000 sub" oid=$subtree ;; +esac +# git mktree only writes canonical modes, so write the legacy mode literally. +escaped=$(printf %s "$oid" | sed 's/../\\x&/g') +{ printf '%s\0' "$entry"; printf %b "$escaped"; } | + git hash-object -w -t tree --literally --stdin >../tree-id +git ls-tree "$(cat ../tree-id)" >../entries From 334d3189cee1f3354068509078e5b34281080dd4 Mon Sep 17 00:00:00 2001 From: Codex GPT-5 Date: Sun, 15 Nov 2026 12:17:54 +0100 Subject: [PATCH 10/14] fix(refs): require a boolean checkout force flag Triaging `GHSA-j3hg-fv33-37rw` revealed that `Head.checkout()` did not validate its documented boolean `force` parameter before assembling the Git command. Dynamically typed callers could therefore supply values outside the flag's contract and alter checkout argument parsing. Reject non-boolean values with `TypeError` before any Git call, including when unsafe options are explicitly allowed. Preserve `True` and `False` behavior instead of coercing strings into permission to discard local changes. Document the exception and compatibility change. The regression failed before the fix for an option-shaped string reaching the command runner. It covers positional and keyword calls, both unsafe option settings and short-option formatting modes, strings, bytes, sequences, integers, and `None`. No private advisory reproduction is included. Git reference: local source at `6de20f6092dcf9bdb1c8efe03db4b70c82b423dd`, `Documentation/git-checkout.adoc` and `builtin/checkout.c` specify `--force` as a no-argument switch (`OPT__FORCE` / `PARSE_OPT_NOARG`). Tests used Git `2.54.0 (Apple Git-157)` and GixPython `0.3.0`. Assisted-by: GPT 6.1 Sol Co-authored-by: GPT 6.1 Sol --- doc/source/changes.rst | 1 + git/refs/head.py | 2 ++ test/test_refs.py | 20 ++++++++++++++++++++ 3 files changed, 23 insertions(+) diff --git a/doc/source/changes.rst b/doc/source/changes.rst index b44e1ac14..b105ef73b 100644 --- a/doc/source/changes.rst +++ b/doc/source/changes.rst @@ -17,6 +17,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 Git executable and safety ------------------------- diff --git a/git/refs/head.py b/git/refs/head.py index d73f8b0af..8c6db8eca 100644 --- a/git/refs/head.py +++ b/git/refs/head.py @@ -299,6 +299,8 @@ def checkout( the HEAD detached which is allowed and possible, but remains a special state that some tools might not be able to handle. """ + if not isinstance(force, bool): + raise TypeError("force must be a bool") if not allow_unsafe_options: Git.check_unsafe_options( options=Git._option_candidates([self], kwargs), diff --git a/test/test_refs.py b/test/test_refs.py index 7d8f7f7fe..e323bd0cc 100644 --- a/test/test_refs.py +++ b/test/test_refs.py @@ -43,6 +43,26 @@ from test.lib.fixtures import fixture_repo, read_only_fixture, writable_fixture +@pytest.mark.parametrize("force", [None, 0, 1, "", "false", "--detach", b"--detach", ["--detach"], ("--detach",)]) +@pytest.mark.parametrize("allow_unsafe_options", [False, True]) +@pytest.mark.parametrize("positional", [False, True]) +def test_head_checkout_requires_boolean_force(force, allow_unsafe_options, positional): + with fixture_repo("refs.sh", "rename-prefixed") as repo: + branch = repo.active_branch + for split_single_char_options in (False, True): + with patch.object(Git, "_call_process_safe", side_effect=AssertionError("Git must not run")) as run: + with pytest.raises(TypeError, match="force must be a bool"): + options = { + "allow_unsafe_options": allow_unsafe_options, + "split_single_char_options": split_single_char_options, + } + if positional: + branch.checkout(force, **options) + else: + branch.checkout(force=force, **options) + run.assert_not_called() + + @pytest.mark.parametrize( "branch_name", ["--pathspec-from-file=pathspecs", "-f", "--orphan=checkout-target", "-Bcheckout-target"] ) From 03827908b84ab53bc30689efa0f91a76638eb2cf Mon Sep 17 00:00:00 2001 From: Byron Date: Sun, 15 Nov 2026 13:02:49 +0100 Subject: [PATCH 11/14] review once again, keep only the reference to the gix bug, which will be fixed before the next GitPython release. --- git/_backend.py | 11 +---------- test/test_tree.py | 14 +------------- tests/fixtures/tree-modes.sh | 16 ---------------- 3 files changed, 2 insertions(+), 39 deletions(-) delete mode 100644 tests/fixtures/tree-modes.sh diff --git a/git/_backend.py b/git/_backend.py index 1e79cc4e6..51d25c436 100644 --- a/git/_backend.py +++ b/git/_backend.py @@ -314,16 +314,7 @@ def _rev_parse(repo: Any, args: List[str], kwargs: Dict[str, Any]) -> bytes: def _ls_tree(repo: Any, args: List[str], kwargs: Dict[str, Any]) -> bytes: if len(args) != 3 or args[:2] != ["-z", "--full-tree"]: raise _Unsupported("tree options") - oid = _oid(repo, args[2]) - tree = repo.find_tree(oid) - # Native entries keep modes Git canonicalizes and read a 14xxxx gitlink as a - # tree (GIX-36); Git lists trees whose entries do not round-trip natively. - if repo.with_object_memory().write_object("tree", tree.data) != oid: - raise _Unsupported("tree entries Git canonicalizes (GIX-36)") - with tree.iter() as entries: - if any(entry.mode() not in _ENTRY_KINDS for entry in entries): - raise _Unsupported("tree entries Git canonicalizes (GIX-36)") - with tree.iter() as entries: + with repo.find_tree(_oid(repo, args[2])).iter() as entries: return b"".join( b"%06o %s %s\t%s\0" % ( diff --git a/test/test_tree.py b/test/test_tree.py index 192702a94..8d29319fb 100644 --- a/test/test_tree.py +++ b/test/test_tree.py @@ -56,19 +56,7 @@ def test_invalid_tree_does_not_retain_partial_cache(object_format, invalid_posit list(tree) -@pytest.mark.parametrize("object_format", ["sha1", "sha256"]) -@pytest.mark.parametrize("scenario", ["executable", "symlink", "gitlink"]) -def test_tree_entry_modes_match_git(object_format, scenario): - with fixture_repo("tree-modes.sh", object_format, scenario) as repo: - root = read_only_fixture("tree-modes.sh", object_format, scenario) - oid = (root / "tree-id").read_text().strip() - metadata, name = (root / "entries").read_text().rstrip("\n").split("\t") - mode, kind, entry_oid = metadata.split() - assert mode in ("100755", "120000", "160000") - tree = Tree(repo, bytes.fromhex(oid), path="") - assert [(entry.mode, entry.type, entry.hexsha, entry.path) for entry in tree] == [ - (int(mode, 8), "submodule" if kind == "commit" else kind, entry_oid, name) - ] +def test_tree_modifier_orders_non_utf8_names_as_git_does(): with fixture_repo("tree-byte-order.sh") as repo: expected = (read_only_fixture("tree-byte-order.sh") / "order").read_bytes().split(b"\0")[:-1] tree = repo.head.commit.tree diff --git a/tests/fixtures/tree-modes.sh b/tests/fixtures/tree-modes.sh deleted file mode 100644 index fe1938a85..000000000 --- a/tests/fixtures/tree-modes.sh +++ /dev/null @@ -1,16 +0,0 @@ -#!/usr/bin/env bash -set -eu -git init --initial-branch=main --object-format="$1" repo -cd repo -blob=$(printf 'content\n' | git hash-object -w --stdin) -subtree=$(printf '100644 blob %s\tinner\n' "$blob" | git mktree) -case "$2" in -executable) entry="100700 exe" oid=$blob ;; -symlink) entry="120777 link" oid=$blob ;; -gitlink) entry="140000 sub" oid=$subtree ;; -esac -# git mktree only writes canonical modes, so write the legacy mode literally. -escaped=$(printf %s "$oid" | sed 's/../\\x&/g') -{ printf '%s\0' "$entry"; printf %b "$escaped"; } | - git hash-object -w -t tree --literally --stdin >../tree-id -git ls-tree "$(cat ../tree-id)" >../entries From 2f5d70b8f9b656548def788493c1eb9ebadaeb6b Mon Sep 17 00:00:00 2001 From: Codex Date: Sun, 15 Nov 2026 13:11:53 +0100 Subject: [PATCH 12/14] ci: cancel superseded workflow runs Repeated updates currently leave older GitHub Actions runs executing even when their results are superseded. Add top-level `concurrency` to all ten workflows in `.github/workflows`, which perform validation and analysis rather than release publishing or deployment. Group runs by `${{ github.workflow }}-${{ github.ref }}` and enable `cancel-in-progress` so a newer run cancels older runs for the same workflow and ref while other workflows, branches, and pull requests remain independent. This also applies to scheduled and manually dispatched runs on the same ref. Validated all ten files with the existing `ruamel.yaml` installation, asserted the exact concurrency settings and unchanged remaining configuration, and ran `git diff --check`. GitHub Actions execution was not run locally. Agent-Model: unknown --- .github/workflows/alpine-test.yml | 4 ++++ .github/workflows/backend-benchmark.yml | 4 ++++ .github/workflows/codeql.yml | 4 ++++ .github/workflows/cygwin-test.yml | 4 ++++ .github/workflows/dependencies.yml | 4 ++++ .github/workflows/downstream.yml | 4 ++++ .github/workflows/git-cli.yml | 4 ++++ .github/workflows/lint.yml | 4 ++++ .github/workflows/pythonpackage.yml | 4 ++++ .github/workflows/zizmor.yml | 4 ++++ 10 files changed, 40 insertions(+) diff --git a/.github/workflows/alpine-test.yml b/.github/workflows/alpine-test.yml index 189826c0f..920f93066 100644 --- a/.github/workflows/alpine-test.yml +++ b/.github/workflows/alpine-test.yml @@ -1,5 +1,9 @@ name: test-alpine +concurrency: + group: ${{ github.workflow }}-${{ github.ref }} + cancel-in-progress: true + on: push: branches: [main] diff --git a/.github/workflows/backend-benchmark.yml b/.github/workflows/backend-benchmark.yml index 140aa27cb..85503235a 100644 --- a/.github/workflows/backend-benchmark.yml +++ b/.github/workflows/backend-benchmark.yml @@ -1,5 +1,9 @@ name: Backend benchmark +concurrency: + group: ${{ github.workflow }}-${{ github.ref }} + cancel-in-progress: true + on: push: branches: [main] diff --git a/.github/workflows/codeql.yml b/.github/workflows/codeql.yml index 143c2de52..25c207db1 100644 --- a/.github/workflows/codeql.yml +++ b/.github/workflows/codeql.yml @@ -11,6 +11,10 @@ # name: "CodeQL" +concurrency: + group: ${{ github.workflow }}-${{ github.ref }} + cancel-in-progress: true + on: push: branches: [main] diff --git a/.github/workflows/cygwin-test.yml b/.github/workflows/cygwin-test.yml index c55b73dd8..d5955958d 100644 --- a/.github/workflows/cygwin-test.yml +++ b/.github/workflows/cygwin-test.yml @@ -1,5 +1,9 @@ name: import-cygwin +concurrency: + group: ${{ github.workflow }}-${{ github.ref }} + cancel-in-progress: true + on: push: branches: [main] diff --git a/.github/workflows/dependencies.yml b/.github/workflows/dependencies.yml index b58704fe9..e31a633b1 100644 --- a/.github/workflows/dependencies.yml +++ b/.github/workflows/dependencies.yml @@ -1,5 +1,9 @@ name: Dependency packages +concurrency: + group: ${{ github.workflow }}-${{ github.ref }} + cancel-in-progress: true + on: push: branches: [main] diff --git a/.github/workflows/downstream.yml b/.github/workflows/downstream.yml index 85cf0062f..673122afe 100644 --- a/.github/workflows/downstream.yml +++ b/.github/workflows/downstream.yml @@ -1,5 +1,9 @@ name: Downstream compatibility +concurrency: + group: ${{ github.workflow }}-${{ github.ref }} + cancel-in-progress: true + on: push: branches: [main] diff --git a/.github/workflows/git-cli.yml b/.github/workflows/git-cli.yml index b104fd4f4..bf12d1fa9 100644 --- a/.github/workflows/git-cli.yml +++ b/.github/workflows/git-cli.yml @@ -1,5 +1,9 @@ name: Minimum Git CLI +concurrency: + group: ${{ github.workflow }}-${{ github.ref }} + cancel-in-progress: true + on: push: branches: [main] diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index 496f4b093..ab2171ba3 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -1,5 +1,9 @@ name: Lint +concurrency: + group: ${{ github.workflow }}-${{ github.ref }} + cancel-in-progress: true + on: push: branches: [main] diff --git a/.github/workflows/pythonpackage.yml b/.github/workflows/pythonpackage.yml index e8ef05cd4..842695409 100644 --- a/.github/workflows/pythonpackage.yml +++ b/.github/workflows/pythonpackage.yml @@ -3,6 +3,10 @@ name: Python package +concurrency: + group: ${{ github.workflow }}-${{ github.ref }} + cancel-in-progress: true + on: push: branches: [main] diff --git a/.github/workflows/zizmor.yml b/.github/workflows/zizmor.yml index 201d28ab0..b38d834cb 100644 --- a/.github/workflows/zizmor.yml +++ b/.github/workflows/zizmor.yml @@ -1,5 +1,9 @@ name: zizmor +concurrency: + group: ${{ github.workflow }}-${{ github.ref }} + cancel-in-progress: true + on: push: branches: [main] From a1a9a015180832b68cb0c6da06da566a1125ddaa Mon Sep 17 00:00:00 2001 From: Byron Date: Sun, 15 Nov 2026 13:04:08 +0100 Subject: [PATCH 13/14] fix(submodule): validate clone flag and depth arguments An audit of higher-level Git calls prompted by `GHSA-j3hg-fv33-37rw` found that `Submodule.add()` passed its documented boolean `no_checkout` parameter directly to clone's short `-n` option. Non-boolean strings or sequences could introduce additional Git arguments. The manual clone path also accepted boolean `depth` values as integers, allowing `True` to become a bare `--depth` instead of a numeric option value. Require boolean `no_checkout` before repository or filesystem operations. Move the existing positive, non-boolean integer depth validation before path selection and reuse it for both direct submodule add and manual cloning. `Repo.create_submodule()` receives the same protection through its existing delegation. Keep valid booleans and positive depths intact; invalid values are rejected instead of coerced into behavior flags. The regression tests failed before the fix and now verify rejection before any Git process for both entry points, positional and keyword flags, unsafe opt-in, strings, bytes, sequences, and invalid numeric depths. The source audit identified no additional named boolean parameter forwarded raw into a short no-argument flag in refs, repo, remote, diff, index, or the remaining object/submodule operations. Arbitrary Git keyword options remain the explicit command-option interface. Git baseline: local source at `6de20f6092dcf9bdb1c8efe03db4b70c82b423dd` registers clone's `--no-checkout` as `OPT_BOOL` / `PARSE_OPT_NOARG`, while `--depth` requires a numeric value of at least one (`builtin/clone.c`, `transport.c`, and `Documentation/git-clone.adoc`). Assisted-by: GPT 6.1 Sol Co-authored-by: GPT 6.1 Sol --- git/objects/submodule/base.py | 19 +++++++++---------- test/test_submodule.py | 28 ++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 10 deletions(-) diff --git a/git/objects/submodule/base.py b/git/objects/submodule/base.py index 0c24dbc87..840f32d50 100644 --- a/git/objects/submodule/base.py +++ b/git/objects/submodule/base.py @@ -630,15 +630,15 @@ def add( :param no_checkout: If ``True``, and if the repository has to be cloned manually, no checkout - will be performed. + will be performed. Must be a boolean. Retained metadata can instead be reconnected without checking out files or changing refs. Its URL and any explicit branch must match; ``depth`` and ``clone_multi_options`` require a new clone and are rejected here. :param depth: - Create a shallow clone with a history truncated to the specified number of - commits. + Create a shallow clone with a history truncated to the specified positive + number of commits. :param env: Optional dictionary containing the desired environment variables. @@ -666,6 +666,10 @@ def add( Works atomically, such that no change will be done if, for example, the repository update fails. """ + if not isinstance(no_checkout, bool): + raise TypeError("no_checkout must be a bool") + if depth is not None and (not isinstance(depth, int) or isinstance(depth, bool) or depth <= 0): + raise ValueError("depth must be a positive integer") if repo.bare: raise InvalidGitRepositoryError("Cannot add submodules to bare repositories") # END handle bare repos @@ -725,8 +729,6 @@ def add( if branch is not None: args.extend(("--branch", Git._check_operand(branch, "branch"))) if depth is not None: - if not isinstance(depth, int) or isinstance(depth, bool) or depth <= 0: - raise ValueError("depth must be a positive integer") args.extend(("--depth", str(depth))) args.extend(("--", url, str(path))) # An explicitly requested local submodule is also supported by clone(). @@ -819,11 +821,8 @@ def add( kwargs["b"] = br.name # END setup checkout-branch - if depth: - if isinstance(depth, int): - kwargs["depth"] = depth - else: - raise ValueError("depth should be an integer") + if depth is not None: + kwargs["depth"] = depth if clone_multi_options: kwargs["multi_options"] = clone_multi_options diff --git a/test/test_submodule.py b/test/test_submodule.py index 751235e21..f293e8742 100644 --- a/test/test_submodule.py +++ b/test/test_submodule.py @@ -33,6 +33,34 @@ from test.lib.fixtures import fixture_repo, read_only_fixture, writable_fixture +@pytest.mark.parametrize("no_checkout", [None, 0, 1, "", "false", "--bare", b"--bare", ["--bare"], ("--bare",)]) +@pytest.mark.parametrize("allow_unsafe_options", [False, True]) +@pytest.mark.parametrize("positional", [False, True]) +def test_submodule_add_requires_boolean_no_checkout(no_checkout, allow_unsafe_options, positional): + with fixture_repo("empty.sh", "sha1") as repo: + for add, args in ( + (Submodule.add, (repo, "module", "module", "source", None)), + (repo.create_submodule, ("module", "module", "source", None)), + ): + with mock.patch.object(Git, "execute", side_effect=AssertionError("Git must not run")) as run: + with pytest.raises(TypeError, match="no_checkout must be a bool"): + if positional: + add(*args, no_checkout, allow_unsafe_options=allow_unsafe_options) + else: + add(*args, no_checkout=no_checkout, allow_unsafe_options=allow_unsafe_options) + run.assert_not_called() + + +@pytest.mark.parametrize("depth", [True, False, 0, -1, 1.5, "1", [1]]) +@pytest.mark.parametrize("no_checkout", [False, True]) +def test_submodule_add_requires_positive_integer_depth(depth, no_checkout): + with fixture_repo("empty.sh", "sha1") as repo: + with mock.patch.object(Git, "execute", side_effect=AssertionError("Git must not run")) as run: + with pytest.raises(ValueError, match="depth must be a positive integer"): + repo.create_submodule("module", "module", "source", no_checkout=no_checkout, depth=depth) + run.assert_not_called() + + @pytest.mark.parametrize("scenario, name", [("ascii-name", "module"), ("byte-name", "module-\udcff")]) def test_submodule_configuration_preserves_non_utf8_bytes(scenario, name): with fixture_repo("submodule-bytes.sh", scenario) as repo: From b056f397396061af764e89f169fd92df466ba606 Mon Sep 17 00:00:00 2001 From: Byron Date: Mon, 16 Nov 2026 04:46:43 +0100 Subject: [PATCH 14/14] 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 ;;