From 92a07cfa89cc1873807a6bbc4b399beea44e8068 Mon Sep 17 00:00:00 2001 From: Keerthana KT Date: Fri, 13 Nov 2026 21:21:32 +0530 Subject: [PATCH 1/8] 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 2/8] 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 3/8] 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 4/8] 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 5/8] 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 6/8] 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 7/8] 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 8/8] 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 ;;