Skip to content

Commit 4750df4

Browse files
Merge pull request #3139 from modelcontextprotocol/fix-git-server-pyright-errors
fix(git): import BadName directly to fix pyright errors
2 parents a37158b + 88320da commit 4750df4

File tree

2 files changed

+13
-11
lines changed

2 files changed

+13
-11
lines changed

src/git/src/mcp_server_git/server.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
)
1414
from enum import Enum
1515
import git
16+
from git.exc import BadName
1617
from pydantic import BaseModel, Field
1718

1819
# Default number of context lines to show in diff output
@@ -119,7 +120,7 @@ def git_diff(repo: git.Repo, target: str, context_lines: int = DEFAULT_CONTEXT_L
119120
# Defense in depth: reject targets starting with '-' to prevent flag injection,
120121
# even if a malicious ref with that name exists (e.g. via filesystem manipulation)
121122
if target.startswith("-"):
122-
raise git.exc.BadName(f"Invalid target: '{target}' - cannot start with '-'")
123+
raise BadName(f"Invalid target: '{target}' - cannot start with '-'")
123124
repo.rev_parse(target) # Validates target is a real git ref, throws BadName if not
124125
return repo.git.diff(f"--unified={context_lines}", target)
125126

@@ -187,7 +188,7 @@ def git_checkout(repo: git.Repo, branch_name: str) -> str:
187188
# Defense in depth: reject branch names starting with '-' to prevent flag injection,
188189
# even if a malicious ref with that name exists (e.g. via filesystem manipulation)
189190
if branch_name.startswith("-"):
190-
raise git.exc.BadName(f"Invalid branch name: '{branch_name}' - cannot start with '-'")
191+
raise BadName(f"Invalid branch name: '{branch_name}' - cannot start with '-'")
191192
repo.rev_parse(branch_name) # Validates branch_name is a real git ref, throws BadName if not
192193
repo.git.checkout(branch_name)
193194
return f"Switched to branch '{branch_name}'"

src/git/tests/test_server.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import pytest
22
from pathlib import Path
33
import git
4+
from git.exc import BadName
45
from mcp_server_git.server import (
56
git_checkout,
67
git_branch,
@@ -40,7 +41,7 @@ def test_git_checkout_existing_branch(test_repository):
4041

4142
def test_git_checkout_nonexistent_branch(test_repository):
4243

43-
with pytest.raises(git.exc.BadName):
44+
with pytest.raises(BadName):
4445
git_checkout(test_repository, "nonexistent-branch")
4546

4647
def test_git_branch_local(test_repository):
@@ -316,25 +317,25 @@ def test_validate_repo_path_symlink_escape(tmp_path: Path):
316317

317318
def test_git_diff_rejects_flag_injection(test_repository):
318319
"""git_diff should reject flags that could be used for argument injection."""
319-
with pytest.raises(git.exc.BadName):
320+
with pytest.raises(BadName):
320321
git_diff(test_repository, "--output=/tmp/evil")
321322

322-
with pytest.raises(git.exc.BadName):
323+
with pytest.raises(BadName):
323324
git_diff(test_repository, "--help")
324325

325-
with pytest.raises(git.exc.BadName):
326+
with pytest.raises(BadName):
326327
git_diff(test_repository, "-p")
327328

328329

329330
def test_git_checkout_rejects_flag_injection(test_repository):
330331
"""git_checkout should reject flags that could be used for argument injection."""
331-
with pytest.raises(git.exc.BadName):
332+
with pytest.raises(BadName):
332333
git_checkout(test_repository, "--help")
333334

334-
with pytest.raises(git.exc.BadName):
335+
with pytest.raises(BadName):
335336
git_checkout(test_repository, "--orphan=evil")
336337

337-
with pytest.raises(git.exc.BadName):
338+
with pytest.raises(BadName):
338339
git_checkout(test_repository, "-f")
339340

340341

@@ -398,7 +399,7 @@ def test_git_diff_rejects_malicious_refs(test_repository):
398399
malicious_ref_path.write_text(sha)
399400

400401
# Even though the ref exists, it should be rejected
401-
with pytest.raises(git.exc.BadName):
402+
with pytest.raises(BadName):
402403
git_diff(test_repository, "--output=evil.txt")
403404

404405
# Verify no file was created (the attack was blocked)
@@ -417,7 +418,7 @@ def test_git_checkout_rejects_malicious_refs(test_repository):
417418
malicious_ref_path.write_text(sha)
418419

419420
# Even though the ref exists, it should be rejected
420-
with pytest.raises(git.exc.BadName):
421+
with pytest.raises(BadName):
421422
git_checkout(test_repository, "--orphan=evil")
422423

423424
# Cleanup

0 commit comments

Comments
 (0)