From 676781223d78559abc5fb26a58139094f9c0aeb5 Mon Sep 17 00:00:00 2001 From: kronecker <2026692687@qq.com> Date: Mon, 12 Oct 2026 03:28:41 +0800 Subject: [PATCH] test(admin): verify bounded archive and private storage refusals --- backend/tests/test_admin_archive_vm.py | 180 +++++++++++++++++++++++++ 1 file changed, 180 insertions(+) create mode 100644 backend/tests/test_admin_archive_vm.py diff --git a/backend/tests/test_admin_archive_vm.py b/backend/tests/test_admin_archive_vm.py new file mode 100644 index 0000000..4fbc954 --- /dev/null +++ b/backend/tests/test_admin_archive_vm.py @@ -0,0 +1,180 @@ +"""Finite adverse archive/private-storage acceptance: independent capped VM only.""" + +import hashlib +import io +import json +import os +import stat +import struct +import time +import zipfile +from pathlib import Path +from uuid import uuid4 + +import pytest +from test_admin_import_vm import independent_import_vm as independent_import_vm +from test_admin_import_vm import package + +from kf_oj.contracts.judging_v2 import AdmissionRegistry +from kf_oj.infra.artifacts.archive_reader import ArchiveError, ArchiveReader +from kf_oj.infra.artifacts.staging import ImportStaging +from kf_oj.infra.artifacts.store import ArtifactError, ArtifactStore + +ROOT = Path(__file__).resolve().parents[2] + + +def policy(): + return AdmissionRegistry.model_validate_json( + (ROOT / "tests/fixtures/contracts-v2/judge-registry.valid.json").read_bytes() + ) + + +def proof(name, value): + print( + "KF_FINAL_ACCEPTANCE " + json.dumps({"name": name, "result": value}, default=str), + flush=True, + ) + + +def test_adverse_zip_and_private_storage_boundaries(independent_import_vm, tmp_path): + outside = tmp_path / "outside-sentinel" + outside.write_bytes(b"unchanged synthetic sentinel") + original = hashlib.sha256(outside.read_bytes()).hexdigest() + good, _ = package(policy()) + cases = {} + for name in ("../outside-sentinel", "/outside-sentinel", "a/b", r"a\b", "a:b"): + cases["path-" + repr(name)] = package( + policy(), payload_mutation=lambda p, n=name: p.update({n: b"bad"}) + )[0] + for kind in ( + stat.S_IFLNK, + stat.S_IFCHR, + stat.S_IFBLK, + stat.S_IFIFO, + stat.S_IFSOCK, + stat.S_IFDIR, + ): + out = io.BytesIO() + with zipfile.ZipFile(out, "w") as archive: + entry = zipfile.ZipInfo("unsafe") + entry.create_system = 3 + entry.external_attr = (kind | 0o600) << 16 + archive.writestr(entry, b"synthetic") + cases[f"type-{kind}"] = out.getvalue() + for duplicate in ("statement.md", "STATEMENT.MD"): + out = io.BytesIO(good) + with zipfile.ZipFile(out, "a") as archive: + archive.writestr(duplicate, b"different") + cases["duplicate-" + duplicate] = out.getvalue() + for signature in ( + b"PK\x03\x04", + b"\x1f\x8b", + b"BZh", + b"\xfd7zXZ\x00", + b"Rar!\x1a\x07", + b"7z\xbc\xaf\x27\x1c", + b"X" * 257 + b"ustar", + ): + cases["nested-" + signature[:6].hex()] = package( + policy(), + payload_mutation=lambda p, s=signature: p.update({"hidden.in": s + b"synthetic"}), + )[0] + central = good.index(b"PK\x01\x02") + for label, local_offset, central_offset, value, fmt in ( + ("encrypted", 6, 8, 1, "