diff --git a/docs/operations/release-cache-rollback.md b/docs/operations/release-cache-rollback.md index 2ec859a3..d3661342 100644 --- a/docs/operations/release-cache-rollback.md +++ b/docs/operations/release-cache-rollback.md @@ -72,7 +72,12 @@ for): the host must answer 200 with the same `index.html` (the same entry script) and the HTML headers, so a host without the SPA fallback fails (review 1101/13-04). When the build ships a worker it requests `/service-worker.js` and expects `no-cache`. The surfaces and their headers are -declared in `config/hosting/cache-policy.json`. +declared in `config/hosting/cache-policy.json`. For each cacheable extension the +release has under `assets/` (`.css`, `.js`, `.woff2` and the rest of +`CACHEABLE_ASSET_CONTENT_TYPES`) it requests the first such file and expects +exactly that type (`hashedAsset.css` and so on). The worker's install refuses +the whole release when one precached asset has another type, while a page +still uses a font served as `application/octet-stream` (review 1103/06-03). The served `release-manifest.json` must name the `buildId` and `releaseId` of the release being checked: correct headers on an earlier release are not diff --git a/scripts/lib/hosting-probe.ts b/scripts/lib/hosting-probe.ts index faf6473b..63cacf94 100644 --- a/scripts/lib/hosting-probe.ts +++ b/scripts/lib/hosting-probe.ts @@ -1,4 +1,7 @@ +import path from "node:path"; + import type { InstalledServiceWorkerSelection } from "../../src/contracts/service-worker.ts"; +import { CACHEABLE_ASSET_CONTENT_TYPES } from "../../src/contracts/service-worker-static-manifest.ts"; const LOOPBACK_IPV4 = /^127(?:\.\d{1,3}){3}$/; @@ -148,6 +151,68 @@ export async function probeLiveRelease( return { responses, results }; } +/** + * 1103/06-03. The types the probe accepts for a hashed asset: the worker's + * table (`CACHEABLE_ASSET_CONTENT_TYPES`), plus `application/javascript`, + * which servers still send for `.js` (R-6.5) and the `hashedAsset` surface in + * `config/hosting/cache-policy.json` also accepts. + */ +export function acceptedAssetContentTypes(extension: string): readonly string[] { + const type = Object.hasOwn(CACHEABLE_ASSET_CONTENT_TYPES, extension) + ? CACHEABLE_ASSET_CONTENT_TYPES[extension] + : undefined; + if (type === undefined) return []; + return type === "text/javascript" ? [type, "application/javascript"] : [type]; +} + +/** + * 1103/06-03. The worker's install compares every precached asset's + * Content-Type with the manifest's and refuses the whole release on one + * mismatch, while a page still uses, say, a font served as octet-stream. The + * entry script alone does not show that, so the live probe requests the first + * built `assets/` file of each cacheable extension. + */ +export async function probeAssetContentTypes( + base: URL, + distFiles: readonly string[], + fetcher: HostingFetcher = (url) => fetch(url, { redirect: "follow" }), +): Promise { + const samples = new Map(); + for (const file of distFiles.map((entry) => entry.split(path.sep).join("/")).sort()) { + const extension = path.posix.extname(file).toLowerCase(); + if ( + file.startsWith("assets/") && + acceptedAssetContentTypes(extension).length > 0 && + !samples.has(extension) + ) { + samples.set(extension, file); + } + } + const results: ProbeResult[] = []; + for (const [extension, file] of [...samples].sort(([left], [right]) => left.localeCompare(right))) { + const expected = acceptedAssetContentTypes(extension); + let observed: string; + try { + const response = await fetcher(new URL(file, base)); + await response.body?.cancel().catch(() => undefined); + observed = + response.status === 200 + ? (response.headers.get("content-type")?.split(";", 1)[0]?.trim().toLowerCase() ?? "") + : `HTTP ${response.status}`; + } catch (error: unknown) { + observed = error instanceof Error ? error.name : "UnknownError"; + } + results.push({ + surface: `hashedAsset${extension}`, + header: "content-type", + expected, + observed, + passed: expected.includes(observed), + }); + } + return results; +} + /** A build ships a worker script for these selections (06-03: REMOVE_REGISTRATION ships the self-unregistering one). */ export function shipsServiceWorker(selection: InstalledServiceWorkerSelection | null): boolean { return selection?.mode === "ACTIVE" || selection?.mode === "REMOVE_REGISTRATION"; diff --git a/scripts/verify-hosting-headers.ts b/scripts/verify-hosting-headers.ts index 5d883661..5005c1c7 100644 --- a/scripts/verify-hosting-headers.ts +++ b/scripts/verify-hosting-headers.ts @@ -7,6 +7,7 @@ import { checkPublicServiceWorkers, classifyLiveHostingBaseUrl, hostingVerdict, + probeAssetContentTypes, probeLiveRelease, releaseIdentity, shipsServiceWorker, @@ -106,6 +107,8 @@ if (liveTarget?.passed && !slotEmpty) { const probed = await probeLiveRelease(liveTarget.url, paths, candidate); responses = probed.responses; probeResults.push(...probed.results); + // 1103/06-03. The worker needs every precached asset kind typed exactly. + probeResults.push(...(await probeAssetContentTypes(liveTarget.url, distFiles))); } else if (liveTarget && !liveTarget.passed) { mode = "invalid-live"; probeResults.push({ diff --git a/tests/unit/hosting-probe.test.ts b/tests/unit/hosting-probe.test.ts index 258e0825..ad0818e3 100644 --- a/tests/unit/hosting-probe.test.ts +++ b/tests/unit/hosting-probe.test.ts @@ -6,9 +6,11 @@ import path from "node:path"; import { describe, expect, it } from "vitest"; import { + acceptedAssetContentTypes, checkPublicServiceWorkers, classifyLiveHostingBaseUrl, hostingVerdict, + probeAssetContentTypes, probeLiveRelease, servedEntryScript, } from "../../scripts/lib/hosting-probe.ts"; @@ -207,6 +209,95 @@ describe("SPA fallback and worker script surfaces (1101/13-04)", () => { }); }); +// 1103/06-03. The worker's install requires each precached asset's exact +// Content-Type (CACHEABLE_ASSET_CONTENT_TYPES) and refuses the whole release on +// one mismatch. A page still uses a font served as octet-stream, so only the +// worker notices. The live probe requests one built asset per extension. +describe("hashed asset content types (1103/06-03)", () => { + const base = new URL("https://frontend.example.test/"); + const DIST_FILES = [ + "index.html", + "config.json", + "assets/index-A1.js", + "assets/chunk-B2.js", + "assets/index-C3.css", + "assets/inter-D4.woff2", + "assets/notes-E5.txt", + "robots.txt", + ]; + const host = + (types: Readonly>, requested: string[] = []) => + async (url: URL) => { + requested.push(url.pathname); + const extension = /\.[^./]+$/u.exec(url.pathname)?.[0] ?? ""; + const type = types[extension]; + const response = new Response("", { + status: type ? 200 : 404, + headers: type ? { "content-type": type } : {}, + }); + Object.defineProperty(response, "url", { value: url.href }); + return response; + }; + const CORRECT = { + ".js": "text/javascript; charset=utf-8", + ".css": "text/css", + ".woff2": "font/woff2", + }; + + it("requests one built asset per cacheable extension and passes correct types", async () => { + const requested: string[] = []; + const results = await probeAssetContentTypes(base, DIST_FILES, host(CORRECT, requested)); + expect(requested).toEqual([ + "/assets/index-C3.css", + "/assets/chunk-B2.js", + "/assets/inter-D4.woff2", + ]); + expect(results.map((result) => [result.surface, result.passed])).toEqual([ + ["hashedAsset.css", true], + ["hashedAsset.js", true], + ["hashedAsset.woff2", true], + ]); + }); + + it("fails a host that serves CSS as text/plain", async () => { + const results = await probeAssetContentTypes( + base, + DIST_FILES, + host({ ...CORRECT, ".css": "text/plain" }), + ); + expect(results.filter((result) => !result.passed)).toEqual([ + expect.objectContaining({ + surface: "hashedAsset.css", + header: "content-type", + expected: ["text/css"], + observed: "text/plain", + }), + ]); + }); + + it("fails a font served as octet-stream and an asset the host does not serve", async () => { + const results = await probeAssetContentTypes( + base, + DIST_FILES, + host({ ".js": "application/javascript", ".woff2": "application/octet-stream" }), + ); + expect( + results.filter((result) => !result.passed).map((result) => [result.surface, result.observed]), + ).toEqual([ + ["hashedAsset.css", "HTTP 404"], + ["hashedAsset.woff2", "application/octet-stream"], + ]); + }); + + it("accepts for JavaScript what the hashedAsset surface declares", async () => { + const policy = JSON.parse(await readFile("config/hosting/cache-policy.json", "utf8")) as { + surfaces: { hashedAsset: { contentTypes: string[] } }; + }; + expect(acceptedAssetContentTypes(".js")).toEqual(policy.surfaces.hashedAsset.contentTypes); + expect(acceptedAssetContentTypes(".woff2")).toEqual(["font/woff2"]); + }); +}); + // 1101/13-01. FE-GATE-019 checks the promoted candidate it downloaded, not a // local rebuild, and an empty deployment slot proves nothing about the host. describe("verify-hosting-headers on a promoted release root (1101/13-01)", () => {