import type { InstalledServiceWorkerSelection } from "../../src/contracts/service-worker.ts"; const LOOPBACK_IPV4 = /^127(?:\.\d{1,3}){3}$/; export type HostingMode = "live" | "invalid-live" | "fixture"; export type HostingVerdict = "PASS" | "FAIL" | "UNVERIFIED"; /** * 13-05 (D-14). The fixture compares the declared headers with a copy of * themselves, so it can only find a contradiction, never prove hosting. Only a * live probe of the deployed release can PASS. */ export function hostingVerdict(mode: HostingMode, passed: boolean): HostingVerdict { if (!passed) return "FAIL"; return mode === "live" ? "PASS" : "UNVERIFIED"; } /** * 13-05. The live probe requests the entry script the served index.html names, * so it checks the release that is deployed rather than the first asset of a * local build, which the deployed release may not contain. */ export function servedEntryScript(indexHtml: string): string | null { for (const tag of indexHtml.match(/]*>/gu) ?? []) { if (!/\btype=["']module["']/u.test(tag)) continue; const source = /\bsrc=["']([^"']+\.js)["']/u.exec(tag)?.[1]; if (source) return source; } return null; } const PUBLIC_SERVICE_WORKER = /(?:^|\/)(?:service-worker|sw)(?:[.-][^/]*)?\.js$/i; /** * A built `dist` may ship a worker script only when the static selection is * `ACTIVE`, and then only the selected script at the app root. */ export function checkPublicServiceWorkers( distFiles: readonly string[], selection: InstalledServiceWorkerSelection | null, ) { const found = distFiles.filter((file) => PUBLIC_SERVICE_WORKER.test(file)); const allowed: string[] = selection?.mode === "ACTIVE" ? [selection.scriptPath] : []; const passed = found.length === allowed.length && found.every((file) => allowed.includes(file)); return { surface: "serviceWorker", header: "enabled", expected: allowed.length > 0, observed: found.length > 0, passed, ...(passed ? {} : { reason: `dist worker scripts: ${found.join(", ") || "none"}` }), }; } /** * A release gate must not promote a local preview server as live hosting * evidence. * */ export function classifyLiveHostingBaseUrl(value: string): | { passed: true; reason: null; url: URL; observedOrigin: string; } | { passed: false; reason: string; url: URL | null; observedOrigin: string | null; } { let url: URL; try { url = new URL(value); } catch { return { passed: false, reason: "HOSTING_BASE_URL must be an absolute URL", url: null, observedOrigin: null, }; } const observedOrigin = url.origin; const hostname = url.hostname.toLowerCase().replace(/^\[|\]$/g, ""); if (url.protocol !== "https:") { return { passed: false, reason: "live hosting evidence requires HTTPS", url, observedOrigin, }; } if (url.username || url.password) { return { passed: false, reason: "HOSTING_BASE_URL must not contain credentials", url, observedOrigin, }; } if ( hostname === "localhost" || hostname.endsWith(".localhost") || hostname === "::1" || hostname === "0.0.0.0" || LOOPBACK_IPV4.test(hostname) ) { return { passed: false, reason: "local or loopback hosts are not live deployment evidence", url, observedOrigin, }; } if (url.pathname !== "/" || url.search || url.hash) { return { passed: false, reason: "HOSTING_BASE_URL must be the canonical root URL", url, observedOrigin, }; } return { passed: true, reason: null, url, observedOrigin }; }