Skip to content

fix(git): add missing argument injection guards#3545

Merged
olaservo merged 1 commit intomodelcontextprotocol:mainfrom
ElliotJLT:fix/git-argument-injection-guards
Mar 15, 2026
Merged

fix(git): add missing argument injection guards#3545
olaservo merged 1 commit intomodelcontextprotocol:mainfrom
ElliotJLT:fix/git-argument-injection-guards

Conversation

@ElliotJLT
Copy link
Contributor

Summary

  • git_diff and git_checkout already reject user inputs starting with - to block flag injection attacks — but four other functions lacked the same protection: git_show, git_create_branch, git_log (timestamps), and git_branch (contains/not_contains)
  • This PR adds consistent defense-in-depth guards to all four, matching the existing pattern and comment style
  • Adds 6 new test cases covering each unguarded parameter

Context

An attacker with filesystem write access (e.g. via mcp-filesystem) could create malicious git refs like --format=evil under .git/refs/heads/. Without the leading-dash check, these values would be passed as flags to git commands via GitPython. The existing guards on git_diff and git_checkout (+ tests for malicious refs) demonstrate this threat model is already recognised — this PR closes the remaining gaps.

Test plan

  • All 41 tests pass (35 existing + 6 new)
  • New tests verify rejection of flag-like inputs for git_show, git_create_branch, git_log, and git_branch
  • New test verifies git_show rejects malicious refs even when they exist on disk
  • Existing tests confirm no regressions in normal operation

🤖 Generated with Claude Code

…te_branch, git_log, and git_branch

git_diff and git_checkout already reject user-supplied values starting
with '-' to prevent flag injection (even when a malicious ref exists via
filesystem manipulation). The same defense-in-depth pattern was missing
from four other functions:

- git_show: revision parameter passed directly to repo.commit()
- git_create_branch: branch_name and base_branch unchecked
- git_log: start_timestamp and end_timestamp passed to --since/--until
- git_branch: contains and not_contains passed as raw args to repo.git.branch()

Adds the same startswith("-") guards with matching tests for each function.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Copy link
Member

@olaservo olaservo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Solid security fix that closes real gaps in argument injection protection. The threat model is well-understood — GitPython shells out to git, so user-supplied values starting with - can be interpreted as CLI flags. The existing guards on git_diff and git_checkout already acknowledge this; this PR extends the same pattern to the four remaining vulnerable functions.

All four guard placements are correct:

  • git_show: guards revision before repo.commit()
  • git_create_branch: guards branch_name and base_branch before repo.references[]
  • git_log: guards start_timestamp and end_timestamp before repo.git.log()
  • git_branch: guards contains and not_contains before they reach repo.git.branch()

Using ValueError for timestamps (instead of BadName) is a reasonable choice since timestamps aren't git refs. Test coverage is thorough — 6 new tests including a malicious-ref-on-disk scenario for git_show.

LGTM — thanks @ElliotJLT!


Reviewed with the assistance of Claude Code (claude-opus-4-6). Claims verified against the existing codebase and CI results.

@olaservo olaservo merged commit ae40ec2 into modelcontextprotocol:main Mar 15, 2026
15 of 18 checks passed
nielskaspers pushed a commit to nielskaspers/servers that referenced this pull request Mar 15, 2026
…te_branch, git_log, and git_branch (modelcontextprotocol#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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants