repos: refuse links anywhere in a git dir; check the directory again at clone time

- Any link in .git (objects, refs, logs, packed-refs, ...) refuses an import or update; git writes through them.
- A Repo's directory, and every directory between it and the media directory, must not be a link when it is cloned
  into or updated, not only when the Repo was added.
- Directories with control characters are refused; inspect's alternates names only the clone's own objects.
- Every git directory in .git/modules is sanitized, however deep (a planted HEAD can no longer hide one), and a
  submodule's .git file must point to one of them.
- sanitize_git_config checks every config before it rewrites any.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Roland 2026-10-07 09:54:41 -06:00
parent e2432d4ca9
commit b3f6fb144c
No known key found for this signature in database
GPG key ID: 1BA620CE6C9A4680
4 changed files with 240 additions and 45 deletions

View file

@ -234,7 +234,8 @@ async def test_inspect_never_reads_the_clones_config(test_session, test_director
def test_working_tree_git_file_is_read_like_git(tmp_path):
"""git trims only the line ending from a `.git` file, not spaces; so does WROLPi."""
repo = tmp_path / 'repo'
(repo / '.git/modules/library').mkdir(parents=True)
(repo / '.git/modules/library/objects').mkdir(parents=True)
(repo / '.git/modules/library/HEAD').write_text('ref: refs/heads/main\n')
(repo / 'vendor/library').mkdir(parents=True)
# git reads this as "<...>/library " (with the space): not the checked git directory.
(repo / 'vendor/library/.git').write_text('gitdir: ../../.git/modules/library/../../../.. \n')

View file

@ -186,10 +186,14 @@ def test_marker_and_config_do_not_follow_links(tmp_path):
assert not (git_dir / lib.REPO_MARKER).is_symlink()
assert (git_dir / lib.REPO_MARKER).read_text().strip() == 'a1b2c3'
# A git directory holding any link is refused.
(git_dir / 'config').write_text('[http]\n\tproxy = evil\n[core]\n\tbare = false\n')
(git_dir / 'config.wrolpi').symlink_to(victim)
lib.sanitize_git_config(repo)
with pytest.raises(ValueError, match='link'):
lib.sanitize_git_config(repo)
assert victim.read_text() == 'precious'
(git_dir / 'config.wrolpi').unlink()
lib.sanitize_git_config(repo)
assert 'proxy' not in (git_dir / 'config').read_text()
# A config which is a link is refused.
@ -405,7 +409,7 @@ def test_working_tree_git_files(tmp_path, plant):
"""A submodule's `.git` (in the working tree) decides which git directory git uses for it; it must be a file
pointing inside the repo's own .git/modules."""
repo = tmp_path / 'repo'
(repo / '.git/modules/library').mkdir(parents=True)
_real_submodule_git_dir(repo / '.git/modules/library')
(repo / 'vendor/library').mkdir(parents=True)
(repo / 'vendor/library/.git').write_text('gitdir: ../../.git/modules/library\n')
assert lib.working_tree_git_error(repo) is None
@ -446,3 +450,128 @@ async def test_update_refuses_planted_submodule_git_file(test_session, test_dire
assert download.status == DownloadStatus.deferred
assert 'outside' in download.error
assert list(evil.iterdir()) == []
def _real_submodule_git_dir(path):
(path / 'objects').mkdir(parents=True)
(path / 'refs').mkdir()
(path / 'HEAD').write_text('ref: refs/heads/main\n')
(path / 'config').write_text('[core]\n\tbare = false\n')
def test_planted_head_does_not_hide_a_submodule_git_dir(tmp_path):
"""git finds a submodule's git directory at .git/modules/<name>, however deep; a git directory planted on a path
prefix (or anywhere else) cannot hide it from being sanitized."""
repo = tmp_path / 'repo'
(repo / '.git').mkdir(parents=True)
(repo / '.git/config').write_text('[core]\n\tbare = false\n')
_real_submodule_git_dir(repo / '.git/modules/vendor')
real = repo / '.git/modules/vendor/library'
_real_submodule_git_dir(real)
(real / 'config').write_text('[protocol "file"]\n\tallow = always\n[url "file:///tmp/evil/"]\n'
'\tinsteadOf = https://example.com/\n')
hidden = repo / '.git/modules/vendor/refs/library'
_real_submodule_git_dir(hidden)
(hidden / 'config').write_text('[http]\n\tproxy = evil\n')
lib.sanitize_git_config(repo)
assert 'protocol' not in (real / 'config').read_text()
assert 'insteadOf' not in (real / 'config').read_text()
assert 'proxy' not in (hidden / 'config').read_text()
# A submodule's `.git` must point at one of the (sanitized) git directories.
(repo / 'vendor/library').mkdir(parents=True)
(repo / 'vendor/library/.git').write_text('gitdir: ../../.git/modules/vendor/library\n')
assert lib.working_tree_git_error(repo) is None
(repo / 'vendor/library/.git').write_text('gitdir: ../../.git/modules/vendor/library/objects\n')
assert lib.working_tree_git_error(repo)
@pytest.mark.parametrize('link', ['objects', 'refs', 'logs', 'packed-refs', 'refs/remotes', 'objects/pack'])
@pytest.mark.asyncio
async def test_links_in_git_dir_are_refused(test_session, test_directory, async_client, test_wrolpi_config,
git_remote_factory, repos_download_manager, link):
"""git writes through a link in a git directory (fetch writes objects, refs and logs), so a git directory
holding one is neither imported nor updated."""
from modules.repos.errors import InvalidRepo
import shutil
def plant(git_dir):
target = git_dir / link
outside = test_directory / f'outside-{link.replace("/", "-")}-{git_dir.parent.name}'
if target.exists():
shutil.move(target, outside)
else:
outside.write_text('')
target.symlink_to(outside)
git(test_directory, 'init', '--quiet', 'code/linked')
git(test_directory / 'code/linked', 'commit', '--quiet', '--allow-empty', '-m', 'x')
git(test_directory / 'code/linked', 'pack-refs', '--all')
git(test_directory / 'code/linked', 'gc', '--quiet')
plant(test_directory / 'code/linked/.git')
with pytest.raises(InvalidRepo, match='link'):
lib.inspect_import(test_session, 'code/linked')
remote = git_remote_factory()
repo = lib.create_repository(test_session, remote.url)
await repos_download_manager.wait_for_all_downloads()
git(repo.directory, 'pack-refs', '--all')
git(repo.directory, 'gc', '--quiet')
plant(repo.directory / '.git')
download = await update(test_session, repos_download_manager)
assert download.status == DownloadStatus.deferred
assert 'link' in download.error
@pytest.mark.asyncio
async def test_control_characters_in_directories_are_refused(test_session, test_directory, async_client,
test_wrolpi_config):
"""A newline would split a line git reads (e.g. alternates)."""
from modules.repos.errors import InvalidRepo
for directory in ('foo\n/tmp/abs', 'foo\rbar', 'tab\there'):
with pytest.raises(InvalidRepo, match='character'):
lib.resolve_new_repo_directory(test_session, directory)
@pytest.mark.parametrize('link_at', ['directory', 'parent'])
@pytest.mark.asyncio
async def test_clone_refuses_links_made_after_adding(test_session, test_directory, async_client, test_wrolpi_config,
git_remote_factory, repos_download_manager, tmp_path_factory,
link_at):
"""A repo's directory (or a parent) which became a link after the repo was added is not cloned into."""
remote = git_remote_factory()
lib.create_repository(test_session, remote.url, directory='projects/example')
outside = tmp_path_factory.mktemp('outside')
if link_at == 'directory':
(test_directory / 'projects').mkdir()
(test_directory / 'projects/example').symlink_to(outside)
else:
(test_directory / 'projects').symlink_to(outside)
await repos_download_manager.wait_for_all_downloads()
download = test_session.query(Download).one()
assert download.status == DownloadStatus.deferred
assert 'link' in download.error
assert list(outside.iterdir()) == []
@pytest.mark.asyncio
async def test_refused_sanitize_changes_nothing(test_session, test_directory, async_client, test_wrolpi_config):
"""Every refusal is found before any config is rewritten."""
from modules.repos.errors import InvalidRepo
git(test_directory, 'init', '--quiet', 'code/clone')
directory = test_directory / 'code/clone'
git(directory, 'commit', '--quiet', '--allow-empty', '-m', 'x')
git(directory, 'remote', 'add', 'origin', 'https://git.example.com/owner/clone')
with (directory / '.git/config').open('a') as fh:
fh.write('[http]\n\tproxy = http://127.0.0.1:9\n')
module = directory / '.git/modules/library'
_real_submodule_git_dir(module)
# Found only after the clone's own config was checked.
(module / 'commondir').write_text('../..\n')
before = (directory / '.git/config').read_text()
with pytest.raises(InvalidRepo):
await lib.import_repository(test_session, 'code/clone', confirm=True)
assert (directory / '.git/config').read_text() == before