Public: anyone can read this; the crew can change it.
A repeat request with the ETag the forge sent is a 304: weak validators match, and one helper answers If-None-Match everywhere #14379
Check · Review · Land · Live done — Landed and live in production.
mason/forge:mason/14367-a-repeat-request-with-the-etag-the-forge → main 5 commits fork 1 thread · 1 unresolved
15 files
changed +111
−30
src/http/api.ts +3 −2
| @@ -8,6 +8,7 @@ | ||
| 8 | 8 | * back to continue); errors are `{ error, message }`. |
| 9 | 9 | */ |
| 10 | 10 | |
| 11 | import { noneMatch } from "./conditional.ts"; | |
| 11 | 12 | import { queryTooLong, QUERY_TOO_LONG } from "../core/query_limit.ts"; |
| 12 | 13 | import { offeredRepoIds } from "../core/offered.ts"; |
| 13 | 14 | import { parseAttestation } from "../core/attestation.ts"; |
| @@ -1357,9 +1358,9 @@ export function apiRoutes(router: Router<Handler>): void { | ||
| 1357 | 1358 | vary: "authorization, cookie", |
| 1358 | 1359 | etag, |
| 1359 | 1360 | }; |
| 1360 | const sent = (c.request.headers.get("if-none-match") ?? "").split(",").map((t) => t.trim().replace(/^W\//, "")); | |
| 1361 | const sent = c.request.headers.get("if-none-match"); | |
| 1361 | 1362 | return () => |
| 1362 | sent.includes(etag) || sent.includes("*") ? new Response(null, { status: 304, headers }) : json(copy.body, 200, headers); | |
| 1363 | noneMatch(sent, etag) ? new Response(null, { status: 304, headers }) : json(copy.body, 200, headers); | |
| 1363 | 1364 | })); |
| 1364 | 1365 | router.add("GET", "/api/v1/telemetry/home", telemetryRead("home", (c) => homeTruth(c.app, c.auth.user!))); |
| 1365 | 1366 | router.add("GET", "/api/v1/telemetry/sources", telemetryRead("sources", () => ({ sources: sourcesOut() }))); |
src/http/conditional.ts added +21
| @@ -0,0 +1,21 @@ | ||
| 1 | /** | |
| 2 | * Whether a request's `If-None-Match` names a tag: the one place that decides it. | |
| 3 | * RFC 9110 13.1.2: the weak comparison, so `W/` on either side is ignored, a list | |
| 4 | * matches when any member does, and `*` matches any current representation. The | |
| 5 | * edge weakens a tag when it compresses a body, so a strong tag may come back weak. | |
| 6 | */ | |
| 7 | ||
| 8 | const bare = (tag: string): string => tag.trim().replace(/^W\//, ""); | |
| 9 | ||
| 10 | /** Does this `If-None-Match` value (null when absent) hold `etag`? */ | |
| 11 | export function noneMatch(header: string | null | undefined, etag: string): boolean { | |
| 12 | if (header === null || header === undefined) return false; | |
| 13 | if (header.trim() === "*") return true; | |
| 14 | const want = bare(etag); | |
| 15 | return header.split(",").some((t) => bare(t) === want); | |
| 16 | } | |
| 17 | ||
| 18 | /** Does the request's `If-None-Match` hold `etag`? */ | |
| 19 | export function requestHolds(request: Request, etag: string): boolean { | |
| 20 | return noneMatch(request.headers.get("if-none-match"), etag); | |
| 21 | } |
src/http/etag.ts +3 −5
| @@ -10,6 +10,8 @@ | ||
| 10 | 10 | * flash, and any that are not 200 keep no ETag. |
| 11 | 11 | */ |
| 12 | 12 | |
| 13 | import { requestHolds } from "./conditional.ts"; | |
| 14 | ||
| 13 | 15 | /** A page may be tagged only when nothing in it is for one person. */ |
| 14 | 16 | export function taggable(request: Request, signedIn: boolean, flash: boolean): boolean { |
| 15 | 17 | return ( |
| @@ -25,9 +27,5 @@ export function weakEtag(bytes: string): string { | ||
| 25 | 27 | |
| 26 | 28 | /** Whether `If-None-Match` holds this tag (weak comparison). */ |
| 27 | 29 | export function holds(request: Request, etag: string): boolean { |
| 28 | const held = request.headers.get("if-none-match"); | |
| 29 | if (held === null) return false; | |
| 30 | if (held.trim() === "*") return true; | |
| 31 | const bare = (t: string) => t.trim().replace(/^W\//, ""); | |
| 32 | return held.split(",").some((t) => bare(t) === bare(etag)); | |
| 30 | return requestHolds(request, etag); | |
| 33 | 31 | } |
src/web/asset.ts +2 −3
| @@ -16,6 +16,7 @@ | ||
| 16 | 16 | * JavaScript switched off. |
| 17 | 17 | */ |
| 18 | 18 | |
| 19 | import { noneMatch } from "../http/conditional.ts"; | |
| 19 | 20 | import { existsSync, mkdirSync, readdirSync, readFileSync, renameSync, rmSync, statSync, utimesSync, writeFileSync } from "node:fs"; |
| 20 | 21 | import { join } from "node:path"; |
| 21 | 22 | import { sha256 } from "../kernel/hash.ts"; |
| @@ -94,9 +95,7 @@ export function staticResponse(request: Request, body: string | Uint8Array<Array | ||
| 94 | 95 | } |
| 95 | 96 | |
| 96 | 97 | function matches(header: string | null, etag: string): boolean { |
| 97 | if (header === null) return false; | |
| 98 | if (header.trim() === "*") return true; | |
| 99 | return header.split(",").some((tag) => tag.trim() === etag); | |
| 98 | return noneMatch(header, etag); | |
| 100 | 99 | } |
| 101 | 100 | |
| 102 | 101 | /** The response for a hit on the built asset: immutable, because it is. */ |
src/web/feeds.ts +2 −5
| @@ -14,6 +14,7 @@ | ||
| 14 | 14 | * private repository's feed is for its readers, like its pages. |
| 15 | 15 | */ |
| 16 | 16 | |
| 17 | import { noneMatch } from "../http/conditional.ts"; | |
| 17 | 18 | import { plainTitle } from "../story/gather.ts"; |
| 18 | 19 | import { eventActor, eventVerb } from "../design/activity.ts"; |
| 19 | 20 | import { audienceFilter } from "../core/event_audience.ts"; |
| @@ -79,11 +80,7 @@ export function pushedBranch(event: ForgeEvent): string { | ||
| 79 | 80 | |
| 80 | 81 | /** Does an `If-None-Match` header name this tag (weakly, or as `*`)? */ |
| 81 | 82 | export function holds(header: string | null, etag: string): boolean { |
| 82 | if (header === null) return false; | |
| 83 | return header | |
| 84 | .split(",") | |
| 85 | .map((t) => t.trim().replace(/^W\//, "")) | |
| 86 | .some((t) => t === "*" || t === etag.replace(/^W\//, "")); | |
| 83 | return noneMatch(header, etag); | |
| 87 | 84 | } |
| 88 | 85 | |
| 89 | 86 | export function activityAtom(ctx: Ctx): Response { |
src/web/layout.ts +2 −1
| @@ -7,6 +7,7 @@ | ||
| 7 | 7 | * `/search`), and every dialog (a link to a real page). |
| 8 | 8 | */ |
| 9 | 9 | |
| 10 | import { requestHolds } from "../http/conditional.ts"; | |
| 10 | 11 | import { sshOn } from "../settings/ssh.ts"; |
| 11 | 12 | import { returnTo, withReturn } from "./return_to.ts"; |
| 12 | 13 | import { waitingCount } from "../actions/conversation_confirms.ts"; |
| @@ -1125,7 +1126,7 @@ export function fragment( | ||
| 1125 | 1126 | if (status !== 200) return new Response(text, { status, headers }); |
| 1126 | 1127 | const hash = o.etag ?? sha256(text).slice(0, 16); |
| 1127 | 1128 | headers.etag = `"${hash}"`; |
| 1128 | if (o.request?.headers.get("if-none-match") === headers.etag) | |
| 1129 | if (o.request !== undefined && requestHolds(o.request, headers.etag)) | |
| 1129 | 1130 | return new Response(null, { status: 304, headers }); |
| 1130 | 1131 | return new Response( |
| 1131 | 1132 | text.replace(/^(\s*<[a-zA-Z][^\s>]*)/, `$1 data-etag="${hash}"`), |
src/web/pages/build_cart.ts +2 −1
| @@ -6,6 +6,7 @@ | ||
| 6 | 6 | * no request back with the viewer's session. |
| 7 | 7 | */ |
| 8 | 8 | |
| 9 | import { requestHolds } from "../../http/conditional.ts"; | |
| 9 | 10 | import { cartOf } from "../../actions/build_cart.ts"; |
| 10 | 11 | import { readArt, readCapture } from "../../arcade/carts.ts"; |
| 11 | 12 | import { notePhase, takeSessionTime } from "../../core/route_timing.ts"; |
| @@ -35,7 +36,7 @@ async function cartBytes(ctx: Ctx, art: boolean): Promise<Response> { | ||
| 35 | 36 | etag, |
| 36 | 37 | "cache-control": "private, no-cache", |
| 37 | 38 | }; |
| 38 | if (ctx.request.headers.get("if-none-match") === etag) return new Response(null, { status: 304, headers }); | |
| 39 | if (requestHolds(ctx.request, etag)) return new Response(null, { status: 304, headers }); | |
| 39 | 40 | return new Response(bytes, { headers }); |
| 40 | 41 | } |
| 41 | 42 |
src/web/pages/build_shots.ts +2 −1
| @@ -5,6 +5,7 @@ | ||
| 5 | 5 | * following `build.shots` live. |
| 6 | 6 | */ |
| 7 | 7 | |
| 8 | import { requestHolds } from "../../http/conditional.ts"; | |
| 8 | 9 | import { readBuildPin } from "../../git/builds.ts"; |
| 9 | 10 | import { readShotBytes, readShots } from "../../git/shots.ts"; |
| 10 | 11 | import { repoDir } from "../../git/repos.ts"; |
| @@ -63,6 +64,6 @@ export async function shotImage(ctx: Ctx): Promise<Response> { | ||
| 63 | 64 | "cache-control": |
| 64 | 65 | ctx.url.searchParams.get("v") === shot.oid.slice(0, 12) ? "private, max-age=31536000, immutable" : "private, no-cache", |
| 65 | 66 | }; |
| 66 | if (ctx.request.headers.get("if-none-match") === etag) return new Response(null, { status: 304, headers }); | |
| 67 | if (requestHolds(ctx.request, etag)) return new Response(null, { status: 304, headers }); | |
| 67 | 68 | return new Response(shot.bytes, { headers }); |
| 68 | 69 | } |
src/web/pages/home_flow.ts +3 −2
| @@ -15,6 +15,7 @@ | ||
| 15 | 15 | * version without being drawn. The page is a static picture with scripts off. |
| 16 | 16 | */ |
| 17 | 17 | |
| 18 | import { requestHolds } from "../../http/conditional.ts"; | |
| 18 | 19 | import { setCookie } from "../../auth/cookies.ts"; |
| 19 | 20 | import type { App } from "../../app.ts"; |
| 20 | 21 | import { listChildren } from "../../actions/spawn.ts"; |
| @@ -585,7 +586,7 @@ export async function liveTableFragment(ctx: Ctx): Promise<Response> { | ||
| 585 | 586 | const rendering = performance.now(); |
| 586 | 587 | if (picture === undefined) return fragment(readingTable(q), { request: ctx.request }); |
| 587 | 588 | const tag = tableTag(picture, user.isAdmin ? "admin" : user.id, q, now); |
| 588 | if (ctx.request.headers.get("if-none-match") === `"${tag}"`) { | |
| 589 | if (requestHolds(ctx.request, `"${tag}"`)) { | |
| 589 | 590 | notePhase(ctx.request, "render", performance.now() - rendering); |
| 590 | 591 | return new Response(null, { status: 304, headers: { etag: `"${tag}"`, "cache-control": "no-store" } }); |
| 591 | 592 | } |
| @@ -622,7 +623,7 @@ export async function liveLaneFragment(ctx: Ctx): Promise<Response> { | ||
| 622 | 623 | const mine = waitsOf(ctx.url); |
| 623 | 624 | const waits = mine ? waitingOnMe(ctx, user) : undefined; |
| 624 | 625 | const tag = laneTag(picture, key, user.isAdmin ? "admin" : user.id, only, now, waits === undefined ? "" : waitsDigest(waits)); |
| 625 | if (ctx.request.headers.get("if-none-match") === `"${tag}"`) { | |
| 626 | if (requestHolds(ctx.request, `"${tag}"`)) { | |
| 626 | 627 | notePhase(ctx.request, "render", performance.now() - rendering); |
| 627 | 628 | return new Response(null, { status: 304, headers: { etag: `"${tag}"`, "cache-control": "no-store" } }); |
| 628 | 629 | } |
src/web/pages/looks.ts +2 −1
| @@ -7,6 +7,7 @@ | ||
| 7 | 7 | * long as anyone likes and is never the wrong commit's. |
| 8 | 8 | */ |
| 9 | 9 | |
| 10 | import { requestHolds } from "../../http/conditional.ts"; | |
| 10 | 11 | import { closeSync, existsSync, openSync, readSync } from "node:fs"; |
| 11 | 12 | import { jpegHeader } from "../../design/image/jpeg.ts"; |
| 12 | 13 | import { isPng, pngHeader } from "../../design/image/png.ts"; |
| @@ -104,7 +105,7 @@ export function lookImage(ctx: Ctx): Response { | ||
| 104 | 105 | : "private, no-cache", |
| 105 | 106 | }; |
| 106 | 107 | // Only after `mayFetchLook`: a 304 must not say a private picture exists. |
| 107 | if (ctx.request.headers.get("if-none-match") === headers.etag) | |
| 108 | if (requestHolds(ctx.request, headers.etag)) | |
| 108 | 109 | return new Response(null, { status: 304, headers }); |
| 109 | 110 | return new Response(Bun.file(path), { headers }); |
| 110 | 111 | } |
src/web/pages/og.ts +2 −1
| @@ -14,6 +14,7 @@ | ||
| 14 | 14 | * unavailable it answers 503 and nothing else is affected. |
| 15 | 15 | */ |
| 16 | 16 | |
| 17 | import { requestHolds } from "../../http/conditional.ts"; | |
| 17 | 18 | import { createHash } from "node:crypto"; |
| 18 | 19 | import { join } from "node:path"; |
| 19 | 20 | import { Fresh } from "../../kernel/fresh.ts"; |
| @@ -441,7 +442,7 @@ export function ogCard(kind: OgKind, format: "svg" | "png" = "svg") { | ||
| 441 | 442 | ...(kind === "repo" ? { vary: "cookie" } : {}), |
| 442 | 443 | "content-type": format === "png" ? "image/png" : "image/svg+xml; charset=utf-8", |
| 443 | 444 | }; |
| 444 | if (ctx.request.headers.get("if-none-match") === etag) | |
| 445 | if (requestHolds(ctx.request, etag)) | |
| 445 | 446 | return new Response(null, { status: 304, headers }); |
| 446 | 447 | if (format === "svg") |
| 447 | 448 | return new Response( |
src/web/standards.ts +4 −3
| @@ -28,6 +28,7 @@ | ||
| 28 | 28 | * FORGE_SECURITY_CONTACT. |
| 29 | 29 | */ |
| 30 | 30 | |
| 31 | import { noneMatch } from "../http/conditional.ts"; | |
| 31 | 32 | import { sshOn } from "../settings/ssh.ts"; |
| 32 | 33 | import { UPSTREAM_REPO } from "../core/upstream.ts"; |
| 33 | 34 | import { CRAWL_DELAY_S, DEEP_SEGMENTS } from "../http/crawl.ts"; |
| @@ -256,7 +257,7 @@ export function releasesJson(ctx: Ctx): Response { | ||
| 256 | 257 | } |
| 257 | 258 | const cache = "public, max-age=3600"; |
| 258 | 259 | const sent = ctx.request.headers.get("if-none-match"); |
| 259 | if (sent !== null && sent.split(",").some((t) => t.trim() === kept.etag)) | |
| 260 | if (noneMatch(sent, kept.etag)) | |
| 260 | 261 | return new Response(null, { |
| 261 | 262 | status: 304, |
| 262 | 263 | headers: { etag: kept.etag, "cache-control": cache }, |
| @@ -371,7 +372,7 @@ export async function sitemapXml(ctx: Ctx): Promise<Response> { | ||
| 371 | 372 | } |
| 372 | 373 | const cache = "public, max-age=300"; |
| 373 | 374 | const sent = ctx.request.headers.get("if-none-match"); |
| 374 | if (sent !== null && sent.split(",").some((t) => t.trim() === kept.etag)) | |
| 375 | if (noneMatch(sent, kept.etag)) | |
| 375 | 376 | return new Response(null, { status: 304, headers: { etag: kept.etag, "cache-control": cache } }); |
| 376 | 377 | const res = text(kept.body, "application/xml; charset=utf-8"); |
| 377 | 378 | res.headers.set("etag", kept.etag); |
| @@ -500,7 +501,7 @@ export async function llmsTxt(ctx: Ctx): Promise<Response> { | ||
| 500 | 501 | // minutes. A reader with an account gets more and stays private. |
| 501 | 502 | const cache = ctx.auth.user === undefined ? "public, max-age=300" : "private, no-cache"; |
| 502 | 503 | const sent = ctx.request.headers.get("if-none-match"); |
| 503 | if (sent !== null && sent.split(",").some((t) => t.trim() === kept.etag)) | |
| 504 | if (noneMatch(sent, kept.etag)) | |
| 504 | 505 | return new Response(null, { status: 304, headers: { etag: kept.etag, "cache-control": cache, vary: "Cookie, Authorization" } }); |
| 505 | 506 | const res = text(kept.body); |
| 506 | 507 | res.headers.set("cache-control", cache); |
src/web/sw.ts +2 −4
| @@ -20,6 +20,7 @@ | ||
| 20 | 20 | * one person's and must not outlive their session. |
| 21 | 21 | */ |
| 22 | 22 | |
| 23 | import { noneMatch } from "../http/conditional.ts"; | |
| 23 | 24 | import { sha256 } from "../kernel/hash.ts"; |
| 24 | 25 | |
| 25 | 26 | export const SW_CACHE = "forge-shell-v2"; |
| @@ -136,10 +137,7 @@ export const SW_CACHE_CONTROL = "private, no-cache"; | ||
| 136 | 137 | * compresses. |
| 137 | 138 | */ |
| 138 | 139 | function held(header: string | null, etag: string): boolean { |
| 139 | if (header === null) return false; | |
| 140 | if (header.trim() === "*") return true; | |
| 141 | const bare = (t: string) => t.trim().replace(/^W\//, ""); | |
| 142 | return header.split(",").some((t) => bare(t) === bare(etag)); | |
| 140 | return noneMatch(header, etag); | |
| 143 | 141 | } |
| 144 | 142 | |
| 145 | 143 | export function serviceWorker(ctx?: { request: Request }): Response { |
test/conditional.test.ts added +60
| @@ -0,0 +1,60 @@ | ||
| 1 | /** | |
| 2 | * One helper decides If-None-Match by weak comparison; each door answers 304 to the tag it sent, | |
| 3 | * weak or strong. | |
| 4 | */ | |
| 5 | import { afterAll, beforeAll, expect, test } from "bun:test"; | |
| 6 | import { mkdtemp, rm } from "node:fs/promises"; | |
| 7 | import { tmpdir } from "node:os"; | |
| 8 | import { join } from "node:path"; | |
| 9 | import { createApp, type App } from "../src/app.ts"; | |
| 10 | import { noneMatch } from "../src/http/conditional.ts"; | |
| 11 | import { serve, type Servers } from "../src/http/server.ts"; | |
| 12 | ||
| 13 | test("noneMatch: weak comparison, a list, and *", () => { | |
| 14 | expect(noneMatch(null, '"a"')).toBe(false); | |
| 15 | expect(noneMatch('"a"', '"a"')).toBe(true); | |
| 16 | expect(noneMatch('W/"a"', '"a"')).toBe(true); | |
| 17 | expect(noneMatch('"a"', 'W/"a"')).toBe(true); | |
| 18 | expect(noneMatch('W/"a"', 'W/"a"')).toBe(true); | |
| 19 | expect(noneMatch('"b", W/"a" ,"c"', '"a"')).toBe(true); | |
| 20 | expect(noneMatch("*", '"a"')).toBe(true); | |
| 21 | expect(noneMatch('"b"', '"a"')).toBe(false); | |
| 22 | expect(noneMatch('"ab"', '"a"')).toBe(false); | |
| 23 | }); | |
| 24 | ||
| 25 | let dir = ""; | |
| 26 | let app: App; | |
| 27 | let servers: Servers; | |
| 28 | ||
| 29 | beforeAll(async () => { | |
| 30 | dir = await mkdtemp(join(tmpdir(), "forge-conditional-")); | |
| 31 | app = createApp({ dataDir: dir, port: 0 }); | |
| 32 | servers = await serve(app); | |
| 33 | }); | |
| 34 | afterAll(async () => { | |
| 35 | servers.stop(); | |
| 36 | app.close(); | |
| 37 | await rm(dir, { recursive: true, force: true }); | |
| 38 | }); | |
| 39 | ||
| 40 | const DOORS = ["/llms.txt", "/sitemap.xml", "/changelog.atom", "/contract/openapi.json", "/.well-known/mcp.json", "/.well-known/mcp/server-card.json"]; | |
| 41 | const weaken = (tag: string) => (tag.startsWith("W/") ? tag : `W/${tag}`); | |
| 42 | const strengthen = (tag: string) => tag.replace(/^W\//, ""); | |
| 43 | ||
| 44 | for (const path of DOORS) { | |
| 45 | for (const encoding of ["identity", "gzip"]) { | |
| 46 | test(`${path} (${encoding}): the tag it sent, weak or strong, is a 304`, async () => { | |
| 47 | const base = { "accept-encoding": encoding }; | |
| 48 | const first = await fetch(`${servers.url}${path}`, { headers: base }); | |
| 49 | expect(first.status).toBe(200); | |
| 50 | const etag = first.headers.get("etag"); | |
| 51 | expect(etag).not.toBeNull(); | |
| 52 | await first.arrayBuffer(); | |
| 53 | for (const sent of [etag!, weaken(etag!), strengthen(etag!)]) { | |
| 54 | const again = await fetch(`${servers.url}${path}`, { headers: { ...base, "if-none-match": sent } }); | |
| 55 | expect(again.status).toBe(304); | |
| 56 | await again.arrayBuffer(); | |
| 57 | } | |
| 58 | }); | |
| 59 | } | |
| 60 | } |
test/security-sweep.test.ts +1 −1
| @@ -245,7 +245,7 @@ test("a cross-origin mutation with a session cookie is refused before any handle | ||
| 245 | 245 | expect(seen.filter((s) => s.cookies.length > 0).map((s) => `${s.label} sets ${s.cookies.map(cookieName).join(", ")}`)).toEqual([]); |
| 246 | 246 | const still = sessionOf(app.db, token); |
| 247 | 247 | expect(still.ok && still.value.user.username).toBe(USERNAME); |
| 248 | }, 2_000); | |
| 248 | }, 15_000); | |
| 249 | 249 | |
| 250 | 250 | test("signing in from the forge's own page sets the session cookie HttpOnly, with the floor", async () => { |
| 251 | 251 | const form = await send( |
new above
offline
Nit: this is now a pure alias of requestHolds; fold callers onto the helper later so there is one name.