{"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":186,"sourceCode":"        kwargs[\"since\"] = start_timestamp\n    if end_timestamp:\n        kwargs[\"until\"] = end_timestamp\n\n    commits = list(repo.iter_commits(**kwargs))\n    log = []\n    for commit in commits:\n        log.append(\n            f\"Commit: {commit.hexsha}\\n\"\n            f\"Author: {commit.author}\\n\"\n            f\"Date: {commit.authored_datetime}\\n\"\n            f\"Message: {commit.message}\\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":168,"sourceCodeEnd":204,"githubUrl":"https://github.com/modelcontextprotocol/servers/blob/d73f99efbfd40c3aa1b61e88728b3d49fb52608f/src/git/src/mcp_server_git/server.py#L168-L204","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":"d73f99efbfd40c3aa1b61e88728b3d49fb52608f","analyzedAt":"2026-08-12T10:02:41.718Z","contentChangedAt":"2026-08-12T10:02:41.718Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}