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

One person reloading app pages cannot evict other people's panel proofs #15203

Merged opened this

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

Merged

scout/forge:scout/15201-one-person-reloading-app-pages-cannot-ev → main 2 commits fork

  • Merged. scout/15201-one-person-reloading-app-pages-cannot-ev was merged into main as 39d9660.

Review

Sign in to review this change.
2 files changed +106 −9
src/apps/feedback.ts +55 −9
@@ -109,26 +109,72 @@ function doorFor(ask: FeedbackAsk): Door | undefined {
109109
110110 export const PROOF_HEADER = "x-forge-proof";
111111 const PROOF_TTL_MS = 24 * 3_600_000;
112 const PROOF_MAX = 20_000;
113 const proofs = new Map<string, { bind: string; expires: number }>();
112 /** Every proof outstanding: the memory bound. */
113 export const PROOF_MAX = 20_000;
114 /** Outstanding proofs one grant on one host keeps: its newest open tabs. */
115 export const PROOF_PER_BIND = 32;
116 /**
117 * Each bind's outstanding proofs, oldest first; the binds ordered by their
118 * latest mint. Keyed by bind so one bind reloading evicts only its own.
119 */
120 const proofs = new Map<string, { token: string; expires: number }[]>();
121 let held = 0;
122 /** No proof expires before this, so a full sweep before it would free nothing. */
123 let soonest = Infinity;
114124
115125 const bindOf = (grantCookie: string, host: string): string => sha256(`${host}\n${grantCookie}`);
116126
127 /** Drop every expired proof, of every bind. */
128 function sweep(now: number): void {
129 soonest = Infinity;
130 for (const [bind, list] of proofs) {
131 const live = list.filter((p) => p.expires > now);
132 held -= list.length - live.length;
133 if (live.length === 0) proofs.delete(bind);
134 else if (live.length !== list.length) proofs.set(bind, live);
135 if (live.length > 0) soonest = Math.min(soonest, live[0]!.expires);
136 }
137 }
138
117139 /** A fresh token for this grant cookie on this host. Never put it in a URL. */
118140 export function mintProof(grantCookie: string, host: string, now = Date.now()): string {
119 while (proofs.size >= PROOF_MAX) proofs.delete(proofs.keys().next().value!);
141 const bind = bindOf(grantCookie, host);
142 const old = proofs.get(bind) ?? [];
143 // This bind's own expired and oldest go first: reloading reaches no one else's.
144 const mine = old.filter((p) => p.expires > now).slice(-(PROOF_PER_BIND - 1));
145 held -= old.length - mine.length;
146 proofs.delete(bind);
147 // Full: sweep the expired before evicting any live proof, then the bind
148 // that minted least recently gives up its oldest.
149 if (held >= PROOF_MAX && now >= soonest) {
150 sweep(now);
151 if (mine.length > 0) soonest = Math.min(soonest, mine[0]!.expires);
152 }
153 while (held >= PROOF_MAX) {
154 const [first, list] = proofs.entries().next().value!;
155 list.shift();
156 held--;
157 if (list.length === 0) proofs.delete(first);
158 }
120159 const token = randomBytes(24).toString("base64url");
121 proofs.set(token, { bind: bindOf(grantCookie, host), expires: now + PROOF_TTL_MS });
160 mine.push({ token, expires: now + PROOF_TTL_MS });
161 soonest = Math.min(soonest, now + PROOF_TTL_MS);
162 held++;
163 proofs.set(bind, mine);
122164 return token;
123165 }
124166
125167 /** Spend a token: true once, for the grant and host it was minted for. */
126 function spendProof(token: string | null, grantCookie: string | undefined, host: string | null, now = Date.now()): boolean {
168 export function spendProof(token: string | null, grantCookie: string | undefined, host: string | null, now = Date.now()): boolean {
127169 if (token === null || grantCookie === undefined || host === null) return false;
128 const held = proofs.get(token);
129 if (held === undefined) return false;
130 proofs.delete(token);
131 return held.expires > now && held.bind === bindOf(grantCookie, host);
170 const bind = bindOf(grantCookie, host);
171 const list = proofs.get(bind);
172 const at = list === undefined ? -1 : list.findIndex((p) => p.token === token);
173 if (list === undefined || at < 0) return false;
174 const [spent] = list.splice(at, 1);
175 held--;
176 if (list.length === 0) proofs.delete(bind);
177 return spent!.expires > now;
132178 }
133179
134180 /** The sender: a person's key and name, or the anonymous cookie's. */
test/feedback-proof.test.ts added +51
@@ -0,0 +1,51 @@
1 // The panel's one-use proofs: one bind reloading evicts only its own.
2 import { expect, test } from "bun:test";
3 import { mintProof, PROOF_MAX, PROOF_PER_BIND, spendProof } from "../src/apps/feedback.ts";
4
5 const DAY = 24 * 3_600_000;
6
7 test("one bind minting past every cap leaves another bind's proof spendable", () => {
8 const theirs = mintProof("their-grant", "a.example");
9 for (let i = 0; i < PROOF_MAX + 10; i++) mintProof("my-grant", "a.example");
10 expect(spendProof(theirs, "their-grant", "a.example")).toBe(true);
11 });
12
13 test("a bind keeps only its newest proofs", () => {
14 const first = mintProof("tabs-grant", "b.example");
15 const rest = Array.from({ length: PROOF_PER_BIND }, () => mintProof("tabs-grant", "b.example"));
16 expect(spendProof(first, "tabs-grant", "b.example")).toBe(false);
17 for (const t of rest) expect(spendProof(t, "tabs-grant", "b.example")).toBe(true);
18 });
19
20 test("a proof spends once, and only for its own grant and host", () => {
21 const t = mintProof("g", "c.example");
22 expect(spendProof(t, "other", "c.example")).toBe(false);
23 expect(spendProof(t, "g", "other.example")).toBe(false);
24 expect(spendProof(t, "g", "c.example")).toBe(true);
25 expect(spendProof(t, "g", "c.example")).toBe(false);
26 });
27
28 test("the same grant on another host is another bind", () => {
29 const there = mintProof("g", "c.example");
30 for (let i = 0; i < PROOF_PER_BIND * 2; i++) mintProof("g", "d.example");
31 expect(spendProof(there, "g", "c.example")).toBe(true);
32 });
33
34 const fill = (tag: string, count: number) => {
35 for (let i = 0; i < count; i++) mintProof(`${tag}-${Math.floor(i / PROOF_PER_BIND)}`, "f.example");
36 };
37
38 test("the global cap bounds memory: the least recent bind gives up its oldest", () => {
39 const oldest = mintProof("cap-first", "f.example");
40 fill("cap", PROOF_MAX);
41 expect(spendProof(oldest, "cap-first", "f.example")).toBe(false);
42 });
43
44 test("expired proofs are swept before a live one is evicted", () => {
45 const now = Date.now();
46 const live = mintProof("sweep-live", "g.example", now);
47 mintProof("sweep-stale", "g.example", now - DAY - 1);
48 fill("sweep", PROOF_MAX - 2);
49 mintProof("sweep-next", "g.example", now);
50 expect(spendProof(live, "sweep-live", "g.example", now)).toBe(true);
51 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs