Spaces:
Running
Running
File size: 1,256 Bytes
628ab0d | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | """Unpack the checked resource archive used by the browser deployment."""
from pathlib import Path
import hashlib
import json
import zipfile
def ensure_resources():
root = Path(__file__).resolve().parent
archive = root / "resources.zip"
if not archive.exists(): return
expected = json.loads((root / "resources_manifest.json").read_text())
if hashlib.sha256(archive.read_bytes()).hexdigest() != expected["archive_sha256"]:
raise RuntimeError("Resource archive hash mismatch.")
with zipfile.ZipFile(archive) as z:
if set(z.namelist()) != set(expected["files"]):
raise RuntimeError("Unexpected resource archive members.")
for name in z.namelist():
path = Path(name)
if path.is_absolute() or ".." in path.parts or path.parts[0] not in {"data", "evidence", "assets", "tests", "scripts", "validation"}:
raise RuntimeError("Unsafe resource path.")
content = z.read(name)
if hashlib.sha256(content).hexdigest() != expected["files"][name]:
raise RuntimeError("Resource file hash mismatch.")
target = root / path
target.parent.mkdir(parents=True,exist_ok=True)
target.write_bytes(content)
|