mirror of
https://github.com/lrnselfreliance/wrolpi.git
synced 2026-11-21 23:46:18 -05:00
repos: close the remaining ways a clone's .git can redirect git
* A clone with .git/commondir (git then reads another directory's config, refs and objects, so the sanitized config is not the one read) or alternates is neither imported nor updated. * branch.*.remote/merge are no longer kept: WROLPi checks out with -B and needs neither, and branch.<name>.remote can be a URL git uses to resolve relative submodule URLs. * An import directory is resolved and confined to the media directory before anything about it is reported, so a link cannot reveal whether a path outside exists. * git_command also turns off automatic maintenance (gc.auto, maintenance.auto) and core.alternateRefsCommand. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
a79278e048
commit
d3cf1b7282
2 changed files with 80 additions and 9 deletions
|
|
@ -280,6 +280,10 @@ def git_command(*args, config: tuple = ()) -> tuple:
|
|||
# Symlinks are checked out as plain files containing the link's target.
|
||||
'-c', 'core.symlinks=false',
|
||||
'-c', 'core.fsmonitor=false',
|
||||
'-c', 'core.alternateRefsCommand=',
|
||||
# Never start maintenance (which runs in the background) on WROLPi's behalf.
|
||||
'-c', 'gc.auto=0',
|
||||
'-c', 'maintenance.auto=false',
|
||||
# The media directory may be owned by another user (e.g. a Docker volume).
|
||||
'-c', 'safe.directory=*',
|
||||
'-c', 'credential.helper=',
|
||||
|
|
@ -319,10 +323,22 @@ REPO_MARKER = 'wrolpi-repo'
|
|||
# core.sshCommand, ...) could change what git runs or where it connects.
|
||||
ALLOWED_GIT_CONFIG_RE = re.compile(
|
||||
r'^(core\.(repositoryformatversion|filemode|bare|logallrefupdates|ignorecase|precomposeunicode|symlinks)'
|
||||
r'|extensions\.objectformat|remote\.origin\.(url|fetch)|branch\..+\.(remote|merge)'
|
||||
r'|extensions\.objectformat|remote\.origin\.(url|fetch)'
|
||||
r'|submodule\..+\.(url|active))$', re.IGNORECASE)
|
||||
|
||||
|
||||
# Files which make git use another directory: commondir (its config, refs and objects, so the sanitized config would
|
||||
# not be the one read) and alternates (its objects). A clone WROLPi makes has neither.
|
||||
BORROWING_GIT_FILES = ('commondir', 'objects/info/alternates', 'objects/info/http-alternates')
|
||||
|
||||
|
||||
def _borrowing_git_file(git_dir: pathlib.Path) -> Optional[str]:
|
||||
for name in BORROWING_GIT_FILES:
|
||||
if (git_dir / name).exists() or (git_dir / name).is_symlink():
|
||||
return name
|
||||
return None
|
||||
|
||||
|
||||
def clone_ownership_error(directory: pathlib.Path, url: str) -> Optional[str]:
|
||||
"""Why the clone in `directory` must not be updated as the Repo with `url`, or None.
|
||||
|
||||
|
|
@ -330,6 +346,8 @@ def clone_ownership_error(directory: pathlib.Path, url: str) -> Optional[str]:
|
|||
git_dir = directory / '.git'
|
||||
if git_dir.is_symlink() or not git_dir.is_dir():
|
||||
return f'{directory} is not a git directory'
|
||||
if borrowed := _borrowing_git_file(git_dir):
|
||||
return f'{directory} uses another git directory (.git/{borrowed})'
|
||||
marker = git_dir / REPO_MARKER
|
||||
if marker.is_symlink() or not marker.is_file() or marker.read_text().strip() != repo_url_key(url):
|
||||
return f'{directory} was not created by WROLPi. Import it from the Repos page'
|
||||
|
|
@ -696,12 +714,13 @@ def resolve_import_directory(session: Session, directory: str) -> pathlib.Path:
|
|||
relative = pathlib.Path((directory or '').strip())
|
||||
if not str(relative) or relative.is_absolute() or '..' in relative.parts:
|
||||
raise InvalidRepo('The directory must be relative to the media directory')
|
||||
path = media_directory / relative
|
||||
if path.is_symlink() or not path.is_dir():
|
||||
raise UnknownDirectory(f'No directory {directory!r}')
|
||||
path = path.resolve()
|
||||
# Resolved (and confined to the media directory) before anything about it is reported, so a link cannot reveal
|
||||
# what exists elsewhere.
|
||||
path = (media_directory / relative).resolve()
|
||||
if path == media_directory or media_directory not in path.parents:
|
||||
raise InvalidRepo('The directory must be in the media directory')
|
||||
if not path.is_dir():
|
||||
raise UnknownDirectory(f'No directory {directory!r}')
|
||||
|
||||
# Imported repos are moved, so no special directory (or anything holding one) may be imported.
|
||||
from wrolpi.files.lib import get_special_directories
|
||||
|
|
@ -721,6 +740,8 @@ def resolve_import_directory(session: Session, directory: str) -> pathlib.Path:
|
|||
config = git_dir / 'config'
|
||||
if config.is_symlink() or not config.is_file():
|
||||
raise InvalidRepo(f'{directory} cannot be imported: its .git/config is not a file')
|
||||
if borrowed := _borrowing_git_file(git_dir):
|
||||
raise InvalidRepo(f'{directory} cannot be imported: it uses another git directory (.git/{borrowed})')
|
||||
return path
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -97,6 +97,7 @@ async def test_update_sanitizes_git_config(test_session, git_remote_factory, rep
|
|||
'[http]\n\tproxy = http://127.0.0.1:9\n'
|
||||
'[include]\n\tpath = /etc/evil.gitconfig\n'
|
||||
'[core]\n\tsshCommand = evil\n\tworktree = /etc\n\tfsmonitor = evil\n'
|
||||
'[branch "main"]\n\tremote = https://127.0.0.1/evil\n'
|
||||
))
|
||||
head = remote.commit({'new.txt': 'new'}, 'New')
|
||||
download = await update(test_session, repos_download_manager)
|
||||
|
|
@ -106,7 +107,7 @@ async def test_update_sanitizes_git_config(test_session, git_remote_factory, rep
|
|||
keys = set(git(repo.directory, 'config', '--local', '--name-only', '--list').splitlines())
|
||||
assert {'remote.origin.url', 'remote.origin.fetch', 'core.repositoryformatversion'} <= keys
|
||||
assert not {'url.https://127.0.0.1/.insteadof', 'http.proxy', 'include.path', 'core.sshcommand',
|
||||
'core.worktree', 'core.fsmonitor'} & keys
|
||||
'core.worktree', 'core.fsmonitor', 'branch.main.remote'} & keys
|
||||
|
||||
|
||||
def test_sanitize_git_config_submodules(tmp_path):
|
||||
|
|
@ -142,7 +143,7 @@ async def test_update_with_duplicate_and_option_like_settings(test_session, git_
|
|||
config.write_text(config.read_text() + (
|
||||
'[remote "origin"]\n\turl = --file=/tmp/evil\n\turl = https://127.0.0.1/evil\n'
|
||||
'\tfetch = +refs/heads/*:refs/heads/*\n'
|
||||
'[branch "main"]\n\tmerge = "quoted \\"value\\" \\\\ and\\ttab"\n'
|
||||
'[submodule "lib"]\n\turl = "quoted \\"value\\" \\\\ and\\ttab"\n'
|
||||
))
|
||||
head = remote.commit({'new.txt': 'new'}, 'New')
|
||||
download = await update(test_session, repos_download_manager)
|
||||
|
|
@ -159,10 +160,10 @@ def test_sanitized_config_round_trips(tmp_path):
|
|||
values = ['--file=/tmp/evil', 'a "quoted" value', 'back\\slash', 'tab\there', ' leading space', 'semi;colon#hash']
|
||||
subprocess.run(['git', 'config', '--file', str(repo / '.git/config'), 'http.proxy', 'evil'], check=True)
|
||||
for value in values:
|
||||
subprocess.run(['git', 'config', '--file', str(repo / '.git/config'), '--add', 'branch.main.merge', value],
|
||||
subprocess.run(['git', 'config', '--file', str(repo / '.git/config'), '--add', 'submodule.lib.url', value],
|
||||
check=True)
|
||||
lib.sanitize_git_config(repo)
|
||||
result = git(repo, 'config', '--file', '.git/config', '--get-all', 'branch.main.merge')
|
||||
result = git(repo, 'config', '--file', '.git/config', '--get-all', 'submodule.lib.url')
|
||||
assert result.split('\n') == values
|
||||
assert 'proxy' not in (repo / '.git/config').read_text()
|
||||
|
||||
|
|
@ -204,3 +205,52 @@ async def test_import_refuses_linked_config(test_session, test_directory, async_
|
|||
(directory / '.git/config').symlink_to(test_directory / 'elsewhere')
|
||||
with pytest.raises(InvalidRepo, match='config'):
|
||||
lib.inspect_import(test_session, 'code/linked')
|
||||
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_import_directory_is_not_an_oracle(test_session, test_directory, async_client, test_wrolpi_config,
|
||||
tmp_path_factory):
|
||||
"""A link in the media directory cannot reveal whether a path outside it exists."""
|
||||
from modules.repos.errors import InvalidRepo
|
||||
outside = tmp_path_factory.mktemp('outside')
|
||||
(outside / 'exists').mkdir()
|
||||
(test_directory / 'code').mkdir()
|
||||
(test_directory / 'code/link').symlink_to(outside)
|
||||
for directory in ('code/link/exists', 'code/link/missing'):
|
||||
with pytest.raises(InvalidRepo, match='must be in the media directory'):
|
||||
lib.resolve_import_directory(test_session, directory)
|
||||
|
||||
|
||||
def test_git_command_disables_automatic_programs():
|
||||
cmd = lib.git_command('rev-parse', 'HEAD')
|
||||
for setting in ('gc.auto=0', 'maintenance.auto=false', 'core.alternateRefsCommand=', 'core.fsmonitor=false',
|
||||
'core.hooksPath=/dev/null'):
|
||||
assert setting in cmd, setting
|
||||
|
||||
|
||||
@pytest.mark.parametrize('planted', ['commondir', 'objects/info/alternates'])
|
||||
@pytest.mark.asyncio
|
||||
async def test_clone_cannot_borrow_another_git_directory(test_session, test_directory, async_client,
|
||||
test_wrolpi_config, git_remote_factory,
|
||||
repos_download_manager, planted):
|
||||
"""`.git/commondir` makes git read another directory's config (bypassing the sanitized one), and alternates
|
||||
read its objects; neither is in a clone WROLPi makes, so neither is imported or updated."""
|
||||
from modules.repos.errors import InvalidRepo
|
||||
elsewhere = test_directory / 'elsewhere'
|
||||
elsewhere.mkdir()
|
||||
|
||||
# Import.
|
||||
git(test_directory, 'init', '--quiet', 'code/borrowing')
|
||||
(test_directory / 'code/borrowing/.git' / planted).write_text(f'{elsewhere}\n')
|
||||
with pytest.raises(InvalidRepo, match='another git directory'):
|
||||
lib.inspect_import(test_session, 'code/borrowing')
|
||||
|
||||
# Update.
|
||||
remote = git_remote_factory()
|
||||
repo = lib.create_repository(test_session, remote.url)
|
||||
await repos_download_manager.wait_for_all_downloads()
|
||||
(repo.directory / '.git' / planted).write_text(f'{elsewhere}\n')
|
||||
download = await update(test_session, repos_download_manager)
|
||||
assert download.status == DownloadStatus.deferred
|
||||
assert 'another git directory' in download.error
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue