Skip to content

fix(git): GHSA-qf89-h82f-h5r5 - git_merge overwrites ignored local files on a merge commit - #5679

Merged
cliffhall merged 3 commits into
v2/mainfrom
v2/fix/ghsa-qf89-h82f-h5r5
Nov 27, 2026
Merged

cliffhall merged 3 commits into
v2/mainfrom
v2/fix/ghsa-qf89-h82f-h5r5

Conversation

@cliffhall

Copy link
Copy Markdown
Member

Closes #5671

Fix for GHSA-qf89-h82f-h5r5. It was written and reviewed in the advisory's private fork, where a subagent reviewer stood in for Copilot (which cannot review a private fork) until a clean round. Per the advisory flow this PR gets no Copilot review. The private fork has no Actions, so this PR's CI is the first CI run on the change; the full npm run local:gate passed on this exact head and on a rollup of all eight advisory fixes.

Description

git_merge passes --no-overwrite-ignore, but git honors that flag only on a fast-forward. When the branches have diverged and fast_forward_only=false, git's merge-commit path (merge-ort, and -s recursive too, on git 2.39 and 2.54) writes the source's version of a path over an ignored, untracked local file (a .env, say) and reports success. The README promises the tool refuses such a merge.

This adds a server-side pre-check, _ignored_files_overwritten, run before a non-fast-forward merge:

  • Incoming paths are those the source tracks and HEAD does not (git diff-tree -r -z --no-renames --diff-filter=A HEAD <source>). The tree is required clean beforehand, so only such a path can land on an untracked file.
  • For each, what is in the way is whatever exists at that path (file, symlink, or a directory and everything in it), or the nearest existing ancestor that is not a directory (a file or symlink where the source needs a directory).
  • git ls-files --others --ignored --exclude-standard over those paths (as :(top,literal) pathspecs) gives the ignored files among them. Any hit refuses the merge before git runs: Merge of '<source>' into '<target>' refused: it would overwrite ignored local files: <paths>. Nothing was changed. Move or remove them first.
  • An untracked file that is not ignored is left to git, which already refuses it (existing behavior and message unchanged). A new file inside an ignored directory, replacing nothing, is still allowed.

The fast-forward path is unchanged: git refuses there itself, and the existing test still covers it.

Server Details

  • Server: git (mcp-server-git)
  • Changed: git_merge and a new private helper _ignored_files_overwritten in server.py; the git_merge entry in README "### Tools" (the refusal now says it holds for merge commits too). No schema, description or annotation change.

Motivation and Context

GHSA-qf89-h82f-h5r5.

How Has This Been Tested?

  • Regression tests in tests/test_merge.py, driven through an in-memory MCP client: an ignored .env on a diverged merge with fast_forward_only=false (failed on v2/main: merged and overwrote; now refused, bytes and HEAD unchanged); an ignored directory where the source tracks a file plus an ignored file where the source needs a directory (both refused, contents kept); and a new file inside an ignored directory (still merges, local ignored file kept). Existing fast-forward and non-ignored untracked tests pass unchanged. server.py coverage 100% lines / 99.5% branches.
  • Inspector CLI (@modelcontextprotocol/inspector@2.12.0, stdio, --repository <fixture>) calling git_merge with fast_forward_only=false on a diverged fixture with an ignored local .env, in both eras:
    • before (v2/main), --protocol-era modern and legacy: Merged 'source' into 'main' with merge commit …, exit 0, .env replaced by the branch's bytes.
    • after, both eras: isError: true, Merge of 'source' into 'main' refused: it would overwrite ignored local files: .env. Nothing was changed. Move or remove them first., exit 5, .env kept.
  • npm run local:gate: EXIT=0.
  • Not tested with an LLM client: not feasible from this private-fork session; the Inspector CLI run above is the client evidence.

What this leaves open

  • ort's unique_path suffix (<path>~HEAD_1, ...) is not predicted: git adds it only when <path>~HEAD is itself a path in the merge, not because of a worktree file.
  • The non-fast-forward merge runs with -c merge.directoryRenames=false, so a path the source adds in a directory HEAD renamed lands where the source put it, not in the renamed directory (a merge-behavior change from git's default conflict, which would otherwise stop on a conflict there).

Round 1 changes

  • Directory renames: the merge runs with merge.directoryRenames=false (through a new optional config keyword on _rewrite_tree; HARDENED_CONFIG untouched), so it writes no path outside what the check predicts.
  • Case/Unicode spellings: each in-the-way entry is respelled from the directory listing by identity (os.path.samestat) before the ls-files lookup.
  • File/directory conflict side names (<path>~HEAD, <path>~<source sha>) are checked too.
  • Docstring: the HEAD-deleted path is required (modify/delete writes the source's version), not "conservative".

Breaking Changes

None. A merge that would have overwritten ignored files is now refused; no configuration change.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature
  • Breaking change
  • Documentation update

Checklist

  • I have read the MCP Protocol Documentation (not applicable: no protocol surface changes; tool errors use the existing error result)
  • My changes follow MCP security best practices
  • I have updated the server's README accordingly
  • I have added a changeset (not applicable: Python server)
  • I have tested this with an LLM client (not feasible here; Inspector CLI evidence above)
  • 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)

🤖 Generated with Claude Code

cliffhall and others added 3 commits November 27, 2026 00:26
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: cliffhall <cliff@futurescale.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: cliffhall <cliff@futurescale.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: cliffhall <cliff@futurescale.com>
@cliffhall cliffhall added the v2 label Nov 27, 2026
@changeset-bot

changeset-bot Bot commented Nov 27, 2026

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: 13816b7

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: 90e6a48c8c90 (Merge pull request #5652 from modelcontextprotocol/v2/feat/5651-everything-events)
Head: the build in this checkout, at df51f6875321 (Merge 13816b7 into 90e6a48)

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 13816b7c9cad846bd07c6fdf1f436a374fa9ac6d.

@cliffhall
cliffhall merged commit 274a275 into v2/main Nov 27, 2026
47 checks passed
@cliffhall
cliffhall deleted the v2/fix/ghsa-qf89-h82f-h5r5 branch November 27, 2026 07:24
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-qf89-h82f-h5r5] - mcp-server-git: git_merge overwrites ignored local files on a merge commit

1 participant