Skip to content

Commit b3777a0

Browse files
authored
Merge pull request #5586 from modelcontextprotocol/v2/fix/GHSA-pm7p-vphh-2cg7
fix(git): GHSA-pm7p-vphh-2cg7
2 parents b3fe0cf + f777ce6 commit b3777a0

3 files changed

Lines changed: 113 additions & 3 deletions

File tree

‎src/git/README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ A repository's configuration and hooks travel with it (an unpacked archive, a sh
184184

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.
187-
- 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.
187+
- Each filter driver (`filter.<name>.clean`, `smudge`, `process`) and merge driver (`merge.<name>.driver`) that the repository's own configuration defines is emptied, including one with an empty name (`[filter ""]`, selected by `filter=`): 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.
188188
- The repository's configuration can depend on its working tree (an `[include]` or `[includeIf]` path into it, or a `.git/config` linked to a tracked file), so a tool that writes the working tree (`git_checkout`, `git_merge`, and the `git merge --abort` after a conflict) reads the drivers again before it runs any other Git command, and a driver that the files it wrote define is emptied as well.
189189
- `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`.
190190
- Git never fetches a missing object from a promisor remote by itself (`GIT_NO_LAZY_FETCH=1`), since that fetch runs the transport program the repository's configuration names (`remote.<name>.uploadpack`, `core.sshCommand`). In a partial clone, or any repository that declares a promisor remote, a tool that needs an object that is not on disk reports it missing. A Git too old to honor the variable (before 2.45.1, other than the maintenance releases 2.39.4, 2.40.2, 2.41.1, 2.42.2, 2.43.4 and 2.44.1 made with it) refuses a repository that declares a promisor remote (`remote.<name>.promisor`, `remote.<name>.partialCloneFilter` or `extensions.partialClone`, with or without a remote name).

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,9 +166,11 @@ def _git_text(repo: git.Repo, command: str, *args: str) -> str:
166166
# `commit.gpgSign` says). Drivers and options from the user's global or
167167
# system configuration (git-lfs, say) are the user's own and keep working.
168168
# `harden_repo` lists these keys, and `core.hooksPath`, whose command-scope
169-
# value shows that git applied the overrides.
169+
# value shows that git applied the overrides. A driver's name may be empty
170+
# (`[filter ""]`, read back as `filter..clean`, selected by `filter=`), so
171+
# the name is matched with `.*`, not `.+`.
170172
_LISTED_KEYS = (
171-
r"^(core\.hookspath|filter\..+\.(clean|smudge|process)|merge\..+\.driver"
173+
r"^(core\.hookspath|filter\..*\.(clean|smudge|process)|merge\..*\.driver"
172174
r"|branch\..+\.mergeoptions)$"
173175
)
174176

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
"""A filter or merge driver with an empty name is overridden like any other.
2+
3+
Git accepts `[filter ""]` and `[merge ""]` sections, whose keys read back as
4+
`filter..clean` and `merge..driver`, and `.gitattributes` selects them with
5+
`filter=` and `merge=`. `harden_repo` must list and blank those keys too, or
6+
the repository's own configuration runs a command on git_status, git_add,
7+
git_checkout or git_merge. The payload and helpers are those of
8+
`test_repository_config.py`, which covers drivers with ordinary names.
9+
"""
10+
11+
import os
12+
from pathlib import Path
13+
from typing import Any
14+
15+
import git
16+
import pytest
17+
from conftest import connect, wire
18+
from test_repository_config import Payload, commit_file, posix_only, root_of
19+
20+
from mcp_server_git.server import harden_repo
21+
22+
23+
@pytest.fixture
24+
def payload(tmp_path: Path) -> Payload:
25+
return Payload(tmp_path / "payload")
26+
27+
28+
def set_empty_driver(repo: git.Repo, key: str, value: str) -> None:
29+
"""Set `<section>..<key>`, a key in a subsection with an empty name."""
30+
repo.git.config(key, value)
31+
32+
33+
def make_stale(path: Path) -> None:
34+
"""Move a tracked file's mtime so git re-hashes it, as after an unpack."""
35+
stat = path.stat()
36+
os.utime(path, (stat.st_atime, stat.st_mtime + 10))
37+
38+
39+
def test_harden_repo_blanks_an_empty_named_driver(repo: git.Repo):
40+
set_empty_driver(repo, "filter..clean", "false")
41+
set_empty_driver(repo, "filter..smudge", "false")
42+
set_empty_driver(repo, "filter..process", "false")
43+
set_empty_driver(repo, "merge..driver", "false")
44+
45+
harden_repo(repo)
46+
47+
for key in ("filter..clean", "filter..smudge", "filter..process", "merge..driver"):
48+
assert repo.git.config("--get", key) == "", key
49+
50+
51+
@posix_only
52+
@pytest.mark.parametrize("kind", ["clean", "smudge", "process"])
53+
async def test_empty_named_filter_driver_never_runs(
54+
repo: git.Repo, payload: Payload, kind: str
55+
):
56+
commit_file(repo, ".gitattributes", "*.txt filter=\n")
57+
repo.git.checkout("-b", "other")
58+
commit_file(repo, "other.txt", "other\n")
59+
repo.git.checkout("main")
60+
set_empty_driver(repo, f"filter..{kind}", payload.command("filter"))
61+
make_stale(root_of(repo) / "test.txt")
62+
(root_of(repo) / "new.txt").write_text("new\n")
63+
path = str(root_of(repo))
64+
65+
for repository in (None, root_of(repo)):
66+
async with connect(repository) as session:
67+
for tool, arguments in (
68+
("git_status", {}),
69+
("git_diff_unstaged", {}),
70+
("git_add", {"files": ["new.txt"]}),
71+
("git_checkout", {"branch_name": "other"}),
72+
("git_checkout", {"branch_name": "main"}),
73+
):
74+
result: dict[str, Any] = wire(
75+
await session.call_tool(tool, {"repo_path": path, **arguments})
76+
)
77+
assert result["isError"] is False, (tool, result)
78+
79+
assert payload.runs() == []
80+
assert (root_of(repo) / "test.txt").exists()
81+
82+
83+
@posix_only
84+
async def test_empty_named_merge_driver_never_runs(repo: git.Repo, payload: Payload):
85+
# With the driver disabled, the conflicting change is a plain conflict,
86+
# which git_merge aborts.
87+
commit_file(repo, ".gitattributes", "*.txt merge=\n")
88+
repo.git.checkout("-b", "feature")
89+
commit_file(repo, "test.txt", "feature\n")
90+
repo.git.checkout("main")
91+
commit_file(repo, "test.txt", "main\n")
92+
set_empty_driver(repo, "merge..driver", payload.command("merge"))
93+
94+
async with connect(None) as session:
95+
result = wire(
96+
await session.call_tool(
97+
"git_merge",
98+
{
99+
"repo_path": str(root_of(repo)),
100+
"source_branch": "feature",
101+
"fast_forward_only": False,
102+
},
103+
)
104+
)
105+
106+
assert result["isError"] is True
107+
assert "stopped on conflicts in: test.txt" in result["content"][0]["text"]
108+
assert payload.runs() == []

0 commit comments

Comments
 (0)