fix(git): prevent path traversal in git_add and add hardening for git_create_branch#3178
Closed
olaservo wants to merge 2 commits intomodelcontextprotocol:mainfrom
Closed
fix(git): prevent path traversal in git_add and add hardening for git_create_branch#3178olaservo wants to merge 2 commits intomodelcontextprotocol:mainfrom
olaservo wants to merge 2 commits intomodelcontextprotocol:mainfrom
Conversation
Add validation to git_add() to ensure file paths are within the repository boundary. This prevents attackers from staging sensitive files outside the repo (e.g., ~/.ssh/id_rsa, ~/.kube/config) via path traversal attacks using "../" sequences. GitPython's repo.index.add() does not validate paths like the Git CLI does, so explicit validation is required. - Add validate_file_path() function to check paths are within repo - Update git_add() to validate each file before staging - Add comprehensive tests for path traversal prevention 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Add validation to reject branch names and base branches starting with '-' to prevent creating refs that could be interpreted as flags. This completes the defense-in-depth pattern already used in git_diff and git_checkout. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Contributor
|
HI @olaservo I think a more consistent way is to use https://github.com/modelcontextprotocol/servers/pull/3164/files by using
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Fix a critical path traversal vulnerability and add defense-in-depth hardening:
git_add- Allows staging files outside the repositorygit_create_branchhardening - Adds input validation for consistencycc @0dd
Server Details
git_add,git_create_branch), security validationMotivation and Context
Path Traversal in
git_add(Critical)The
git_addfunction uses GitPython'srepo.index.add()which does not validate that file paths are within the repository, unlike the Git CLI. This allows attackers to stage sensitive files:Impact: Complete credential exfiltration via git commit/push.
git_create_branchHardening (Low - Defense-in-depth)The
git_create_branchfunction allowed creating branch names starting with-. While not directly exploitable (other functions likegit_checkoutandgit_diffalready reject such refs), this fix adds consistency with the existing security pattern:Impact: Prevents creating refs that could confuse other tools or future code.
How Has This Been Tested?
../sequences (rejected)~/.kube/config,~/.ssh/id_rsa)-(rejected)Breaking Changes
None. Valid file paths continue to work. Only malicious paths outside the repository are now rejected with a
ValueError.Types of changes
Checklist
Additional context
These fixes follow existing security patterns in the codebase:
validate_repo_path()for repository boundary validationgit_diff()andgit_checkout()that reject inputs starting with-The
git_create_branchfix completes the defense-in-depth pattern across all branch-related functions.