verify-hosting-headers.ts passed the serviceWorker probe only when
cache-policy.json said `enabled: false` AND dist had no worker script, so no
configuration let a supported ACTIVE build through the hosting gate. The
probe now follows the static selection: ACTIVE requires exactly the selected
`service-worker.js` at the dist root; null / REMOVE_REGISTRATION /
PURGE_OWNED_RESOURCES still reject any worker script. The now-unused
`serviceWorker.enabled` policy flag is removed; the artifact keeps the same
`serviceWorker`/`enabled` probe key.
RED (ACTIVE build from the previous commit, `node scripts/verify-hosting-headers.ts`):
Hosting cache/content-type/security header verification failed. exit=1
{ surface: serviceWorker, header: enabled, expected: false, observed: true, passed: false }
GREEN: same ACTIVE build -> PASS exit=0 (expected: true, observed: true);
default build -> PASS; default build + stray dist/service-worker.js -> exit=1
(reason "dist worker scripts: service-worker.js");
tests/unit/hosting-probe.test.ts 13 passed.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
102 lines
2.6 KiB
TypeScript
102 lines
2.6 KiB
TypeScript
import type { InstalledServiceWorkerSelection } from "../../src/contracts/service-worker.ts";
|
|
|
|
const LOOPBACK_IPV4 = /^127(?:\.\d{1,3}){3}$/;
|
|
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 };
|
|
}
|