From 1ed0ebc2f2e74d979cdc367a4864a7731fdcc093 Mon Sep 17 00:00:00 2001
From: Byron <sebastian.thiel@icloud.com>
Date: Tue, 1 Sep 2026 11:33:19 +0200
Subject: [PATCH] Reject submodule checkout paths outside the repository

GHSA-59cr-6r3x-644w identifies that submodule update paths could reach
filesystem operations without the containment check already used by add and
move.

Add a regression that proves update rejects a parent-directory checkout
path before cloning, and override Submodule.abspath to apply the shared
_to_relative_path guard for every filesystem consumer.

Git baseline: git.git read-cache.c verify_path_internal() rejects invalid index
paths, covered for parent traversal by t/t9300-fast-import.sh.

Assisted-by: GPT 5.6
Co-authored-by: GPT 5.6 <codex@openai.com>
---
 doc/source/changes.rst        | 13 ++++++++++++
 git/objects/submodule/base.py | 12 +++++++++++
 test/test_submodule.py        | 40 +++++++++++++++++++++++++++++++++++
 3 files changed, 65 insertions(+)

diff --git a/git/objects/submodule/base.py b/git/objects/submodule/base.py
index 39e912321..563b20a18 100644
--- a/git/objects/submodule/base.py
+++ b/git/objects/submodule/base.py
@@ -414,6 +414,18 @@ def _to_relative_path(cls, parent_repo: "Repo", path: PathLike) -> PathLike:
 
         return path
 
+    @property
+    def abspath(self) -> PathLike:
+        root = self.repo.working_tree_dir
+        if root is None:
+            return super().abspath
+        path = root
+        for component in os.fspath(self._to_relative_path(self.repo, self.path)).split("/"):
+            path = join_path_native(path, component)
+            if osp.islink(path):
+                raise ValueError("Submodule checkout path %r contains a symbolic link" % self.path)
+        return path
+
     @classmethod
     def _write_git_file_and_module_config(cls, working_tree_dir: PathLike, module_abspath: PathLike) -> None:
         """Write a ``.git`` file containing a (preferably) relative path to the actual
diff --git a/test/test_submodule.py b/test/test_submodule.py
index d545cc8d5..ca9078aac 100644
--- a/test/test_submodule.py
+++ b/test/test_submodule.py
@@ -1369,6 +1369,46 @@ class Repo:
             osp.join(Repo.working_tree_dir + "-other", "module"),
         )
 
+    @with_rw_directory
+    def test_update_rejects_checkout_path_outside_parent(self, rwdir):
+        parent = git.Repo.init(osp.join(rwdir, "parent"))
+        submodule = Submodule(
+            parent,
+            Submodule.NULL_BIN_SHA,
+            name="module",
+            path=osp.join("..", "outside"),
+            url="unused",
+        )
+
+        with mock.patch.object(Submodule, "_clone_repo", side_effect=AssertionError("clone attempted")):
+            with pytest.raises(ValueError, match="is not in repository"):
+                submodule.update(init=True)
+
+    @with_rw_directory
+    def test_update_rejects_checkout_path_through_symlink(self, rwdir):
+        parent = git.Repo.init(osp.join(rwdir, "parent"))
+        os.mkdir(osp.join(parent.working_tree_dir, "target"))
+        os.symlink("target", osp.join(parent.working_tree_dir, "link"))
+        submodule = Submodule(
+            parent,
+            Submodule.NULL_BIN_SHA,
+            name="module",
+            path=osp.join("link", "module"),
+            url="unused",
+        )
+
+        with mock.patch.object(Submodule, "_clone_repo", side_effect=AssertionError("clone attempted")):
+            with pytest.raises(ValueError, match="contains a symbolic link"):
+                submodule.update(init=True)
+
+    @with_rw_directory
+    def test_update_rejects_checkout_path_at_parent_root(self, rwdir):
+        parent = git.Repo.init(osp.join(rwdir, "parent"))
+        submodule = Submodule(parent, Submodule.NULL_BIN_SHA, name="module", path=".", url="unused")
+
+        with pytest.raises(ValueError, match="must not be the repository root"):
+            submodule.update(init=True)
+
     @skipUnless(sys.platform == "win32", "Specifically for Windows.")
     @with_rw_directory
     def test_to_relative_path_windows_path_kinds(self, rwdir):
