{"record":{"id":"cb0ebd95d3884403","repo":"modelcontextprotocol/servers","slug":"invalid-base-branch-base-branch-cannot-star","errorCode":null,"errorMessage":"Invalid base branch: '{base_branch}' - cannot start with '-'","messagePattern":"Invalid base branch: '(.+?)' - cannot start with '-'","errorType":"exception","errorClass":"BadName","httpStatus":null,"severity":"error","filePath":"src/git/src/mcp_server_git/server.py","lineNumber":205,"sourceCode":"    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}'\"\n\n","sourceCodeStart":187,"sourceCodeEnd":223,"githubUrl":"https://github.com/modelcontextprotocol/servers/blob/76d64c822f5125032f89eb71dbdb94e42b434821/src/git/src/mcp_server_git/server.py#L187-L223","documentation":"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.","triggerScenarios":"Calling git_create_branch with base_branch beginning with '-'; malicious or malformed ref input.","commonSituations":"Adversarial input; stale/mistyped base ref.","solutions":["Reject base_branch values starting with '-'.","Confirm the base branch exists (git_branch) before creating from it."],"exampleFix":"# before\ngit_create_branch(repo, 'feat', base_branch='-bMalicious')  # -> BadName\n\n# after\nif base_branch and base_branch.startswith('-'):\n    raise ValueError('base_branch must not start with -')\ngit_create_branch(repo, 'feat', base_branch)","handlingStrategy":"validation","validationCode":"def safe_base(base: str | None) -> str | None:\n    if base is not None and base.startswith('-'):\n        raise ValueError('base_branch must not start with -')\n    return base","typeGuard":null,"tryCatchPattern":"from gitdb.exc import BadName\ntry:\n    git_create_branch(repo, name, base_branch=base)\nexcept BadName as e:\n    if 'base branch' in str(e) and 'cannot start with' in str(e):\n        # sanitize or drop base and retry\n    raise","preventionTips":["Reject base_branch values starting with '-' at the input boundary.","Confirm the base branch exists via git_branch before creating from it."],"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"}