Skip to content

Commit 63a0dcc

Browse files
authored
Merge pull request #5581 from modelcontextprotocol/v2/fix/GHSA-h2g3-rwxm-xfm3
fix(git): GHSA-h2g3-rwxm-xfm3
2 parents 4b6f095 + 7d811d1 commit 63a0dcc

3 files changed

Lines changed: 157 additions & 0 deletions

File tree

‎src/git/README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,7 @@ A repository's configuration and hooks travel with it (an unpacked archive, a sh
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.
188188
- `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`.
189+
- `maintenance.auto` is `false` and `gc.auto` is `0`, so no tool starts Git's automatic maintenance (which `git merge` runs when it finishes), whose garbage collection would run the commands a repository's `gc.recentObjectsHook` names. Repositories the server works in are not garbage-collected automatically; your own Git commands still do it.
189190
- `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.
190191

191192
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: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,15 @@ def _git_text(repo: git.Repo, command: str, *args: str) -> str:
130130
# - `submodule.recurse` would carry checkout and merge into submodules, whose
131131
# own configuration this override cannot neutralize (see `_LISTED_KEYS`).
132132
# - `merge.autoStash` keeps git_merge from stashing behind the tool's back.
133+
# - `maintenance.auto` keeps a merge (or any other command that ends in
134+
# `git maintenance run --auto`) from starting git's automatic maintenance,
135+
# whose cruft repacks (git 2.42+) run each command the repository's
136+
# `gc.recentObjectsHook` names. Stopping the launch covers every task,
137+
# including a geometric repack the repository enables, which its own
138+
# `maintenance.<task>.auto` gates; `gc.auto` is a backstop for the gc task
139+
# only, so it does not replace `maintenance.auto`.
140+
# That hook cannot be emptied like a driver: it is multi-valued, and an
141+
# empty override adds a value rather than replacing the repository's.
133142
#
134143
# External diff programs and textconv filters are turned off per command
135144
# (`_DIFF_FLAGS`), and filter and merge drivers per repository (`_LISTED_KEYS`).
@@ -142,6 +151,8 @@ def _git_text(repo: git.Repo, command: str, *args: str) -> str:
142151
("submodule.recurse", "false"),
143152
("status.submoduleSummary", "false"),
144153
("merge.autoStash", "false"),
154+
("maintenance.auto", "false"),
155+
("gc.auto", "0"),
145156
)
146157

147158
# A filter driver (`filter.<name>.clean`/`smudge`/`process`, applied by
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
"""No tool starts git's automatic maintenance in a repository it opens.
2+
3+
`git merge` ends in `git maintenance run --auto`, which runs `git gc` when
4+
the repository's own configuration says it is due (`gc.auto`,
5+
`gc.autoPackLimit`). That gc's cruft repack runs every command the
6+
repository's `gc.recentObjectsHook` names, so a repository could run a
7+
program as the server's user through git_merge. `HARDENED_CONFIG` pins
8+
`maintenance.auto` and `gc.auto` off; these tests plant a repository that
9+
forces a gc on the next merge, with a hook that records its own run in a
10+
canary file, and assert the canary is never written.
11+
12+
The hook is a POSIX shell script, so the tests that run it skip on Windows;
13+
the check of the pinned values runs everywhere.
14+
"""
15+
16+
import os
17+
import shlex
18+
import stat
19+
import subprocess
20+
import sys
21+
from pathlib import Path
22+
23+
import git
24+
import pytest
25+
from conftest import connect, wire
26+
27+
from mcp_server_git.server import harden_repo
28+
29+
posix_only = pytest.mark.skipif(
30+
sys.platform == "win32", reason="the hook is a POSIX shell script"
31+
)
32+
# gc.recentObjectsHook arrived in git 2.42; an older git never runs it.
33+
needs_hook = pytest.mark.skipif(
34+
git.Git().version_info < (2, 42), reason="gc.recentObjectsHook needs git 2.42"
35+
)
36+
37+
38+
def root_of(repo: git.Repo) -> Path:
39+
assert repo.working_dir is not None
40+
return Path(repo.working_dir)
41+
42+
43+
def commit_file(repo: git.Repo, name: str, content: str) -> None:
44+
(root_of(repo) / name).write_text(content, newline="\n")
45+
repo.git.add(name)
46+
repo.git.commit("-m", f"write {name}")
47+
48+
49+
def plant_due_gc(repo: git.Repo, canary: Path, *, diverge: bool) -> None:
50+
"""Make the next merge of `feature` run a gc that asks the hook.
51+
52+
Two packs make gc due under `gc.auto=1` and `gc.autoPackLimit=1`, and
53+
their sizes (6 or 3 objects against 4) are within a factor of two, so a
54+
git whose maintenance repacks geometrically finds them due too. An old
55+
unreachable loose object makes the cruft repack consult
56+
`gc.recentObjectsHook`. `diverge` adds a commit on `main`, so the merge
57+
is not a fast-forward.
58+
"""
59+
# Each commit comes before the pack it lands in: a commit runs automatic
60+
# maintenance too, which could consolidate packs already made.
61+
repo.git.branch("feature")
62+
if diverge:
63+
commit_file(repo, "main.txt", "main\n")
64+
repo.git.repack("-a", "-d")
65+
repo.git.checkout("feature")
66+
(root_of(repo) / "feature-1.txt").write_text("one\n", newline="\n")
67+
(root_of(repo) / "feature-2.txt").write_text("two\n", newline="\n")
68+
repo.git.add("feature-1.txt", "feature-2.txt")
69+
repo.git.commit("-m", "feature")
70+
repo.git.checkout("main")
71+
repo.git.repack("-d")
72+
73+
hook = canary.parent / "hook.sh"
74+
hook.write_text(f"#!/bin/sh\necho ran >> {shlex.quote(str(canary))}\n")
75+
hook.chmod(hook.stat().st_mode | stat.S_IEXEC)
76+
with repo.config_writer() as config:
77+
config.set_value("gc", "auto", "1")
78+
config.set_value("gc", "autoPackLimit", "1")
79+
config.set_value("gc", "autoDetach", "false")
80+
config.set_value("gc", "recentObjectsHook", shlex.quote(str(hook)))
81+
82+
sha = (
83+
subprocess.run(
84+
["git", "hash-object", "-w", "--stdin"],
85+
cwd=root_of(repo),
86+
input=b"stale\n",
87+
capture_output=True,
88+
check=True,
89+
)
90+
.stdout.decode()
91+
.strip()
92+
)
93+
loose = Path(repo.git_dir) / "objects" / sha[:2] / sha[2:]
94+
os.utime(loose, (0, 0))
95+
96+
97+
@posix_only
98+
@pytest.mark.parametrize("fast_forward_only", [True, False])
99+
@pytest.mark.parametrize("pinned", [False, True], ids=["roots", "repository"])
100+
async def test_merge_never_runs_the_recent_objects_hook(
101+
tmp_path: Path, repo: git.Repo, fast_forward_only: bool, pinned: bool
102+
):
103+
canary = tmp_path / "canary"
104+
plant_due_gc(repo, canary, diverge=not fast_forward_only)
105+
106+
async with connect(root_of(repo) if pinned else None) as session:
107+
result = wire(
108+
await session.call_tool(
109+
"git_merge",
110+
{
111+
"repo_path": str(root_of(repo)),
112+
"source_branch": "feature",
113+
"fast_forward_only": fast_forward_only,
114+
},
115+
)
116+
)
117+
118+
assert result["isError"] is False, result
119+
assert (root_of(repo) / "feature-1.txt").exists()
120+
assert not canary.exists()
121+
122+
123+
@posix_only
124+
@needs_hook
125+
@pytest.mark.parametrize("diverge", [False, True], ids=["fast-forward", "merge"])
126+
def test_the_planted_repository_runs_the_hook_without_the_overrides(
127+
tmp_path: Path, repo: git.Repo, diverge: bool
128+
):
129+
# The control: the same merge under plain git does run the hook, so the
130+
# test above fails if the overrides go missing.
131+
canary = tmp_path / "canary"
132+
plant_due_gc(repo, canary, diverge=diverge)
133+
repo.git.merge("--ff", "--no-edit", "feature")
134+
assert canary.read_text() == "ran\n"
135+
136+
137+
def test_harden_repo_turns_automatic_maintenance_off(repo: git.Repo):
138+
with repo.config_writer() as config:
139+
config.set_value("gc", "auto", "1")
140+
config.set_value("maintenance", "auto", "true")
141+
harden_repo(repo)
142+
assert repo.git.config("--show-scope", "--get", "gc.auto") == "command\t0"
143+
assert (
144+
repo.git.config("--show-scope", "--get", "maintenance.auto") == "command\tfalse"
145+
)

0 commit comments

Comments
 (0)