| « Back to Search | Reader Mode | Switch to Full Retro Site View | Links: Stay in Reader | Retro Mode: ON | Original » |
git/repo/base.py:310
git/repo/fun.py:97
git/repo/fun.py:103
💡 Add a AI review requested due to automatic review settings August 25, 2026 04:03 Copilot started reviewing on behalf of Byron August 25, 2026 04:04 View session Byron commented Aug 25, 2026Copy link Copy Markdown Member Author
AI reviewed Aug 25, 2026 View reviewed changes
Copilot
|
|
Codex on behalf of Byron: I reviewed all six suppressed notes from the latest Copilot pass. Commit b8c000e fixes one-level, regular, size-bounded Gitfile resolution, filesystem decoding for .git and commondir metadata, and relative GIT_DIR persistence. Commit 83c3e64 fixes dangling commondir symlinks. I left dangling .git symlinks unchanged because Git read_gitfile_gently() uses stat and treats them as missing; the visible FIFO commondir thread likewise documents Git's file_exists()/strbuf_read_file() behavior. |
The reason will be displayed to describe this comment to others. Learn more .
Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.
Suppressed comments (1)Previously missed (1) -- in code that hasn't changed since the last review.
git/repo/base.py:310
osp.exists() returns False for a dangling symlink. Consequently, a worktree containing a broken .git symlink can still fall through to is_git_dir(curpath) and be accepted as a bare repository if it has HEAD, objects, and refs, defeating the malformed-.git guard above. Use osp.lexists(dotgit) so every non-directory .git entry blocks fallback. if osp.exists(dotgit) and not osp.isdir(dotgit):
Copilot PR reviewer
Copilot
The reason will be displayed to describe this comment to others. Learn more .
Copilot reviewed 4 out of 4 changed files in this pull request and generated no new comments.
Suppressed comments (3)git/repo/base.py:310
.git is a dangling symlink, find_submodule_git_dir() returns None and osp.exists(dotgit) is false, so this guard does not stop discovery. With search_parent_directories=True, the loop can then open an unrelated parent repository, contrary to the malformed .git rejection described by this change. Use osp.lexists(dotgit) so a present but unresolved non-directory .git entry blocks fallback. if osp.exists(dotgit) and not osp.isdir(dotgit):
git/repo/fun.py:130
st_size check is not sufficient to enforce the 1 MiB Gitfile limit: read_bytes() then reads the entire file without a bound. If a regular file grows or is replaced between stat and the read, repository discovery can allocate unbounded input and defeat the size safeguard. Open the file and read at most (1 << 20) + 1 bytes, rejecting an over-limit read. try:
content = os.fsdecode(Path(dotgit).read_bytes()).rstrip("\r\n")
except OSError:
return None
return content[8:] if len(content) >= 9 and content.startswith("gitdir: ") else None
git/repo/fun.py:110
GIT_OBJECT_DIRECTORY as the repository's object store, but Repo does not carry that value into construction: its ODB is still rooted at common_dir/objects and the Git wrapper only pins GIT_DIR/GIT_COMMON_DIR. A repository whose objects exist only in GIT_OBJECT_DIRECTORY can therefore use a different or nonexistent object database (and a relative value will break when the process cwd changes). Resolve and preserve GIT_OBJECT_DIRECTORY as well, or reject this environment mode consistently. object_dir = os.getenv("GIT_OBJECT_DIRECTORY")
if object_dir is None:
object_dir = osp.join(common_dir, "objects")
if valid_head and osp.isdir(object_dir) and osp.isdir(osp.join(common_dir, "refs")):
Copilot PR reviewer
Copilot
The reason will be displayed to describe this comment to others. Learn more .
Copilot reviewed 4 out of 4 changed files in this pull request and generated no new comments.
Suppressed comments (5)Previously missed (2) -- in code that hasn't changed since the last review.
git/repo/base.py:388
GIT_OBJECT_DIRECTORY is validated during discovery, but it is not pinned in the Git wrapper's environment. When this value is relative and Repo(path) gives the wrapper a different cwd, discovery checks the path relative to the caller's cwd while later Git commands resolve it relative to the repository's working directory, so object lookups can target a different store. Resolve and propagate this variable along with GIT_DIR and GIT_COMMON_DIR. if common_dir_env is not None:
self.git.update_environment(GIT_DIR=os.fspath(self.git_dir), GIT_COMMON_DIR=os.fspath(self.common_dir))
elif git_dir_env is not None:
self.git.update_environment(GIT_DIR=os.fspath(self.git_dir))
git/repo/base.py:310
osp.exists follows symlinks, so a dangling .git symlink makes this guard false. If the current directory also has bare-repository markers (or parent search is enabled), discovery can then fall through and accept that directory/parent even though an invalid .git entry is present, defeating the precedence check above. Use lexists so dangling metadata is treated as an existing non-directory entry, as is already done for commondir in fun.py. if osp.exists(dotgit) and not osp.isdir(dotgit):
git/repo/fun.py:110
GIT_OBJECT_DIRECTORY as the repository's object store, but the constructor still creates the object database from self.common_dir/objects (base.py:391). A valid repository whose objects exist only in this environment-provided directory will therefore be opened with an unusable GitDB (and an inaccurate GitCmdObjectDB.root_path()); either use the resolved object-directory environment value when constructing the ODB or reject this configuration consistently. object_dir = os.getenv("GIT_OBJECT_DIRECTORY")
if object_dir is None:
object_dir = osp.join(common_dir, "objects")
if valid_head and osp.isdir(object_dir) and osp.isdir(osp.join(common_dir, "refs")):
git/repo/fun.py:129
os.stat; Path.read_bytes() reopens the path and reads to EOF. A concurrent replacement or growth can therefore bypass the 1 MiB limit and make repository discovery read an arbitrarily large Gitfile. Read from one descriptor with a bound of statbuf.st_size (and reject a short read), as Git does. try:
content = os.fsdecode(Path(dotgit).read_bytes()).rstrip("\r\n")
except (OSError, UnicodeError):
return None
test/test_repo.py:139
Repo.init(path) leaves a valid path/.git directory in place, so even the bare layout is resolved by find_submodule_git_dir(path/.git) before the new implicit-bare branch at base.py:313 runs. Add a bare-only directory without .git and assert Repo(path) discovers it, otherwise this new discovery path has no positive regression coverage. Repo.init(path).close()
Copilot PR reviewer
Copilot
The reason will be displayed to describe this comment to others. Learn more .
Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.
Suppressed comments (8)git/repo/base.py:391
cwd while receiving a relative GIT_DIR. For example, GIT_DIR=git with a commondir file makes working_dir the common directory, so Git resolves GIT_DIR=git below that directory instead of the original repository; the same applies when only GIT_COMMON_DIR is set and git_dir came from a relative path. Normalize GIT_DIR before storing it in both branches. if common_dir_env is not None:
self.git.update_environment(GIT_DIR=os.fspath(self.git_dir), GIT_COMMON_DIR=os.fspath(self.common_dir))
elif git_dir_env is not None:
self.git.update_environment(GIT_DIR=os.fspath(self.git_dir))
git/repo/base.py:314
.git exists as a malformed directory, find_submodule_git_dir(dotgit) returns None, but this condition does not stop discovery because the entry is a directory. A worktree containing repository-looking HEAD, objects, and refs can then be accepted as a bare repository, defeating the new malformed-.git precedence rule; osp.exists also misses dangling .git symlinks. Treat any present but unresolved .git entry as terminal (using lexists) before checking curpath as a bare repository. if osp.exists(dotgit) and not osp.isdir(dotgit):
break
git/repo/base.py:307
find_submodule_git_dir() has already resolved a relative gitdir: target against dirname(d) (lines 147-149). When Repo is opened with a relative path, sm_gitpath is therefore relative to the process cwd, but joining curpath again prefixes the worktree a second time and selects a nonexistent/wrong admin directory. Use the helper's resolved path directly here so relative Gitfiles work with relative Repo paths. git_dir = osp.normpath(osp.join(curpath, os.fspath(sm_gitpath)))
git/repo/base.py:369
common_dir here even though Git's get_common_dir_noenv() removes them. Once discovery accepts such a file, this leaves self.common_dir pointing at the wrong path and causes config/object access to diverge from Git; use rstrip() here as well. try:
common_dir = os.fsdecode((Path(self.git_dir) / "commondir").read_bytes()).rstrip("\r\n")
self._common_dir = osp.join(self.git_dir, common_dir)
git/repo/fun.py:130
read_gitfile_gently() trims trailing whitespace from the gitdir record, not just CR/LF. With this normalization, a valid .git file such as gitdir: /path/to/repo \n is rejected because the target retains the trailing space, so discovery diverges from Git. Strip all trailing whitespace here before extracting the target. content = os.fsdecode(Path(dotgit).read_bytes()).rstrip("\r\n")
except (OSError, UnicodeError):
return None
return content[8:] if len(content) >= 9 and content.startswith("gitdir: ") else None
git/repo/fun.py:93
get_common_dir_noenv() applies Git's trailing-whitespace trimming to the commondir file. Leaving spaces or tabs in common_dir makes this check look in the wrong directory and rejects repositories that Git opens successfully; use the same trimming rule as the later Repo construction path. common_dir_file = Path(d) / "commondir"
try:
common_dir = os.fsdecode(common_dir_file.read_bytes()).rstrip("\r\n")
except FileNotFoundError:
test/test_repo.py:252
cwd(tdir) and the patched environment are still active, so relative GIT_DIR/GIT_OBJECT_DIRECTORY values would still resolve correctly even if the wrapper failed to retain absolute paths. Move the repository and GitDB assertions outside both context managers to actually cover the preservation behavior this regression test is intended to protect. assert repo.git.cat_file("blob", blob_hexsha) == payload.decode()
test/test_repo.py:223
cwd(tdir)/environment context, so it does not verify that the relative GIT_DIR remains usable after construction changes the execution context. Place the assertion after the context managers (as in the preceding environment case) to cover the wrapper path normalization. assert osp.samefile(repo.git.rev_parse("--absolute-git-dir"), git_dir)
Copilot PR reviewer
Copilot
The reason will be displayed to describe this comment to others. Learn more .
Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.
Suppressed comments (3)git/repo/base.py:396
GIT_OBJECT_DIRECTORY is accepted as the active object store and used for rootpath, Repo.alternates still reads and writes common_dir/objects/info/alternates. With objects relocated to the environment-provided directory, that API silently ignores the active alternates file or modifies the wrong repository; derive this path from the resolved object root as well. rootpath = object_dir_env if object_dir_env is not None else osp.join(self.common_dir, "objects")
git/repo/base.py:313
osp.exists here misses a dangling .git symlink: find_submodule_git_dir returns None, then this guard is skipped and the loop can still accept curpath as a bare repository if it has HEAD/objects/refs. That bypasses the new malformed-.git no-fallback rule; use osp.lexists (or an equivalent lstat check) for this guard. if osp.exists(dotgit) and not osp.isdir(dotgit):
git/repo/base.py:391
git_dir_env and common_dir_env are both unset, this code does not pin the resolved self.git_dir into the command wrapper. A valid repository opened from a separate git directory via core.worktree can therefore have self.working_dir set to its worktree while that worktree has no .git entry; subsequent repo.git commands run without GIT_DIR and fail discovery. Preserve the resolved GIT_DIR for path-based discovery too. if common_dir_env is not None:
self.git.update_environment(GIT_DIR=os.fspath(self.git_dir), GIT_COMMON_DIR=os.fspath(self.common_dir))
elif git_dir_env is not None:
self.git.update_environment(GIT_DIR=os.fspath(self.git_dir))
Copilot PR reviewer
Copilot
The reason will be displayed to describe this comment to others. Learn more .
Copilot reviewed 4 out of 4 changed files in this pull request and generated no new comments.
Suppressed comments (3)git/repo/base.py:313
.git symlink is a non-directory entry, but osp.exists() follows symlinks and returns False for it. Since find_submodule_git_dir() has already rejected the dangling target, discovery then reaches is_git_dir(curpath) and can still misclassify a worktree with objects/refs/HEAD as a bare repository. Use a lexists-style check for this guard so any present, invalid .git entry stops fallback. self._working_tree_dir = curpath
git/repo/base.py:391
commondir file and neither GIT_COMMON_DIR nor GIT_DIR was set, neither branch initializes the wrapper environment. For a split bare repository, working_dir becomes self.common_dir, which may have no HEAD or .git, so later repo.git commands fail repository discovery even though construction succeeded. Pin the resolved GIT_DIR/GIT_COMMON_DIR whenever the resolved common directory differs from the git directory, not only when those variables were inherited. self._working_tree_dir = None
# END working dir handling
self.working_dir: PathLike = self._working_tree_dir or self.common_dir
git/repo/fun.py:127
read_gitfile_gently() trims trailing whitespace from the single gitdir: record, so a valid .git file such as gitdir: /path/to/git \n is accepted by Git. This parser only strips CR/LF and returns a target with the trailing space, causing is_git_dir() to reject an otherwise valid worktree; trim the same whitespace set before extracting the target. with open(dotgit, "rb") as fp:
Copilot PR reviewer
Copilot
The reason will be displayed to describe this comment to others. Learn more .
Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.
Copilot PR reviewer CopilotThe reason will be displayed to describe this comment to others. Learn more .
Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.
Copilot PR reviewer CopilotThe reason will be displayed to describe this comment to others. Learn more .
Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.
fix: prefer .git during repository discovery
c7cf4d1
<!-- agent --> GitPython considered worktree administration and bare-repository signatures before a worktree's real .git entry. Align discovery with Git so .git files and directories win, malformed .git files stop discovery, and candidate git directories validate HEAD plus commondir-backed object and ref storage. This addresses GHSA-239g-whfq-7xj9. Regression coverage compares ambiguous layouts with git rev-parse and rejects invalid HEAD/.git metadata. Git baseline: 15c6308cf7ad276b306aa5b3ababfbdebfb1a917; setup.c setup_git_directory_gently_1(), is_git_directory(), and validate_headref(). Repository validation can use GIT_COMMON_DIR for refs and objects. Preserve the same value on Repo so later config, ref, and object access uses the directory that made discovery succeed. Capture GIT_COMMON_DIR before the first repository config read so bare-state detection uses the same metadata location as discovery. Resolve relative environment values immediately so later working-directory changes cannot retarget the Repo. Review feedback: relative GIT_COMMON_DIR left Git subprocesses resolving GIT_DIR and GIT_COMMON_DIR from a different working directory; malformed commondir data, empty GIT_OBJECT_DIRECTORY, and the linked-worktree signature were also handled inconsistently. Pin the repository environment to resolved paths, reject invalid metadata without consulting the process working directory, and restore HEAD-based linked-worktree detection. Keep the loose HEAD and dangling .git behavior because both match Git setup.c at baseline 15c6308cf7ad276b306aa5b3ababfbdebfb1a917. Review feedback identified that chained or self-referential .git pointers recurse, filesystem-encoded metadata can fail text decoding, and a relative GIT_DIR is not retained for later Git commands. Parse one regular, size-bounded Gitfile exactly once, decode Gitfile and commondir paths with the filesystem codec, and retain the resolved GIT_DIR for subprocesses. This rejects cycles like Git instead of recursing and keeps commands stable after working-directory changes. Review feedback noted that a dangling commondir symlink was treated as absent, allowing local objects and refs to validate the repository. Distinguish a truly missing commondir from a dangling symlink. This follows Git's get_common_dir_noenv(), whose file_exists check uses lstat before attempting to read the entry. Python 3.9 on Windows raised UnicodeDecodeError while the repository-discovery regression test parsed invalid commondir bytes, causing the Python package test (windows, 3.9) check to fail. Treat UnicodeError like an unreadable metadata file in both commondir and gitfile parsing. Invalid bytes now make discovery reject the candidate repository, matching Git's behavior. Review feedback noted that GIT_OBJECT_DIRECTORY made discovery succeed without becoming the Repo ODB root, while a relative value could later be resolved from the Git wrapper's different working directory. Resolve the environment value against the construction directory, use it as the ODB root, and preserve the absolute value for later Git commands. The regression moves the only object store outside the git directory and verifies access through both GitDB and git cat-file after the original environment and current directory are restored. Review feedback identified four setup mismatches: non-missing .git stat failures could fall through to another repository, explicit GIT_DIR could be redirected through a nested .git entry, Gitfile reads were not bounded to the stat-reported size, and alternates ignored GIT_OBJECT_DIRECTORY. Match Git setup.c by bypassing discovery for an environment-selected GIT_DIR, stopping discovery when Gitfile stat fails for reasons other than a missing path, and reading exactly the previously observed Gitfile size. Resolve alternates below the active ODB root so custom object stores remain internally consistent. The commit review noted that treating every present GIT_DIR as explicit broke two documented cases: an empty GIT_DIR must fall back to current-directory discovery, while an empty Repo path must still use a nonempty GIT_DIR. Assisted-by: GPT 5.6 Co-authored-by: GPT 5.6 <codex@openai.com>Copilot PR reviewer Copilot
553a70a to
c7cf4d1
The reason will be displayed to describe this comment to others. Learn more .
Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.
prepare new release
56636c3
Copilot PR reviewer
Copilot
The reason will be displayed to describe this comment to others. Learn more .
Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.
Byron merged commit3481da9
into
main
Aug 25, 2026
53 checks passed
Byron
deleted the
fix-repo-open
branch
October 5, 2026 03:24
This was referenced Oct 26, 2026
🟠 [High] GitPython: Repository Content Can Impersonate the Git Directory, Leading to Arbitrary Code Execution
Latch/connectedhomeip#63
Closed
🟠 [High] GitPython: Repository Content Can Impersonate the Git Directory, Leading to Arbitrary Code Execution
Latch/connectedhomeip#71
Closed
🟠 [High] GitPython: Repository Content Can Impersonate the Git Directory, Leading to Arbitrary Code Execution
Latch/connectedhomeip#76
Closed
Sign up for free
to join this conversation on GitHub .
Already have an account?
Sign in to comment