Public: anyone can read this; the crew can change it.

A missing /assets/ file answers no-store, so the edge never keeps a 404 for a file the next release ships #14033

Merged opened this

Check · Review · Land · Live done — Landed and live in production.

Merged

scout/forge:scout/13848-a-missing-assets-file-answers-no-store-s → main 4 commits fork 2 threads · 2 unresolved

  • Merged. scout/13848-a-missing-assets-file-answers-no-store-s was merged into main as 4495327.

Review

Sign in to review this change.
4 files changed +51 −18
src/http/asset_miss.ts +18 −7
@@ -1,8 +1,8 @@
11 /**
22 * One rule decides the shape of a miss on a GET or HEAD that matched no route:
33 * - a machine prefix (`/.well-known/…`, `/oauth/…`) answers `{error, message, hint}` JSON;
4 * - a file-like path (`/feed.xml`, `/x.png`, `/docs.atom`) answers a few bytes of `no-store` plain
5 * text;
4 * - a file-like path (`/feed.xml`, `/x.png`, `/docs.atom`) or anything under `/assets/` answers a
5 * few bytes of `no-store` plain text;
66 * - everything else keeps the full not-found page.
77 * A crawler or tool is not a person, so it never costs the 10 KB page. `/api`
88 * keeps its own `no_route` body (server.ts). The security floor is added by
@@ -18,9 +18,24 @@ export type MissShape = "machine" | "file" | "page";
1818 export function missShape(request: Request, pathname: string): MissShape {
1919 if (request.method !== "GET" && request.method !== "HEAD") return "page";
2020 if (MACHINE_PREFIX.test(pathname)) return "machine";
21 // Anything under /assets/ is a file whatever its shape (`.woff2`, no extension, nested), so no
22 // asset miss ever answers the cacheable page.
23 if (pathname.startsWith("/assets/")) return "file";
2124 return FILE_EXTENSION.test(pathname) ? "file" : "page";
2225 }
2326
27 /**
28 * The one answer for a file that is not here: a few bytes of plain text, `no-store`. Every
29 * `/assets/` miss answers this, so the edge never keeps a 404 for a hashed name the next release
30 * ships; the security floor is added on the way out (`securityFloor`).
31 */
32 export function assetNotFound(): Response {
33 return new Response("not found\n", {
34 status: 404,
35 headers: { "content-type": "text/plain; charset=utf-8", "cache-control": "no-store" },
36 });
37 }
38
2439 /**
2540 * The tiny answer for a miss that is not a page, or undefined when it is one. `/api` is left to its
2641 * own.
@@ -28,11 +43,7 @@ export function missShape(request: Request, pathname: string): MissShape {
2843 export function typedMiss(request: Request, pathname: string): Response | undefined {
2944 if (pathname === "/api" || pathname.startsWith("/api/")) return undefined;
3045 const shape = missShape(request, pathname);
outdated

This is the only fallback, and it fires only when the path matches a file-extension regex and is not routed. Paths under /assets/ that fall outside that (woff2, png, no extension) still get the HTML no-cache 404 on the preview. Route all /assets/ misses to assetNotFound() in the fallback, whatever the shape.

31 if (shape === "file")
32 return new Response("not found\n", {
33 status: 404,
34 headers: { "content-type": "text/plain; charset=utf-8", "cache-control": "no-store" },
35 });
46 if (shape === "file") return assetNotFound();
3647 if (shape === "machine")
3748 return Response.json(
3849 {
src/http/server.ts +8 −8
@@ -543,7 +543,7 @@ import {
543543 } from "../web/pages/browse.ts";
544544 import { bringConnect, bringEnvSubmit, bringJobPage, bringPage, bringRun, bringSubmit } from "../web/pages/bring.ts";
545545 import { pushThrough } from "../origins/push-through.ts";
546 import { MACHINE_PREFIX, typedMiss } from "./asset_miss.ts";
546 import { assetNotFound, MACHINE_PREFIX, typedMiss } from "./asset_miss.ts";
547547 import { CAME_FROM_ELSEWHERE, forbiddenPage, notFoundPage, SENT_FROM_ELSEWHERE, signInHref } from "../web/chrome.ts";
548548 import { targetForShortCode } from "../origins/oauth.ts";
549549 import { apiRoutes, oauthCallbackUri } from "./api.ts";
@@ -851,7 +851,7 @@ export function buildRouter(): Router<Handler> {
851851 const dir = ctx.app.config.dataDir;
852852 if (ctx.params.name !== stylesheetAsset().name) {
853853 const old = keptAssetNamed(dir, ctx.params.name!, "css");
854 return old === undefined ? notFound() : fileResponse(ctx.request, old);
854 return old === undefined ? assetNotFound() : fileResponse(ctx.request, old);
855855 }
856856 return stylesheetResponse(ctx.request);
857857 });
@@ -890,15 +890,15 @@ export function buildRouter(): Router<Handler> {
890890 if (own !== undefined) return fileResponse(ctx.request, own);
891891 if (ctx.params.name!.startsWith("machine-term-")) {
892892 const term = await machineTermAsset();
893 return term !== null && ctx.params.name === term.name ? assetResponse(ctx.request, term) : notFound();
893 return term !== null && ctx.params.name === term.name ? assetResponse(ctx.request, term) : assetNotFound();
894894 }
895895 if (ctx.params.name!.startsWith("model3d-")) {
896896 const viewer = await model3dAsset();
897 if (viewer === null || ctx.params.name !== viewer.name) return notFound();
897 if (viewer === null || ctx.params.name !== viewer.name) return assetNotFound();
898898 return assetResponse(ctx.request, viewer);
899899 }
900900 const asset = await highlightAsset();
901 if (asset === null || ctx.params.name !== asset.name) return notFound();
901 if (asset === null || ctx.params.name !== asset.name) return assetNotFound();
902902 return assetResponse(ctx.request, asset);
903903 });
904904 // The icon sprite and the speculation rules: hashed, immutable, one file each.
@@ -906,16 +906,16 @@ export function buildRouter(): Router<Handler> {
906906 const dir = ctx.app.config.dataDir;
907907 if (ctx.params.name !== spriteAsset().name) {
908908 const old = keptAssetNamed(dir, ctx.params.name!, "svg");
909 return old === undefined ? notFound() : fileResponse(ctx.request, old);
909 return old === undefined ? assetNotFound() : fileResponse(ctx.request, old);
910910 }
911911 return fileResponse(ctx.request, spriteAsset());
912912 });
913913 // The machine shell's terminal emulator (`web/machine-term-asset.ts`).
914914 router.add("GET", "/assets/:name.wasm", (ctx) =>
915 ctx.params.name === ghosttyWasmAsset().name ? wasmResponse(ctx.request) : notFound(),
915 ctx.params.name === ghosttyWasmAsset().name ? wasmResponse(ctx.request) : assetNotFound(),
916916 );
917917 router.add("GET", "/assets/:name.json", (ctx) =>
918 ctx.params.name === speculationAsset().name ? fileResponse(ctx.request, speculationAsset()) : notFound(),
918 ctx.params.name === speculationAsset().name ? fileResponse(ctx.request, speculationAsset()) : assetNotFound(),
919919 );
920920 // The service worker: root scope needs a root path, and it must never be
921921 // cached by the browser's HTTP cache or an update would wait a day.
src/web/chrome.ts +6 −3
@@ -74,9 +74,12 @@ export function crumbsFor(repo: Repo): { label: string; href: string }[] {
7474 * this address; `plain` leaves that out for a miss that must reveal nothing
7575 * (a dead `/go/:code`). */
7676 export function notFoundPage(ctx: Ctx, opts: { plain?: boolean } = {}): Response {
77 // A machine path, or a miss at the owner level (no repository named), may be the few typed bytes;
78 // a person's missing repo file (`/o/r/blob/x.md`) keeps the page and its nearby links.
79 const typed = opts.plain || (ctx.params.name !== undefined && !MACHINE_PREFIX.test(ctx.url.pathname)) ? undefined : typedMiss(ctx.request, ctx.url.pathname);
77 // A machine path, anything under `/assets/` (a reserved owner, so `/assets/x.woff2` lands on a
78 // repository route), or a miss at the owner level (no repository named), may be the few typed
79 // bytes; a person's missing repo file (`/o/r/blob/x.md`) keeps the page and its nearby links.
80 const path = ctx.url.pathname;
81 const typedPath = ctx.params.name === undefined || MACHINE_PREFIX.test(path) || path.startsWith("/assets/");
82 const typed = opts.plain || !typedPath ? undefined : typedMiss(ctx.request, path);
8083 if (typed) return typed;
8184 const get = !opts.plain && ctx.request.method === "GET";
8285 const near = get ? nearTo(ctx.app.db, ctx.auth.user, ctx.params.owner ?? "", ctx.params.name ?? "") : [];
test/assets.test.ts +19
@@ -117,3 +117,22 @@ test("an unhashed or unknown asset path is never immutable", async () => {
117117 expect(res.headers.get("cache-control") ?? "").not.toContain("max-age=31536000");
118118 }
119119 });
120
121 test("a miss on each asset kind answers no-store plain text under the security floor", async () => {

The list covers only css, js, svg, wasm and json. Add an unrouted extension (woff2, png), an extension-less name and a nested path, which currently answer text/html with no-cache.

122 const misses = ["css", "js", "svg", "wasm", "json"].flatMap((ext) => [
123 `/assets/nope.${ext}`,
124 `/assets/forge-${"f".repeat(16)}.${ext}`,
125 ]);
126 misses.push("/assets/nope.woff2", "/assets/nope.png", "/assets/nope", "/assets/a/b.js", "/assets/");
127 misses.push(`/assets/machine-term-${"0".repeat(16)}.js`, `/assets/model3d-${"0".repeat(16)}.js`);
128 for (const path of misses) {
129 const res = await fetch(`${servers.url}${path}`);
130 expect(res.status, path).toBe(404);
131 expect(res.headers.get("content-type"), path).toBe("text/plain; charset=utf-8");
132 expect(res.headers.get("cache-control"), path).toBe("no-store");
133 expect(res.headers.get("x-content-type-options"), path).toBe("nosniff");
134 expect(res.headers.get("content-security-policy"), path).not.toBeNull();
135 expect(res.headers.get("referrer-policy"), path).not.toBeNull();
136 expect(await res.text(), path).toBe("not found\n");
137 }
138 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs