Public: anyone can read this; the crew can change it.
Every route answers HEAD and OPTIONS as HTTP says: HEAD carries GET's length and encoding, OPTIONS answers 204 with Allow, a wrong method on a page is 405 #14386
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/14363-every-route-answers-head-and-options-as → main 6 commits fork 3 threads · 3 unresolved
16 files
changed +268
−41
src/http/compress.ts −1
| @@ -151,7 +151,6 @@ export async function compress(request: Request, response: Response): Promise<Re | ||
| 151 | 151 | const encoding = negotiate(request.headers.get("accept-encoding")); |
| 152 | 152 | if ( |
| 153 | 153 | encoding === null || |
| 154 | request.method === "HEAD" || | |
| 155 | 154 | response.body === null || |
| 156 | 155 | response.status === 206 || |
| 157 | 156 | response.headers.has("content-encoding") || |
src/http/flow_stream.ts +1 −1
| @@ -63,7 +63,7 @@ function subscribe(ctx: Ctx, flowId: string, firstDelaySecs: number, onSettled: | ||
| 63 | 63 | |
| 64 | 64 | export function originFlowStream(ctx: Ctx): Response { |
| 65 | 65 | const user = ctx.auth.user; |
| 66 | if (!user) return new Response("sign in first", { status: 401 }); | |
| 66 | if (!user) return new Response("sign in first", { status: 401, headers: { "content-type": "text/plain; charset=utf-8" } }); | |
| 67 | 67 | const flowId = ctx.params.id ?? ""; |
| 68 | 68 | const flow = getFlowFor(ctx.app.db, user.id, flowId); |
| 69 | 69 | if (!flow) return notFoundPage(ctx); |
src/http/head.ts added +71
| @@ -0,0 +1,71 @@ | ||
| 1 | /** | |
| 2 | * HEAD and OPTIONS as HTTP says. HEAD is GET's answer without the body: the | |
| 3 | * same status and headers, Content-Length and Content-Encoding included, so | |
| 4 | * the render that produced it runs once. OPTIONS on a path that exists is a | |
| 5 | * 204 naming its methods; no CORS header is added (same-origin stays the rule). | |
| 6 | */ | |
| 7 | ||
| 8 | /** Bodies that never end or are not worth holding to measure: their HEAD carries no length. */ | |
| 9 | const OPEN_ENDED = /^(text\/event-stream|application\/x-git-)/i; | |
| 10 | ||
| 11 | /** The most a HEAD reads to measure a body; past it, no Content-Length is sent. */ | |
| 12 | export const MEASURE_CAP = 1 << 20; | |
| 13 | ||
| 14 | /** The body's bytes up to the cap, or null once it is past it (the rest is cancelled). */ | |
| 15 | async function measure(body: ReadableStream<Uint8Array>): Promise<Uint8Array[] | null> { | |
| 16 | const reader = body.getReader(); | |
| 17 | const chunks: Uint8Array[] = []; | |
| 18 | let size = 0; | |
| 19 | try { | |
| 20 | for (;;) { | |
| 21 | const { done, value } = await reader.read(); | |
| 22 | if (done) return chunks; | |
| 23 | size += value.byteLength; | |
| 24 | if (size > MEASURE_CAP) return null; | |
| 25 | chunks.push(value); | |
| 26 | } | |
| 27 | } catch { | |
| 28 | return null; | |
| 29 | } finally { | |
| 30 | void reader.cancel().catch(() => {}); | |
| 31 | } | |
| 32 | } | |
| 33 | ||
| 34 | /** True when the chunks are text: what the runtime types as text/plain when it writes a string. */ | |
| 35 | function isText(chunks: Uint8Array[]): boolean { | |
| 36 | try { | |
| 37 | const decoder = new TextDecoder("utf-8", { fatal: true }); | |
| 38 | for (const chunk of chunks) decoder.decode(chunk, { stream: true }); | |
| 39 | decoder.decode(); | |
| 40 | return true; | |
| 41 | } catch { | |
| 42 | return false; | |
| 43 | } | |
| 44 | } | |
| 45 | ||
| 46 | /** GET's response as HEAD answers it: its headers, the length of the body it had, no body. */ | |
| 47 | export async function headOf(response: Response): Promise<Response> { | |
| 48 | const body = response.body; | |
| 49 | if (body === null) return response; | |
| 50 | const type = response.headers.get("content-type") ?? ""; | |
| 51 | const headers = new Headers(response.headers); | |
| 52 | if (response.headers.has("content-length") || OPEN_ENDED.test(type)) { | |
| 53 | // The body nobody will read is cancelled, so a stream behind it ends now. | |
| 54 | void body.cancel().catch(() => {}); | |
| 55 | return new Response(null, { status: response.status, headers }); | |
| 56 | } | |
| 57 | // A bounded read: a large or endless stream costs the cap, never its whole length. | |
| 58 | const chunks = await measure(body); | |
| 59 | if (chunks !== null) { | |
| 60 | headers.set("content-length", String(chunks.reduce((n, c) => n + c.byteLength, 0))); | |
| 61 | // A string body's type is the runtime's to add when it writes GET; HEAD names it too. | |
| 62 | if (type === "" && chunks.length > 0 && isText(chunks)) headers.set("content-type", "text/plain;charset=utf-8"); | |
| 63 | } | |
| 64 | return new Response(null, { status: response.status, headers }); | |
| 65 | } | |
| 66 | ||
| 67 | /** The 204 an OPTIONS on an existing path answers: Allow, with OPTIONS among the methods. */ | |
| 68 | export function optionsAnswer(allowed: string[]): Response { | |
| 69 | const methods = [...new Set([...allowed, "OPTIONS"])].sort(); | |
| 70 | return new Response(null, { status: 204, headers: { allow: methods.join(", ") } }); | |
| 71 | } |
src/http/plugin_routes.ts +2 −2
| @@ -55,7 +55,7 @@ export const opencodePlugin: Handler = (ctx) => { | ||
| 55 | 55 | const { bytes, etag } = kept; |
| 56 | 56 | return holds(ctx.request, etag) |
| 57 | 57 | ? new Response(null, { status: 304, headers: { etag, "cache-control": headers["cache-control"] } }) |
| 58 | : new Response(ctx.request.method === "HEAD" ? null : bytes, { headers: { ...headers, etag } }); | |
| 58 | : new Response(bytes, { headers: { ...headers, etag } }); | |
| 59 | 59 | }; |
| 60 | 60 | |
| 61 | 61 | /** |
| @@ -70,7 +70,7 @@ export function marketplaceManifest(dir: string = COMMANDS, upstream: { repo: st | ||
| 70 | 70 | // The configured address, never the Host header: the response is publicly cacheable. |
| 71 | 71 | const origin = publicOrigin(ctx.app.config.url, ctx.url.origin); |
| 72 | 72 | const body = JSON.stringify(claudeMarketplace(origin, repo, files), null, 2) + "\n"; |
| 73 | return new Response(ctx.request.method === "HEAD" ? null : body, { | |
| 73 | return new Response(body, { | |
| 74 | 74 | headers: { "content-type": "application/json; charset=utf-8", "cache-control": "public, max-age=300" }, |
| 75 | 75 | }); |
| 76 | 76 | }; |
src/http/server.ts +16 −23
| @@ -655,6 +655,7 @@ import { | ||
| 655 | 655 | tryAgain, |
| 656 | 656 | } from "./respond.ts"; |
| 657 | 657 | import { compress } from "./compress.ts"; |
| 658 | import { headOf, optionsAnswer } from "./head.ts"; | |
| 658 | 659 | import { RateLimiter, type Verdict } from "../core/ratelimit.ts"; |
| 659 | 660 | import { |
| 660 | 661 | MAX_KEY_LENGTH, |
| @@ -2938,7 +2939,8 @@ export async function serve(app: App): Promise<Servers> { | ||
| 2938 | 2939 | if (url.pathname.startsWith("/api/v1/")) |
| 2939 | 2940 | response.headers.set("www-authenticate", bearerChallenge(url.origin, request)); |
| 2940 | 2941 | } |
| 2941 | const done = await compress(request, response).then((r) => securityFloor(resourcePolicy(request, r))).then((r) => privateWhenSignedIn(request, r)); | |
| 2942 | const whole = await compress(request, response).then((r) => securityFloor(resourcePolicy(request, r))).then((r) => privateWhenSignedIn(request, r)); | |
| 2943 | const done = request.method === "HEAD" ? await headOf(whole) : whole; | |
| 2942 | 2944 | if (namesFrame(request)) notePhase(request, "compress", performance.now() - compressing); |
| 2943 | 2945 | return done; |
| 2944 | 2946 | }, |
| @@ -3231,7 +3233,8 @@ export async function serve(app: App): Promise<Servers> { | ||
| 3231 | 3233 | * HEAD is answered here too, in one place: it routes as the GET it stands |
| 3232 | 3234 | * in for — so a path with only a POST route still 404s — and the body is |
| 3233 | 3235 | * dropped from whatever GET would have returned, after the one render that |
| 3234 | * produced it. | |
| 3236 | * produced it and after compression, so its length and encoding are GET's | |
| 3237 | * (`headOf`, `head.ts`). | |
| 3235 | 3238 | */ |
| 3236 | 3239 | async function answer( |
| 3237 | 3240 | app: App, |
| @@ -3239,23 +3242,10 @@ async function answer( | ||
| 3239 | 3242 | apiRouter: Router<Handler>, |
| 3240 | 3243 | limiter: RateLimiter, |
| 3241 | 3244 | request: Request, |
| 3242 | ): Promise<Response> { | |
| 3243 | const response = await answerGet(app, router, apiRouter, limiter, request); | |
| 3244 | if (request.method !== "HEAD") return response; | |
| 3245 | // The body nobody will read is cancelled, so a stream behind it (an | |
| 3246 | // event stream's slot, a git process) ends now rather than never. | |
| 3247 | void response.body?.cancel().catch(() => {}); | |
| 3248 | return new Response(null, { status: response.status, headers: response.headers }); | |
| 3249 | } | |
| 3250 | ||
| 3251 | async function answerGet( | |
| 3252 | app: App, | |
| 3253 | router: Router<Handler>, | |
| 3254 | apiRouter: Router<Handler>, | |
| 3255 | limiter: RateLimiter, | |
| 3256 | request: Request, | |
| 3257 | 3245 | ): Promise<Response> { |
| 3258 | 3246 | const url = publicUrl(request, app.config.url); |
outdated
|
||
| 3247 | if ((url.pathname === "/healthz" || url.pathname === "/readyz") && request.method === "OPTIONS") | |
| 3248 | return optionsAnswer(["GET", "HEAD"]); | |
| 3259 | 3249 | if (url.pathname === "/healthz") { |
| 3260 | 3250 | noteUsage(request, { pattern: "/healthz", role: "anon" }); |
| 3261 | 3251 | const dbStart = performance.now(); |
| @@ -3305,6 +3295,15 @@ async function answerGet( | ||
| 3305 | 3295 | // even for someone who is. |
| 3306 | 3296 | // A plain GET (curl, a crawler, a link unfurler) names no type and gets the page too; |
| 3307 | 3297 | // machine paths and anyone who asks for JSON keep the JSON body. |
| 3298 | // The path exists under other methods: OPTIONS names them, anything else is a 405 with Allow, | |
| 3299 | // before any page's 404. The API root is read-only and answers a write as it always did: no | |
| 3300 | // route. | |
| 3301 | const apiRootPath = url.pathname === "/api/v1" || url.pathname === "/api/v1/"; | |
| 3302 | if (!apiRootPath) { | |
| 3303 | const allowed = (isApiPath || url.pathname === "/metrics" || url.pathname === "/health" ? apiRouter : router).allowed(url.pathname); | |
| 3304 | if (allowed.length > 0) | |
| 3305 | return request.method === "OPTIONS" ? optionsAnswer(allowed) : methodNotAllowed(request.method, allowed); | |
| 3306 | } | |
| 3308 | 3307 | const typed = typedMiss(request, url.pathname); |
| 3309 | 3308 | if (!isApiPath && typed) return typed; |
| 3310 | 3309 | const machinePath = MACHINE_PREFIX.test(url.pathname) || /\.(?:json|git)(?:\/|$)|\.git\//.test(url.pathname); |
| @@ -3323,12 +3322,6 @@ async function answerGet( | ||
| 3323 | 3322 | // it offers no sign-in rather than one to someone who has signed in. |
| 3324 | 3323 | return notFoundPage(anonCtx, { plain: true }); |
| 3325 | 3324 | } |
| 3326 | // The API root is read-only and answers a write as it always did: no route. | |
| 3327 | const apiRootPath = url.pathname === "/api/v1" || url.pathname === "/api/v1/"; | |
| 3328 | if (isApiPath && !apiRootPath && request.method !== "OPTIONS") { | |
| 3329 | const allowed = apiRouter.allowed(url.pathname); | |
| 3330 | if (allowed.length > 0) return methodNotAllowed(request.method, allowed); | |
| 3331 | } | |
| 3332 | 3325 | return noRoute(request.method, url.pathname); |
| 3333 | 3326 | } |
| 3334 | 3327 | const authStart = performance.now(); |
src/web/asset.ts +1 −1
| @@ -88,7 +88,7 @@ export function staticResponse(request: Request, body: string | Uint8Array<Array | ||
| 88 | 88 | return new Response(null, { status: 304, headers }); |
| 89 | 89 | const encoding = negotiate(request.headers.get("accept-encoding")); |
| 90 | 90 | const bytes = typeof body === "string" ? new TextEncoder().encode(body) : body; |
| 91 | if (encoding === null || bytes.length < MIN_BYTES || request.method === "HEAD") return new Response(bytes, { headers }); | |
| 91 | if (encoding === null || bytes.length < MIN_BYTES) return new Response(bytes, { headers }); | |
| 92 | 92 | // Hashed, so the encoded bytes are as immutable as the plain ones: made once. |
| 93 | 93 | return new Response(encodeOnce(etag, bytes, encoding), { headers: { ...headers, "content-encoding": encoding } }); |
| 94 | 94 | } |
src/web/pages/docs_link.ts +1 −1
| @@ -150,7 +150,7 @@ function guideMarkdown(ctx: Ctx, name: string): Response { | ||
| 150 | 150 | const etag = `"${Bun.hash(text).toString(36)}"`; |
| 151 | 151 | const headers = { "content-type": "text/markdown; charset=utf-8", etag, "cache-control": "public, no-cache" }; |
| 152 | 152 | if (holds(ctx.request, etag)) return new Response(null, { status: 304, headers }); |
| 153 | return new Response(ctx.request.method === "HEAD" ? null : text, { headers }); | |
| 153 | return new Response(text, { headers }); | |
| 154 | 154 | } |
| 155 | 155 | |
| 156 | 156 | /** /docs/features/NAME.md: the reference notes the guide pages link to. */ |
test/asks.test.ts +1 −1
| @@ -304,7 +304,7 @@ describe("the asks page", () => { | ||
| 304 | 304 | body: new URLSearchParams({ key: "checks.concurrency", answer: "put" }), |
| 305 | 305 | redirect: "manual", |
| 306 | 306 | }); |
| 307 | expect(res.status).toBe(404); | |
| 307 | expect(res.status).toBe(405); // the path only fits a page pattern that takes GET | |
outdated
Either/or status passes on the old behaviour too. Assert the exact status this path now gets. |
||
| 308 | 308 | expect(storedCapacity(app.db, "checks.concurrency")).toBe("4"); |
| 309 | 309 | app.db.query("DELETE FROM forge_setting").run(); |
| 310 | 310 | app.db.query("DELETE FROM sentinel_finding").run(); |
test/compress.test.ts +2 −1
| @@ -41,7 +41,8 @@ test("small bodies, other types, HEAD, and clients without support are left alon | ||
| 41 | 41 | expect(s.headers.get("vary")).toBe(null); |
| 42 | 42 | const git = new Response(big, { headers: { "content-type": "application/x-git-upload-pack-result" } }); |
| 43 | 43 | expect((await compress(req("br"), git)).headers.get("content-encoding")).toBe(null); |
| 44 | expect((await compress(req("br", "HEAD"), html(big))).headers.get("content-encoding")).toBe(null); | |
| 44 | // HEAD carries GET's encoding (#14363); its body is dropped after, in `headOf`. | |
| 45 | expect((await compress(req("br", "HEAD"), html(big))).headers.get("content-encoding")).toBe("br"); | |
| 45 | 46 | expect((await compress(req(), html(big))).headers.get("content-encoding")).toBe(null); |
| 46 | 47 | expect((await compress(req("br"), html(big, { "content-encoding": "gzip" }))).headers.get("content-encoding")).toBe("gzip"); |
| 47 | 48 | }); |
test/error-page-500.test.ts +1 −1
| @@ -84,7 +84,7 @@ test("a machine route keeps its JSON error, the id in the log and in x-request-i | ||
| 84 | 84 | |
| 85 | 85 | test("no Accept header, a wildcard, or a mutation gets JSON, not a page", async () => { |
| 86 | 86 | for (const [accept, method] of [[undefined, "GET"], ["*/*", "GET"], ["text/html", "POST"]] as const) { |
| 87 | const { res, body } = await failing("/", accept, method); | |
| 87 | const { res, body } = await failing(method === "POST" ? "/welcome" : "/", accept, method); | |
| 88 | 88 | expect(res.status).toBe(500); |
| 89 | 89 | expect(res.headers.get("content-type")).toStartWith("application/json"); |
| 90 | 90 | const data = JSON.parse(body); |
test/head-options-sweep.test.ts added +165
| @@ -0,0 +1,165 @@ | ||
| 1 | /** | |
| 2 | * HEAD and OPTIONS as HTTP says, on every route (#14363): HEAD on a GET route | |
| 3 | * carries GET's status, length, encoding and validators with no body; OPTIONS | |
| 4 | * on a path that exists is a 204 with Allow and no CORS header; a method the | |
| 5 | * path does not take is a 405 with Allow, on a page as on the API. The floor | |
| 6 | * is on each answer. | |
| 7 | */ | |
| 8 | import { afterAll, beforeAll, expect, test } from "bun:test"; | |
| 9 | import { mkdtemp, rm } from "node:fs/promises"; | |
| 10 | import { tmpdir } from "node:os"; | |
| 11 | import { join } from "node:path"; | |
| 12 | import { createApp, type App } from "../src/app.ts"; | |
| 13 | import { createUser } from "../src/core/users.ts"; | |
| 14 | import { setLimit } from "../src/settings/limits.ts"; | |
| 15 | import { setStrangerLimits, strangerLimits } from "../src/http/strangers.ts"; | |
| 16 | import { headOf, MEASURE_CAP } from "../src/http/head.ts"; | |
| 17 | import { buildApiRouter, buildRouter, serve, type Servers } from "../src/http/server.ts"; | |
| 18 | import { quietLogs } from "./helpers/globals.ts"; | |
| 19 | import { pool } from "./helpers/sweep-clock.ts"; | |
| 20 | ||
| 21 | quietLogs(); | |
| 22 | ||
| 23 | let dataDir = ""; | |
| 24 | let app: App, servers: Servers; | |
| 25 | ||
| 26 | beforeAll(async () => { | |
| 27 | dataDir = await mkdtemp(join(tmpdir(), "forge-head-")); | |
| 28 | app = createApp({ dataDir, port: 0 }); | |
| 29 | // A sweep of every route from one address is not a stranger to be throttled. | |
| 30 | const per = strangerLimits(app).perAddress; | |
| 31 | setStrangerLimits(app, { perAddress: Object.fromEntries(Object.keys(per).map((k) => [k, 100_000])) as typeof per }); | |
| 32 | servers = await serve(app); | |
| 33 | const admin = await createUser(app.db, { origin: "seed", trusted: true, username: "alice", email: "[email protected]", password: "a-good-passphrase", adminIfFirst: true }); | |
| 34 | if (!admin.ok) throw new Error(String(admin.error)); | |
| 35 | const set = setLimit(app.db, "api.anonymous_per_source", 100_000, admin.value.id); | |
| 36 | if (!set.ok) throw new Error(set.error.message); | |
| 37 | }); | |
| 38 | afterAll(async () => { | |
| 39 | servers?.stop(); | |
| 40 | app?.close(); | |
| 41 | await rm(dataDir, { recursive: true, force: true }); | |
| 42 | }); | |
| 43 | ||
| 44 | const FLOOR = ["content-security-policy", "x-content-type-options", "referrer-policy"] as const; | |
| 45 | const lacking = (res: Response, what: string) => | |
| 46 | FLOOR.filter((h) => !res.headers.get(h)).map((h) => `${what} lacks ${h}`); | |
| 47 | ||
| 48 | const fill = (pattern: string) => pattern.replace(/[:*][a-z_]+(\.[a-z]+)?/g, (m) => "x" + (m.match(/\.[a-z]+$/)?.[0] ?? "")); | |
| 49 | const request = (path: string, method: string, headers: Record<string, string> = {}) => | |
| 50 | fetch(`${servers.url}${path}`, { method, headers, redirect: "manual", signal: AbortSignal.timeout(5_000) }); | |
| 51 | ||
| 52 | /** Every distinct path the two routers name, with the methods each router takes there. */ | |
| 53 | function table() { | |
| 54 | const out = new Map<string, { path: string; methods: Set<string> }>(); | |
| 55 | for (const build of [buildRouter, buildApiRouter]) | |
| 56 | for (const r of build().routes()) { | |
| 57 | const entry = out.get(r.pattern) ?? { path: fill(r.pattern), methods: new Set<string>() }; | |
| 58 | entry.methods.add(r.method); | |
| 59 | out.set(r.pattern, entry); | |
| 60 | } | |
| 61 | // The API's root is read-only and answers as no route, on purpose. | |
| 62 | out.delete("/api/v1"); | |
| 63 | out.delete("/api/v1/"); | |
| 64 | return [...out.values()]; | |
| 65 | } | |
| 66 | ||
| 67 | test("OPTIONS on every path that exists is a 204 with Allow, the floor, and no CORS", async () => { | |
| 68 | const bad = ( | |
| 69 | await pool(table(), async ({ path, methods }) => { | |
| 70 | const res = await request(path, "OPTIONS"); | |
| 71 | const allow = (res.headers.get("allow") ?? "").split(", "); | |
| 72 | const problems = lacking(res, `OPTIONS ${path}`); | |
| 73 | if (res.status !== 204) problems.push(`OPTIONS ${path} answered ${res.status}`); | |
| 74 | if (!allow.includes("OPTIONS")) problems.push(`OPTIONS ${path} allow ${allow.join(",")} lacks OPTIONS`); | |
| 75 | if (![...methods].some((m) => allow.includes(m))) problems.push(`OPTIONS ${path} allow names none of ${[...methods]}`); | |
| 76 | if ([...res.headers.keys()].some((h) => h.startsWith("access-control-"))) problems.push(`OPTIONS ${path} has CORS`); | |
| 77 | return problems; | |
| 78 | }) | |
| 79 | ).flat(); | |
| 80 | expect(bad).toEqual([]); | |
| 81 | }, 20_000); | |
| 82 | ||
| 83 | /** Each half of the table is its own test, so each stays inside the per-test budget. */ | |
| 84 | for (const half of [0, 1]) | |
| 85 | test(`a method a path does not take is a 405 with Allow, never a 404, on pages as on the API (half ${half + 1} of 2)`, async () => { | |
| 86 | const [pages, api] = [buildRouter(), buildApiRouter()]; | |
| 87 | const mine = table().filter(({ path }, i) => i % 2 === half && !pages.match("DELETE", path) && !api.match("DELETE", path)); | |
| 88 | const bad = ( | |
| 89 | await pool(mine, async ({ path }) => { | |
| 90 | const res = await request(path, "DELETE"); | |
| 91 | const problems = lacking(res, `DELETE ${path}`); | |
| 92 | if (res.status !== 405) problems.push(`DELETE ${path} answered ${res.status}`); | |
| 93 | else if (!res.headers.get("allow")) problems.push(`DELETE ${path} has no allow`); | |
| 94 | return problems; | |
| 95 | }) | |
| 96 | ).flat(); | |
| 97 | expect(bad).toEqual([]); | |
| 98 | }, 20_000); | |
| 99 | ||
| 100 | /** Each half of the GET routes is its own test, so each stays inside the per-test budget. */ | |
| 101 | for (const half of [0, 1]) | |
| 102 | test(`HEAD on every GET route answers GET's status and headers with no body (half ${half + 1} of 2)`, async () => { | |
| 103 | const mine = table().filter(({ methods }, i) => i % 2 === half && methods.has("GET")); | |
| 104 | const headers = { "accept-encoding": "gzip" }; | |
| 105 | const bad = ( | |
| 106 | await pool(mine, async ({ path }) => { | |
| 107 | const problems: string[] = []; | |
| 108 | const get = await request(path, "GET", headers).catch(() => null); | |
| 109 | if (get === null) return [`GET ${path} did not answer`]; | |
| 110 | // An event stream has no end to read; its HEAD is the same headers, so only that is | |
| 111 | // checked. | |
| 112 | const open = /^text\/event-stream/.test(get.headers.get("content-type") ?? ""); | |
| 113 | if (open) void get.body?.cancel(); | |
| 114 | const bytes = open ? new Uint8Array() : await get.arrayBuffer().then((b) => new Uint8Array(b), () => null); | |
| 115 | if (bytes === null) return [`GET ${path} did not end`]; | |
| 116 | const head = await request(path, "HEAD", headers).catch(() => null); | |
| 117 | if (head === null) return [`HEAD ${path} did not answer`]; | |
| 118 | if ((await head.text()) !== "") problems.push(`HEAD ${path} has a body`); | |
| 119 | if (head.status !== get.status) problems.push(`HEAD ${path} ${head.status} vs GET ${get.status}`); | |
| 120 | for (const name of ["content-type", "content-encoding", "etag", "cache-control"]) | |
| 121 | if (head.headers.get(name) !== get.headers.get(name)) | |
| 122 | problems.push(`${path} ${name}: ${head.headers.get(name)} vs ${get.headers.get(name)}`); | |
| 123 | // A GET that names a length is matched; one that is chunked may be chunked on HEAD too. | |
| 124 | const length = get.headers.get("content-length"); | |
| 125 | const got = head.headers.get("content-length"); | |
| 126 | // A page that embeds a per-request id differs by a few bytes between two renders; one with | |
| 127 | // a validator must match exactly. | |
| 128 | const slack = get.headers.has("etag") ? 0 : 64; | |
| 129 | const off = (a: number, b: number) => Math.abs(a - b) > slack; | |
| 130 | if (!open && length !== null && (got === null || off(Number(got), Number(length)))) | |
| 131 | problems.push(`${path} content-length ${got} vs ${length}`); | |
| 132 | if (!open && get.headers.get("content-encoding") === null && got !== null && off(Number(got), bytes.length)) | |
| 133 | problems.push(`${path} content-length ${got} vs ${bytes.length} bytes`); | |
| 134 | return [...problems, ...lacking(head, `HEAD ${path}`)]; | |
| 135 | }) | |
| 136 | ).flat(); | |
| 137 | expect(bad).toEqual([]); | |
| 138 | }, 30_000); | |
| 139 | ||
| 140 | test("HEAD's length is GET's on a known page", async () => { | |
| 141 | const health = await request("/healthz", "HEAD"); | |
| 142 | expect(health.headers.get("content-length")).toBe("2"); | |
| 143 | }); | |
| 144 | ||
| 145 | test("HEAD on an endless body reads a bounded amount, sends no length and ends", async () => { | |
| 146 | let pulled = 0; | |
| 147 | let cancelled = false; | |
| 148 | const endless = new ReadableStream<Uint8Array>({ | |
| 149 | pull(controller) { | |
| 150 | pulled += 65536; | |
| 151 | controller.enqueue(new Uint8Array(65536)); | |
| 152 | }, | |
| 153 | cancel() { | |
| 154 | cancelled = true; | |
| 155 | }, | |
| 156 | }); | |
| 157 | const head = await headOf(new Response(endless, { headers: { "content-type": "application/x-ndjson" } })); | |
| 158 | expect(head.body).toBeNull(); | |
| 159 | expect(head.headers.get("content-length")).toBeNull(); | |
| 160 | expect(head.headers.get("content-type")).toBe("application/x-ndjson"); | |
| 161 | expect(cancelled).toBe(true); | |
| 162 | expect(pulled).toBeLessThan(MEASURE_CAP * 4); | |
| 163 | const small = await headOf(new Response("hello", { headers: { "content-type": "text/plain" } })); | |
| 164 | expect(small.headers.get("content-length")).toBe("5"); | |
| 165 | }); |
test/security-floor-doors.test.ts +1 −1
| @@ -101,7 +101,7 @@ test("each return of the public door and of `answered` is floored", () => { | ||
| 101 | 101 | // `answered` floors what the work returns, and its failure path floors too. |
| 102 | 102 | // The floor wraps the compressed response itself; only `resourcePolicy`, which sets one header on it, sits between. |
| 103 | 103 | expect(region).toMatch( |
| 104 | /const done = await compress\(request, response\)\.then\(\(r\) => securityFloor\(resourcePolicy\(request, r\)\)\)/, | |
| 104 | /const whole = await compress\(request, response\)\.then\(\(r\) => securityFloor\(resourcePolicy\(request, r\)\)\)/, | |
| 105 | 105 | ); |
| 106 | 106 | expect(server).toMatch(/function failure\([^]*?\n}\n/); |
| 107 | 107 | const failure = server.match(/function failure\([^]*?\n}\n/)![0]; |
test/security-floor-errors.test.ts +3 −3
| @@ -93,10 +93,10 @@ const PROVOKED = (): Provoke[] => [ | ||
| 93 | 93 | { status: 404, side: "page", path: "/no/such/path/at/all", init: { headers: HTML } }, |
| 94 | 94 | { status: 404, side: "api", path: "/api/v1/repos/ada/nothing-here" }, |
| 95 | 95 | // 405 |
| 96 | // A page path under the wrong method answers 404 (the page router keeps no | |
| 97 | // method table); the page that answers 405 is /mcp, to a browser. | |
| 96 | // A page path under the wrong method answers 405 with Allow (#14363); the | |
| 97 | // router names its methods per pattern. /mcp is the same, to a browser. | |
| 98 | 98 | { status: 405, side: "page", path: "/mcp", init: { headers: HTML } }, |
| 99 | { status: 404, side: "page", path: "/ada/notes", init: { method: "PUT", headers: HTML } }, | |
| 99 | { status: 405, side: "page", path: "/ada/notes", init: { method: "PUT", headers: HTML } }, | |
| 100 | 100 | { status: 405, side: "api", path: "/api/v1/repos/ada/notes", init: { method: "PUT" } }, |
| 101 | 101 | // 413: a body past the cap, a form and JSON |
| 102 | 102 | { |
test/security-sweep.test.ts +1 −3
| @@ -72,8 +72,6 @@ const FLOOR_EXCEPTIONS: Record<string, string> = {}; | ||
| 72 | 72 | const NO_405: Record<string, string> = { |
| 73 | 73 | "/api/v1": "the API root is read-only and answers a write as it always did: no route (`answerGet`, src/http/server.ts)", |
| 74 | 74 | "/api/v1/": "the API root with its slash, the same read-only root as /api/v1", |
| 75 | "/metrics": "a scraper's path outside /api/, so its miss is the machine-path 404, not the API's 405", | |
| 76 | "/health": "a monitor's path outside /api/ (#13191), so its miss is the machine-path 404 as /metrics's is", | |
| 77 | 75 | }; |
| 78 | 76 | |
| 79 | 77 | /** Cookies a page's own script must read, so not HttpOnly → why. */ |
| @@ -232,7 +230,7 @@ test("an API path asked with a verb it does not take answers 405, naming the one | ||
| 232 | 230 | ) |
| 233 | 231 | .map(({ s }) => `${s.label} allow=${s.allow}`); |
| 234 | 232 | expect(wrongAnswers).toEqual([]); |
| 235 | expect(wrongMethod.at(-1)!.status).toBe(404); | |
| 233 | expect(wrongMethod.at(-1)!.status).toBe(405); // the home page takes GET, so a PATCH is a wrong method, not a miss | |
| 236 | 234 | }, 2_000); |
| 237 | 235 | |
| 238 | 236 | test("a cross-origin mutation with a session cookie is refused before any handler, and the session lives on", async () => { |
test/spawn-first-boot.test.ts +1 −1
| @@ -271,7 +271,7 @@ describe("a spawned child's first boot", () => { | ||
| 271 | 271 | body: new URLSearchParams(maria), |
| 272 | 272 | redirect: "manual", |
| 273 | 273 | }); |
| 274 | expect(res.status).toBe(404); | |
| 274 | expect(res.status).toBe(405); | |
| 275 | 275 | } |
| 276 | 276 | expect(getUserByName(child.db, "maria")).toBeUndefined(); |
| 277 | 277 | expect(unspent(token)).toBe(true); |
test/studio.test.ts +1 −1
| @@ -164,7 +164,7 @@ describe("the studio", () => { | ||
| 164 | 164 | expect(withDev).not.toContain('href="/settings/keys#terminal"'); |
| 165 | 165 | // The toggle's POST is gone with it. |
| 166 | 166 | const flipped = await post("/mode", { mode: "dev", back: "/" }); |
| 167 | expect(flipped.status).toBe(404); | |
| 167 | expect(flipped.status).toBe(405); | |
| 168 | 168 | }); |
| 169 | 169 | |
| 170 | 170 | test("the palette lists apps first, then the other repositories (#906)", async () => { |
new above
offline
Unbounded buffering of an arbitrary body for HEAD. Cap the bytes read (cancel the stream past the cap and send no Content-Length) so an anonymous HEAD cannot cost memory or hang on an open stream.