Skip to content

fix(git): GHSA-f33v-89w7-f9vc - repo_path expands environment variables and error messages disclose their values - #5675

Merged
cliffhall merged 3 commits into
v2/mainfrom
v2/fix/ghsa-f33v-89w7-f9vc
Nov 27, 2026
Merged

cliffhall merged 3 commits into
v2/mainfrom
v2/fix/ghsa-f33v-89w7-f9vc

Conversation

@cliffhall

@cliffhall cliffhall commented Nov 27, 2026 •

Copy link
Copy Markdown
Member

Closes #5667

Fix for GHSA-f33v-89w7-f9vc. 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

Fixes GHSA-f33v-89w7-f9vc. repo_path (and --repository) is now taken literally: a $NAME / ${NAME} in it is never replaced with the value of an environment variable of the server process, so no error message can echo one.

  • git.Repo(...) gets expand_vars=False at all three call sites: open_repo in create_server, the allowed-root reopen in validate_opened_repo, and the --repository root lookup in serve.
  • expand_vars=False alone is not enough. GitPython's Repo.__init__ builds its command wrapper as Git(self.working_dir) from a str, and git.Git.__init__ runs expand_path on a str, which calls os.path.expandvars whatever expand_vars the repo was opened with. Without a further change, a repository whose path really contains $NAME (with NAME set) would open literally, but its git commands would run in the expanded directory. When that directory does not exist, they would run in the server's own cwd, because GitPython falls back to cwd=None. The same wrapper is also handed to the object database. So open_repo now constructs a small _LiteralRepo(git.Repo), whose GitCommandWrapperType is _LiteralGit(git.Git). _LiteralGit passes its working directory on as a Path, which GitPython only resolves and never expands. The other two call sites run no git commands (they read only git_dir / common_dir / working_dir), so the argument change is enough there.
  • Checked and not affected: validate_repo_path does no expansion (only Path.resolve()). The server never calls Repo.init or clone_from. git.Git(submodule) in _submodule_drivers gets a Path.

Server Details

  • Server: git (mcp-server-git)
  • Changed: open_repo in create_server, validate_opened_repo (one argument), serve (one argument), and new classes _LiteralGit / _LiteralRepo just above create_server. New test file tests/test_expand_vars.py. Tool schemas and descriptions are unchanged. README: one sentence under "### Tools" saying repo_path is taken literally (no ~, $NAME or ${NAME} expansion).

Motivation and Context

Advisory GHSA-f33v-89w7-f9vc. Through GitPython's default expand_vars=True, git_status with repo_path="$DEMO_SECRET" returned Error executing tool git_status: <cwd>/demo-secret-value-123. That happened always without --repository. With --repository it happened whenever the cwd was inside the repository, and for an absolute path ending in /${NAME} regardless of cwd. Confinement itself was not affected.

What the fix leaves open: Error messages still echo the literal path the caller sent, which the caller already knows. The advisory's optional "stop echoing the full resolved path in error messages" is not done, because it is a separate hardening.

How Has This Been Tested?

Regression tests in tests/test_expand_vars.py, driven through the in-process MCP client. All fail on origin/v2/main and pass here:

  • $NAME and ${NAME} as a relative repo_path without --repository: no variable value appears in the result, and the literal reference does.
  • Under --repository with the cwd inside the root, both as a relative path and as <root>/${NAME}: no value leaks.
  • A repository whose directory is literally named $NAME (with NAME set) opens, with and without --repository. git_status reports that repository's branch, not the cwd's, and git_log reads its commit, which covers the object database's git. This pins the _LiteralGit part of the fix.
  • --repository pointing at a $NAME directory with a separate git dir. This takes the validate_opened_repo reopen of the allowed root and serve's root lookup.

The full git suite passes (526 passed, 6 skipped), along with ruff, ruff format and pyright. npm run local:gate passed with EXIT=0.

Inspector CLI (@modelcontextprotocol/inspector@2.12.0 --cli, git_status, server started as env DEMO_SECRET=demo-secret-value-123 uv run --project <checkout>/src/git mcp-server-git [--repository R], both eras). SP is the scratch directory and R = SP/repo:

Setup repo_path Before (origin/v2/main) After (this branch)
--repository R, cwd R, legacy and modern $DEMO_SECRET Error executing tool git_status: SP/repo/demo-secret-value-123 Error executing tool git_status: SP/repo/$DEMO_SECRET
--repository R, cwd R, legacy and modern R/${DEMO_SECRET} … SP/repo/demo-secret-value-123 … SP/repo/${DEMO_SECRET}
--repository R, legacy and modern R Repository status: On branch main … the same
no --repository, cwd SP, modern $DEMO_SECRET … SP/demo-secret-value-123 … SP/$DEMO_SECRET

Modern results carry _meta["io.modelcontextprotocol/serverInfo"], which confirms that the 2026-07-28 era answered.

LLM client: not run. The change is confined to how a path argument is opened, and it was exercised over the protocol in both eras with the Inspector CLI. An LLM-client run was not feasible from this automated fixer session.

Combining with the sibling fixes: The GHSA-3whj-m8jj-x97q branch replaces the git.Repo(allowed) reopen in validate_opened_repo with a root recorded at startup, and that recording runs git commands on the root. When the branches are combined, its constructor should be _LiteralRepo(allowed, expand_vars=False).

Breaking Changes

None for normal use. Since review round 1, repo_path is opened as path.absolute(), so a leading ~ is no longer expanded either. A caller that sent ~/repo without --repository now gets an error and must send the real path. The README now says, under "### Tools", that repo_path is taken literally. A repo_path or --repository that relied on GitPython expanding $VAR now gets it literally. GitPython already marked that behavior deprecated "for security reasons", and the shell still expands an unquoted --repository $VAR before the server sees it.

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
  • 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: Python server)
  • I have tested this with an LLM client (not done: see above; Inspector CLI evidence in both eras instead)
  • 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)

Merging with the GHSA-3whj-m8jj-x97q fix (#5673)

This PR and the GHSA-3whj-m8jj-x97q fix (#5673) both edit the escaped-directory branch of validate_opened_repo in src/git/src/mcp_server_git/server.py, so whichever of the two merges into v2/main second hits a textual conflict when it catches up (git merge origin/v2/main).

Keep the GHSA-3whj side: its if allowed_repository in _ROOT_GIT_DIRS: … record_root_git_dirs(allowed_repository) block, and drop this PR's with git.Repo(allowed, expand_vars=False) as allowed_repo: lines. That fix removes the per-call reopen of the root this PR hardened, and its replacement (_RootRepo) does not expand the root's path. The rest of this PR (_LiteralGit, _LiteralRepo, open_repo, serve) merges cleanly and stays. Verified on a trial merge of all eight advisory branches: the git suite passes with that resolution.

🤖 Generated with Claude Code

cliffhall and others added 3 commits November 27, 2026 00:32
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: 0475c7c

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 3543fed2a0d8 (Merge 0475c7c 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 0475c7c8cbed93c78c5f8f2674c4932b8ca62b4f.

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-f33v-89w7-f9vc] - mcp-server-git: repo_path expands environment variables and error messages disclose their values

1 participant