mirror of
https://github.com/lrnselfreliance/wrolpi.git
synced 2026-11-25 01:16:18 -05:00
repos: validate branch names and repo directories
Branch names are passed to git as arguments and come from the API, the config, a `/tree/<branch>` URL, or the remote itself (its default branch). A name starting with `-` would be read as an option, so every branch must pass git's ref-name rules, including in the downloader just before use. A repo URL with a `.` or `..` path segment (or a `..` name) could format a directory outside its template (e.g. `repos/<tag>/..` is the whole repos directory). Such URLs and names are rejected, and every repo directory, including one from repos.yaml, must be strictly inside the media directory with no `..`. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
b3255e715c
commit
f49288b8ec
4 changed files with 145 additions and 7 deletions
|
|
@ -6,7 +6,7 @@ from modules.repos.models import Repository
|
|||
from wrolpi.collections.models import Collection
|
||||
from wrolpi.common import get_wrolpi_config
|
||||
from wrolpi.downloader import Download, DownloadFrequency
|
||||
from wrolpi.errors import UnknownDirectory
|
||||
from wrolpi.errors import UnknownDirectory, ValidationError
|
||||
from wrolpi.files.models import FileGroup
|
||||
|
||||
|
||||
|
|
@ -34,6 +34,9 @@ def test_parse_repo_url(url, expected):
|
|||
'https://user:token@github.com/kiwix/kiwix-tools',
|
||||
'https://github.com/kiwix/kiwix-tools?tab=readme',
|
||||
'https://github.com',
|
||||
'https://github.com/a/..',
|
||||
'https://github.com/../b',
|
||||
'https://github.com/a/./b',
|
||||
'',
|
||||
])
|
||||
def test_parse_repo_url_invalid(url):
|
||||
|
|
@ -208,3 +211,68 @@ async def test_repos_directory_is_ignored(test_session, test_directory, test_wro
|
|||
await refresh_files()
|
||||
|
||||
assert [i.primary_path.name for i in test_session.query(FileGroup)] == ['other.txt']
|
||||
|
||||
|
||||
@pytest.mark.parametrize('branch,valid', [
|
||||
('main', True),
|
||||
('feature/x-1', True),
|
||||
('release-1.2', True),
|
||||
('--upload-pack=touch /tmp/pwned', False),
|
||||
('-b', False),
|
||||
('a..b', False),
|
||||
('a b', False),
|
||||
('a~1', False),
|
||||
('a^', False),
|
||||
('a:b', False),
|
||||
('a?', False),
|
||||
('a*', False),
|
||||
('a[', False),
|
||||
('a\\b', False),
|
||||
('a@{1}', False),
|
||||
('@', False),
|
||||
('a/', False),
|
||||
('/a', False),
|
||||
('a//b', False),
|
||||
('a.lock', False),
|
||||
('a/.b', False),
|
||||
('a.', False),
|
||||
('a\x00b', False),
|
||||
('', False),
|
||||
])
|
||||
def test_is_valid_branch(branch, valid):
|
||||
assert lib.is_valid_branch(branch) is valid
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_invalid_branch_rejected(test_session, async_client, test_wrolpi_config):
|
||||
"""A branch is passed to git, it must never be read as an option."""
|
||||
with pytest.raises(ValidationError):
|
||||
lib.create_repository(test_session, 'https://github.com/a/b', branch='--upload-pack=x')
|
||||
with pytest.raises(ValidationError):
|
||||
lib.create_repository(test_session, 'https://github.com/a/b/tree/a..b')
|
||||
repo = lib.create_repository(test_session, 'https://github.com/a/b')
|
||||
with pytest.raises(ValidationError):
|
||||
lib.update_repository(test_session, repo.id, branch='-x')
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_repo_directory_traversal(test_session, test_directory, async_client, test_wrolpi_config):
|
||||
"""A Repo's directory is always strictly inside the media directory, and never escapes its template."""
|
||||
for name in ('..', '.', ' '):
|
||||
with pytest.raises(InvalidRepo):
|
||||
lib.create_repository(test_session, 'https://github.com/a/b', tag_name='tag', name=name)
|
||||
test_session.rollback()
|
||||
|
||||
with pytest.raises(InvalidRepo):
|
||||
lib.create_repository(test_session, 'https://github.com/a/b', directory=test_directory / 'repos/../../etc')
|
||||
test_session.rollback()
|
||||
with pytest.raises(InvalidRepo):
|
||||
lib.create_repository(test_session, 'https://github.com/a/b', directory=test_directory)
|
||||
test_session.rollback()
|
||||
|
||||
# The config can not move an existing Repo outside the media directory.
|
||||
lib.create_repository(test_session, 'https://github.com/a/b')
|
||||
with pytest.raises(InvalidRepo):
|
||||
lib.ReposConfig._import_repo(test_session, dict(url='https://github.com/a/b', directory='../outside'))
|
||||
test_session.rollback()
|
||||
assert test_session.query(Repository).one().directory == test_directory / 'repos/b'
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue