Skip to content

Commit ae40ec2

Browse files
authored
fix(git): add missing argument injection guards to git_show, git_create_branch, git_log, and git_branch (#3545)
fix(git): add missing argument injection guards Extends existing startswith("-") input validation to git_show, git_create_branch, git_log, and git_branch, preventing user-supplied values from being interpreted as CLI flags by GitPython's subprocess calls to git.
1 parent 81f8301 commit ae40ec2

File tree

2 files changed

+79
-0
lines changed

2 files changed

+79
-0
lines changed

src/git/src/mcp_server_git/server.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,11 @@ def git_reset(repo: git.Repo) -> str:
142142

143143
def git_log(repo: git.Repo, max_count: int = 10, start_timestamp: Optional[str] = None, end_timestamp: Optional[str] = None) -> list[str]:
144144
if start_timestamp or end_timestamp:
145+
# Defense in depth: reject timestamps starting with '-' to prevent flag injection
146+
if start_timestamp and start_timestamp.startswith("-"):
147+
raise ValueError(f"Invalid start_timestamp: '{start_timestamp}' - cannot start with '-'")
148+
if end_timestamp and end_timestamp.startswith("-"):
149+
raise ValueError(f"Invalid end_timestamp: '{end_timestamp}' - cannot start with '-'")
145150
# Use git log command with date filtering
146151
args = []
147152
if start_timestamp:
@@ -177,6 +182,11 @@ def git_log(repo: git.Repo, max_count: int = 10, start_timestamp: Optional[str]
177182
return log
178183

179184
def git_create_branch(repo: git.Repo, branch_name: str, base_branch: str | None = None) -> str:
185+
# Defense in depth: reject names starting with '-' to prevent flag injection
186+
if branch_name.startswith("-"):
187+
raise BadName(f"Invalid branch name: '{branch_name}' - cannot start with '-'")
188+
if base_branch and base_branch.startswith("-"):
189+
raise BadName(f"Invalid base branch: '{base_branch}' - cannot start with '-'")
180190
if base_branch:
181191
base = repo.references[base_branch]
182192
else:
@@ -197,6 +207,10 @@ def git_checkout(repo: git.Repo, branch_name: str) -> str:
197207

198208

199209
def git_show(repo: git.Repo, revision: str) -> str:
210+
# Defense in depth: reject revisions starting with '-' to prevent flag injection,
211+
# even if a malicious ref with that name exists (e.g. via filesystem manipulation)
212+
if revision.startswith("-"):
213+
raise BadName(f"Invalid revision: '{revision}' - cannot start with '-'")
200214
commit = repo.commit(revision)
201215
output = [
202216
f"Commit: {commit.hexsha!r}\n"
@@ -241,6 +255,12 @@ def validate_repo_path(repo_path: Path, allowed_repository: Path | None) -> None
241255

242256

243257
def git_branch(repo: git.Repo, branch_type: str, contains: str | None = None, not_contains: str | None = None) -> str:
258+
# Defense in depth: reject values starting with '-' to prevent flag injection
259+
if contains and contains.startswith("-"):
260+
raise BadName(f"Invalid contains value: '{contains}' - cannot start with '-'")
261+
if not_contains and not_contains.startswith("-"):
262+
raise BadName(f"Invalid not_contains value: '{not_contains}' - cannot start with '-'")
263+
244264
match contains:
245265
case None:
246266
contains_sha = (None,)

src/git/tests/test_server.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,3 +423,62 @@ def test_git_checkout_rejects_malicious_refs(test_repository):
423423

424424
# Cleanup
425425
malicious_ref_path.unlink()
426+
427+
428+
# Tests for argument injection protection in git_show, git_create_branch,
429+
# git_log, and git_branch — matching the existing guards on git_diff and
430+
# git_checkout.
431+
432+
def test_git_show_rejects_flag_injection(test_repository):
433+
"""git_show should reject revisions starting with '-'."""
434+
with pytest.raises(BadName):
435+
git_show(test_repository, "--output=/tmp/evil")
436+
437+
with pytest.raises(BadName):
438+
git_show(test_repository, "-p")
439+
440+
441+
def test_git_show_rejects_malicious_refs(test_repository):
442+
"""git_show should reject refs starting with '-' even if they exist."""
443+
sha = test_repository.head.commit.hexsha
444+
refs_dir = Path(test_repository.git_dir) / "refs" / "heads"
445+
malicious_ref_path = refs_dir / "--format=evil"
446+
malicious_ref_path.write_text(sha)
447+
448+
with pytest.raises(BadName):
449+
git_show(test_repository, "--format=evil")
450+
451+
malicious_ref_path.unlink()
452+
453+
454+
def test_git_create_branch_rejects_flag_injection(test_repository):
455+
"""git_create_branch should reject branch names starting with '-'."""
456+
with pytest.raises(BadName):
457+
git_create_branch(test_repository, "--track=evil")
458+
459+
with pytest.raises(BadName):
460+
git_create_branch(test_repository, "-f")
461+
462+
463+
def test_git_create_branch_rejects_base_branch_flag_injection(test_repository):
464+
"""git_create_branch should reject base branch names starting with '-'."""
465+
with pytest.raises(BadName):
466+
git_create_branch(test_repository, "new-branch", "--track=evil")
467+
468+
469+
def test_git_log_rejects_timestamp_flag_injection(test_repository):
470+
"""git_log should reject timestamps starting with '-'."""
471+
with pytest.raises(ValueError):
472+
git_log(test_repository, start_timestamp="--exec=evil")
473+
474+
with pytest.raises(ValueError):
475+
git_log(test_repository, end_timestamp="--exec=evil")
476+
477+
478+
def test_git_branch_rejects_contains_flag_injection(test_repository):
479+
"""git_branch should reject contains/not_contains values starting with '-'."""
480+
with pytest.raises(BadName):
481+
git_branch(test_repository, "local", contains="--exec=evil")
482+
483+
with pytest.raises(BadName):
484+
git_branch(test_repository, "local", not_contains="--exec=evil")

0 commit comments

Comments
 (0)