"""Allowlisted application source shared by CT creation and in-container updates.""" import hashlib from pathlib import Path import sys import tarfile sys.path.insert(0, str(Path(__file__).resolve().parents[1])) from source_policy import is_private_path PROJECTS = ("FinalToken.Core", "FinalToken.Fleet", "FinalToken.Web", "FinalToken.Worker") def snapshot(source, destination): """Allowlist source inputs, including uncommitted changes, excluding secrets, outputs and symlinks.""" files = [source / n for n in ("Directory.Build.props", "Directory.Packages.props", "global.json", "LICENSE", "tools/lxc_update.py")] for project in PROJECTS: directory = source / "src" / project if directory.is_symlink() or directory.resolve() != directory.absolute(): raise ValueError("Source snapshot refuses a symlinked project or parent: " + project) files.extend(directory.rglob("*")) total, count = 0, 0 with tarfile.open(destination, "w:gz") as archive: for path in sorted(set(files)): relative = path.relative_to(source) if is_private_path(relative): continue # .gitignore does not apply to tarfile: do not copy local deployment data. if any(p in ("bin", "obj", "artifacts", ".git", "node_modules") or p.startswith(".") for p in relative.parts): continue if path.is_symlink() or path.resolve() != path.absolute(): raise ValueError("Source snapshot refuses symlinks: " + str(relative)) if path.is_dir(): continue if not path.is_file(): raise ValueError("Missing or non-regular source file: " + str(relative)) size = path.stat().st_size total += size count += 1 if size > 16 * 1024 * 1024 or total > 256 * 1024 * 1024 or count > 10000: raise ValueError("Source snapshot exceeds the guest archive bounds") archive.add(path, arcname=relative.as_posix(), recursive=False) return hashlib.sha256(destination.read_bytes()).hexdigest()