"""User ISO catalog and remote-download security tests.""" from __future__ import annotations import hashlib import json import os import socket from pathlib import Path from types import SimpleNamespace from unittest.mock import patch import pytest from virsh_panel.cloud_templates import TemplateError from virsh_panel.user_isos import ( MAX_USER_ISO_BYTES, MAX_USER_ISOS, MAX_USER_ISO_TOTAL_BYTES, PERSISTENT_FREE_RESERVE_BYTES, UserISOFiles, UserISORuntime, UserISOStore, _resolve_global_addresses, download_user_iso, user_iso_scope, user_iso_volume_name, validate_user_iso_url, verify_user_iso, ) class MemoryStore: def __init__(self): self.records = {} def get(self, kind, key): return self.records.get((kind, key)) def set(self, kind, key, value): self.records[kind, key] = value def insert(self, kind, key, value): if (kind, key) in self.records: return False self.records[kind, key] = value return True def delete(self, kind, key): self.records.pop((kind, key), None) def list(self, kind): return {key: value for (row_kind, key), value in self.records.items() if row_kind == kind} def bootable_iso() -> bytes: image = bytearray(1024 * 1024) boot = memoryview(image)[16 * 2048:17 * 2048] boot[0] = 0 boot[1:6] = b"CD001" boot[6] = 1 boot[7:30] = b"EL TORITO SPECIFICATION" boot[71:75] = (20).to_bytes(4, "little") end = memoryview(image)[17 * 2048:18 * 2048] end[0] = 255 end[1:6] = b"CD001" end[6] = 1 return bytes(image) def record(number: int, *, size: int = 1024 * 1024) -> dict: iso_id = f"{number:032x}" return { "id": iso_id, "name": f"Installer {number}", "volume_name": user_iso_volume_name(iso_id), "size_bytes": size, "sha256": f"{number:064x}", "created_at": number, } def test_catalog_is_owner_scoped_and_enforces_count_quota(): store = UserISOStore(None) first = user_iso_scope(41) second = user_iso_scope(42) store.add(first, record(1)) assert store.get(first, f"{1:032x}")["name"] == "Installer 1" assert store.list(second) == [] with pytest.raises(TemplateError): store.get(second, f"{1:032x}") for number in range(2, MAX_USER_ISOS + 1): store.add(first, record(number)) with pytest.raises(TemplateError, match="最多保存 5 个"): store.remaining_bytes(first) def test_catalog_enforces_10_gib_total_quota(): store = UserISOStore(None) scope = user_iso_scope(41) store.add(scope, record(1, size=MAX_USER_ISO_TOTAL_BYTES)) with pytest.raises(TemplateError, match="10 GiB"): store.remaining_bytes(scope) def test_panel_storage_reserves_256_mib_for_engine_health(tmp_path): files = UserISOFiles(tmp_path) with patch("virsh_panel.user_isos.shutil.disk_usage", return_value=SimpleNamespace( free=PERSISTENT_FREE_RESERVE_BYTES + 12345)): assert files.available_bytes(41) == 12345 def test_reconcile_removes_only_safe_temporary_and_orphan_files(tmp_path): files = UserISOFiles(tmp_path) catalog = UserISOStore(None) owner = files.owner_directory(41) active = record(1) catalog.add(user_iso_scope(41), active) active_path = owner / active["volume_name"] active_path.write_bytes(b"active") temporary = owner / ".user-iso-abcdef.iso" temporary.write_bytes(b"partial") orphan = owner / user_iso_volume_name(f"{2:032x}") orphan.write_bytes(b"orphan") symlink = owner / ".user-iso-symlink.iso" symlink.symlink_to(active_path) files.reconcile(catalog) assert active_path.exists() assert not temporary.exists() assert not orphan.exists() assert symlink.is_symlink() def test_reconcile_does_not_delete_from_insecure_owner_directory(tmp_path): files = UserISOFiles(tmp_path) owner = files.owner_directory(41) temporary = owner / ".user-iso-abcdef.iso" temporary.write_bytes(b"partial") os.chmod(owner, 0o755) files.reconcile(UserISOStore(None)) assert temporary.exists() def test_restore_finishes_catalog_delete_after_source_unlink_crash(tmp_path): store = MemoryStore() runtime = UserISORuntime(store, tmp_path) item = record(3) scope = user_iso_scope(41) owner = runtime.files.owner_directory(41) source = owner / item["volume_name"] source.write_bytes(b"x" * item["size_bytes"]) os.chmod(source, 0o600) runtime.catalog.add(scope, item) key = f"41:{item['id']}" store.set("user_iso_tombstone", key, json.dumps({ "user_id": 41, "iso_id": item["id"], "volume_name": item["volume_name"], "pending_hosts": ["hk"], "source_deleted": False, "created_at": 1, })) source.unlink() try: runtime.restore_jobs() with pytest.raises(TemplateError) as missing: runtime.catalog.get(scope, item["id"]) assert missing.value.status == 404 tombstone = json.loads(store.get("user_iso_tombstone", key)) assert tombstone["source_deleted"] is True assert tombstone["pending_hosts"] == ["hk"] finally: runtime.close() def test_restore_rejects_tombstone_key_mismatch_without_catalog(tmp_path): store = MemoryStore() runtime = UserISORuntime(store, tmp_path) first_id = f"{4:032x}" second_id = f"{5:032x}" store.set("user_iso_tombstone", f"41:{first_id}", json.dumps({ "user_id": 41, "iso_id": second_id, "volume_name": user_iso_volume_name(second_id), "pending_hosts": ["hk"], "source_deleted": True, "created_at": 1, })) try: with pytest.raises(TemplateError, match="回收记录损坏"): runtime.restore_jobs() finally: runtime.close() @pytest.mark.parametrize("url", [ "http://downloads.example.test/os.iso", "https://user:pass@downloads.example.test/os.iso", "https://downloads.example.test:444/os.iso", "https://downloads.example.test/os.iso#fragment", ]) def test_remote_url_requires_plain_https_443(url): with pytest.raises(TemplateError): validate_user_iso_url(url) @pytest.mark.parametrize("url", [ "https://downloads.example.test/os.iso", "https://downloads.example.test/releases/OS.ISO?token=abc123", ]) def test_remote_url_accepts_iso_path_case_insensitively_with_query(url): assert validate_user_iso_url(url).path.lower().endswith(".iso") @pytest.mark.parametrize("url", [ "https://downloads.example.test/os.img", "https://downloads.example.test/download", "https://downloads.example.test/os.iso.img", "https://downloads.example.test/os.iso/extra", "https://downloads.example.test/download?file=os.iso", ]) def test_remote_url_rejects_path_without_real_iso_suffix(url): with pytest.raises(TemplateError, match=r"\.iso 结尾") as invalid: validate_user_iso_url(url) assert invalid.value.status == 422 assert UserISORuntime.public_error(str(invalid.value)) == str(invalid.value) def test_dns_rejects_private_or_mixed_answers(): answers = [ (socket.AF_INET, socket.SOCK_STREAM, socket.IPPROTO_TCP, "", ("8.8.8.8", 443)), (socket.AF_INET, socket.SOCK_STREAM, socket.IPPROTO_TCP, "", ("127.0.0.1", 443)), ] with patch("virsh_panel.user_isos.socket.getaddrinfo", return_value=answers): with pytest.raises(TemplateError, match="内网、回环或保留"): _resolve_global_addresses("downloads.example.test") def test_bootable_iso_validation(tmp_path): valid = tmp_path / "valid.iso" valid.write_bytes(bootable_iso()) verify_user_iso(valid) invalid = tmp_path / "invalid.iso" invalid.write_bytes(b"x" * (1024 * 1024)) with pytest.raises(TemplateError, match="ISO 9660"): verify_user_iso(invalid) class FakeHeaders: def __init__(self, size: int): self.size = size def get_all(self, name: str, default): return [str(self.size)] if name == "Content-Length" else default class FakeResponse: def __init__(self, content: bytes, *, status: int = 200): self.content = content self.status = status self.offset = 0 self.headers = FakeHeaders(len(content)) self.closed = False def getheader(self, _name: str, default=None): return default def read(self, size: int): chunk = self.content[self.offset:self.offset + size] self.offset += len(chunk) return chunk def close(self): self.closed = True class FakeConnection: def __init__(self): self.closed = False def close(self): self.closed = True def test_download_uses_resolved_pinned_addresses_and_private_temp_file(tmp_path): content = bootable_iso() response = FakeResponse(content) connection = FakeConnection() with patch("virsh_panel.user_isos._resolve_global_addresses", return_value=["8.8.8.8"]) as resolve, patch( "virsh_panel.user_isos._open_pinned_response", return_value=(connection, response)) as opened: path, size, digest = download_user_iso( "https://downloads.example.test/os.iso?signature=secret", "", tmp_path, ) try: assert size == len(content) assert digest == hashlib.sha256(content).hexdigest() assert path.stat().st_mode & 0o077 == 0 resolve.assert_called_once_with("downloads.example.test") assert opened.call_args.args[1] == ["8.8.8.8"] assert response.closed and connection.closed finally: Path(path).unlink(missing_ok=True) def test_download_rejects_redirect_without_following_or_leaving_temp_file(tmp_path): response = FakeResponse(b"", status=302) connection = FakeConnection() with patch("virsh_panel.user_isos._resolve_global_addresses", return_value=["8.8.8.8"]), patch( "virsh_panel.user_isos._open_pinned_response", return_value=(connection, response)): with pytest.raises(TemplateError, match="重定向"): download_user_iso( "https://downloads.example.test/redirect.iso", "", tmp_path) assert list(tmp_path.iterdir()) == [] assert response.closed and connection.closed def test_download_rejects_declared_file_over_global_10_gib_quota(tmp_path): response = FakeResponse(b"") response.headers = FakeHeaders(MAX_USER_ISO_BYTES + 1) connection = FakeConnection() with patch("virsh_panel.user_isos._resolve_global_addresses", return_value=["8.8.8.8"]), patch( "virsh_panel.user_isos._open_pinned_response", return_value=(connection, response)): with pytest.raises(TemplateError, match="10 GiB") as oversized: download_user_iso( "https://downloads.example.test/oversized.iso", "", tmp_path) assert oversized.value.status == 413 assert list(tmp_path.iterdir()) == []