modelcontextprotocol/servers · error · BadName

Invalid base branch: '{base_branch}' - cannot start with '-'

Error message

Invalid base branch: '{base_branch}' - cannot start with '-'

What it means

git_create_branch() rejects a base_branch that starts with '-' to prevent flag injection. Raises gitdb BadName; propagates raw to the MCP client. Note the check fires before repo.references[base_branch] lookup, so an invalid base is blocked even if no such ref exists.

Source

Thrown at src/git/src/mcp_server_git/server.py:205

    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

  1. Reject base_branch values starting with '-'.
  2. Confirm the base branch exists (git_branch) before creating from it.

Example fix

# before
git_create_branch(repo, 'feat', base_branch='-bMalicious')  # -> BadName

# after
if base_branch and base_branch.startswith('-'):
    raise ValueError('base_branch must not start with -')
git_create_branch(repo, 'feat', base_branch)
Defensive patterns

Strategy: validation

Validate before calling

def safe_base(base: str | None) -> str | None:
    if base is not None and base.startswith('-'):
        raise ValueError('base_branch must not start with -')
    return base

Try / catch

from gitdb.exc import BadName
try:
    git_create_branch(repo, name, base_branch=base)
except BadName as e:
    if 'base branch' in str(e) and 'cannot start with' in str(e):
        # sanitize or drop base and retry
    raise

Prevention

When it happens

Trigger: Calling git_create_branch with base_branch beginning with '-'; malicious or malformed ref input.

Common situations: Adversarial input; stale/mistyped base ref.

Related errors


AI-assisted analysis of modelcontextprotocol/servers@76d64c822f (2026-08-12). Data as JSON: /api/errors/cb0ebd95d3884403. Report an issue: GitHub.