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

Publishing an app from the tab forge mints the visitor principal, and only then #18243

opened this

Its check is red, 9h 26m ago

check failed: 1 of 2242 tests — "the cookie floor > every cookie constructed under src/ reaches the helper, or is named with its reason"

torque/forge:torque/18216-publishing-an-app-from-the-tab-forge-min → main 2 commits fork

Review

Sign in to review this change.
6 files changed +158 −4
docs/features/returning-visitors.md +3
@@ -25,3 +25,6 @@ Settings → Traffic ("Who comes back"), the Billing page ("What led to a forge"
2525 - **Becoming someone.** Spawning a forge (`POST /api/v1/forges`) links the request's cookie id to the forge name
2626 (`started a forge`; `paid` when a gift/plan code was redeemed). Stripe-paid checkouts are not linked yet.
2727 - `/privacy` says it in three lines.
28 - **Not the visitor principal.** A person with no account who makes something (a note on an app's page, a game client link,
29 or publishing a tab forge app as a link, #18216) gets a separate signed `forge_maker` cookie (`src/core/visitors.ts`),
30 minted only at that moment and capped by `VISITOR_CAPS`. It shares no id, key or table with `forge_visitor`.
docs/features/tabforge.md +1 −1
@@ -18,6 +18,6 @@ The page is `src/web/pages/tabforge.ts`; the words are `src/web/tabforge/words.t
1818
1919 An app made in a tab (`/tab`, format `src/tab/blocks.ts`) can be sent to the forge as one read-only link that opens on any device, with no login. The app's page in the tab offers **Get a link** beside its source: a plain form whose hidden field carries the app's JSON, so it works with script off. `POST /tabforge-link` (`src/web/pages/tabforge-link.ts`, logic in `src/tab/share.ts`) checks it is same-origin, reads at most 80 KB, validates it with `decodeApp` (its `LIMITS` apply), stores the decoded app only, never its entries, in `tab_shared`, and answers a 16-character random id as `/tabforge-link/<id>`. A spec that does not decode stores nothing. `GET /tabforge-link/<id>` draws the app read-only (the preview renderer: no forms, no entries), escaped, with the floor's headers; an unknown or malformed id is the forge's one 404.
2020
21 The route sits outside `/tabforge/` and `/tab/f/` on purpose: a service worker in either scope would answer for it in a browser that started a tab forge. It is an open door for strangers, so it is limited by rate, not by count: 10 posts a minute per address (the `share` door in `src/http/strangers.ts`), taken before the body is read. Storage is bounded the other way: the forge keeps the newest 5000 links and the oldest go as a new one is made, so a link can stop opening once enough newer ones exist, and no one can fill the table and lock the door. It mints no account and no visitor principal, and there is no way to edit or delete a link yet.
21 The route sits outside `/tabforge/` and `/tab/f/` on purpose: a service worker in either scope would answer for it in a browser that started a tab forge. It is an open door for strangers, so it is limited by rate, not by count: 10 posts a minute per address (the `share` door in `src/http/strangers.ts`), taken before the body is read. Storage is bounded the other way: the forge keeps the newest 5000 links and the oldest go as a new one is made, so a link can stop opening once enough newer ones exist, and no one can fill the table and lock the door. It mints no account. Publishing is the one moment a person with no account becomes the visitor principal (#18216, `src/core/visitors.ts`): after the door and the body, the forge asks `may` for `visitor.make` (a refused credential is 403) and then `VISITOR_CAPS`, per visitor, per connection and per forge (429 past any), before anything is stored; once the link is made it is counted, and only then does the answer carry the signed `forge_maker` cookie. A spec that does not decode counts and mints nothing; `DNT: 1` or `Sec-GPC: 1` publishes and counts with no id kept; a signed-in person mints nothing. Reading or editing in the tab, and opening a link, mint nothing. There is no way to edit or delete a link yet.
2222
2323 The terminal's twin is the tab forge's palette entry (`src/tui/screens/tab.ts`): `tabLine()` says the same words (`TAB_LINK` in `src/tab/words.ts`). The terminal cannot hold a tab's app, so it has nothing to send.
src/core/visitors.ts +3 −2
@@ -1,8 +1,9 @@
11 /**
22 * A visitor principal (#18112): someone with no account the forge knows, minted only at the moment
33 * they make something (a note on an app's page), never on a
4 * read. Notes (`apps/feedback.ts`) and a game client link (#18189,
5 * `web/pages/games_client_pack.ts`) mint today; the shared app (#18188) follows. The id rides its
4 * read. Notes (`apps/feedback.ts`), a game client link (#18189,
5 * `web/pages/games_client_pack.ts`) and publishing a tab app as a link (#18216,
6 * `web/pages/tabforge-link.ts`) mint. The id rides its
67 * own cookie, `forge_maker`, signed with a key derived for this purpose alone (HKDF, "forge visitor
78 * principal v1"), so the analytics cookie (`perf/visitor_id.ts`, `forge_visitor`) can never verify
89 * here and is never joined to it: the two share no id, no key and no table. `Sec-GPC: 1` or
test/tab-share-visitor.test.ts added +127
@@ -0,0 +1,127 @@
1 /**
2 * Publishing a tab app as a link is the one moment a visitor with no account is minted (#18216):
3 * the publish mints and counts; opening a link or the tab mints nothing; DNT and GPC mint nothing;
4 * a refused credential is 403 and a spent cap is 429, each storing nothing.
5 */
6 import { afterAll, beforeAll, expect, test } from "bun:test";
7 import { mkdtemp, rm } from "node:fs/promises";
8 import { tmpdir } from "node:os";
9 import { join } from "node:path";
10 import { createApp, type App } from "../src/app.ts";
11 import { VISITOR_CAPS } from "../src/http/limits.ts";
12 import { forgeHttp, type ForgeHttp } from "../src/http/server.ts";
13 import { compile } from "../src/tab/compile.ts";
14 import { quietLogs } from "./helpers/globals.ts";
15
16 quietLogs();
17
18 let dir = "";
19 let app: App;
20 let http: ForgeHttp;
21 let maker = "";
22 const links = () => Number((app.db.query("SELECT count(*) AS n FROM tab_shared").get() as { n: number }).n);
23 const made = () =>
24 app.db.query("SELECT 1 FROM sqlite_master WHERE name = 'visitor_made'").get() === null
25 ? 0
26 : app.db.query<{ n: number }, []>("SELECT count(*) AS n FROM visitor_made").get()!.n;
27 const body = new URLSearchParams({ app: JSON.stringify(compile("a journal").app) }).toString();
28 const publish = (ip: string, extra: Record<string, string> = {}) =>
29 http.handle(
30 new Request("http://forge.test/tabforge-link", {
31 method: "POST",
32 body,
33 headers: { "content-type": "application/x-www-form-urlencoded", origin: "http://forge.test", "x-forwarded-for": ip, ...extra },
34 }),
35 );
36
37 beforeAll(async () => {
38 dir = await mkdtemp(join(tmpdir(), "forge-tabshare-visitor-"));
39 app = createApp({ dataDir: dir, port: 0 });
40 http = forgeHttp(app);
41 });
42 afterAll(async () => {
43 http.close();
44 app.close();
45 await rm(dir, { recursive: true, force: true });
46 });
47
48 test("a publish mints the visitor and counts it once; opening the link and the tab mint nothing", async () => {
49 const r = await publish("198.51.100.20");
50 expect(r.status).toBe(201);
51 const cookie = r.headers.get("set-cookie") ?? "";
52 expect(cookie).toMatch(/^forge_maker=v[0-9a-f]{32}\.[0-9a-f]{32};/);
53 expect(cookie).toMatch(/HttpOnly/i);
54 expect(made()).toBe(1);
55 maker = cookie.split(";")[0]!;
56 const link = /href="(http:\/\/forge\.test\/tabforge-link\/[A-Za-z0-9_-]{16})"/.exec(await r.text())![1]!;
57 for (const path of [link, "http://forge.test/tab", "http://forge.test/tabforge"]) {
58 const read = await http.handle(new Request(path, { headers: { accept: "text/html", "x-forwarded-for": "198.51.100.21" } }));
59 expect(read.headers.get("set-cookie") ?? "").not.toContain("forge_maker");
60 }
61 expect(made()).toBe(1);
62 // The same visitor publishing again is counted again and minted nothing new.
63 const again = await publish("198.51.100.20", { cookie: cookie.split(";")[0]! });
64 expect(again.status).toBe(201);
65 expect(again.headers.get("set-cookie")).toBeNull();
66 expect(made()).toBe(2);
67 });
68
69 test("DNT and GPC publish and count, and mint nothing", async () => {
70 const before = made();
71 for (const extra of [{ dnt: "1" }, { "sec-gpc": "1" }]) {
72 const r = await publish("198.51.100.22", extra);
73 expect(r.status).toBe(201);
74 expect(r.headers.get("set-cookie")).toBeNull();
75 }
76 expect(made()).toBe(before + 2);
77 });
78
79 test("a spec that does not decode mints and counts nothing", async () => {
80 const before = made();
81 const r = await http.handle(
82 new Request("http://forge.test/tabforge-link", {
83 method: "POST",
84 body: "app=not+json",
85 headers: { "content-type": "application/x-www-form-urlencoded", origin: "http://forge.test", "x-forwarded-for": "198.51.100.23" },
86 }),
87 );
88 expect(r.status).toBe(422);
89 expect(r.headers.get("set-cookie")).toBeNull();
90 expect(made()).toBe(before);
91 });
92
93 test("a refused credential is 403 from may, and a spent cap is 429: no link, no cookie, nothing counted", async () => {
94 const before = { links: links(), made: made() };
95 const bad = await publish("198.51.100.24", { authorization: "Bearer not-a-token" });
96 expect(bad.status).toBe(403);
97 expect(bad.headers.get("set-cookie")).toBeNull();
98 expect(await bad.text()).toContain("Sign in to make this.");
99 // One visitor past their cap, made from other connections: refused on a fresh one.
100 const rows = app.db.prepare("INSERT INTO visitor_made (visitor_id, client, at) VALUES (?, ?, ?)");
101 const id = maker.slice("forge_maker=".length).split(".")[0]!;
102 app.db.transaction(() => {
103 for (let i = 0; i < VISITOR_CAPS.perVisitor.perDay; i++) rows.run(id, `elsewhere-${i}`, Date.now());
104 })();
105 const mine = await publish("198.51.100.26", { cookie: maker });
106 expect(mine.status).toBe(429);
107 expect(await mine.text()).toContain("A visitor may make");
108 // One connection past its cap (the DNT publishes above are its rows).
109 const client = app.db.query<{ client: string }, []>("SELECT client FROM visitor_made WHERE visitor_id = '' ORDER BY rowid DESC LIMIT 1").get()!.client;
110 app.db.transaction(() => {
111 for (let i = 0; i < VISITOR_CAPS.perClient.perDay; i++) rows.run("", client, Date.now());
112 })();
113 const full = await publish("198.51.100.22");
114 expect(full.status).toBe(429);
115 expect(full.headers.get("set-cookie")).toBeNull();
116 expect(await full.text()).toContain("sign in to make more");
117 expect(links()).toBe(before.links);
118 // The forge's cap, spent by everyone: the next publish is refused whoever asks.
119 app.db.transaction(() => {
120 for (let i = made(); i < VISITOR_CAPS.perForge.perDay; i++) rows.run("", `other-${i}`, Date.now());
121 })();
122 const forge = await publish("198.51.100.25");
123 expect(forge.status).toBe(429);
124 expect(await forge.text()).toContain("all it takes from visitors today");
125 expect(links()).toBe(before.links);
126 expect(made()).toBe(VISITOR_CAPS.perForge.perDay);
127 });

Search
↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs