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

Merged opened this

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

Merged

mason/forge:mason/14367-a-repeat-request-with-the-etag-the-forge → main 5 commits fork 1 thread · 1 unresolved

  • Merged. mason/14367-a-repeat-request-with-the-etag-the-forge was merged into main as d272bd0.

Review

Sign in to review this change.
15 files changed +111 −30
src/http/api.ts +3 −2
@@ -8,6 +8,7 @@
88 * back to continue); errors are `{ error, message }`.
99 */
1010
11 import { noneMatch } from "./conditional.ts";
1112 import { queryTooLong, QUERY_TOO_LONG } from "../core/query_limit.ts";
1213 import { offeredRepoIds } from "../core/offered.ts";
1314 import { parseAttestation } from "../core/attestation.ts";
@@ -1357,9 +1358,9 @@ export function apiRoutes(router: Router<Handler>): void {
13571358 vary: "authorization, cookie",
13581359 etag,
13591360 };
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");
13611362 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);
13631364 }));
13641365 router.add("GET", "/api/v1/telemetry/home", telemetryRead("home", (c) => homeTruth(c.app, c.auth.user!)));
13651366 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 @@
1010 * flash, and any that are not 200 keep no ETag.
1111 */
1212
13 import { requestHolds } from "./conditional.ts";
14
1315 /** A page may be tagged only when nothing in it is for one person. */
1416 export function taggable(request: Request, signedIn: boolean, flash: boolean): boolean {
1517 return (
@@ -25,9 +27,5 @@ export function weakEtag(bytes: string): string {
2527
2628 /** Whether `If-None-Match` holds this tag (weak comparison). */
2729 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);

Nit: this is now a pure alias of requestHolds; fold callers onto the helper later so there is one name.

3331 }
src/web/asset.ts +2 −3
@@ -16,6 +16,7 @@
1616 * JavaScript switched off.
1717 */
1818
19 import { noneMatch } from "../http/conditional.ts";
1920 import { existsSync, mkdirSync, readdirSync, readFileSync, renameSync, rmSync, statSync, utimesSync, writeFileSync } from "node:fs";
2021 import { join } from "node:path";
2122 import { sha256 } from "../kernel/hash.ts";
@@ -94,9 +95,7 @@ export function staticResponse(request: Request, body: string | Uint8Array<Array
9495 }
9596
9697 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);
10099 }
101100
102101 /** The response for a hit on the built asset: immutable, because it is. */
src/web/feeds.ts +2 −5
@@ -14,6 +14,7 @@
1414 * private repository's feed is for its readers, like its pages.
1515 */
1616
17 import { noneMatch } from "../http/conditional.ts";
1718 import { plainTitle } from "../story/gather.ts";
1819 import { eventActor, eventVerb } from "../design/activity.ts";
1920 import { audienceFilter } from "../core/event_audience.ts";
@@ -79,11 +80,7 @@ export function pushedBranch(event: ForgeEvent): string {
7980
8081 /** Does an `If-None-Match` header name this tag (weakly, or as `*`)? */
8182 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);
8784 }
8885
8986 export function activityAtom(ctx: Ctx): Response {
src/web/layout.ts +2 −1
@@ -7,6 +7,7 @@
77 * `/search`), and every dialog (a link to a real page).
88 */
99
10 import { requestHolds } from "../http/conditional.ts";
1011 import { sshOn } from "../settings/ssh.ts";
1112 import { returnTo, withReturn } from "./return_to.ts";
1213 import { waitingCount } from "../actions/conversation_confirms.ts";
@@ -1125,7 +1126,7 @@ export function fragment(
11251126 if (status !== 200) return new Response(text, { status, headers });
11261127 const hash = o.etag ?? sha256(text).slice(0, 16);
11271128 headers.etag = `"${hash}"`;
1128 if (o.request?.headers.get("if-none-match") === headers.etag)
1129 if (o.request !== undefined && requestHolds(o.request, headers.etag))
11291130 return new Response(null, { status: 304, headers });
11301131 return new Response(
11311132 text.replace(/^(\s*<[a-zA-Z][^\s>]*)/, `$1 data-etag="${hash}"`),
src/web/pages/build_cart.ts +2 −1
@@ -6,6 +6,7 @@
66 * no request back with the viewer's session.
77 */
88
9 import { requestHolds } from "../../http/conditional.ts";
910 import { cartOf } from "../../actions/build_cart.ts";
1011 import { readArt, readCapture } from "../../arcade/carts.ts";
1112 import { notePhase, takeSessionTime } from "../../core/route_timing.ts";
@@ -35,7 +36,7 @@ async function cartBytes(ctx: Ctx, art: boolean): Promise<Response> {
3536 etag,
3637 "cache-control": "private, no-cache",
3738 };
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 });
3940 return new Response(bytes, { headers });
4041 }
4142
src/web/pages/build_shots.ts +2 −1
@@ -5,6 +5,7 @@
55 * following `build.shots` live.
66 */
77
8 import { requestHolds } from "../../http/conditional.ts";
89 import { readBuildPin } from "../../git/builds.ts";
910 import { readShotBytes, readShots } from "../../git/shots.ts";
1011 import { repoDir } from "../../git/repos.ts";
@@ -63,6 +64,6 @@ export async function shotImage(ctx: Ctx): Promise<Response> {
6364 "cache-control":
6465 ctx.url.searchParams.get("v") === shot.oid.slice(0, 12) ? "private, max-age=31536000, immutable" : "private, no-cache",
6566 };
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 });
6768 return new Response(shot.bytes, { headers });
6869 }
src/web/pages/home_flow.ts +3 −2
@@ -15,6 +15,7 @@
1515 * version without being drawn. The page is a static picture with scripts off.
1616 */
1717
18 import { requestHolds } from "../../http/conditional.ts";
1819 import { setCookie } from "../../auth/cookies.ts";
1920 import type { App } from "../../app.ts";
2021 import { listChildren } from "../../actions/spawn.ts";
@@ -585,7 +586,7 @@ export async function liveTableFragment(ctx: Ctx): Promise<Response> {
585586 const rendering = performance.now();
586587 if (picture === undefined) return fragment(readingTable(q), { request: ctx.request });
587588 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}"`)) {
589590 notePhase(ctx.request, "render", performance.now() - rendering);
590591 return new Response(null, { status: 304, headers: { etag: `"${tag}"`, "cache-control": "no-store" } });
591592 }
@@ -622,7 +623,7 @@ export async function liveLaneFragment(ctx: Ctx): Promise<Response> {
622623 const mine = waitsOf(ctx.url);
623624 const waits = mine ? waitingOnMe(ctx, user) : undefined;
624625 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}"`)) {
626627 notePhase(ctx.request, "render", performance.now() - rendering);
627628 return new Response(null, { status: 304, headers: { etag: `"${tag}"`, "cache-control": "no-store" } });
628629 }
src/web/pages/looks.ts +2 −1
@@ -7,6 +7,7 @@
77 * long as anyone likes and is never the wrong commit's.
88 */
99
10 import { requestHolds } from "../../http/conditional.ts";
1011 import { closeSync, existsSync, openSync, readSync } from "node:fs";
1112 import { jpegHeader } from "../../design/image/jpeg.ts";
1213 import { isPng, pngHeader } from "../../design/image/png.ts";
@@ -104,7 +105,7 @@ export function lookImage(ctx: Ctx): Response {
104105 : "private, no-cache",
105106 };
106107 // 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))
108109 return new Response(null, { status: 304, headers });
109110 return new Response(Bun.file(path), { headers });
110111 }
src/web/pages/og.ts +2 −1
@@ -14,6 +14,7 @@
1414 * unavailable it answers 503 and nothing else is affected.
1515 */
1616
17 import { requestHolds } from "../../http/conditional.ts";
1718 import { createHash } from "node:crypto";
1819 import { join } from "node:path";
1920 import { Fresh } from "../../kernel/fresh.ts";
@@ -441,7 +442,7 @@ export function ogCard(kind: OgKind, format: "svg" | "png" = "svg") {
441442 ...(kind === "repo" ? { vary: "cookie" } : {}),
442443 "content-type": format === "png" ? "image/png" : "image/svg+xml; charset=utf-8",
443444 };
444 if (ctx.request.headers.get("if-none-match") === etag)
445 if (requestHolds(ctx.request, etag))
445446 return new Response(null, { status: 304, headers });
446447 if (format === "svg")
447448 return new Response(
src/web/standards.ts +4 −3
@@ -28,6 +28,7 @@
2828 * FORGE_SECURITY_CONTACT.
2929 */
3030
31 import { noneMatch } from "../http/conditional.ts";
3132 import { sshOn } from "../settings/ssh.ts";
3233 import { UPSTREAM_REPO } from "../core/upstream.ts";
3334 import { CRAWL_DELAY_S, DEEP_SEGMENTS } from "../http/crawl.ts";
@@ -256,7 +257,7 @@ export function releasesJson(ctx: Ctx): Response {
256257 }
257258 const cache = "public, max-age=3600";
258259 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))
260261 return new Response(null, {
261262 status: 304,
262263 headers: { etag: kept.etag, "cache-control": cache },
@@ -371,7 +372,7 @@ export async function sitemapXml(ctx: Ctx): Promise<Response> {
371372 }
372373 const cache = "public, max-age=300";
373374 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))
375376 return new Response(null, { status: 304, headers: { etag: kept.etag, "cache-control": cache } });
376377 const res = text(kept.body, "application/xml; charset=utf-8");
377378 res.headers.set("etag", kept.etag);
@@ -500,7 +501,7 @@ export async function llmsTxt(ctx: Ctx): Promise<Response> {
500501 // minutes. A reader with an account gets more and stays private.
501502 const cache = ctx.auth.user === undefined ? "public, max-age=300" : "private, no-cache";
502503 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))
504505 return new Response(null, { status: 304, headers: { etag: kept.etag, "cache-control": cache, vary: "Cookie, Authorization" } });
505506 const res = text(kept.body);
506507 res.headers.set("cache-control", cache);
src/web/sw.ts +2 −4
@@ -20,6 +20,7 @@
2020 * one person's and must not outlive their session.
2121 */
2222
23 import { noneMatch } from "../http/conditional.ts";
2324 import { sha256 } from "../kernel/hash.ts";
2425
2526 export const SW_CACHE = "forge-shell-v2";
@@ -136,10 +137,7 @@ export const SW_CACHE_CONTROL = "private, no-cache";
136137 * compresses.
137138 */
138139 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);
143141 }
144142
145143 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
245245 expect(seen.filter((s) => s.cookies.length > 0).map((s) => `${s.label} sets ${s.cookies.map(cookieName).join(", ")}`)).toEqual([]);
246246 const still = sessionOf(app.db, token);
247247 expect(still.ok && still.value.user.username).toBe(USERNAME);
248 }, 2_000);
248 }, 15_000);
249249
250250 test("signing in from the forge's own page sets the session cookie HttpOnly, with the floor", async () => {
251251 const form = await send(

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs