Skip to content

Commit 759cbbc

Browse files
cliffhallclaude
andcommitted
fix(git): address review findings for GHSA-6hqx-gqrx-c5r2
Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: cliffhall <[email protected]>
1 parent a4a43ac commit 759cbbc

4 files changed

Lines changed: 88 additions & 7 deletions

File tree

‎src/git/README.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ python -m mcp_server_git
176176
A tool call's `repo_path` must be inside that repository, and so must the directories Git then uses for it: a repository nested inside whose `.git` file, `commondir` or `core.worktree` points outside is refused. A linked worktree can still be the `--repository`, though its Git directory lives under the main repository's `.git`; only the worktree itself may use that directory, so a repository nested in the worktree whose `.git` names it, or the main repository's `.git`, is refused. With `--repository` set, the server also refuses a repository whose Git directory:
177177

178178
- names alternate object stores (`objects/info/alternates` or `objects/info/http-alternates` with any entry), wherever they are. A clone made with `--reference` or `--shared` therefore cannot be used under `--repository`; `git repack -a -d` and then deleting `objects/info/alternates` makes it self-contained.
179-
- holds a symbolic link (at `objects`, `refs`, `index`, `HEAD` or anywhere else in it) that resolves outside both the allowed repository and that Git directory. A `hooks` directory linked elsewhere is allowed, since no hook runs.
179+
- holds a symbolic link (at `objects`, `refs`, `index`, `HEAD` or anywhere else in it) that resolves outside both the allowed repository and that Git directory. A link that stays inside is followed, and what it leads to is checked the same way. A `hooks` directory linked elsewhere is allowed, since no hook runs.
180180

181181
### A repository's own programs
182182

@@ -185,7 +185,7 @@ A repository's configuration and hooks travel with it (an unpacked archive, a sh
185185
- `core.fsmonitor` is `false`, and `core.hooksPath` points at the null device, so no hook runs; `git_commit` also skips the hooks GitPython would run itself (`pre-commit`, `commit-msg`, `post-commit`).
186186
- Diffs pass `--no-ext-diff` and `--no-textconv`, so `diff.external`, `diff.<driver>.command` and `diff.<driver>.textconv` are ignored.
187187
- Each filter driver (`filter.<name>.clean`, `smudge`, `process`) and merge driver (`merge.<name>.driver`) that the repository's own configuration defines is emptied: files are staged and checked out unfiltered, and a file a merge driver would have merged is a conflict, which `git_merge` aborts. Drivers from your global or system configuration, such as Git LFS's, keep working.
188-
- `commit.gpgSign` and `merge.verifySignatures` are `false`, `submodule.recurse` and `merge.autoStash` are `false`, and `core.pager` is `cat`.
188+
- `commit.gpgSign` and `merge.verifySignatures` are `false`, and `git_merge` passes `--no-gpg-sign` and `--no-verify-signatures`. Each `branch.<name>.mergeOptions` the repository's own configuration sets is emptied, since Git reads it as command-line options. `submodule.recurse` and `merge.autoStash` are `false`, and `core.pager` is `cat`.
189189
- `git_status`, `git_diff_unstaged` and `git_diff` pass `--ignore-submodules=dirty`, because looking for changes inside a submodule runs Git under the submodule's own configuration. A submodule whose checked-out commit moved is still reported; changes inside one are not, so run the tool on the submodule's path to see them.
190190

191191
This needs Git 2.31 or later; with an older Git every tool call fails with an error saying so, rather than run unprotected.

‎src/git/src/mcp_server_git/server.py‎

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -147,12 +147,16 @@ def _git_text(repo: git.Repo, command: str, *args: str) -> str:
147147
# (`merge.<name>.driver`) is a command too, under a name the repository
148148
# chooses, so each one the repository's own configuration defines is
149149
# overridden with an empty command, which git skips (a filter) or reports as
150-
# a conflict (a merge driver). Drivers from the user's global or system
151-
# configuration (git-lfs, say) are the user's own and keep working.
150+
# a conflict (a merge driver). `branch.<name>.mergeOptions` is emptied the
151+
# same way: git merge reads it as command-line options, which outrank the
152+
# overrides above (`--gpg-sign` there would run `gpg.program` whatever
153+
# `commit.gpgSign` says). Drivers and options from the user's global or
154+
# system configuration (git-lfs, say) are the user's own and keep working.
152155
# `harden_repo` lists these keys, and `core.hooksPath`, whose command-scope
153156
# value shows that git applied the overrides.
154157
_LISTED_KEYS = (
155-
r"^(core\.hookspath|filter\..+\.(clean|smudge|process)|merge\..+\.driver)$"
158+
r"^(core\.hookspath|filter\..+\.(clean|smudge|process)|merge\..+\.driver"
159+
r"|branch\..+\.mergeoptions)$"
156160
)
157161

