Raise ValueError unless the current app + worker build evidence holds.
(ui_dir)
| 33 | |
| 34 | |
| 35 | def validate_browser_build(ui_dir): |
| 36 | """Raise ValueError unless the current app + worker build evidence holds.""" |
| 37 | root = Path(ui_dir) |
| 38 | try: |
| 39 | proof = json.loads((root / ".vite/license-build.json").read_text()) |
| 40 | if proof["version"] != 1 or proof["worker_builds"] < 1: |
| 41 | raise ValueError("app and worker build inventories are required") |
| 42 | if proof["lock_sha256"] != sha256(root / "package-lock.json"): |
| 43 | raise ValueError("browser build lockfile changed") |
| 44 | inputs, outputs = proof["inputs"], proof["outputs"] |
| 45 | if not inputs or not outputs: |
| 46 | raise ValueError("empty browser build inventory") |
| 47 | for name, expected in inputs.items(): |
| 48 | if sha256(checked_path(root, name)) != expected: |
| 49 | raise ValueError(f"browser build input changed: {name}") |
| 50 | for name in [*proof["modules"], *proof["externals"], *inputs]: |
| 51 | if SHARP_INPUT.search(name.replace("\0", "/")): |
| 52 | raise ValueError("Sharp code appears in the browser build graph") |
| 53 | if "transformers.node." in name or "onnxruntime-node" in name: |
| 54 | raise ValueError("Node inference code appears in the browser build graph") |
| 55 | # A browser build must resolve this reviewed export, including in the |
| 56 | # inference worker. Do not infer it from npm's dependency declarations. |
| 57 | meta = json.loads((root / "node_modules/@huggingface/transformers/package.json").read_text()) |
| 58 | if (meta["version"] != "4.2.0" or |
| 59 | meta["exports"]["default"]["default"] != "./dist/transformers.web.js" or |
| 60 | BROWSER_ENTRY not in proof["modules"] or |
| 61 | inputs.get(BROWSER_ENTRY) != BROWSER_SHA256): |
| 62 | raise ValueError("Transformers browser export needs a new license review") |
| 63 | dist = root / "dist" |
| 64 | actual = {p.relative_to(dist).as_posix() for p in dist.rglob("*") if p.is_file()} |
| 65 | if actual != set(outputs): |
| 66 | raise ValueError("browser build asset inventory changed") |
| 67 | for name, expected in outputs.items(): |
| 68 | path = checked_path(dist, name) |
| 69 | if sha256(path) != expected: |
| 70 | raise ValueError(f"browser build output changed: {name}") |
| 71 | suffix = path.suffix.lower() |
| 72 | # Hashing a public copy does not make it a bundler output. Bind |
| 73 | # generated HTML/CSS to bytes observed in the Rollup output hook. |
| 74 | generated = proof["generated_assets"].get(name) |
| 75 | if suffix == ".css" and generated == expected: |
| 76 | continue |
| 77 | if (name == "index.html" and generated == expected and |
| 78 | "index.html" in inputs and "index.html" in proof["modules"]): |
| 79 | continue |
| 80 | if suffix in {".js", ".mjs"} and name in proof["chunks"]: |
| 81 | continue |
| 82 | # ONNX ships a prebuilt runtime module and Wasm via Vite URL |
| 83 | # assets. Accept only an exact copy of the inventoried package |
| 84 | # input, never a filename or extension as evidence of provenance. |
| 85 | source = proof["runtime_inputs"].get(name, "") |
| 86 | if (suffix in {".wasm", ".js", ".mjs"} and |
| 87 | source.startswith("node_modules/onnxruntime-web/dist/") and |
| 88 | inputs.get(source) == expected): |
| 89 | continue |
| 90 | raise ValueError(f"browser asset has unknown provenance: {name}") |
| 91 | except (OSError, KeyError, TypeError, json.JSONDecodeError) as exc: |
| 92 | raise ValueError("browser build evidence is missing or incomplete; run npm run build") from exc |
no test coverage detected