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
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/13848-a-missing-assets-file-answers-no-store-s → main 4 commits fork 2 threads · 2 unresolved
4 files
changed +51
−18
src/http/asset_miss.ts +18 −7
| @@ -1,8 +1,8 @@ | ||
| 1 | 1 | /** |
| 2 | 2 | * One rule decides the shape of a miss on a GET or HEAD that matched no route: |
| 3 | 3 | * - 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; | |
| 6 | 6 | * - everything else keeps the full not-found page. |
| 7 | 7 | * A crawler or tool is not a person, so it never costs the 10 KB page. `/api` |
| 8 | 8 | * keeps its own `no_route` body (server.ts). The security floor is added by |
| @@ -18,9 +18,24 @@ export type MissShape = "machine" | "file" | "page"; | ||
| 18 | 18 | export function missShape(request: Request, pathname: string): MissShape { |
| 19 | 19 | if (request.method !== "GET" && request.method !== "HEAD") return "page"; |
| 20 | 20 | 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"; | |
| 21 | 24 | return FILE_EXTENSION.test(pathname) ? "file" : "page"; |
| 22 | 25 | } |
| 23 | 26 | |
| 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 | ||
| 24 | 39 | /** |
| 25 | 40 | * The tiny answer for a miss that is not a page, or undefined when it is one. `/api` is left to its |
| 26 | 41 | * own. |
| @@ -28,11 +43,7 @@ export function missShape(request: Request, pathname: string): MissShape { | ||
| 28 | 43 | export function typedMiss(request: Request, pathname: string): Response | undefined { |
| 29 | 44 | if (pathname === "/api" || pathname.startsWith("/api/")) return undefined; |
| 30 | 45 | const shape = missShape(request, pathname); |
| 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(); | |
| 36 | 47 | if (shape === "machine") |
| 37 | 48 | return Response.json( |
| 38 | 49 | { |
src/http/server.ts +8 −8
| @@ -543,7 +543,7 @@ import { | ||
| 543 | 543 | } from "../web/pages/browse.ts"; |
| 544 | 544 | import { bringConnect, bringEnvSubmit, bringJobPage, bringPage, bringRun, bringSubmit } from "../web/pages/bring.ts"; |
| 545 | 545 | 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"; | |
| 547 | 547 | import { CAME_FROM_ELSEWHERE, forbiddenPage, notFoundPage, SENT_FROM_ELSEWHERE, signInHref } from "../web/chrome.ts"; |
| 548 | 548 | import { targetForShortCode } from "../origins/oauth.ts"; |
| 549 | 549 | import { apiRoutes, oauthCallbackUri } from "./api.ts"; |
| @@ -851,7 +851,7 @@ export function buildRouter(): Router<Handler> { | ||
| 851 | 851 | const dir = ctx.app.config.dataDir; |
| 852 | 852 | if (ctx.params.name !== stylesheetAsset().name) { |
| 853 | 853 | 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); | |
| 855 | 855 | } |
| 856 | 856 | return stylesheetResponse(ctx.request); |
| 857 | 857 | }); |
| @@ -890,15 +890,15 @@ export function buildRouter(): Router<Handler> { | ||
| 890 | 890 | if (own !== undefined) return fileResponse(ctx.request, own); |
| 891 | 891 | if (ctx.params.name!.startsWith("machine-term-")) { |
| 892 | 892 | 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(); | |
| 894 | 894 | } |
| 895 | 895 | if (ctx.params.name!.startsWith("model3d-")) { |
| 896 | 896 | 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(); | |
| 898 | 898 | return assetResponse(ctx.request, viewer); |
| 899 | 899 | } |
| 900 | 900 | 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(); | |
| 902 | 902 | return assetResponse(ctx.request, asset); |
| 903 | 903 | }); |
| 904 | 904 | // The icon sprite and the speculation rules: hashed, immutable, one file each. |
| @@ -906,16 +906,16 @@ export function buildRouter(): Router<Handler> { | ||
| 906 | 906 | const dir = ctx.app.config.dataDir; |
| 907 | 907 | if (ctx.params.name !== spriteAsset().name) { |
| 908 | 908 | 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); | |
| 910 | 910 | } |
| 911 | 911 | return fileResponse(ctx.request, spriteAsset()); |
| 912 | 912 | }); |
| 913 | 913 | // The machine shell's terminal emulator (`web/machine-term-asset.ts`). |
| 914 | 914 | 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(), | |
| 916 | 916 | ); |
| 917 | 917 | 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(), | |
| 919 | 919 | ); |
| 920 | 920 | // The service worker: root scope needs a root path, and it must never be |
| 921 | 921 | // 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 }[] { | ||
| 74 | 74 | * this address; `plain` leaves that out for a miss that must reveal nothing |
| 75 | 75 | * (a dead `/go/:code`). */ |
| 76 | 76 | 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); | |
| 80 | 83 | if (typed) return typed; |
| 81 | 84 | const get = !opts.plain && ctx.request.method === "GET"; |
| 82 | 85 | 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 () => { | ||
| 117 | 117 | expect(res.headers.get("cache-control") ?? "").not.toContain("max-age=31536000"); |
| 118 | 118 | } |
| 119 | 119 | }); |
| 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 | }); |
new above
offline
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.