GeezerVison is showing github.com — Validate submodule names before filesystem operations by Byron · Pull Request #2202 · gitpython-developers/GitPython
Validate submodule names before filesystem operations
#2202
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and
privacy statement . We’ll occasionally send you account related emails.
Already on GitHub?
Sign in
to your account
Uh oh!
There was an error while loading. Please reload this page.
Show file tree
Hide file tree
Changes from all commits
Commits
File filter
Filter by extension
.py
(2)
All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Hide whitespace
Diff view
Hide whitespace
There are no files selected for viewing
Original file line number
Diff line number
Diff line change
Expand Up
@@ -6,6 +6,7 @@
import gc
from io import BytesIO
import logging
import ntpath
import os
import os.path as osp
import stat
Expand Down
Expand Up
@@ -302,10 +303,21 @@ def _config_parser_constrained(self, read_only: bool) -> SectionConstraint:
parser.set_submodule(self)
return SectionConstraint(parser, sm_section(self.name))
@classmethod
def _validated_name(cls, name: str) -> str:
if (
not name
or name.startswith(("/", "\\"))
or ntpath.splitdrive(name)[0]
or ".." in name.replace("\\", "/").split("/")
):
raise ValueError("Invalid submodule name %r" % name)
return name
@classmethod
def _module_abspath(cls, parent_repo: "Repo", path: PathLike, name: str) -> PathLike:
if cls._need_gitfile_submodules(parent_repo.git):
return osp.join(parent_repo.git_dir, "modules", name)
return osp.join(parent_repo.git_dir, "modules", cls._validated_name( name) )
if parent_repo.working_tree_dir:
return osp.join(parent_repo.working_tree_dir, path)
Comment thread
Byron marked this conversation as resolved.
Resolved
Uh oh!
There was an error while loading. Please reload this page.
raise NotADirectoryError()
Expand Down
Expand Up
@@ -523,6 +535,7 @@ def add(
raise InvalidGitRepositoryError("Cannot add submodules to bare repositories")
# END handle bare repos
cls._validated_name(name)
path = cls._to_relative_path(repo, path)
# Ensure we never put backslashes into the URL, as might happen on Windows.
Expand Down
Expand Up
@@ -771,6 +784,8 @@ def fetch_remotes(module_repo: "Repo") -> None:
# END fetch new data
try:
self._validated_name(self.name)
Comment thread
Byron marked this conversation as resolved.
Resolved
Uh oh!
There was an error while loading. Please reload this page.
# ENSURE REPO IS PRESENT AND UP-TO-DATE
#######################################
try:
Expand Down
Expand Up
@@ -1020,6 +1035,7 @@ def move(self, module_path: PathLike, configuration: bool = True, module: bool =
raise ValueError("You must specify to move at least the module or the configuration of the submodule")
# END handle input
self._validated_name(self.name)
module_checkout_path = self._to_relative_path(self.repo, module_path)
# VERIFY DESTINATION
Expand Down
Expand Up
@@ -1160,6 +1176,7 @@ def remove(
raise ValueError("Need to specify to delete at least the module, or the configuration")
# END handle parameters
self._validated_name(self.name)
# Recursively remove children of this submodule.
nc = 0
for csm in self.children():
Expand Down
Expand Up
@@ -1416,6 +1433,9 @@ def rename(self, new_name: str) -> "Submodule":
if self.name == new_name:
return self
self._validated_name(self.name)
self._validated_name(new_name)
# .git/config
with self.repo.config_writer() as pw:
# As we ourselves didn't write anything about submodules into the parent
Expand Down
Expand Up
@@ -1463,6 +1483,7 @@ def module(self) -> "Repo":
If a repository was not available.
This could also mean that it was not yet initialized.
"""
self._validated_name(self.name)
module_checkout_abspath = self.abspath
try:
repo = git.Repo(module_checkout_abspath)
Expand Down
Original file line number
Diff line number
Diff line change
Expand Up
@@ -925,6 +925,75 @@ def test_update_submodule_with_relative_path(self, rwdir):
cloned_repo.submodule_update(init=True, recursive=True)
@with_rw_directory
@_patch_git_config("protocol.file.allow", "always")
def test_update_rejects_parent_component_in_name(self, rwdir):
source = git.Repo.init(osp.join(rwdir, "source"))
source.git.commit(m="initial commit", allow_empty=True)
parent = git.Repo.init(osp.join(rwdir, "parent"))
parent.git.submodule("add", source.working_tree_dir, "module")
parent.index.commit("add submodule")
modules_file = Path(parent.working_tree_dir) / ".gitmodules"
modules_file.write_text(
modules_file.read_text().replace('submodule "module"', 'submodule "../../../escaped/module"')
)
parent.index.add([".gitmodules"])
parent.index.commit("change submodule name")
clone = git.Repo.clone_from(parent.working_tree_dir, osp.join(rwdir, "clone"))
with pytest.raises(ValueError, match="submodule name"):
clone.submodules[0].update(init=True)
assert not osp.exists(osp.join(rwdir, "escaped"))
Path(rwdir, "escaped").mkdir()
git.Repo.clone_from(
source.working_tree_dir,
osp.join(clone.working_tree_dir, "module"),
separate_git_dir=osp.join(rwdir, "escaped", "module"),
)
with pytest.raises(ValueError, match="submodule name"):
clone.submodules[0].update(init=True)
invalid_names = (
"",
"..",
"../module",
R"..\module",
"nested/../module",
R"nested\..\module",
"/module",
R"\module",
"C:module",
R"C:\module",
)
for name in invalid_names:
with pytest.raises(ValueError, match="submodule name"):
Submodule._module_abspath(clone, "module", name)
@with_rw_directory
@_patch_git_config("protocol.file.allow", "always")
def test_root_update_keeps_going_after_invalid_submodule_name(self, rwdir):
source = git.Repo.init(osp.join(rwdir, "source"))
source.git.commit(m="initial commit", allow_empty=True)
parent = git.Repo.init(osp.join(rwdir, "parent"))
parent.git.submodule("add", source.working_tree_dir, "invalid")
parent.git.submodule("add", source.working_tree_dir, "valid")
modules_file = Path(parent.working_tree_dir) / ".gitmodules"
modules_file.write_text(modules_file.read_text().replace('submodule "invalid"', 'submodule "../invalid"'))
parent.index.add([".gitmodules"])
parent.index.commit("add submodules")
clone = git.Repo.clone_from(parent.working_tree_dir, osp.join(rwdir, "clone"))
assert [sm.name for sm in clone.submodules] == ["../invalid", "valid"]
clone.submodule_update(keep_going=True)
assert os.listdir(osp.join(clone.working_tree_dir, "invalid")) == []
assert not clone.submodule("../invalid").module_exists()
assert clone.submodule("valid").module_exists()
@with_rw_directory
@_patch_git_config("protocol.file.allow", "always")
def test_list_only_valid_submodules(self, rwdir):
Expand Down
Loading
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.