158162
# Scopes whose configuration the user wrote rather than the repository.
@@ -603,7 +607,9 @@ def git_merge(
603607
# config says (merge.ff, branch.<name>.mergeOptions): --ff/--ff-only
604608
# choose fast-forwarding, --commit and --no-squash make a merge that
605609
# succeeds a completed one that moves HEAD, and --no-edit keeps git from
606-
# opening an editor for the message. --no-overwrite-ignore refuses to
610+
# opening an editor for the message. --no-gpg-sign and
611+
# --no-verify-signatures keep `gpg.program` from running whatever asks
612+
# for it (see `_LISTED_KEYS`). --no-overwrite-ignore refuses to
607613
# replace an ignored local file (a `.env`, say) that the source tracks:
608614
# `is_dirty()` cannot see one, and git would overwrite it unrecoverably.
609615
#
@@ -619,6 +625,8 @@ def git_merge(
619625
"--commit",
620626
"--no-squash",
621627
"--no-edit",
628+
"--no-gpg-sign",
629+
"--no-verify-signatures",
622630
"--no-overwrite-ignore",
623631
"-m",
624632
f"Merge '{source_branch}' into '{target}'",
@@ -802,6 +810,10 @@ def _check_git_dir_contents(
802810
anything else in a git directory is followed by git, for reading and
803811
for writing (a linked `index` takes git_add's writes elsewhere).
804812
813+
A link that is allowed and names a directory is walked too, since git
814+
follows it into that directory and on into any link inside it; each
815+
directory is walked once, so a cycle of links ends.
816+
805817
`hooks` is not walked: no hook runs (`HARDENED_CONFIG`), so a hooks
806818
directory linked to a shared one elsewhere is harmless.
807819
"""
@@ -817,6 +829,7 @@ def _check_git_dir_contents(
817829
"alternate object stores are not allowed with --repository"
818830
)
819831
pending = [directory]
832+
visited = {directory}
820833
while pending:
821834
current = pending.pop()
822835
with os.scandir(current) as entries:
@@ -830,8 +843,14 @@ def _check_git_dir_contents(
830843
or target.is_relative_to(directory)
831844
):
832845
raise outside(target)
846+
if target.is_dir() and target not in visited:
847+
visited.add(target)
848+
pending.append(target)
833849
elif entry.is_dir():
834-
pending.append(Path(entry.path))
850+
real = Path(entry.path).resolve()
851+
if real not in visited:
852+
visited.add(real)
853+
pending.append(real)
835854

836855

837856
def git_branch(

‎src/git/tests/test_protocol.py‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1482,6 +1482,32 @@ async def test_restricted_server_rejects_git_dir_symlink_pointing_outside(
14821482
other.close()
14831483

14841484

1485+
async def test_restricted_server_follows_an_allowed_git_dir_symlink(
1486+
tmp_path: Path, repo: git.Repo
1487+
):
1488+
# A link that stays inside the allowed repository is followed by git, so
1489+
# what it leads to is checked too: here `.git/objects` names an object
1490+
# store in the working tree holding a link outside.
1491+
root = root_of(repo)
1492+
other = make_repo(tmp_path / "other")
1493+
nested = make_repo(root / "nested")
1494+
nested_git = Path(nested.git_dir)
1495+
nested.close()
1496+
store = root / "store"
1497+
shutil.copytree(nested_git / "objects", store, symlinks=True)
1498+
rmtree(nested_git / "objects")
1499+
(nested_git / "objects").symlink_to(store, target_is_directory=True)
1500+
escape = Path(other.git_dir) / "objects"
1501+
(store / "elsewhere").symlink_to(escape, target_is_directory=True)
1502+
# A cycle of allowed links ends rather than being walked forever.
1503+
(store / "loop").symlink_to(store, target_is_directory=True)
1504+
try:
1505+
result = await call(root, "git_status", {"repo_path": str(root / "nested")})
1506+
assert uses_outside(result, escape.resolve(), root), result
1507+
finally:
1508+
other.close()
1509+
1510+
14851511
async def test_restricted_server_accepts_git_dir_symlinks_that_stay_inside(
14861512
tmp_path: Path, repo: git.Repo
14871513
):

‎src/git/tests/test_repository_config.py‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,40 @@ async def test_signing_program_never_runs(repo: git.Repo, payload: Payload):
346346
assert payload.runs() == []
347347

348348

349+
@posix_only
350+
async def test_merge_options_never_run_the_signing_program(
351+
repo: git.Repo, payload: Payload
352+
):
353+
# `branch.<name>.mergeOptions` reaches git merge as command-line options,
354+
# which outrank `commit.gpgSign=false`: `--gpg-sign` there would sign the
355+
# merge commit with the repository's `gpg.program`.
356+
gpg = payload.script.parent / "gpg.sh"
357+
gpg.write_text(f"#!/bin/sh\n{payload.command('gpg')}\nexit 1\n")
358+
gpg.chmod(gpg.stat().st_mode | stat.S_IEXEC)
359+
repo.git.checkout("-b", "feature")
360+
commit_file(repo, "feature.txt", "feature\n")
361+
repo.git.checkout("main")
362+
commit_file(repo, "main.txt", "main\n")
363+
set_config(
364+
repo,
365+
branch__main__mergeOptions="--gpg-sign --verify-signatures",
366+
user__signingKey="ABCDEF",
367+
gpg__program=str(gpg),
368+
)
369+
370+
result = await call(
371+
"git_merge",
372+
{
373+
"repo_path": str(root_of(repo)),
374+
"source_branch": "feature",
375+
"fast_forward_only": False,
376+
},
377+
)
378+
379+
assert result["isError"] is False, result
380+
assert payload.runs() == []
381+
382+
349383
@posix_only
350384
async def test_submodule_configuration_is_not_reached(
351385
tmp_path: Path, repo: git.Repo, payload: Payload
@@ -407,6 +441,7 @@ def test_harden_repo_keeps_the_users_own_drivers(
407441
filter__lfs__clean="evil",
408442
filter__Evil__process="evil",
409443
merge__evil__driver="evil",
444+
branch__main__mergeOptions="--gpg-sign",
410445
)
411446

412447
harden_repo(repo)
@@ -415,6 +450,7 @@ def test_harden_repo_keeps_the_users_own_drivers(
415450
assert config_value(repo, "filter.lfs.clean") == ""
416451
assert config_value(repo, "filter.Evil.process") == ""
417452
assert config_value(repo, "merge.evil.driver") == ""
453+
assert config_value(repo, "branch.main.mergeOptions") == ""
418454

419455

420456
def test_harden_repo_keeps_the_users_command_line_configuration(

0 commit comments

Comments
 (0)