From 1d51b891d7f236044a6aa17498ec682b63dad6e6 Mon Sep 17 00:00:00 2001
From: "GPT 5.6" <codex@openai.com>
Date: Wed, 22 Jul 2026 04:46:13 +0200
Subject: [PATCH] fix: guard diff output options

<!-- agent -->

Reject unsafe diff options before revision parsing or Git invocation so
callers cannot write command output to arbitrary filesystem paths.

Cover commit and index diffs, including option-like revisions, and
preserve an explicit allow_unsafe_options escape hatch.

References GHSA-fjr4-x663-mwxc.
Validated against Git baseline a23bace9.

Co-authored-by: Sebastian Thiel <sebastian.thiel@icloud.com>
---
 git/diff.py       | 14 ++++++++++++--
 git/index/base.py | 18 ++++++++++++++++--
 test/test_diff.py | 20 ++++++++++++++++++++
 3 files changed, 48 insertions(+), 4 deletions(-)

Index: GitPython-3.1.34.1693646983.2a2ae77/git/diff.py
===================================================================
--- GitPython-3.1.34.1693646983.2a2ae77.orig/git/diff.py
+++ GitPython-3.1.34.1693646983.2a2ae77/git/diff.py
@@ -5,7 +5,7 @@
 # the BSD License: http://www.opensource.org/licenses/bsd-license.php
 
 import re
-from git.cmd import handle_process_output
+from git.cmd import Git, handle_process_output
 from git.compat import defenc
 from git.util import finalize_process, hex_to_bin
 
@@ -36,7 +36,6 @@ if TYPE_CHECKING:
     from git.repo.base import Repo
     from git.objects.base import IndexObject
     from subprocess import Popen
-    from git import Git
 
 Lit_change_type = Literal["A", "D", "C", "M", "R", "T", "U"]
 
@@ -108,6 +107,7 @@ class Diffable(object):
         other: Union[Type["Index"], "Tree", "Commit", None, str, object] = Index,
         paths: Union[PathLike, List[PathLike], Tuple[PathLike, ...], None] = None,
         create_patch: bool = False,
+        allow_unsafe_options: bool = False,
         **kwargs: Any,
     ) -> "DiffIndex":
         """Creates diffs between two items being trees, trees and index or an
@@ -131,6 +131,10 @@ class Diffable(object):
             makes the self to other. Patches are somewhat costly as blobs have to be read
             and diffed.
 
+        :param allow_unsafe_options:
+            If ``True``, allow options such as ``--output`` that can write to arbitrary
+            filesystem paths.
+
         :param kwargs:
             Additional arguments passed to git-diff, such as
             R=True to swap both sides of the diff.
@@ -140,6 +144,12 @@ class Diffable(object):
         :note:
             On a bare repository, 'other' needs to be provided as Index or as
             as Tree/Commit, or a git command error will occur"""
+        if not allow_unsafe_options:
+            Git.check_unsafe_options(
+                options=Git._option_candidates([other], kwargs),
+                unsafe_options=self.repo.unsafe_git_revision_options,
+            )
+
         args: List[Union[PathLike, Diffable, Type["Diffable.Index"], object]] = []
         args.append("--abbrev=40")  # we need full shas
         args.append("--full-index")  # get full index paths, not only filenames
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
@@ -17,6 +17,7 @@ from git.compat import (
     force_bytes,
     defenc,
 )
+from git.cmd import Git
 from git.exc import GitCommandError, CheckoutError, GitError, InvalidGitRepositoryError
 from git.objects import (
     Blob,
@@ -1355,6 +1356,7 @@ class IndexFile(LazyMixin, git_diff.Diff
         other: Union[Type["git_diff.Diffable.Index"], "Tree", "Commit", str, None] = git_diff.Diffable.Index,
         paths: Union[PathLike, List[PathLike], Tuple[PathLike, ...], None] = None,
         create_patch: bool = False,
+        allow_unsafe_options: bool = False,
         **kwargs: Any,
     ) -> git_diff.DiffIndex:
         """Diff this index against the working copy or a Tree or Commit object
@@ -1366,6 +1368,11 @@ class IndexFile(LazyMixin, git_diff.Diff
             Will only work with indices that represent the default git index as
             they have not been initialized with a stream.
         """
+        if not allow_unsafe_options:
+            Git.check_unsafe_options(
+                options=Git._option_candidates([other], kwargs),
+                unsafe_options=self.repo.unsafe_git_revision_options,
+            )
 
         # only run if we are the default repository index
         if self._file_path != self._index_path():
@@ -1385,7 +1392,13 @@ class IndexFile(LazyMixin, git_diff.Diff
             # invert the existing R flag
             cur_val = kwargs.get("R", False)
             kwargs["R"] = not cur_val
-            return other.diff(self.Index, paths, create_patch, **kwargs)
+            return other.diff(
+                self.Index,
+                paths,
+                create_patch,
+                allow_unsafe_options=allow_unsafe_options,
+                **kwargs,
+            )
         # END diff against other item handling
 
         # if other is not None here, something is wrong
@@ -1393,4 +1406,4 @@ class IndexFile(LazyMixin, git_diff.Diff
             raise ValueError("other must be None, Diffable.Index, a Tree or Commit, was %r" % other)
 
         # diff against working copy - can be handled by superclass natively
-        return super(IndexFile, self).diff(other, paths, create_patch, **kwargs)
+        return super(IndexFile, self).diff(other, paths, create_patch, allow_unsafe_options=allow_unsafe_options, **kwargs)
Index: GitPython-3.1.34.1693646983.2a2ae77/test/test_diff.py
===================================================================
--- GitPython-3.1.34.1693646983.2a2ae77.orig/test/test_diff.py
+++ GitPython-3.1.34.1693646983.2a2ae77/test/test_diff.py
@@ -17,6 +17,7 @@ from git import (
     Submodule,
 )
 from git.cmd import Git
+from git.exc import UnsafeOptionError
 from test.lib import (
     TestBase,
     StringProcessAdapter,
@@ -357,6 +358,25 @@ class TestDiff(TestBase):
         self.assertIsInstance(diff.a_blob.size, int)
         self.assertIsInstance(diff.b_blob.size, int)
 
+    def test_diff_rejects_unsafe_output_options(self):
+        commit = self.rorepo.head.commit
+
+        calls = (
+            lambda target: commit.diff(output=target),
+            lambda target: commit.diff(other=f"--output={target}"),
+            lambda target: self.rorepo.index.diff(NULL_TREE, output=target),
+            lambda target: self.rorepo.index.diff(f"--output={target}"),
+        )
+        for index, call in enumerate(calls):
+            target = osp.join(self.repo_dir, f"diff-output-{index}")
+            with self.assertRaises(UnsafeOptionError):
+                call(target)
+            self.assertFalse(osp.exists(target))
+
+        allowed_target = osp.join(self.repo_dir, "allowed-diff-output")
+        commit.diff(output=allowed_target, allow_unsafe_options=True)
+        self.assertTrue(osp.isfile(allowed_target))
+
     def test_diff_interface(self):
         # test a few variations of the main diff routine
         assertion_map = {}
