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

GET /api/v1/keeper carries an ETag and answers a repeat with 304 until a release is kept, promoted or rolled back #9231

Merged opened this

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

Merged

mason/forge:mason/9204-get-api-v1-keeper-carries-an-etag-and-an → main 2 commits fork

  • Merged. mason/9204-get-api-v1-keeper-carries-an-etag-and-an was merged into main as 866c014.

Review

Sign in to review this change.
2 files changed +106 −2
src/http/api/keeper.ts +9 −2
@@ -8,7 +8,8 @@ import { notePhase, takeSessionTime } from "../../core/route_timing.ts";
88 import { keeperAnswer } from "../../keeper/landed.ts";
99 import { mayRead } from "../../release/gate.ts";
1010 import type { Handler } from "../context.ts";
11 import { apiError, json } from "../respond.ts";
11 import { holds, weakEtag } from "../etag.ts";
12 import { apiError } from "../respond.ts";
1213 import type { Router } from "../router.ts";
1314
1415 export function keeperRoutes(router: Router<Handler>): void {
@@ -16,6 +17,12 @@ export function keeperRoutes(router: Router<Handler>): void {
1617 notePhase(ctx.request, "session", takeSessionTime(ctx.request));
1718 if (!mayRead(ctx.app, ctx.auth.user))
1819 return apiError("forbidden", 403, "the release history is for the forge's administrators and its repository's readers");
19 return json(keeperAnswer(ctx.app.db));
20 // Tagged only after the access check, over the exact bytes sent: a recorded, promoted, held or
21 // rolled-back release moves it, and a viewer shown a different body gets a different tag.
22 const text = JSON.stringify(keeperAnswer(ctx.app.db));
23 const etag = weakEtag(text);
24 const headers = { etag, "cache-control": "private, no-cache", vary: "authorization, cookie" };
25 if (holds(ctx.request, etag)) return new Response(null, { status: 304, headers });
26 return new Response(text, { headers: { "content-type": "application/json", ...headers } });
2027 });
2128 }
test/keeper-etag.test.ts added +97
@@ -0,0 +1,97 @@
1 /** GET /api/v1/keeper carries a weak ETag and answers a repeat with 304 until a release moves (#9204). */
2 import { afterAll, beforeAll, expect, test } from "bun:test";
3 import { mkdtemp, rm } from "node:fs/promises";
4 import { tmpdir } from "node:os";
5 import { join } from "node:path";
6 import { createApp, type App } from "../src/app.ts";
7 import { createToken } from "../src/core/tokens.ts";
8 import { createUser } from "../src/core/users.ts";
9 import { serve, type Servers } from "../src/http/server.ts";
10 import { lift, setHold } from "../src/keeper/hold.ts";
11 import { finish, markLive, recordCut } from "../src/keeper/record.ts";
12 import { quietLogs } from "./helpers/globals.ts";
13
14 quietLogs();
15
16 let dataDir = "";
17 let app: App, servers: Servers;
18 const tok: Record<string, string> = {};
19 const get = (token: string | null, headers: Record<string, string> = {}) =>
20 fetch(`${servers.url}/api/v1/keeper`, { headers: { ...(token ? { authorization: `Bearer ${token}` } : {}), ...headers } });
21 const tagOf = async () => {
22 const r = await get(tok["admin"]!);
23 expect(r.status).toBe(200);
24 await r.text();
25 return r.headers.get("etag")!;
26 };
27
28 beforeAll(async () => {
29 dataDir = await mkdtemp(join(tmpdir(), "forge-keeper-etag-"));
30 app = createApp({ dataDir, port: 0 });
31 servers = await serve(app);
32 const mk = async (username: string, first: boolean) => {
33 const made = await createUser(app.db, {
34 origin: "seed", trusted: true, username, email: `${username}@forge.local`, password: "a-good-passphrase", adminIfFirst: first,
35 });
36 if (!made.ok) throw new Error(String(made.error));
37 return made.value;
38 };
39 const ada = await mk("ada", true);
40 const bob = await mk("bob", false);
41 tok["admin"] = createToken(app.db, ada.id, "a", "admin").token;
42 tok["bob"] = createToken(app.db, bob.id, "b", "admin").token;
43 }, 30_000);
44
45 afterAll(async () => {
46 servers?.stop();
47 app?.close();
48 await rm(dataDir, { recursive: true, force: true });
49 });
50
51 test("200 carries a weak tag, private no-cache and vary; a repeat is a bodiless 304 with the floor headers", async () => {
52 const res = await get(tok["admin"]!);
53 expect(res.status).toBe(200);
54 const tag = res.headers.get("etag")!;
55 expect(tag).toMatch(/^W\//);
56 expect(res.headers.get("cache-control")).toBe("private, no-cache");
57 expect(res.headers.get("vary")).toContain("authorization, cookie");
58 expect(res.headers.get("content-type")).toBe("application/json");
59 expect(tag).toBe(`W/"${Bun.hash(await res.text()).toString(36)}"`);
60 const again = await get(tok["admin"]!, { "if-none-match": tag });
61 expect(again.status).toBe(304);
62 expect(again.headers.get("etag")).toBe(tag);
63 expect(again.headers.get("cache-control")).toBe("private, no-cache");
64 expect(again.headers.get("vary")).toContain("authorization, cookie");
65 expect(again.headers.get("x-content-type-options")).toBe("nosniff");
66 expect(again.headers.get("content-security-policy")).not.toBeNull();
67 expect(await again.text()).toBe("");
68 });
69
70 test("recording, promoting, holding and rolling back a release each move the tag", async () => {
71 const t0 = await tagOf();
72 const cut = recordCut(app.db, { version: "v1", sha: "abcdef1234", knownGood: { version: "v0", image: "img0" }, at: 1_000 });
73 const t1 = await tagOf();
74 expect(t1).not.toBe(t0);
75 expect((await get(tok["admin"]!, { "if-none-match": t0 })).status).toBe(200);
76 expect(markLive(app.db, cut.id, 2_000)).toBe(true);
77 const t2 = await tagOf();
78 expect(t2).not.toBe(t1);
79 const hold = setHold(app.db, { kind: "owner", version: "v1", reason: "hold", at: 3_000 });
80 const t3 = await tagOf();
81 expect(t3).not.toBe(t2);
82 expect((await get(tok["admin"]!, { "if-none-match": t3 })).status).toBe(304);
83 finish(app.db, cut.id, "rolled_back", "bad", 4_000);
84 const t4 = await tagOf();
85 expect(t4).not.toBe(t3);
86 lift(app.db, hold.id, "ada", 5_000);
87 expect(await tagOf()).not.toBe(t4);
88 });
89
90 test("a stranger and an anonymous caller answer 403 as before, with no tag, even for *", async () => {
91 for (const token of [tok["bob"]!, null]) {
92 const res = await get(token, { "if-none-match": "*" });
93 expect(res.status).toBe(403);
94 expect(res.headers.get("etag")).toBeNull();
95 expect(((await res.json()) as any).error).toBe("forbidden");
96 }
97 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs