modelcontextprotocol/servers · error · BadName
Invalid branch name: '{branch_name}' - cannot start with '-'
Error message
Invalid branch name: '{branch_name}' - cannot start with '-' What it means
git_create_branch() rejects a branch_name that starts with '-' to prevent flag injection into `git branch`. Raises gitdb BadName before repo.create_head is invoked; the exception propagates raw to the MCP client (server runs raise_exceptions=True).
Source
Thrown at src/git/src/mcp_server_git/server.py:203
)
return log
else:
# Use existing logic for simple log without date filtering
commits = list(repo.iter_commits(max_count=max_count))
log = []
for commit in commits:
log.append(
f"Commit: {commit.hexsha!r}\n"
f"Author: {commit.author!r}\n"
f"Date: {commit.authored_datetime}\n"
f"Message: {commit.message!r}\n"
)
return log
def git_create_branch(repo: git.Repo, branch_name: str, base_branch: str | None = None) -> str:
# Defense in depth: reject names starting with '-' to prevent flag injection
if branch_name.startswith("-"):
raise BadName(f"Invalid branch name: '{branch_name}' - cannot start with '-'")
if base_branch and base_branch.startswith("-"):
raise BadName(f"Invalid base branch: '{base_branch}' - cannot start with '-'")
if base_branch:
base = repo.references[base_branch]
else:
base = repo.active_branch
repo.create_head(branch_name, base)
return f"Created branch '{branch_name}' from '{base.name}'"
def git_checkout(repo: git.Repo, branch_name: str) -> str:
# Defense in depth: reject branch names starting with '-' to prevent flag injection,
# even if a malicious ref with that name exists (e.g. via filesystem manipulation)
if branch_name.startswith("-"):
raise BadName(f"Invalid branch name: '{branch_name}' - cannot start with '-'")
repo.rev_parse(branch_name) # Validates branch_name is a real git ref, throws BadName if not
repo.git.checkout(branch_name)
return f"Switched to branch '{branch_name}'"View on GitHub (pinned to 76d64c822f)
Solutions
- Reject branch names starting with '-' (git also forbids refnames beginning with '-').
- Validate the name against git's ref naming rules before calling.
Example fix
# before
git_create_branch(repo, branch_name='-bMalicious') # -> BadName
# after
if branch_name.startswith('-'):
raise ValueError('branch_name must not start with -')
git_create_branch(repo, branch_name) Defensive patterns
Strategy: validation
Validate before calling
def safe_branch(name: str) -> str:
if not name or name.startswith('-'):
raise ValueError('branch_name must not start with -')
return name Try / catch
from gitdb.exc import BadName
try:
git_create_branch(repo, branch_name)
except BadName as e:
if 'cannot start with' in str(e):
# sanitize name and retry
raise Prevention
- Reject branch names starting with '-' at the input boundary.
- Validate against git refname rules (no leading dash, no spaces, no '..').
When it happens
Trigger: Calling git_create_branch with branch_name beginning with '-'; malicious ref-name input.
Common situations: Adversarial input; UI allowing arbitrary branch names.
Related errors
- Invalid target: '{target}' - cannot start with '-'
- Invalid start_timestamp: '{start_timestamp}' - cannot start
- Invalid end_timestamp: '{end_timestamp}' - cannot start with
- Invalid base branch: '{base_branch}' - cannot start with '-'
- Invalid revision: '{revision}' - cannot start with '-'
AI-assisted analysis of modelcontextprotocol/servers@76d64c822f (2026-08-12).
Data as JSON: /api/errors/562db5bae2c57b39.
Report an issue: GitHub.