repos: hold submodule git directories to the same rules as the repo's

The checks covered .git but not the submodules' git directories in
.git/modules: a linked submodule config was skipped (and still read by
git), and a submodule's commondir or alternates were not refused.  Every
git directory now needs a regular config and must not borrow another
directory, and nothing under .git/modules may be a link; otherwise the
update fails.  A git directory is one with HEAD and objects (a ref named
HEAD is not one).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Roland 2026-10-06 18:24:48 -06:00
parent d3cf1b7282
commit 9e8947767b
No known key found for this signature in database
GPG key ID: 1BA620CE6C9A4680
2 changed files with 73 additions and 5 deletions

View file

@ -125,6 +125,7 @@ def test_sanitize_git_config_submodules(tmp_path):
(evil / 'config').write_text('[core]\n\tworktree = /etc\n')
for gitdir in (module, evil):
(gitdir / 'HEAD').write_text('ref: refs/heads/main\n')
(gitdir / 'objects').mkdir()
lib.sanitize_git_config(repo)
assert 'worktree = ../../../vendor/library' in (module / 'config').read_text()
@ -254,3 +255,56 @@ async def test_clone_cannot_borrow_another_git_directory(test_session, test_dire
download = await update(test_session, repos_download_manager)
assert download.status == DownloadStatus.deferred
assert 'another git directory' in download.error
@pytest.mark.parametrize('plant', ['config-link', 'commondir', 'alternates', 'modules-link', 'gitdir-link'])
def test_submodule_git_directories_are_checked_too(tmp_path, plant):
"""A submodule's git directory is held to the same rules as the repo's: no linked config, no borrowing."""
repo = tmp_path / 'repo'
git_dir = repo / '.git'
git_dir.mkdir(parents=True)
(git_dir / 'config').write_text('[core]\n\tbare = false\n')
victim = tmp_path / 'victim'
victim.mkdir()
(victim / 'config').write_text('[core]\n\tfsmonitor = evil\n')
module = git_dir / 'modules/library'
(module / 'objects').mkdir(parents=True)
(module / 'HEAD').write_text('ref: refs/heads/main\n')
(module / 'config').write_text('[core]\n\tbare = false\n')
# A ref named HEAD is not a git directory.
(module / 'refs/remotes/origin').mkdir(parents=True)
(module / 'refs/remotes/origin/HEAD').write_text('ref: refs/remotes/origin/main\n')
if plant == 'config-link':
(module / 'config').unlink()
(module / 'config').symlink_to(victim / 'config')
elif plant == 'commondir':
(module / 'commondir').write_text(f'{victim}\n')
elif plant == 'alternates':
(module / 'objects/info').mkdir()
(module / 'objects/info/alternates').write_text(f'{victim}\n')
elif plant == 'modules-link':
import shutil
shutil.rmtree(git_dir / 'modules')
(git_dir / 'modules').symlink_to(victim)
elif plant == 'gitdir-link':
(git_dir / 'modules/other').symlink_to(victim)
with pytest.raises(ValueError):
lib.sanitize_git_config(repo)
assert (victim / 'config').read_text() == '[core]\n\tfsmonitor = evil\n'
def test_submodule_git_directories_pass(tmp_path):
repo = tmp_path / 'repo'
git_dir = repo / '.git'
module = git_dir / 'modules/library'
(module / 'objects').mkdir(parents=True)
(module / 'HEAD').write_text('ref: refs/heads/main\n')
(module / 'config').write_text('[core]\n\tbare = false\n[http]\n\tproxy = evil\n')
(module / 'refs/remotes/origin').mkdir(parents=True)
(module / 'refs/remotes/origin/HEAD').write_text('ref: refs/remotes/origin/main\n')
(git_dir / 'config').write_text('[core]\n\tbare = false\n')
lib.sanitize_git_config(repo)
assert 'proxy' not in (module / 'config').read_text()