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

Desk store: notes of at most 280 characters with kind, author, expiry, a 50-note cap and a secret and URL filter #12546

Merged opened this

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

Merged

scout/forge:scout/12526-desk-store-notes-of-at-most-280-characte → main 4 commits fork 1 thread · 1 unresolved

  • Merged. scout/12526-desk-store-notes-of-at-most-280-characte was merged into main as 3d2d8e1.

Review

Sign in to review this change.
6 files changed +288 −0
scripts/unreached-allow.json +1
@@ -1,4 +1,5 @@
11 [
2 "src/core/desk.ts",
23 "src/perf/page_budget_read.ts",
34 "src/spawn/pod.ts"
45 ]
scripts/unused-exports-allow.json +2
@@ -25,6 +25,8 @@
2525 "src/core/capacity.ts provideCpusForTest",
2626 "src/core/chapters.ts forgetFrozenItems",
2727 "src/core/credit.ts clearCreditCache",
28 "src/core/desk.ts clearNote",
29 "src/core/desk.ts listNotes",
2830 "src/core/device_grants.ts appIdOfSlug",
2931 "src/core/device_grants.ts grantedSlugs",
3032 "src/core/feed_chips.ts noChips",
src/core/desk.ts added +154
@@ -0,0 +1,154 @@
1 /**
2 * The desk: short notes people leave where the crew can see them. A note is
3 * at most 280 characters of one kind (doing, hold, found, ask), carries its
4 * author and an expiry (4 h unless asked, 48 h at most), and is never
5 * edited: it is cleared by its author or an admin, or it expires. The desk
6 * holds at most 50 live notes; past that the oldest go first. A note that
7 * holds a secret-shaped string or a link to anywhere but the forge is
8 * refused, so the desk cannot carry a credential out or a stranger's page
9 * in. Pure data, no events, no page (#12526).
10 */
11 import type { Database } from "bun:sqlite";
12 import { secretFindings } from "../checks/scan_rules.ts";
13 import { id } from "../kernel/id.ts";
14 import { err, ok, type Result } from "../kernel/result.ts";
15 import { plainText } from "./reports.ts";
16
17 export const KINDS = ["doing", "hold", "found", "ask"] as const;
18 export type DeskKind = (typeof KINDS)[number];
19 export const NOTE_MAX = 280;
20 export const DEFAULT_TTL_MS = 4 * 3_600_000;
21 export const MAX_TTL_MS = 48 * 3_600_000;
22 export const DESK_MAX = 50;
23
24 export interface DeskNote {
25 readonly id: string;
26 readonly authorId: string;
27 readonly kind: DeskKind;
28 readonly body: string;
29 readonly createdAt: string;
30 readonly expiresAt: string;
31 }
32
33 export type DeskError =
34 | "invalid_kind"
35 | "empty"
36 | "too_long"
37 | "invalid_expiry"
38 | "has_secret"
39 | "has_link"
40 | "not_found"
41 | "not_yours";
42
43 export interface DeskInput {
44 readonly authorId: string;
45 readonly kind: string;
46 readonly body: string;
47 /** How long it stays, in milliseconds; 4 hours when absent. */
48 readonly ttlMs?: number;
49 /** Hosts a link may point at: the forge's own. Any other link is refused. */
50 readonly forgeHosts?: readonly string[];
51 readonly now?: Date;
52 }
53
54 interface Row {
55 id: string;
56 author_id: string;
57 kind: DeskKind;
58 body: string;
59 created_at: string;
60 expires_at: string;
61 }
62
63 const fromRow = (r: Row): DeskNote => ({
64 id: r.id,
65 authorId: r.author_id,
66 kind: r.kind,
67 body: r.body,
68 createdAt: r.created_at,
69 expiresAt: r.expires_at,
70 });
71
72 /**
outdated

The TLD allowlist is a hole: any bare host on a TLD not listed (evil.zip, evil.ru, evil.page) is accepted. Match any dotted host with an alphabetic TLD of 2+ letters, plus IPv4 hosts, and test each.

73 * A scheme://, www., IPv4 or any dotted host whose last label is 2+ letters (`evil.zip/x`);
74 * version numbers like 1.2.3 pass.
75 */
76 const LINK =
77 /\b[a-z][a-z0-9+.-]*:\/\/[^\s]*|\bwww\.[^\s]+|\b(?:\d{1,3}(?:\.\d{1,3}){3}|[a-z0-9-]+(?:\.[a-z0-9-]+)*\.[a-z]{2,})(?![a-z0-9-])(?::\d+)?[^\s]*/gi;
78
79 const hostOf = (link: string): string => {
80 const bare = /^[a-z][a-z0-9+.-]*:\/\//i.test(link) ? link : `https://${link}`;
81 try {
82 return new URL(bare).hostname.toLowerCase();
83 } catch {
84 return "";
85 }
86 };
87
88 /** Whether `text` holds a link whose host is not one of `forgeHosts`. */
89 export function hasOutsideLink(text: string, forgeHosts: readonly string[] = []): boolean {
90 const own = new Set(forgeHosts.map((h) => h.toLowerCase()));
91 return [...text.matchAll(LINK)].some((m) => !own.has(hostOf(m[0])));
92 }
93
94 /** Whether `text` holds a string shaped like a credential (the code scanner's rules). */
95 export const hasSecret = (text: string): boolean => secretFindings("desk", text).length > 0;
96
97 /** Drop the notes whose time is up. */
98 export function expireNotes(db: Database, now: Date = new Date()): number {
99 return db.query("DELETE FROM desk_note WHERE expires_at <= ?1").run(now.toISOString()).changes;
100 }
101
102 /** Leave a note. The refusal names the rule; nothing is stored then. */
103 export function addNote(db: Database, input: DeskInput): Result<DeskNote, DeskError> {
104 if (!(KINDS as readonly string[]).includes(input.kind)) return err("invalid_kind");
105 const body = plainText(input.body).trim();
106 if (body === "") return err("empty");
107 if ([...body].length > NOTE_MAX) return err("too_long");
108 const ttl = input.ttlMs ?? DEFAULT_TTL_MS;
109 if (!Number.isFinite(ttl) || ttl <= 0 || ttl > MAX_TTL_MS) return err("invalid_expiry");
110 if (hasSecret(body)) return err("has_secret");
111 if (hasOutsideLink(body, input.forgeHosts)) return err("has_link");
112 const now = input.now ?? new Date();
113 expireNotes(db, now);
114 const note: DeskNote = {
115 id: id(),
116 authorId: input.authorId,
117 kind: input.kind as DeskKind,
118 body,
119 createdAt: now.toISOString(),
120 expiresAt: new Date(now.getTime() + ttl).toISOString(),
121 };
122 db.query(
123 "INSERT INTO desk_note (id, author_id, kind, body, created_at, expires_at) VALUES (?1,?2,?3,?4,?5,?6)",
124 ).run(note.id, note.authorId, note.kind, note.body, note.createdAt, note.expiresAt);
125 // Past the cap the oldest go first.
126 db.query(
127 `DELETE FROM desk_note WHERE id IN (
128 SELECT id FROM desk_note ORDER BY created_at DESC, id DESC LIMIT -1 OFFSET ?1)`,
129 ).run(DESK_MAX);
130 return ok(note);
131 }
132
133 /** The live notes, newest first. */
134 export function listNotes(db: Database, now: Date = new Date()): DeskNote[] {
135 return db
136 .query<Row, [string]>(
137 "SELECT * FROM desk_note WHERE expires_at > ?1 ORDER BY created_at DESC, id DESC LIMIT 50",
138 )
139 .all(now.toISOString())
140 .map(fromRow);
141 }
142
143 /** Clear a note: its author or an admin only. */
144 export function clearNote(
145 db: Database,
146 noteId: string,
147 by: { readonly id: string; readonly isAdmin: boolean },
148 ): Result<void, DeskError> {
149 const row = db.query<Row, [string]>("SELECT * FROM desk_note WHERE id = ?1").get(noteId);
150 if (row === null) return err("not_found");
151 if (row.author_id !== by.id && !by.isAdmin) return err("not_yours");
152 db.query("DELETE FROM desk_note WHERE id = ?1").run(noteId);
153 return ok();
154 }
src/core/migration-pins/20261114130000-desk-note.pin added +1
@@ -0,0 +1,1 @@
1 a7960ab670a9af935e35de2fd98da0663d24821726d1bc4cb387fdf4bf4d3cb8
src/core/migrations/20261114130000-desk-note.ts added +13
@@ -0,0 +1,13 @@
1 // The desk's notes (#12526): short, expiring words one person leaves for the
2 // crew's desk. Additive: one table, one index for the newest-first read.
3 export default `
4 CREATE TABLE IF NOT EXISTS desk_note (
5 id TEXT PRIMARY KEY,
6 author_id TEXT NOT NULL REFERENCES user(id) ON DELETE CASCADE,
7 kind TEXT NOT NULL CHECK (kind IN ('doing','hold','found','ask')),
8 body TEXT NOT NULL,
9 created_at TEXT NOT NULL,
10 expires_at TEXT NOT NULL
11 );
12 CREATE INDEX IF NOT EXISTS desk_note_created ON desk_note(created_at DESC, id DESC);
13 `;
test/desk.test.ts added +117
@@ -0,0 +1,117 @@
1 /** The desk's notes at the rows: kinds, length, expiry, the cap, the filter, who clears. */
2 import { afterAll, beforeAll, describe, 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 {
8 addNote, clearNote, expireNotes, hasOutsideLink, listNotes,
9 DESK_MAX, MAX_TTL_MS, NOTE_MAX, DEFAULT_TTL_MS,
10 } from "../src/core/desk.ts";
11 import { createUser } from "../src/core/users.ts";
12 import { quietLogs } from "./helpers/globals.ts";
13
14 quietLogs();
15
16 describe("the desk's notes", () => {
17 let dataDir = "";
18 let app: App;
19 let alice = "";
20 let bob = "";
21 const T0 = new Date("2026-11-14T10:00:00Z");
22 const mk = async (username: string, first = false): Promise<string> => {
23 const u = await createUser(app.db, { origin: "seed", trusted: true, username,
24 email: `${username}@forge.test`, password: "a-good-passphrase", adminIfFirst: first });
25 if (!u.ok) throw new Error(u.error);
26 return u.value.id;
27 };
28 const add = (body: string, extra: Record<string, unknown> = {}) =>
29 addNote(app.db, { authorId: alice, kind: "doing", body, now: T0, ...extra });
30
31 beforeAll(async () => {
32 dataDir = await mkdtemp(join(tmpdir(), "forge-desk-"));
33 app = createApp({ dataDir, port: 0 });
34 alice = await mk("alice", true);
35 bob = await mk("bob");
36 });
37 afterAll(async () => {
38 app.close();
39 await rm(dataDir, { recursive: true, force: true });
40 });
41 const reset = () => app.db.query("DELETE FROM desk_note").run();
42
43 test("adds a note of each kind, expiring in 4 hours by default", () => {
44 reset();
45 for (const kind of ["doing", "hold", "found", "ask"]) {
46 expect(addNote(app.db, { authorId: alice, kind, body: "x", now: T0 }).ok).toBe(true);
47 }
48 const n = listNotes(app.db, T0)[0]!;
49 expect(Date.parse(n.expiresAt) - Date.parse(n.createdAt)).toBe(DEFAULT_TTL_MS);
50 });
51
52 test("refuses an unknown kind, an empty note and one past 280 characters", () => {
53 expect(add("x", { kind: "shout" })).toEqual({ ok: false, error: "invalid_kind" });
54 expect(add(" \n ")).toEqual({ ok: false, error: "empty" });
55 expect(add("a".repeat(NOTE_MAX)).ok).toBe(true);
56 expect(add("a".repeat(NOTE_MAX + 1))).toEqual({ ok: false, error: "too_long" });
57 });
58
59 test("expiry: at most 48 hours, and an expired note is gone", () => {
60 reset();
61 expect(add("x", { ttlMs: MAX_TTL_MS }).ok).toBe(true);
62 expect(add("x", { ttlMs: MAX_TTL_MS + 1 })).toEqual({ ok: false, error: "invalid_expiry" });
63 expect(add("x", { ttlMs: 0 })).toEqual({ ok: false, error: "invalid_expiry" });
64 expect(add("short", { ttlMs: 1000 }).ok).toBe(true);
65 const later = new Date(T0.getTime() + 2000);
66 expect(listNotes(app.db, later).map((n) => n.body)).toEqual(["x"]);
67 expect(expireNotes(app.db, later)).toBe(1);
68 });
69
70 test("lists newest first", () => {
71 reset();
72 add("one", { now: new Date(T0.getTime() + 1) });
73 add("two", { now: new Date(T0.getTime() + 2) });
74 expect(listNotes(app.db, T0).map((n) => n.body)).toEqual(["two", "one"]);
75 });
76
77 test("past 50 the oldest go first", () => {
78 reset();
79 for (let i = 0; i < DESK_MAX + 3; i++) add(`n${i}`, { now: new Date(T0.getTime() + i) });
80 const bodies = listNotes(app.db, T0).map((n) => n.body);
81 expect(bodies.length).toBe(DESK_MAX);
82 expect(bodies[0]).toBe(`n${DESK_MAX + 2}`);
83 expect(bodies).not.toContain("n0");
84 expect(bodies).not.toContain("n2");
85 expect(bodies).toContain("n3");
86 });
87
88 test("refuses a secret-shaped string", () => {
89 expect(add("key AKIAABCDEFGHIJKLMNOP here")).toEqual({ ok: false, error: "has_secret" });
90 expect(add(`token ghp_${"a".repeat(36)}`)).toEqual({ ok: false, error: "has_secret" });
91 });
92
93 test("refuses a link outside the forge, allows the forge's own", () => {
94 expect(add("see https://evil.example/x")).toEqual({ ok: false, error: "has_link" });
95 expect(add("see www.evil.com")).toEqual({ ok: false, error: "has_link" });
96 expect(add("see evil.io/page")).toEqual({ ok: false, error: "has_link" });
97 for (const bad of ["see evil.zip", "see evil.ru/x", "see evil.page/x", "see evil.link", "ping 10.0.0.1:8080/x", "go to 10.0.0.1"])
98 expect(add(bad)).toEqual({ ok: false, error: "has_link" });
99 expect(add("shipped v1.2.3, then done.").ok).toBe(true);
100 const hosts = ["forge.open-platform.sh"];
101 expect(add("see https://forge.open-platform.sh/trevato/forge/issues/1", { forgeHosts: hosts }).ok).toBe(true);
102 expect(add("see https://forge.open-platform.sh.evil.com/", { forgeHosts: hosts }).ok).toBe(false);
103 expect(hasOutsideLink("plain words, version 1.2.3")).toBe(false);
104 });
105
106 test("only the author or an admin clears; there is no editing", () => {
107 reset();
108 const note = (add("mine") as { ok: true; value: { id: string } }).value;
109 expect(clearNote(app.db, note.id, { id: bob, isAdmin: false })).toEqual({ ok: false, error: "not_yours" });
110 expect(listNotes(app.db, T0).length).toBe(1);
111 expect(clearNote(app.db, note.id, { id: alice, isAdmin: false }).ok).toBe(true);
112 expect(clearNote(app.db, note.id, { id: alice, isAdmin: false })).toEqual({ ok: false, error: "not_found" });
113 const again = (add("again") as { ok: true; value: { id: string } }).value;
114 expect(clearNote(app.db, again.id, { id: bob, isAdmin: true }).ok).toBe(true);
115 expect(listNotes(app.db, T0).length).toBe(0);
116 });
117 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs