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

A note from the app's own origin is a visitor's unless it carries the forge panel's one-use proof #5604

Parked opened this
Closed without merging

scout/forge:scout/5395-a-note-from-the-app-s-own-origin-is-a-vi → main 2 commits fork

  • Closed without merging. Nothing was applied to main.

Review

Sign in to review this change.
7 files changed +177 −17
src/apps/feedback.ts +14
@@ -19,6 +19,7 @@ import type { AppRecord } from "../core/apps.ts";
1919 import { canAdmin, canRead, getRepoById, type Repo } from "../core/repos.ts";
2020 import { getUserById, isActive, type User } from "../core/users.ts";
2121 import { sha256 } from "../kernel/hash.ts";
22 import { issueProof, PROOF_HEADER, spendProof } from "./proof.ts";
2223
2324 export const REQUEST_MAX = 16 * 1024;
2425 export const VISITOR_COOKIE = "forge_visitor";
@@ -35,6 +36,8 @@ export interface FeedbackAsk {
3536 /** The person the app's grant names, if any. */
3637 readonly viewerId: string | undefined;
3738 readonly secure: boolean;
39 /** The grant cookie's value, which a proof is bound to. */
40 readonly grant?: string | undefined;
3841 /** The client's limit key, from the socket (`clientBucket`). */
3942 readonly source: string;
4043 }
@@ -166,6 +169,17 @@ function list(ask: FeedbackAsk, door: Door): Response {
166169
167170 /** Answer a `/__forge/note*` request; `undefined` when the path is not one. */
168171 export async function feedback(ask: FeedbackAsk): Promise<Response | undefined> {
172 const path = ask.url.pathname;
173 if (path !== "/__forge/note" && path !== "/__forge/notes") return answer(ask);
174 // A person is known only by the panel's one-use proof; without it, a visitor.
175 const host = ask.request.headers.get("host") ?? "";
176 const proven = spendProof(ask.request.headers.get(PROOF_HEADER), ask.grant, host);
177 const res = await answer(proven ? ask : { ...ask, viewerId: undefined });
178 if (proven && res !== undefined && ask.grant !== undefined) res.headers.set(PROOF_HEADER, issueProof(ask.grant, host));
179 return res;
180 }
181
182 async function answer(ask: FeedbackAsk): Promise<Response | undefined> {
169183 const path = ask.url.pathname;
170184 const method = ask.request.method;
171185 if (path === "/__forge/note/moderate") return json(404, { error: "not_found" });
src/apps/proof.ts added +48
@@ -0,0 +1,48 @@
1 /**
2 * The forge panel's one-use proof. A note posted from an app's own origin
3 * could have been written by the app's own code, so a request there is a
4 * visitor's unless it carries a token only the forge-served panel was given:
5 * issued into a signed-in person's page load, bound to their grant cookie and
6 * the host, spent by the first request that shows it, replaced by the next in
7 * `x-forge-proof`. Held in memory: a restart makes every panel a visitor's
8 * until the page is loaded again.
9 */
10 import { randomBytes } from "node:crypto";
11 import { sha256 } from "../kernel/hash.ts";
12
13 export const PROOF_HEADER = "x-forge-proof";
14 const TTL_MS = 60 * 60 * 1000;
15 const MAX = 20_000;
16
17 const live = new Map<string, { bound: string; expires: number }>();
18
19 const bind = (grant: string, host: string): string => sha256(`${grant}\n${host}`);
20
21 /** A fresh token for this grant on this host. */
22 export function issueProof(grant: string, host: string, now = Date.now()): string {
23 if (live.size >= MAX) {
24 for (const [token, v] of live) {
25 if (v.expires <= now || live.size >= MAX) live.delete(token);
26 else break;
27 }
28 }
29 const token = randomBytes(24).toString("hex");
30 live.set(token, { bound: bind(grant, host), expires: now + TTL_MS });
31 return token;
32 }
33
34 /** Whether `token` is live for this grant and host; it is spent either way. */
35 export function spendProof(token: string | null, grant: string | undefined, host: string, now = Date.now()): boolean {
36 if (token === null || grant === undefined || !/^[0-9a-f]{48}$/.test(token)) return false;
37 const have = live.get(token);
38 if (have === undefined) return false;
39 live.delete(token);
40 return have.expires > now && have.bound === bind(grant, host);
41 }
42
43 /** Whether a request is a page load a person's panel is born in. */
44 export function pageLoad(headers: Headers): boolean {
45 const dest = headers.get("sec-fetch-dest");
46 if (dest === null || dest === "document") return true;
47 return dest === "iframe" && headers.get("sec-fetch-site") === "cross-site";
48 }
src/apps/proxy.ts +10
@@ -37,6 +37,7 @@ import { previewEnv, type EnvName } from "./data.ts";
3737 import type { AppsRuntime, EnvStatus } from "./runtime.ts";
3838 import { crewNow, tellLink } from "./crew.ts";
3939 import { feedback } from "./feedback.ts";
40 import { issueProof, pageLoad } from "./proof.ts";
4041 import { clientBucket } from "../http/peer.ts";
4142 import { doorPage } from "./door.ts";
4243 import { notFound } from "../http/respond.ts";
@@ -673,6 +674,7 @@ export async function proxyApp(
673674 env: target.env,
674675 sealed,
675676 viewerId,
677 grant: cookie,
676678 secure:
677679 url.protocol === "https:" ||
678680 request.headers.get("x-forwarded-proto") === "https",
@@ -811,10 +813,18 @@ export async function proxyApp(
811813 } catch {
812814 return unavailable(runtime, target, status, "the app stopped answering");
813815 }
816 // A signed-in person's page load carries the panel's one-use proof.
817 const grantCookie = cookieValue(request, grantCookieName(target));
818 const proof =
819 vouched?.who !== undefined && grantCookie !== undefined && pageLoad(request.headers)
820 ? issueProof(grantCookie, request.headers.get("host") ?? "")
821 : undefined;
822 if (proof !== undefined) out.set("cache-control", "private, no-store");
814823 const page = withToolbar(
815824 bytes,
816825 runtime.options.forgeUrl,
817826 pillWhere(runtime, target, vouched?.who),
827 proof,
818828 );
819829 let sent = page;
820830 // Whether it is gzipped depends on the client, so every copy says so.
src/apps/toolbar.ts +28 −6
@@ -30,6 +30,27 @@ export const toolbarJs = `(function () {
3030 var pill = document.createElement("button");
3131 pill.type = "button";
3232 var d = me.dataset || {};
33 // The one-use proof: taken off the tag at once, so the page's own script never reads it.
34 var proof = d.proof || "";
35 if (me.removeAttribute) me.removeAttribute("data-proof");
36 var turn = Promise.resolve();
37 // Requests to the notes door go one at a time: each spends the proof and brings the next.
38 function door(path, init) {
39 var run = function () {
40 init.credentials = "same-origin";
41 init.headers = Object.assign({}, init.headers || {});
42 if (proof) init.headers["x-forge-proof"] = proof;
43 proof = "";
44 return fetch(path, init).then(function (r) {
45 var next = r.headers.get("x-forge-proof");
46 if (next) proof = next;
47 return r;
48 });
49 };
50 var p = turn.then(run, run);
51 turn = p.catch(function () {});
52 return p;
53 }
3354 var where = d.env === "production" ? "production" : d.pull ? "preview of pull #" + d.pull : "";
3455 pill.setAttribute("aria-label", [d.app, where].filter(Boolean).join(", ") || "Forge");
3556 pill.setAttribute("aria-expanded", "false");
@@ -140,7 +161,7 @@ export const toolbarJs = `(function () {
140161 });
141162 }
142163 function refresh(sent) {
143 fetch("/__forge/notes", { credentials: "same-origin", cache: "no-store" })
164 door("/__forge/notes", { cache: "no-store" })
144165 .then(function (r) { return r.ok ? r.json() : null; })
145166 .then(function (j) {
146167 if (!j || !Array.isArray(j.notes)) return;
@@ -159,9 +180,8 @@ export const toolbarJs = `(function () {
159180 tellGo.setAttribute("aria-busy", "true");
160181 tellGo.style.opacity = ".6";
161182 function idle() { tellGo.disabled = false; tellGo.removeAttribute("aria-busy"); tellGo.style.opacity = "1"; }
162 fetch("/__forge/note", {
183 door("/__forge/note", {
163184 method: "POST",
164 credentials: "same-origin",
165185 headers: { "content-type": "application/json" },
166186 body: JSON.stringify({ text: text, page: location.href, viewport: window.innerWidth + "x" + window.innerHeight })
167187 })
@@ -420,7 +440,7 @@ export function escapeAttr(text: string): string {
420440 }
421441
422442 /** The script tag, with where-you-are as escaped data attributes. */
423 export function toolbarTag(forgeUrl: string, where?: PillWhere): string {
443 export function toolbarTag(forgeUrl: string, where?: PillWhere, proof?: string): string {
424444 const data =
425445 where === undefined
426446 ? ""
@@ -434,7 +454,8 @@ export function toolbarTag(forgeUrl: string, where?: PillWhere): string {
434454 .filter((pair): pair is [string, string] => pair[1] !== undefined)
435455 .map(([k, v]) => ` data-${k}="${escapeAttr(v)}"`)
436456 .join("");
437 return `<script src="${new URL(TOOLBAR_PATH, forgeUrl).href}"${data} defer></script>`;
457 const token = proof === undefined ? "" : ` data-proof="${escapeAttr(proof)}"`;
458 return `<script src="${new URL(TOOLBAR_PATH, forgeUrl).href}"${data}${token} defer></script>`;
438459 }
439460
440461 /** `bytes` with the pill's tag before the last `</body>`, or at the end. */
@@ -442,8 +463,9 @@ export function withToolbar(
442463 bytes: Uint8Array,
443464 forgeUrl: string,
444465 where?: PillWhere,
466 proof?: string,
445467 ): Uint8Array<ArrayBuffer> {
446 const tag = Buffer.from(toolbarTag(forgeUrl, where));
468 const tag = Buffer.from(toolbarTag(forgeUrl, where, proof));
447469 const buffer = Buffer.from(bytes.buffer, bytes.byteOffset, bytes.byteLength);
448470 // latin1 is one character per byte, so an index in it is a byte offset.
449471 const at = buffer.toString("latin1").toLowerCase().lastIndexOf("</body");
test/apps-crew-pill.test.ts +1 −1
@@ -199,7 +199,7 @@ describe("the crew line", () => {
199199 expect(toolbarJs).toContain("/__forge/crew");
200200 // The crew line is asked once; the feedback door (#4758) adds the
201201 // sender's notes, polled only while the panel is open, and the send.
202 expect(toolbarJs.match(/fetch\(/g)?.length).toBe(3);
202 expect(toolbarJs.match(/fetch\(/g)?.length).toBe(2);
203203 expect(toolbarJs.match(/setInterval/g)?.length).toBe(1);
204204 expect(toolbarJs).not.toMatch(
205205 /document\.cookie|querySelector|getElementById|setTimeout/,
test/apps-feedback.test.ts +73 −7
@@ -4,16 +4,17 @@
44 * a status that follows the issue, and the owner can hide or ban.
55 */
66
7 import { afterAll, beforeAll, describe, expect, test } from "bun:test";
7 import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, test } from "bun:test";
88 import { mkdtemp, rm } from "node:fs/promises";
99 import { tmpdir } from "node:os";
1010 import { join } from "node:path";
1111 import { createApp, type App } from "../src/app.ts";
1212 import { newApp } from "../src/actions/apps.ts";
1313 import { AppsRuntime } from "../src/apps/runtime.ts";
14 import { toolbarJs } from "../src/apps/toolbar.ts";
14 import { toolbarJs, toolbarTag } from "../src/apps/toolbar.ts";
1515 import { filtered, moderateNote, noteClock, PER_APP_DAY, PER_SENDER, PER_SOURCE, saysFromApp } from "../src/actions/app_notes.ts";
1616 import { feedback, REQUEST_MAX } from "../src/apps/feedback.ts";
17 import { issueProof, pageLoad, PROOF_HEADER } from "../src/apps/proof.ts";
1718 import { getAppBySlug } from "../src/core/apps.ts";
1819 import { getNote, isBanned } from "../src/core/app_notes.ts";
1920 import { grantedCookie } from "./helpers/identity.ts";
@@ -83,8 +84,10 @@ describe("the forge button takes notes from anyone", () => {
8384 },
8485 redirect: "manual",
8586 });
86 const post = (text: string, cookie = "", source = "", slug = "notes") =>
87 at(slug, "/__forge/note", { method: "POST", body: JSON.stringify({ text, page: `http://${host(slug)}/lists?x=1`, viewport: "390x844" }) }, cookie, source);
87 const post = (text: string, cookie = "", source = "", slug = "notes", proof = "") =>
88 at(slug, "/__forge/note", { method: "POST", body: JSON.stringify({ text, page: `http://${host(slug)}/lists?x=1`, viewport: "390x844" }), headers: proof ? { [PROOF_HEADER]: proof } : {} }, cookie, source);
89 /** What the forge puts in a signed-in person's page load: a token for this grant on this host. */
90 const proofFor = (cookie: string, slug: string) => issueProof(cookie.slice(cookie.indexOf("=") + 1), host(slug));
8891 const cookieOf = (res: Response) => (res.headers.get("set-cookie") ?? "").split(";")[0] ?? "";
8992
9093 test("a visitor's note files a from-app issue with page, viewport and time", async () => {
@@ -186,7 +189,7 @@ describe("the forge button takes notes from anyone", () => {
186189 expect(issues()).toBe(before);
187190 expect((await at("notes--98", "/__forge/notes")).status).toBe(404);
188191 const owner = await grantedCookie(app, servers.url, alice, getAppBySlug(app.db, "notes")!, "pull-98");
189 expect((await post("from a person", owner, "", "notes--98")).status).toBe(201);
192 expect((await post("from a person", owner, "", "notes--98", proofFor(owner, "notes--98"))).status).toBe(201);
190193 expect(issues()).toBe(before + 1);
191194 // The same app's production stays open to visitors.
192195 expect((await post("still open", "", "", "notes")).status).toBe(201);
@@ -297,16 +300,79 @@ describe("the forge button takes notes from anyone", () => {
297300 expect(await Bun.file(join(import.meta.dir, "../src/actions/app_notes.ts")).text()).not.toContain("x-forwarded-for");
298301 });
299302
303 describe("the panel's one-use proof", () => {
304 // Each test sends in a window of its own, clear of the bounds the tests before spent.
305 let window = 0;
306 beforeEach(() => {
307 const at = Date.now() + ++window * 3 * 86_400_000;
308 noteClock.now = () => at;
309 });
310 afterEach(() => {
311 noteClock.now = () => Date.now();
312 });
313 const named = async (res: Response) => { const j = (await res.json()) as { name: string }; if (j.name === undefined) throw new Error(JSON.stringify(j)); return j.name; };
314
315 test("an unproven post from a signed-in browser is a visitor's, and its list is the visitor's", async () => {
316 const owner = await grantedCookie(app, servers.url, alice, getAppBySlug(app.db, "notes")!);
317 const res = await post("no proof here", owner, "10.7.0.1");
318 expect(res.status).toBe(201);
319 expect(await named(res)).toMatch(/^visitor-/);
320 expect(res.headers.get(PROOF_HEADER)).toBeNull();
321 const list = (await (await at("notes", "/__forge/notes", {}, owner)).json()) as { name: string; admin: boolean };
322 expect(list.name).toMatch(/^visitor-/);
323 expect(list.admin).toBe(false);
324 });
325
326 test("a proven post is @name, the list is theirs and admin, and each answer brings the next proof", async () => {
327 const owner = await grantedCookie(app, servers.url, alice, getAppBySlug(app.db, "notes")!);
328 const res = await post("with proof", owner, "10.7.0.2", "notes", proofFor(owner, "notes"));
329 expect(await named(res)).toBe("@alice");
330 const next = res.headers.get(PROOF_HEADER)!;
331 expect(next).toMatch(/^[0-9a-f]{48}$/);
332 const list = await at("notes", "/__forge/notes", { headers: { [PROOF_HEADER]: next } }, owner);
333 const j = (await list.json()) as { name: string; admin: boolean };
334 expect(j.name).toBe("@alice");
335 expect(j.admin).toBe(true);
336 expect(list.headers.get(PROOF_HEADER)).toMatch(/^[0-9a-f]{48}$/);
337 expect(list.headers.get(PROOF_HEADER)).not.toBe(next);
338 });
339
340 test("a reused proof, one for another grant and one for another host are a visitor's", async () => {
341 const owner = await grantedCookie(app, servers.url, alice, getAppBySlug(app.db, "notes")!);
342 const token = proofFor(owner, "notes");
343 expect(await named(await post("first", owner, "10.7.0.3", "notes", token))).toBe("@alice");
344 expect(await named(await post("again", owner, "10.7.0.3", "notes", token))).toMatch(/^visitor-/);
345 const foreign = issueProof("someone-elses-grant", host("notes"));
346 expect(await named(await post("foreign grant", owner, "10.7.0.4", "notes", foreign))).toMatch(/^visitor-/);
347 const elsewhere = issueProof(owner.slice(owner.indexOf("=") + 1), host("diary"));
348 expect(await named(await post("foreign host", owner, "10.7.0.5", "notes", elsewhere))).toMatch(/^visitor-/);
349 });
350
351 test("only a page load is given a proof: a same-origin fetch of the page gets none", () => {
352 expect(pageLoad(new Headers())).toBe(true);
353 expect(pageLoad(new Headers({ "sec-fetch-dest": "document", "sec-fetch-site": "none" }))).toBe(true);
354 expect(pageLoad(new Headers({ "sec-fetch-dest": "iframe", "sec-fetch-site": "cross-site" }))).toBe(true);
355 expect(pageLoad(new Headers({ "sec-fetch-dest": "empty", "sec-fetch-site": "same-origin" }))).toBe(false);
356 expect(pageLoad(new Headers({ "sec-fetch-dest": "iframe", "sec-fetch-site": "same-origin" }))).toBe(false);
357 });
358 });
359
360 test("the panel's tag carries the proof as a data attribute, and the script strips it on load", () => {
361 expect(toolbarTag("http://f.test", undefined, "ab12")).toContain(' data-proof="ab12"');
362 expect(toolbarTag("http://f.test")).not.toContain("data-proof");
363 expect(toolbarJs).toContain('removeAttribute("data-proof")');
364 });
365
300366 test("a private app takes notes from a signed-in reader, as themselves, and from no one else", async () => {
301367 const reader = await grantedCookie(app, servers.url, alice, getAppBySlug(app.db, "diary")!);
302 const res = await post("A note on the diary", reader, "10.5.0.1", "diary");
368 const res = await post("A note on the diary", reader, "10.5.0.1", "diary", proofFor(reader, "diary"));
303369 expect(res.status).toBe(201);
304370 expect(((await res.json()) as { name: string }).name).toBe("@alice");
305371 const repo = getRepo(app.db, "alice", "diary")!;
306372 const issue = app.db.query<{ body: string }, [string]>("SELECT body FROM issue WHERE repo_id = ?1 ORDER BY number DESC").get(repo.id)!;
307373 expect(issue.body).toContain("by @alice.");
308374 expect(issue.body).toContain("Untrusted data");
309 const mine = (await (await at("diary", "/__forge/notes", {}, reader)).json()) as { notes: unknown[]; admin: boolean };
375 const mine = (await (await at("diary", "/__forge/notes", { headers: { [PROOF_HEADER]: proofFor(reader, "diary") } }, reader)).json()) as { notes: unknown[]; admin: boolean };
310376 expect(mine.notes.length).toBe(1);
311377 expect(mine.admin).toBe(true);
312378 expect((await at("diary", "/__forge/notes")).status).toBe(404);
test/toolbar-actions.test.ts +3 −3
@@ -89,10 +89,10 @@ describe("toolbar actions", () => {
8989 test("the bar links to the forge page and never posts or reads the app", () => {
9090 expect(toolbarJs).toContain('"/toolbar"');
9191 // Its requests are all to the app's own origin: the crew line (#2233) and,
92 // for the feedback door (#4758), the sender's notes and their status.
93 expect(toolbarJs.match(/fetch\(/g)?.length).toBe(3);
92 // for the feedback door (#4758), the sender's notes and their status, through one function.
93 expect(toolbarJs.match(/fetch\(/g)?.length).toBe(2);
9494 expect(toolbarJs).toContain('fetch("/__forge/crew"');
95 expect(toolbarJs).toContain('fetch("/__forge/note"');
95 expect(toolbarJs).toContain('door("/__forge/note"');
9696 expect(toolbarJs).not.toMatch(/FormData|\.send\(/);
9797 expect(toolbarJs).not.toContain("XMLHttpRequest");
9898 expect(toolbarJs).not.toContain("document.cookie");

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs