{"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":188,"sourceCode":"        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}'\"\n\n","sourceCodeStart":170,"sourceCodeEnd":206,"githubUrl":"https://github.com/modelcontextprotocol/servers/blob/d73f99efbfd40c3aa1b61e88728b3d49fb52608f/src/git/src/mcp_server_git/server.py#L170-L206","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":"d73f99efbfd40c3aa1b61e88728b3d49fb52608f","analyzedAt":"2026-08-12T10:02:41.718Z","contentChangedAt":"2026-08-12T10:02:41.718Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}