Skip to content

fix(git): repeat the --repository check after a tool writes the working tree - #5627

Open
cliffhall wants to merge 1 commit into
v2/mainfrom
v2/fix/5599-git-recheck-confinement
Open

cliffhall wants to merge 1 commit into
v2/mainfrom
v2/fix/5599-git-recheck-confinement

Conversation

@cliffhall

Copy link
Copy Markdown
Member

Closes #5599

Description

Under --repository, the git server checks the directories git will use (working tree, git directory, common directory, and what they contain) once per tool call, when it opens the repository. A command that writes the working tree partway through a call can change the repository's configuration for the rest of that call. The fix for GHSA-9vjp-x34c-xg6v (published, fixed in v2.5.0) re-reads the filter and merge drivers after each such write (_rewrite_tree), but did not repeat the confinement check.

This PR repeats it. _rewrite_tree now runs validate_opened_repo again, after harden_repo, whenever the call was opened under --repository. If the repository no longer passes, the call stops with an error before any other git command runs, cleanup included.

The gap, reproduced on v2/main (7f383a8, git 2.54.0):

  • .git/config is a symbolic link to a tracked file inside the repository (repo.cfg). The link stays inside, so the check at open allows it.
  • A branch adds core.worktree = <a directory outside> to that file and conflicts with the current branch.
  • git_merge of that branch: git merge writes repo.cfg, stops on the conflict, and the tool's cleanup runs git merge --abort, which reads the configuration afresh and restores files into the outside directory: it overwrote outside/m.txt and created outside/repo.cfg.

An [include] path into the working tree does not reproduce this: git reads core.worktree only from the config file itself, never from an included one. The linked .git/config is the route.

Of the issue's two options this is option 1 (repeat the check) rather than option 2 (pin the location keys at command scope). The check covers everything validate_opened_repo covers (git directory, common directory, alternates, links, index entries), not only core.worktree, and it adds no configuration to an ordinary repository's git commands, where pinning core.worktree would change how git resolves the working tree for linked worktrees and bare repositories.

Changes in src/git/src/mcp_server_git/server.py:

  • _rewrite_tree(repo, command, *args, allowed_repository=None): new keyword-only parameter; runs validate_opened_repo after the re-harden and raises git.GitError (not a GitCommandError, so git_merge's cleanup does not catch it and run more git commands).
  • git_checkout, git_merge, git_commit: new optional keyword-only allowed_repository, passed down. _merge_failure and _apply_merge_autostash take it as an optional parameter too.
  • The git_commit, git_checkout and git_merge tools pass the server's --repository.
  • validate_opened_repo and open_repo are unchanged.

Server Details

  • Server: git
  • Changes to: tools (git_merge, git_checkout, git_commit error behavior under --repository), README

Motivation and Context

#5599: after any tree-writing command in a tool call, no later git command in that call may operate outside the allowed repository.

How Has This Been Tested?

Automated: new src/git/tests/test_tree_confinement.py, driven through an MCP client over the in-memory transport: a conflicting merge, a fast-forward merge, a checkout and a reapplied merge autostash that each move core.worktree outside mid-call (each asserts the outside directory is byte-for-byte unchanged and the call reports the stop), an ordinary repository merging and checking out as before, and unit tests of _rewrite_tree (no check without --repository; a failed check of any kind stops the call with a non-GitCommandError). Against the unfixed server.py the four protocol tests fail; the conflicting-merge one fails on outside/m.txt rewritten to ours and a new outside/repo.cfg.

Fixture for the client runs (<fixture>/repo is the --repository):

mkdir -p <fixture>/outside; echo "not the repository's" > <fixture>/outside/m.txt
git init -q -b main <fixture>/repo; cd <fixture>/repo
echo base > m.txt; cp .git/config repo.cfg; git add .; git commit -qm base
git checkout -qb feature; echo theirs > m.txt
printf '[core]\n\tworktree = %s/outside\n' <fixture> >> repo.cfg
git add .; git commit -qm theirs
git checkout -q main; echo ours > m.txt; git commit -qam ours
rm .git/config; ln -s ../repo.cfg .git/config

Inspector CLI 2.9.0, same request against v2/main and this branch, in both eras:

npx -y @modelcontextprotocol/inspector@2.9.0 --cli \
  uv --directory src/git run mcp-server-git --repository <fixture>/repo -- \
  --protocol-era <legacy|modern> --method tools/call --tool-name git_merge \
  --tool-args-json '{"repo_path":"<fixture>/repo","source_branch":"feature","fast_forward_only":false}' \
  --format json
Result text (isError: true in every case) <fixture>/outside afterwards MERGE_HEAD
v2/main, legacy (2025-11-25) Error executing tool git_merge: Merge of 'feature' into 'main' stopped on conflicts in: m.txt. The merge was aborted and the repository is unchanged. Resolve the conflicts by hand. m.txt now reads ours; new file repo.cfg absent
v2/main, modern (2026-07-28) the same the same absent
branch, legacy (2025-11-25) Error executing tool git_merge: Stopped: after git merge changed the working tree, the repository no longer passes the --repository check. The working tree was left as git merge wrote it (a merge may still be in progress), and no further git command was run: Repository path '<fixture>/repo' uses '<fixture>/outside', which is outside the allowed repository '<fixture>/repo' unchanged: only m.txt, reading not the repository's present
branch, modern (2026-07-28) the same unchanged present

The modern results carry _meta["io.modelcontextprotocol/serverInfo"], which shows the 2026-07-28 era answered.

Ordinary repository, on this branch (Inspector CLI 2.9.0, both eras, same results in each): a conflicting git_merge returns the unchanged ... stopped on conflicts in: m.txt. The merge was aborted and the repository is unchanged. ... and leaves no MERGE_HEAD; a fast-forward git_merge returns Fast-forwarded 'main' to 'ff' (<sha>); git_checkout returns Switched to branch 'feature'.

LLM client: Claude Code 2.1.327, headless, with --strict-mcp-config naming this branch's server under --repository <fixture>/repo (Claude Code has no era switch, so the era could not be selected). Prompt: "Use the git server's git_merge tool to merge the branch 'feature' into the current branch of the repository at <fixture>/repo with fast_forward_only false. Then reply with exactly the text the tool returned, nothing else." Reply: Error executing tool git_merge: Stopped: after git merge changed the working tree, the repository no longer passes the --repository check. ... uses '<fixture>/outside', which is outside the allowed repository '<fixture>/repo'. Afterwards <fixture>/outside held only its original m.txt, unchanged.

Gate: npm run local:gate exited 0.

Breaking Changes

None. No client configuration changes. Only a repository whose configuration moves outside --repository during a call behaves differently: the call now stops with an error and leaves the working tree as the last git command wrote it (a conflicting merge is left in progress rather than aborted), where before it carried on.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation update

Checklist

  • I have read the MCP Protocol Documentation
  • My changes follow MCP security best practices
  • I have updated the server's README accordingly
  • I have added a changeset (npm run changeset) if this changes what a TypeScript server publishes (not applicable: a Python server)
  • I have tested this with an LLM client
  • My code follows the repository's style guidelines
  • New and existing tests pass locally
  • I have added appropriate error handling
  • I have documented all environment variables and configuration options (not applicable: none added)

Additional context

The stop leaves a conflicting merge in progress on purpose: once the repository no longer validates, no git command can be trusted to stay inside it, and git merge --abort is the command that wrote outside. The next call on that repository is refused at open by the existing check, with the same reason.

The tests link .git/config to a tracked file, so the four protocol tests skip on Windows (creating a symbolic link needs a privilege there); the _rewrite_tree unit tests run everywhere.

🤖 Generated with Claude Code

…ng tree

Under --repository the directories git uses were checked once per tool
call, when the repository was opened. A command that writes the working
tree can change the repository's configuration for the rest of the call:
with .git/config linked to a tracked file, a merge that brings in
core.worktree moved the working tree, and the `git merge --abort` after a
conflict then wrote files outside the allowed repository.

_rewrite_tree, which already re-reads the drivers after each such write,
now also runs validate_opened_repo again when the call was opened under
--repository, and stops the call before any other git command if the
repository no longer passes. git_checkout, git_merge and git_commit take
the allowed repository as an optional keyword argument and pass it down.

Closes #5599

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Signed-off-by: cliffhall <cliff@futurescale.com>
@cliffhall cliffhall added the v2 label Nov 18, 2026
@changeset-bot

changeset-bot Bot commented Nov 18, 2026

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: d893d97

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@github-actions

Copy link
Copy Markdown
Contributor

git: MCP interface diff

Base: 7f383a8e0f34 (Merge pull request #5597 from modelcontextprotocol/changeset-release/v2/main)
Head: the build in this checkout, at 47cdc3f9c191 (Merge d893d97 into 7f383a8)

Diffed because it was named (--server).

✅ No interface changes detected.

Tools Prompts Resources Resource templates
Base 14 2 0 0
Head 14 2 0 0

Updated by this run for d893d97104b6d22ed96f0add5f2a9c7eee9e2452.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues remain for the changes that revalidate repository confinement after working-tree writes.

No open issues 🎉

What changed in this PR

This PR addresses #5599 by repeating the Git reference server’s --repository confinement check after working-tree writes, preventing unsafe continuation when repository configuration changes mid-call.

Changes:

  • Passes the allowed repository through checkout, merge, and commit-autostash paths and revalidates after re-hardening.
  • Adds regression tests for redirected working trees, validation failures, and unchanged ordinary-repository behavior.
  • Documents that failed revalidation stops the call and may leave a merge in progress.
File Description
src/​git/​tests/​test_tree_confinement.py Adds protocol and helper tests for post-write confinement checks.
src/​git/​src/​mcp_server_git/​server.py Revalidates confinement after tree writes and prevents cleanup after validation failure.
src/​git/​README.md Explains repeated checks and the repository state left after a stop.

Review effort: Balanced

@cliffhall

Copy link
Copy Markdown
Member Author

Copilot round 1: clean. The review reported no open issues and posted no inline or suppressed comments, so the loop ends here on the first clean round. Nothing was pushed after the review.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[GHSA-9vjp-x34c-xg6v follow-up] - git: repeat the --repository confinement check after a tool writes the working tree

2 participants