From 3af0c2516c5e18c829da30338614688f6b69b49c Mon Sep 17 00:00:00 2001
From: "GPT 5.6" <codex@openai.com>
Date: Sun, 26 Jul 2026 05:54:27 +0200
Subject: [PATCH] Block unsafe checkout-index and tag file options

GHSA-3f7w-8rr8-f37f reports that caller-controlled options forwarded by
IndexFile.checkout() and TagReference.create() can make Git write to or read
from arbitrary filesystem paths.

Add regression coverage proving the unsafe options are rejected by default while
preserving the explicit allow_unsafe_options escape hatch. Reuse GitPython's
existing option candidate normalization and unsafe-option guard at both public
API boundaries.

Git baseline: a23bace963d508bd96983cc637131392d3face18.
Documentation/git-checkout-index.adoc defines --prefix as prepending an output
path, and Documentation/git-tag.adoc defines -F/--file as reading a tag message
from a file.

Co-authored-by: Sebastian Thiel <sebastian.thiel@icloud.com>
---
 git/index/base.py  | 12 ++++++++++++
 git/refs/tag.py    | 13 +++++++++++++
 test/test_index.py | 10 ++++++++++
 test/test_refs.py  | 13 +++++++++++++
 4 files changed, 48 insertions(+)

Index: GitPython-3.1.34.1693646983.2a2ae77/git/index/base.py
===================================================================
--- GitPython-3.1.34.1693646983.2a2ae77.orig/git/index/base.py
+++ GitPython-3.1.34.1693646983.2a2ae77/git/index/base.py
@@ -118,6 +118,8 @@ class IndexFile(LazyMixin, git_diff.Diff
     Make sure you use index.write() once you are done manipulating the index directly
     before operating on it using the git command"""
 
+    unsafe_git_checkout_index_options = ["--prefix"]
+
     __slots__ = ("repo", "version", "entries", "_extension_data", "_file_path")
     _VERSION = 2  # latest version we support
     S_IFGITLINK = S_IFGITLINK  # a submodule
@@ -1100,6 +1102,7 @@ class IndexFile(LazyMixin, git_diff.Diff
         paths: Union[None, Iterable[PathLike]] = None,
         force: bool = False,
         fprogress: Callable = lambda *args: None,
+        allow_unsafe_options: bool = False,
         **kwargs: Any,
     ) -> Union[None, Iterator[PathLike], Sequence[PathLike]]:
         """Checkout the given paths or all files from the version known to the index into
@@ -1123,6 +1126,9 @@ class IndexFile(LazyMixin, git_diff.Diff
             explicit paths are given. Otherwise progress information will be send
             prior and after a file has been checked out
 
+        :param allow_unsafe_options:
+            Allow unsafe options, such as ``--prefix``.
+
         :param kwargs:
             Additional arguments to be passed to git-checkout-index
 
@@ -1144,6 +1150,12 @@ class IndexFile(LazyMixin, git_diff.Diff
             different to *head.checkout*, i.e. if you want git-checkout like behaviour,
             use head.checkout instead of index.checkout.
         """
+        if not allow_unsafe_options:
+            Git.check_unsafe_options(
+                options=Git._option_candidates([], kwargs),
+                unsafe_options=self.unsafe_git_checkout_index_options,
+            )
+
         args = ["--index"]
         if force:
             args.append("--force")
Index: GitPython-3.1.34.1693646983.2a2ae77/git/refs/tag.py
===================================================================
--- GitPython-3.1.34.1693646983.2a2ae77.orig/git/refs/tag.py
+++ GitPython-3.1.34.1693646983.2a2ae77/git/refs/tag.py
@@ -5,6 +5,8 @@ __all__ = ["TagReference", "Tag"]
 # typing ------------------------------------------------------------------
 
 from typing import Any, Type, Union, TYPE_CHECKING
+
+from git.cmd import Git
 from git.types import Commit_ish, PathLike
 
 if TYPE_CHECKING:
@@ -32,6 +34,9 @@ class TagReference(Reference):
         print(tagref.tag.message)"""
 
     __slots__ = ()
+
+    unsafe_git_tag_options = ["--file", "-F"]
+
     _common_default = "tags"
     _common_path_default = Reference._common_path_default + "/" + _common_default
 
@@ -81,6 +86,7 @@ class TagReference(Reference):
         reference: Union[str, "SymbolicReference"] = "HEAD",
         logmsg: Union[str, None] = None,
         force: bool = False,
+        allow_unsafe_options: bool = False,
         **kwargs: Any,
     ) -> "TagReference":
         """Create a new tag reference.
@@ -106,10 +112,19 @@ class TagReference(Reference):
         :param force:
             If True, to force creation of a tag even though that tag already exists.
 
+        :param allow_unsafe_options:
+            Allow unsafe options, such as ``--file``.
+
         :param kwargs:
             Additional keyword arguments to be passed to git-tag
 
         :return: A new TagReference"""
+        if not allow_unsafe_options:
+            Git.check_unsafe_options(
+                options=Git._option_candidates([], kwargs),
+                unsafe_options=cls.unsafe_git_tag_options,
+            )
+
         if "ref" in kwargs and kwargs["ref"]:
             reference = kwargs["ref"]
 
Index: GitPython-3.1.34.1693646983.2a2ae77/test/test_index.py
===================================================================
--- GitPython-3.1.34.1693646983.2a2ae77.orig/test/test_index.py
+++ GitPython-3.1.34.1693646983.2a2ae77/test/test_index.py
@@ -24,7 +24,7 @@ from git import (
     CheckoutError,
 )
 from git.compat import is_win
-from git.exc import HookExecutionError, InvalidGitRepositoryError
+from git.exc import HookExecutionError, InvalidGitRepositoryError, UnsafeOptionError
 from git.index.fun import hook_path
 from git.index.typ import BaseIndexEntry, IndexEntry
 from git.objects import Blob
@@ -58,6 +58,15 @@ def _make_hook(git_dir, name, content, m
 
 
 class TestIndex(TestBase):
+    @with_rw_repo("HEAD")
+    def test_checkout_rejects_unsafe_prefix(self, rw_repo):
+        with tempfile.TemporaryDirectory() as target:
+            with self.assertRaises(UnsafeOptionError):
+                rw_repo.index.checkout(prefix=f"{target}/")
+
+            rw_repo.index.checkout(prefix=f"{target}/", allow_unsafe_options=True)
+            self.assertTrue(osp.isfile(osp.join(target, "CHANGES")))
+
     def __init__(self, *args):
         super(TestIndex, self).__init__(*args)
         self._reset_progress()
@@ -953,4 +962,4 @@ class TestIndex(TestBase):
         file = git_dir / "file.txt"
         file.touch()
 
-        rw_repo.index.add(file)
\ No newline at end of file
+        rw_repo.index.add(file)
Index: GitPython-3.1.34.1693646983.2a2ae77/test/test_refs.py
===================================================================
--- GitPython-3.1.34.1693646983.2a2ae77.orig/test/test_refs.py
+++ GitPython-3.1.34.1693646983.2a2ae77/test/test_refs.py
@@ -20,6 +20,7 @@ from git import (
     RefLog,
     GitConfigParser,
 )
+from git.exc import UnsafeOptionError
 from git.objects.tag import TagObject
 from test.lib import TestBase, with_rw_repo
 from git.util import Actor
@@ -58,6 +59,18 @@ class TestRefs(TestBase):
         # works without path check
         TagReference(self.rorepo, "refs/invalid/tag", check_path=False)
 
+    @with_rw_repo("HEAD")
+    def test_tag_create_rejects_unsafe_file_options(self, rw_repo):
+        with tempfile.NamedTemporaryFile("w", encoding="utf-8") as message:
+            message.write("private tag message")
+            message.flush()
+            for index, option in enumerate(({"F": message.name}, {"file": message.name})):
+                with self.assertRaises(UnsafeOptionError):
+                    TagReference.create(rw_repo, f"unsafe-{index}", **option)
+
+            tag = TagReference.create(rw_repo, "allowed-file", F=message.name, allow_unsafe_options=True)
+            self.assertEqual(tag.tag.message, "private tag message")
+
     def test_tag_base(self):
         tag_object_refs = []
         for tag in self.rorepo.tags:
