{"record":{"id":"562db5bae2c57b39","repo":"modelcontextprotocol/servers","slug":"invalid-branch-name-branch-name-cannot-star","errorCode":null,"errorMessage":"Invalid branch name: '{branch_name}' - cannot start with '-'","messagePattern":"Invalid branch name: '(.+?)' - cannot start with '-'","errorType":"exception","errorClass":"BadName","httpStatus":null,"severity":"error","filePath":"src/git/src/mcp_server_git/server.py","lineNumber":203,"sourceCode":"                )\n        return log\n    else:\n        # Use existing logic for simple log without date filtering\n        commits = list(repo.iter_commits(max_count=max_count))\n        log = []\n        for commit in commits:\n            log.append(\n                f\"Commit: {commit.hexsha!r}\\n\"\n                f\"Author: {commit.author!r}\\n\"\n                f\"Date: {commit.authored_datetime}\\n\"\n                f\"Message: {commit.message!r}\\n\"\n            )\n        return log\n\ndef git_create_branch(repo: git.Repo, branch_name: str, base_branch: str | None = None) -> str:\n    # Defense in depth: reject names starting with '-' to prevent flag injection\n    if branch_name.startswith(\"-\"):\n        raise BadName(f\"Invalid branch name: '{branch_name}' - cannot start with '-'\")\n    if base_branch and base_branch.startswith(\"-\"):\n        raise BadName(f\"Invalid base branch: '{base_branch}' - cannot start with '-'\")\n    if base_branch:\n        base = repo.references[base_branch]\n    else:\n        base = repo.active_branch\n\n    repo.create_head(branch_name, base)\n    return f\"Created branch '{branch_name}' from '{base.name}'\"\n\ndef git_checkout(repo: git.Repo, branch_name: str) -> str:\n    # Defense in depth: reject branch names starting with '-' to prevent flag injection,\n    # even if a malicious ref with that name exists (e.g. via filesystem manipulation)\n    if branch_name.startswith(\"-\"):\n        raise BadName(f\"Invalid branch name: '{branch_name}' - cannot start with '-'\")\n    repo.rev_parse(branch_name)  # Validates branch_name is a real git ref, throws BadName if not\n    repo.git.checkout(branch_name)\n    return f\"Switched to branch '{branch_name}'\"","sourceCodeStart":185,"sourceCodeEnd":221,"githubUrl":"https://github.com/modelcontextprotocol/servers/blob/76d64c822f5125032f89eb71dbdb94e42b434821/src/git/src/mcp_server_git/server.py#L185-L221","documentation":"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).","triggerScenarios":"Calling git_create_branch with branch_name beginning with '-'; malicious ref-name input.","commonSituations":"Adversarial input; UI allowing arbitrary branch names.","solutions":["Reject branch names starting with '-' (git also forbids refnames beginning with '-').","Validate the name against git's ref naming rules before calling."],"exampleFix":"# before\ngit_create_branch(repo, branch_name='-bMalicious')  # -> BadName\n\n# after\nif branch_name.startswith('-'):\n    raise ValueError('branch_name must not start with -')\ngit_create_branch(repo, branch_name)","handlingStrategy":"validation","validationCode":"def safe_branch(name: str) -> str:\n    if not name or name.startswith('-'):\n        raise ValueError('branch_name must not start with -')\n    return name","typeGuard":null,"tryCatchPattern":"from gitdb.exc import BadName\ntry:\n    git_create_branch(repo, branch_name)\nexcept BadName as e:\n    if 'cannot start with' in str(e):\n        # sanitize name and retry\n    raise","preventionTips":["Reject branch names starting with '-' at the input boundary.","Validate against git refname rules (no leading dash, no spaces, no '..')."],"tags":["git","python","security","flag-injection","validation"],"backgroundTag":null,"analyzedSha":"76d64c822f5125032f89eb71dbdb94e42b434821","analyzedAt":"2026-08-12T10:02:41.718Z","schemaVersion":2},"datasetVersion":"2026-08-12T13:17:24.610Z"}