From d9ddb55bdc66ffe8c9932fe460e6b8c8211e47c7 Mon Sep 17 00:00:00 2001
From: Byron <sebastian.thiel@icloud.com>
Date: Tue, 4 Aug 2026 02:48:01 +0000
Subject: [PATCH] Guard unsafe git init options

<!-- agent -->
GHSA-9rj7-rf2p-w77r reports that Repo.init forwarded git-init
options without applying GitPython's unsafe-option policy. A regression
showed template and abbreviated option spellings reached Git without an
UnsafeOptionError and could create the destination before validation.

Add a git-init denylist for template installation and separate Git
directory redirection, check keyword options before any path or directory
mutation, and provide the standard explicit allow_unsafe_options escape
hatch. This preserves trusted uses while rejecting untrusted forwarding by
default.

An audit against Git cf5497b14c5a24f10c13f7e0ee85cb95af13ea6a
(v2.55.0.windows.3-16-gcf5497b14c) confirmed that init and clone are the
built-in commands that consume repository template directories. Clone,
clone_from, and submodule cloning already share the guarded clone helper;
the similarly named commit option only reads a commit-message template.

Validated with the focused init regression, the clone/init unsafe-option
suite, 185 config/Git/index/clone tests, Ruff, and basedpyright.

Assisted-by: GPT 5.6
Co-authored-by: GPT 5.6 <codex@openai.com>
---
 git/repo/base.py  | 18 ++++++++++++++++++
 test/test_repo.py | 27 +++++++++++++++++++++++++++
 2 files changed, 45 insertions(+)

Index: GitPython-3.1.34.1693646983.2a2ae77/git/repo/base.py
===================================================================
--- GitPython-3.1.34.1693646983.2a2ae77.orig/git/repo/base.py
+++ GitPython-3.1.34.1693646983.2a2ae77/git/repo/base.py
@@ -137,6 +137,14 @@ class Repo(object):
     re_author_committer_start = re.compile(r"^(author|committer)")
     re_tab_full_line = re.compile(r"^\t(.*)$")
 
+    unsafe_git_init_options = [
+        # Can install hooks that execute during later Git commands:
+        "--template",
+        # Redirects the repository metadata to a caller-controlled path:
+        "--separate-git-dir",
+    ]
+    """Options to :manpage:`git-init(1)` that permit unsafe code execution or I/O."""
+
     unsafe_git_clone_options = [
         # This option allows users to execute arbitrary commands.
         # https://git-scm.com/docs/git-clone#Documentation/git-clone.txt---upload-packltupload-packgt
@@ -1187,6 +1195,7 @@ class Repo(object):
         mkdir: bool = True,
         odbt: Type[GitCmdObjectDB] = GitCmdObjectDB,
         expand_vars: bool = True,
+        allow_unsafe_options: bool = False,
         **kwargs: Any,
     ) -> "Repo":
         """Initialize a git repository at the given path if specified
@@ -1211,10 +1220,19 @@ class Repo(object):
             can lead to information disclosure, allowing attackers to
             access the contents of environment variables
 
+        :param allow_unsafe_options:
+            Allow unsafe options to be used, such as ``--template`` and
+            ``--separate-git-dir``.
+
         :param kwargs:
             keyword arguments serving as additional options to the git-init command
 
         :return: ``git.Repo`` (the newly created repo)"""
+        if not allow_unsafe_options:
+            Git.check_unsafe_options(
+                options=Git._option_candidates([], kwargs),
+                unsafe_options=cls.unsafe_git_init_options,
+            )
         if path:
             path = expand_path(path, expand_vars)
         if mkdir and path and not osp.exists(path):
Index: GitPython-3.1.34.1693646983.2a2ae77/test/test_repo.py
===================================================================
--- GitPython-3.1.34.1693646983.2a2ae77.orig/test/test_repo.py
+++ GitPython-3.1.34.1693646983.2a2ae77/test/test_repo.py
@@ -189,6 +189,33 @@ class TestRepo(TestBase):
             pass
         # END test repos with working tree
 
+    def test_init_rejects_unsafe_options(self):
+        with tempfile.TemporaryDirectory() as tdir:
+            template_dir = osp.join(tdir, "template")
+            os.mkdir(template_dir)
+            unsafe_options = [
+                {"template": template_dir},
+                {"templa": template_dir},
+                {"separate_git_dir": osp.join(tdir, "git-dir")},
+                {"separate_git_di": osp.join(tdir, "git-dir")},
+            ]
+            for index, kwargs in enumerate(unsafe_options):
+                repo_dir = osp.join(tdir, f"repo-{index}")
+                with self.assertRaises(UnsafeOptionError):
+                    Repo.init(repo_dir, **kwargs)
+                assert not osp.exists(repo_dir)
+
+    def test_init_allows_explicitly_unsafe_options(self):
+        with tempfile.TemporaryDirectory() as tdir:
+            template_dir = osp.join(tdir, "template")
+            os.mkdir(template_dir)
+            repo = Repo.init(
+                osp.join(tdir, "repo"),
+                template=template_dir,
+                allow_unsafe_options=True,
+            )
+            assert repo.git_dir
+
     @with_rw_directory
     def test_clone_from_keeps_env(self, rw_dir):
         original_repo = Repo.init(osp.join(rw_dir, "repo"))
