Skip to content

Commit 4299c99

Browse files
Byroncodex
andcommitted
Validate submodule names on every module path branch
<!-- agent --> _module_abspath only validated names when Git used separate gitfile submodule directories, so legacy Git behavior could accept the same invalid name. Validate before selecting the Git-version-dependent path branch and cover the legacy branch explicitly. Git baseline: submodule.c at 883a47ef6496c96a5d6132ed8c87fcd44ebf8d1a validates submodule paths before filesystem operations. Assisted-by: GPT 5.6 Co-authored-by: GPT 5.6 <codex@openai.com>
1 parent b324c83 commit 4299c99

2 files changed

Lines changed: 6 additions & 1 deletion

File tree

git/objects/submodule/base.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -316,8 +316,9 @@ def _validated_name(cls, name: str) -> str:
316316

317317
@classmethod
318318
def _module_abspath(cls, parent_repo: "Repo", path: PathLike, name: str) -> PathLike:
319+
name = cls._validated_name(name)
319320
if cls._need_gitfile_submodules(parent_repo.git):
320-
return osp.join(parent_repo.git_dir, "modules", cls._validated_name(name))
321+
return osp.join(parent_repo.git_dir, "modules", name)
321322
if parent_repo.working_tree_dir:
322323
return osp.join(parent_repo.working_tree_dir, path)
323324
raise NotADirectoryError()

test/test_submodule.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -971,6 +971,10 @@ def test_update_rejects_parent_component_in_name(self, rwdir):
971971
with pytest.raises(ValueError, match="submodule name"):
972972
Submodule._module_abspath(clone, "module", name)
973973

974+
with mock.patch.object(Submodule, "_need_gitfile_submodules", return_value=False):
975+
with pytest.raises(ValueError, match="submodule name"):
976+
Submodule._module_abspath(clone, "module", "../module")
977+
974978
@with_rw_directory
975979
@_patch_git_config("protocol.file.allow", "always")
976980
def test_root_update_keeps_going_after_invalid_submodule_name(self, rwdir):

0 commit comments

Comments
 (0)