fix(build): let the hosting gate accept the selected ACTIVE worker (R3-6)

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>
This commit is contained in:
DongHyeonka
2026-09-24 15:42:09 +09:00
co-authored by Claude Opus 5.5
parent 19e8decf46
commit d1a57ae949
5 changed files with 81 additions and 18 deletions
-3
View File
@@ -27,9 +27,6 @@
},
"sourceMap": {
"public": false
},
"serviceWorker": {
"enabled": false
}
}
}
+3 -1
View File
@@ -18,7 +18,9 @@ The provider-independent cache defaults are:
- HTML: `no-cache`
- runtime config and release manifest: `no-store`
- public source maps: disabled
- service worker/offline cache: disabled
- service worker/offline cache: disabled, unless the static selection in
`src/features/installed-runtime-capabilities.ts` is `ACTIVE`; then `dist`
holds exactly the selected `service-worker.js`
HTML, JSON config/manifest, and hashed JavaScript MIME types are also compared
to the declared allowlist; a cache-correct response with a mismatched
+27
View File
@@ -1,4 +1,31 @@
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
+11 -13
View File
@@ -1,7 +1,11 @@
import { mkdir, readFile, readdir } from "node:fs/promises";
import { INSTALLED_RUNTIME_CAPABILITIES } from "../src/features/installed-runtime-capabilities.ts";
import { hostingHeadersArtifactSchema } from "./contracts/release-artifacts.ts";
import { classifyLiveHostingBaseUrl } from "./lib/hosting-probe.ts";
import {
checkPublicServiceWorkers,
classifyLiveHostingBaseUrl,
} from "./lib/hosting-probe.ts";
import { writeValidatedJsonArtifact } from "./lib/validated-json-artifact.ts";
type Document = Record<string, unknown>;
@@ -63,9 +67,6 @@ const baseUrl = process.env.HOSTING_BASE_URL;
const liveTarget = baseUrl ? classifyLiveHostingBaseUrl(baseUrl) : null;
const distFiles = (await readdir("dist", { recursive: true })).map(String);
const publicSourceMaps = distFiles.filter((file) => file.endsWith(".map"));
const publicServiceWorkers = distFiles.filter((file) =>
/(?:^|\/)(?:service-worker|sw)(?:[.-][^/]*)?\.js$/i.test(file),
);
let responses: ResponseHeaders = {};
let mode: HostingMode;
@@ -184,15 +185,12 @@ results.push({
observed: publicSourceMaps.length > 0,
passed: sourceMapPolicy.public === false && publicSourceMaps.length === 0,
});
const serviceWorkerPolicy = recordValue(cacheSurfaces.serviceWorker);
results.push({
surface: "serviceWorker",
header: "enabled",
expected: false,
observed: publicServiceWorkers.length > 0,
passed:
serviceWorkerPolicy.enabled === false && publicServiceWorkers.length === 0,
});
results.push(
checkPublicServiceWorkers(
distFiles,
INSTALLED_RUNTIME_CAPABILITIES.serviceWorker,
),
);
const passed = results.every((result) => result.passed);
await mkdir("artifacts/release", { recursive: true });
+40 -1
View File
@@ -1,6 +1,45 @@
import { describe, expect, it } from "vitest";
import { classifyLiveHostingBaseUrl } from "../../scripts/lib/hosting-probe.ts";
import {
checkPublicServiceWorkers,
classifyLiveHostingBaseUrl,
} from "../../scripts/lib/hosting-probe.ts";
describe("public service worker in dist", () => {
const app = ["index.html", "config.json", "assets/index-abc.js"];
const active = {
mode: "ACTIVE",
scriptPath: "service-worker.js",
handlers: ["PWA_STATIC_ASSETS"],
} as const;
const removal = { mode: "REMOVE_REGISTRATION", scriptPath: "service-worker.js" } as const;
it("accepts the selected worker when the selection is ACTIVE", () => {
expect(
checkPublicServiceWorkers([...app, "service-worker.js"], active),
).toMatchObject({ expected: true, observed: true, passed: true });
});
it("accepts a worker-free dist when no worker is selected", () => {
expect(checkPublicServiceWorkers(app, null)).toMatchObject({
expected: false,
passed: true,
});
});
it.each([
["no selection", [...app, "service-worker.js"], null],
["a non-ACTIVE selection", [...app, "service-worker.js"], removal],
["ACTIVE without the worker", app, active],
["ACTIVE with an extra worker", [...app, "service-worker.js", "assets/sw.js"], active],
["ACTIVE with the worker off the root", [...app, "assets/service-worker.js"], active],
] as const)("rejects %s", (_, files, selection) => {
expect(checkPublicServiceWorkers(files, selection)).toMatchObject({
passed: false,
reason: expect.stringContaining("dist worker scripts"),
});
});
});
describe("live hosting evidence target", () => {
it("accepts a canonical production HTTPS root", () => {