fix(git): prevent path traversal in git_add and add hardening for git_create_branch #3178
+179
−0
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.