mirror of
https://github.com/lrnselfreliance/wrolpi.git
synced 2026-12-04 13:49:57 -05:00
repos: a clone is owned by a random token, not its URL
The ownership marker held the Repo's URL key, which anyone can write, so a copied clone, an extracted archive or a hand-made `.git` passed the check and was reset and cleaned. Each Repo now has a random clone_token (repository column and repos.yaml, so a Repo restored from the config still owns its clone); the marker holds it, and only a clone holding the Repo's token is updated. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
a1d4c42eee
commit
d0ea6ab313
6 changed files with 116 additions and 20 deletions
|
|
@ -4,6 +4,8 @@ import ipaddress
|
|||
import os
|
||||
import pathlib
|
||||
import re
|
||||
import hmac
|
||||
import secrets
|
||||
import shutil
|
||||
import socket
|
||||
import stat
|
||||
|
|
@ -316,9 +318,13 @@ def git_env() -> dict:
|
|||
return env
|
||||
|
||||
|
||||
# WROLPi marks the clones it makes (and imports) with this file in `.git`; it holds the Repo's `repo_url_key`.
|
||||
# WROLPi marks the clones it makes (and imports) with this file in `.git`; it holds the Repo's `clone_token`.
|
||||
REPO_MARKER = 'wrolpi-repo'
|
||||
|
||||
|
||||
def new_clone_token() -> str:
|
||||
return secrets.token_hex(16)
|
||||
|
||||
# The only settings kept in a clone's config: anything else (include.path, url.*.insteadOf, http.proxy,
|
||||
# core.sshCommand, ...) could change what git runs or where it connects.
|
||||
ALLOWED_GIT_CONFIG_RE = re.compile(
|
||||
|
|
@ -339,8 +345,8 @@ def _borrowing_git_file(git_dir: pathlib.Path) -> Optional[str]:
|
|||
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.
|
||||
def clone_ownership_error(directory: pathlib.Path, clone_token: Optional[str]) -> Optional[str]:
|
||||
"""Why the clone in `directory` must not be updated as the Repo with `clone_token`, or None.
|
||||
|
||||
WROLPi resets and cleans its clones, so it only updates the ones it made (or imported) for this Repo."""
|
||||
git_dir = directory / '.git'
|
||||
|
|
@ -349,19 +355,20 @@ def clone_ownership_error(directory: pathlib.Path, url: str) -> Optional[str]:
|
|||
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):
|
||||
if not clone_token or marker.is_symlink() or not marker.is_file() \
|
||||
or not hmac.compare_digest(marker.read_text()[:100].strip(), clone_token):
|
||||
return f'{directory} was not created by WROLPi. Import it from the Repos page'
|
||||
return None
|
||||
|
||||
|
||||
def mark_clone(directory: pathlib.Path, url: str):
|
||||
def mark_clone(directory: pathlib.Path, clone_token: str):
|
||||
marker = directory / '.git' / REPO_MARKER
|
||||
# The clone may hold a link here (e.g. one being imported); never write through it.
|
||||
if marker.is_symlink():
|
||||
marker.unlink()
|
||||
fd = os.open(marker, os.O_WRONLY | os.O_CREAT | os.O_TRUNC | os.O_NOFOLLOW, 0o644)
|
||||
with os.fdopen(fd, 'w') as fh:
|
||||
fh.write(f'{repo_url_key(url)}\n')
|
||||
fh.write(f'{clone_token}\n')
|
||||
|
||||
|
||||
def _git_config_entries(config_file: pathlib.Path) -> List[Tuple[str, str]]:
|
||||
|
|
@ -653,10 +660,12 @@ def _validate_frequency(frequency: int):
|
|||
|
||||
def create_repository(session: Session, url: str, tag_name: str = None, frequency: int = DEFAULT_REPO_FREQUENCY,
|
||||
mode: str = 'full', branch: str = None, name: str = None, description: str = None,
|
||||
directory: pathlib.Path = None, submodules: bool = False) -> Repository:
|
||||
directory: pathlib.Path = None, submodules: bool = False,
|
||||
clone_token: str = None) -> Repository:
|
||||
"""Create a Repo (Collection + Repository + recurring Download). The download clones it."""
|
||||
_validate_frequency(frequency)
|
||||
repo = _create_repository_records(session, url, tag_name, mode, branch, name, description, directory, submodules)
|
||||
repo = _create_repository_records(session, url, tag_name, mode, branch, name, description, directory, submodules,
|
||||
clone_token)
|
||||
_create_repository_download(session, repo, frequency)
|
||||
return repo
|
||||
|
||||
|
|
@ -674,7 +683,7 @@ def _create_repository_download(session: Session, repo: Repository, frequency: i
|
|||
|
||||
def _create_repository_records(session: Session, url: str, tag_name: str, mode: str, branch: Optional[str],
|
||||
name: Optional[str], description: Optional[str], directory: Optional[pathlib.Path],
|
||||
submodules: bool) -> Repository:
|
||||
submodules: bool, clone_token: str = None) -> Repository:
|
||||
"""Create (but do not commit) a Repo's Collection and Repository, in their directory."""
|
||||
parsed = parse_repo_url(url)
|
||||
_validate_mode(mode)
|
||||
|
|
@ -695,7 +704,8 @@ def _create_repository_records(session: Session, url: str, tag_name: str, mode:
|
|||
session.add(collection)
|
||||
session.flush([collection])
|
||||
repo = Repository(url=parsed.url, host=parsed.host, owner=parsed.owner, mode=mode, branch=branch,
|
||||
submodules=bool(submodules), collection=collection)
|
||||
submodules=bool(submodules), clone_token=clone_token or new_clone_token(),
|
||||
collection=collection)
|
||||
repo.update_search_name()
|
||||
session.add(repo)
|
||||
session.flush([repo])
|
||||
|
|
@ -873,7 +883,7 @@ async def import_repository(session: Session, directory: str, url: str = None, c
|
|||
|
||||
await asyncio.to_thread(sanitize_git_config, destination)
|
||||
await asyncio.to_thread(_git_output_sync, destination, 'config', '--replace-all', 'remote.origin.url', parsed.url)
|
||||
mark_clone(destination, parsed.url)
|
||||
mark_clone(destination, repo.clone_token)
|
||||
|
||||
for key, value in _read_head_commit(destination).items():
|
||||
setattr(repo, key, value)
|
||||
|
|
@ -1251,7 +1261,8 @@ class ReposConfig(ConfigFile):
|
|||
if not repo:
|
||||
return create_repository(session, parsed.url, tag_name=tag_name, frequency=frequency, mode=mode,
|
||||
branch=data.get('branch'), name=name, description=data.get('description'),
|
||||
directory=directory, submodules=bool(data.get('submodules')))
|
||||
directory=directory, submodules=bool(data.get('submodules')),
|
||||
clone_token=data.get('clone_token'))
|
||||
|
||||
collection = repo.collection
|
||||
collection.name = name
|
||||
|
|
@ -1262,6 +1273,8 @@ class ReposConfig(ConfigFile):
|
|||
collection.directory = directory
|
||||
repo.mode = mode
|
||||
repo.submodules = bool(data.get('submodules'))
|
||||
if data.get('clone_token'):
|
||||
repo.clone_token = data['clone_token']
|
||||
branch = data.get('branch') or None
|
||||
_validate_branch(branch)
|
||||
repo.branch = branch
|
||||
|
|
@ -1291,6 +1304,8 @@ class ReposConfig(ConfigFile):
|
|||
branch=repo.branch,
|
||||
submodules=repo.submodules,
|
||||
frequency=download.frequency if download else None,
|
||||
# Restoring a Repo from the config keeps its clone.
|
||||
clone_token=repo.clone_token,
|
||||
)
|
||||
data.append(entry)
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue