Repository navigation
Conversation
…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>
|
|
| Tools | Prompts | Resources | Resource templates | |
|---|---|---|---|---|
| Base | 14 | 2 | 0 | 0 |
| Head | 14 | 2 | 0 | 0 |
Updated by this run for d893d97104b6d22ed96f0add5f2a9c7eee9e2452.
There was a problem hiding this comment.
🟢 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
|
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. |
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_treenow runsvalidate_opened_repoagain, afterharden_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/configis a symbolic link to a tracked file inside the repository (repo.cfg). The link stays inside, so the check at open allows it.core.worktree = <a directory outside>to that file and conflicts with the current branch.git_mergeof that branch:git mergewritesrepo.cfg, stops on the conflict, and the tool's cleanup runsgit merge --abort, which reads the configuration afresh and restores files into the outside directory: it overwroteoutside/m.txtand createdoutside/repo.cfg.An
[include]path into the working tree does not reproduce this: git readscore.worktreeonly from the config file itself, never from an included one. The linked.git/configis 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_repocovers (git directory, common directory, alternates, links, index entries), not onlycore.worktree, and it adds no configuration to an ordinary repository's git commands, where pinningcore.worktreewould 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; runsvalidate_opened_repoafter the re-harden and raisesgit.GitError(not aGitCommandError, sogit_merge's cleanup does not catch it and run more git commands).git_checkout,git_merge,git_commit: new optional keyword-onlyallowed_repository, passed down._merge_failureand_apply_merge_autostashtake it as an optional parameter too.git_commit,git_checkoutandgit_mergetools pass the server's--repository.validate_opened_repoandopen_repoare unchanged.Server Details
git_merge,git_checkout,git_commiterror behavior under--repository), READMEMotivation 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 movecore.worktreeoutside 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 unfixedserver.pythe four protocol tests fail; the conflicting-merge one fails onoutside/m.txtrewritten tooursand a newoutside/repo.cfg.Fixture for the client runs (
<fixture>/repois the--repository):Inspector CLI 2.9.0, same request against
v2/mainand this branch, in both eras:isError: truein every case)<fixture>/outsideafterwardsMERGE_HEADv2/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.txtnow readsours; new filerepo.cfgv2/main,modern(2026-07-28)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'm.txt, readingnot the repository'smodern(2026-07-28)The
modernresults 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_mergereturns the unchanged... stopped on conflicts in: m.txt. The merge was aborted and the repository is unchanged. ...and leaves noMERGE_HEAD; a fast-forwardgit_mergereturnsFast-forwarded 'main' to 'ff' (<sha>);git_checkoutreturnsSwitched to branch 'feature'.LLM client: Claude Code 2.1.327, headless, with
--strict-mcp-confignaming 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>/repowith 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>/outsideheld only its originalm.txt, unchanged.Gate:
npm run local:gateexited 0.Breaking Changes
None. No client configuration changes. Only a repository whose configuration moves outside
--repositoryduring 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
Checklist
npm run changeset) if this changes what a TypeScript server publishes (not applicable: a Python server)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 --abortis 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/configto a tracked file, so the four protocol tests skip on Windows (creating a symbolic link needs a privilege there); the_rewrite_treeunit tests run everywhere.🤖 Generated with Claude Code