From ce51499d1d6a56d6fbec0cb23bf5cc4c8c113a8c Mon Sep 17 00:00:00 2001 From: brainstorm-os Date: Thu, 19 Nov 2026 14:16:06 +0100 Subject: [PATCH 01/42] =?UTF-8?q?style(sdk):=20format=20b59-cells.test=20?= =?UTF-8?q?=E2=80=94=20main's=20lint=20step=20has=20been=20red=20since=20#?= =?UTF-8?q?513?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Claude Fable 5.2 --- packages/sdk/src/property-ui/cells/b59-cells.test.tsx | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/sdk/src/property-ui/cells/b59-cells.test.tsx b/packages/sdk/src/property-ui/cells/b59-cells.test.tsx index 8160d06a..c02549a3 100644 --- a/packages/sdk/src/property-ui/cells/b59-cells.test.tsx +++ b/packages/sdk/src/property-ui/cells/b59-cells.test.tsx @@ -179,7 +179,9 @@ describe("File cells (accept-only)", () => { onChange: vi.fn(), noteId: "n1", }); - expect(h.container.querySelector(".bs-cell-file-pending")?.textContent).toContain("aren't available here"); + expect(h.container.querySelector(".bs-cell-file-pending")?.textContent).toContain( + "aren't available here", + ); expect(h.container.querySelector(".bs-cell-file-empty")).not.toBeNull(); }); From 5814383c3b7c964862c3b2a8c89bc34f1b4baf3f Mon Sep 17 00:00:00 2001 From: brainstorm-os Date: Thu, 19 Nov 2026 14:29:28 +0100 Subject: [PATCH 02/42] fix(first-run): a new vault opens on its dashboard, not the release history; the template step fits the window - A vault created by this build is stamped as having seen the bundled changelog, so What's New no longer opens over a first-time user's dashboard. - The Start step drops the brand hero: with seven template tiles under it the card outgrew a default window and Create vault sat below the fold. - The starter journal entry carries the snippet + word count Journal reads, so it no longer says "0 words" and "No entry yet." beside its own text. - The starter bookmark points at docs.getbrainstorm.online, not a domain the project does not serve. - Chat: a vault with no channels says so instead of "Pick a channel"; the channel sidebar and Preview's file sidebar use the same background as every other app's. - Database list descriptions no longer trail a raw type id; seven first-run strings say "objects"/"nothing" instead of "entities". Co-Authored-By: Claude Fable 5.2 --- apps/chat/src/app.tsx | 4 +-- apps/chat/src/i18n/de.json | 2 ++ apps/chat/src/i18n/en.json | 2 ++ apps/chat/src/i18n/es.json | 2 ++ apps/chat/src/i18n/fr.json | 2 ++ apps/chat/src/i18n/it.json | 2 ++ apps/chat/src/i18n/pt.json | 2 ++ apps/chat/src/styles.css | 2 +- apps/database/src/i18n/de.json | 5 ++- apps/database/src/i18n/en.json | 5 ++- apps/database/src/i18n/es.json | 5 ++- apps/database/src/i18n/fr.json | 5 ++- apps/database/src/i18n/it.json | 5 ++- apps/database/src/i18n/pt.json | 5 ++- apps/database/src/logic/vault-lists.test.ts | 8 ++--- apps/database/src/logic/vault-lists.ts | 12 +++---- apps/files/src/i18n/en.json | 2 +- apps/files/src/i18n/es.json | 2 +- apps/form-designer/src/i18n/en.json | 4 +-- apps/form-designer/src/i18n/es.json | 4 +-- apps/graph/src/i18n/de.json | 4 +-- apps/graph/src/i18n/en.json | 4 +-- apps/graph/src/i18n/es.json | 4 +-- apps/graph/src/i18n/fr.json | 4 +-- apps/graph/src/i18n/it.json | 4 +-- apps/graph/src/i18n/pt.json | 4 +-- apps/preview/src/styles.css | 2 +- packages/shell/src/main/index.ts | 18 +++++++++++ .../src/main/welcome/welcome-content.test.ts | 15 +++++++++ .../shell/src/main/welcome/welcome-content.ts | 31 ++++++++++++++----- .../src/renderer/welcome/template-gallery.tsx | 2 +- .../welcome/welcome-templates.test.tsx | 13 ++++++++ .../shell/src/renderer/welcome/welcome.tsx | 17 +++++++--- 33 files changed, 139 insertions(+), 63 deletions(-) diff --git a/apps/chat/src/app.tsx b/apps/chat/src/app.tsx index 06e71134..4fd0cbf2 100644 --- a/apps/chat/src/app.tsx +++ b/apps/chat/src/app.tsx @@ -932,8 +932,8 @@ export function ChatApp(): ReactElement {
{ links: [], }; const { lists } = buildVaultLists(one, NOW); - expect(lists[0]?.description).toBe("1 item · brainstorm/Doc/v1"); + expect(lists[0]?.description).toBe("1 item"); }); }); @@ -374,7 +374,7 @@ describe("curated People list (9.12.13(b))", () => { (l) => l.source?.kind === ListSourceKind.ByType && l.source.types[0] === "brainstorm/Person/v1", ); expect(people?.name).toBe("People"); - expect(people?.description).toBe("2 contacts · brainstorm/Person/v1"); + expect(people?.description).toBe("2 contacts"); expect(people?.views).toHaveLength(3); expect(people?.defaultViewId).toBe(people?.views[0]); @@ -439,7 +439,7 @@ describe("curated Tasks list (9.14.4)", () => { (l) => l.source?.kind === ListSourceKind.ByType && l.source.types[0] === "brainstorm/Task/v1", ); expect(tasks?.name).toBe("Tasks"); - expect(tasks?.description).toBe("2 tasks · brainstorm/Task/v1"); + expect(tasks?.description).toBe("2 tasks"); expect(tasks?.views).toHaveLength(3); expect(tasks?.defaultViewId).toBe(tasks?.views[0]); @@ -499,6 +499,6 @@ describe("curated Tasks list (9.14.4)", () => { links: [], }; const tasks = buildVaultLists(one, NOW).lists[0]; - expect(tasks?.description).toBe("1 task · brainstorm/Task/v1"); + expect(tasks?.description).toBe("1 task"); }); }); diff --git a/apps/database/src/logic/vault-lists.ts b/apps/database/src/logic/vault-lists.ts index c85caf14..2a2931be 100644 --- a/apps/database/src/logic/vault-lists.ts +++ b/apps/database/src/logic/vault-lists.ts @@ -142,10 +142,10 @@ export function buildVaultLists( id: listId, name: friendlyTypeName(type), icon: null, - description: t("brainstorm.database.vault.typeList.description", { - items: itemCountLabel(rows.length), - type, - }), + // The count alone: the raw type id (`brainstorm/Agent/v1`) used to + // trail it, and it was the first line of text under the first list + // a new user opened. + description: itemCountLabel(rows.length), source: { kind: ListSourceKind.ByType, types: [type] }, members: { include: [], exclude: [] }, views: [viewId], @@ -381,7 +381,7 @@ const CURATED_TYPE_LISTS: Record = { // Columns mirror the `brainstorm/Task/v1` shape (apps/tasks task.ts). "brainstorm/Task/v1": { name: "Tasks", - description: (n) => `${n} ${n === 1 ? "task" : "tasks"} · brainstorm/Task/v1`, + description: (n) => `${n} ${n === 1 ? "task" : "tasks"}`, columns: ["name", "statusKey", "priority", "scheduledAt", "dueAt"], views: [ { @@ -423,7 +423,7 @@ const CURATED_TYPE_LISTS: Record = { }, "brainstorm/Person/v1": { name: "People", - description: (n) => `${n} ${n === 1 ? "contact" : "contacts"} · brainstorm/Person/v1`, + description: (n) => `${n} ${n === 1 ? "contact" : "contacts"}`, columns: ["name", "email", "phone", "company", "role"], views: [ { diff --git a/apps/files/src/i18n/en.json b/apps/files/src/i18n/en.json index 6748b342..94659d62 100644 --- a/apps/files/src/i18n/en.json +++ b/apps/files/src/i18n/en.json @@ -132,7 +132,7 @@ "brainstorm.files.empty.body": "Drag files in, or create a folder to get started.", "brainstorm.files.empty.newFolder": "New folder", "brainstorm.files.empty.searchTitle": "No matches", - "brainstorm.files.empty.searchBody": "No entities in this scope match {query}.", + "brainstorm.files.empty.searchBody": "Nothing in this scope matches {query}.", "brainstorm.files.inspector.tabsRegion": "Inspector tabs", "brainstorm.files.inspector.tabPreview": "Preview", "brainstorm.files.inspector.tabProperties": "Properties", diff --git a/apps/files/src/i18n/es.json b/apps/files/src/i18n/es.json index 94d8e6ce..8889fafc 100644 --- a/apps/files/src/i18n/es.json +++ b/apps/files/src/i18n/es.json @@ -132,7 +132,7 @@ "brainstorm.files.empty.body": "Arrastre archivos aquí o cree una carpeta para empezar.", "brainstorm.files.empty.newFolder": "Nueva carpeta", "brainstorm.files.empty.searchTitle": "Sin coincidencias", - "brainstorm.files.empty.searchBody": "Ninguna entidad en este ámbito coincide con {query}.", + "brainstorm.files.empty.searchBody": "Ningún objeto en este ámbito coincide con {query}.", "brainstorm.files.inspector.tabsRegion": "Pestañas del inspector", "brainstorm.files.inspector.tabPreview": "Vista previa", "brainstorm.files.inspector.tabProperties": "Propiedades", diff --git a/apps/form-designer/src/i18n/en.json b/apps/form-designer/src/i18n/en.json index a4061812..031294e9 100644 --- a/apps/form-designer/src/i18n/en.json +++ b/apps/form-designer/src/i18n/en.json @@ -4,7 +4,7 @@ "sidebar.region": "Saved forms", "sidebar.resize": "Resize form list", "sidebar.newForm": "New form", - "sidebar.empty": "No forms yet — create one to collect entities.", + "sidebar.empty": "No forms yet. Create one to collect new objects.", "sidebar.untitled": "Untitled form", "sidebar.itemActions": "Form actions", "sidebar.delete": "Delete form", @@ -17,7 +17,7 @@ "mode.region": "Form mode", "builder.nameLabel": "Form name", "builder.namePlaceholder": "Name this form…", - "builder.targetLabel": "Creates entities of type", + "builder.targetLabel": "Creates objects of type", "builder.targetPlaceholder": "Entity type URL (e.g. brainstorm/Object/v1)", "builder.layoutLabel": "Layout", "builder.layoutStacked": "Stacked", diff --git a/apps/form-designer/src/i18n/es.json b/apps/form-designer/src/i18n/es.json index 03a94697..93a828b0 100644 --- a/apps/form-designer/src/i18n/es.json +++ b/apps/form-designer/src/i18n/es.json @@ -4,7 +4,7 @@ "sidebar.region": "Formularios guardados", "sidebar.resize": "Redimensionar lista de formularios", "sidebar.newForm": "Nuevo formulario", - "sidebar.empty": "Aún no hay formularios — crea uno para recopilar entidades.", + "sidebar.empty": "Aún no hay formularios — crea uno para recopilar objetos.", "sidebar.untitled": "Formulario sin título", "sidebar.itemActions": "Acciones del formulario", "sidebar.delete": "Eliminar formulario", @@ -17,7 +17,7 @@ "mode.region": "Modo de formulario", "builder.nameLabel": "Nombre del formulario", "builder.namePlaceholder": "Ponle nombre a este formulario…", - "builder.targetLabel": "Crea entidades de tipo", + "builder.targetLabel": "Crea objetos de tipo", "builder.targetPlaceholder": "URL del tipo de entidad (p. ej. brainstorm/Object/v1)", "builder.layoutLabel": "Diseño", "builder.layoutStacked": "Apilado", diff --git a/apps/graph/src/i18n/de.json b/apps/graph/src/i18n/de.json index 5774dcc8..05030be8 100644 --- a/apps/graph/src/i18n/de.json +++ b/apps/graph/src/i18n/de.json @@ -37,7 +37,7 @@ "path.hint.hops": "Verbunden über {count} Sprünge", "path.hint.none": "Kein Pfad verbindet diese beiden Knoten", "show.toggle": "{type} anzeigen", - "show.empty": "Noch keine Entitäten in diesem Vault.", + "show.empty": "Dieser Vault ist noch leer.", "show.systemGroup": "System", "type.any": "Beliebiger Typ", "type.someSelected": "{count} Typen", @@ -73,7 +73,7 @@ "status.noVault": "Kein Vault", "status.loading": "Vault wird geladen…", "status.listMissing": "vaultEntities.list fehlt", - "status.empty": "Keine Entitäten — erstellen Sie eine Notiz", + "status.empty": "Noch nichts zu zeigen — erstellen Sie eine Notiz", "status.count": "{count} {noun}", "status.entitySingular": "Entität", "status.entityPlural": "Entitäten", diff --git a/apps/graph/src/i18n/en.json b/apps/graph/src/i18n/en.json index 0cde1015..8727b9b7 100644 --- a/apps/graph/src/i18n/en.json +++ b/apps/graph/src/i18n/en.json @@ -37,7 +37,7 @@ "path.hint.hops": "Connected in {count} hops", "path.hint.none": "No path connects those two nodes", "show.toggle": "Show {type}", - "show.empty": "No entities in this vault yet.", + "show.empty": "Nothing in this vault yet.", "show.systemGroup": "System", "type.any": "Any type", "type.someSelected": "{count} types", @@ -73,7 +73,7 @@ "status.noVault": "No vault", "status.loading": "Loading vault…", "status.listMissing": "vaultEntities.list missing", - "status.empty": "No entities — create a note", + "status.empty": "Nothing to show yet. Create a note", "status.count": "{count} {noun}", "status.entitySingular": "entity", "status.entityPlural": "entities", diff --git a/apps/graph/src/i18n/es.json b/apps/graph/src/i18n/es.json index 2f92f08d..46e78c57 100644 --- a/apps/graph/src/i18n/es.json +++ b/apps/graph/src/i18n/es.json @@ -37,7 +37,7 @@ "path.hint.hops": "Conectados en {count} saltos", "path.hint.none": "Ninguna ruta conecta esos dos nodos", "show.toggle": "Mostrar {type}", - "show.empty": "Aún no hay entidades en esta bóveda.", + "show.empty": "Esta bóveda aún está vacía.", "show.systemGroup": "Sistema", "type.any": "Cualquier tipo", "type.someSelected": "{count} tipos", @@ -73,7 +73,7 @@ "status.noVault": "Sin bóveda", "status.loading": "Cargando bóveda…", "status.listMissing": "Falta vaultEntities.list", - "status.empty": "Sin entidades — cree una nota", + "status.empty": "Aún no hay nada que mostrar — cree una nota", "status.count": "{count} {noun}", "status.entitySingular": "entidad", "status.entityPlural": "entidades", diff --git a/apps/graph/src/i18n/fr.json b/apps/graph/src/i18n/fr.json index eb0b1001..432e7aff 100644 --- a/apps/graph/src/i18n/fr.json +++ b/apps/graph/src/i18n/fr.json @@ -37,7 +37,7 @@ "path.hint.hops": "Connectés en {count} sauts", "path.hint.none": "Aucun chemin ne relie ces deux nœuds", "show.toggle": "Afficher {type}", - "show.empty": "Aucune entité dans ce coffre pour le moment.", + "show.empty": "Ce coffre est encore vide.", "show.systemGroup": "Système", "type.any": "Tout type", "type.someSelected": "{count} types", @@ -73,7 +73,7 @@ "status.noVault": "Aucun coffre", "status.loading": "Chargement du coffre…", "status.listMissing": "vaultEntities.list manquant", - "status.empty": "Aucune entité — créez une note", + "status.empty": "Rien à afficher pour l'instant — créez une note", "status.count": "{count} {noun}", "status.entitySingular": "entité", "status.entityPlural": "entités", diff --git a/apps/graph/src/i18n/it.json b/apps/graph/src/i18n/it.json index d51745ed..055480b8 100644 --- a/apps/graph/src/i18n/it.json +++ b/apps/graph/src/i18n/it.json @@ -37,7 +37,7 @@ "path.hint.hops": "Connessi in {count} salti", "path.hint.none": "Nessun percorso collega questi due nodi", "show.toggle": "Mostra {type}", - "show.empty": "Ancora nessuna entità in questo vault.", + "show.empty": "Questo vault è ancora vuoto.", "show.systemGroup": "Sistema", "type.any": "Qualsiasi tipo", "type.someSelected": "{count} tipi", @@ -73,7 +73,7 @@ "status.noVault": "Nessun vault", "status.loading": "Caricamento del vault…", "status.listMissing": "vaultEntities.list mancante", - "status.empty": "Nessuna entità — crei una nota", + "status.empty": "Ancora niente da mostrare — crei una nota", "status.count": "{count} {noun}", "status.entitySingular": "entità", "status.entityPlural": "entità", diff --git a/apps/graph/src/i18n/pt.json b/apps/graph/src/i18n/pt.json index 0f5c2a60..d3ae2e60 100644 --- a/apps/graph/src/i18n/pt.json +++ b/apps/graph/src/i18n/pt.json @@ -37,7 +37,7 @@ "path.hint.hops": "Conectados em {count} saltos", "path.hint.none": "Nenhum caminho conecta esses dois nós", "show.toggle": "Mostrar {type}", - "show.empty": "Ainda não há entidades neste vault.", + "show.empty": "Este vault ainda está vazio.", "show.systemGroup": "Sistema", "type.any": "Qualquer tipo", "type.someSelected": "{count} tipos", @@ -73,7 +73,7 @@ "status.noVault": "Nenhum vault", "status.loading": "Carregando vault…", "status.listMissing": "vaultEntities.list ausente", - "status.empty": "Nenhuma entidade — crie uma nota", + "status.empty": "Ainda não há nada para mostrar — crie uma nota", "status.count": "{count} {noun}", "status.entitySingular": "entidade", "status.entityPlural": "entidades", diff --git a/apps/preview/src/styles.css b/apps/preview/src/styles.css index 2922ba7c..0c58fdd5 100644 --- a/apps/preview/src/styles.css +++ b/apps/preview/src/styles.css @@ -86,7 +86,7 @@ button { min-height: 0; display: flex; flex-direction: column; - background: var(--bg-elev); + background: var(--bg); border-right: var(--border-width) solid var(--color-border-subtle); overflow: hidden; } diff --git a/packages/shell/src/main/index.ts b/packages/shell/src/main/index.ts index 7593502b..1e858f91 100644 --- a/packages/shell/src/main/index.ts +++ b/packages/shell/src/main/index.ts @@ -3311,6 +3311,23 @@ void app.whenReady().then(async () => { // well-formed pair. Scoped to the fresh-vault open (the same // `ensureRootFolder().created` signal as the welcome seed) so an existing // vault's chosen appearance is never overwritten. + // A vault this build just created has no earlier version to compare + // against, so "What's new" has nothing to say to it. Without the stamp the + // popover read the missing marker as "every release unseen" and a first-time + // user's first sight of their dashboard was the full release history. + const stampChangelogSeenOnFreshVault = async (): Promise => { + const session = getActiveVaultSession(); + if (!session) return; + try { + const { loadBundledChangelog } = await import("./ipc/help-handlers"); + const newest = loadBundledChangelog().releases[0]; + if (!newest) return; + (await session.dashboardStore()).setLastSeenChangelogVersion(newest.version); + } catch (error) { + console.warn(`[brainstorm] changelog stamp failed: ${(error as Error).message}`); + } + }; + const seedNewVaultDefaults = async (): Promise => { const session = getActiveVaultSession(); if (!session) return; @@ -3614,6 +3631,7 @@ void app.whenReady().then(async () => { } const freshVault = await ensureRootFolder(); if (freshVault) { + await stampChangelogSeenOnFreshVault(); await seedWelcomeOnFreshVault(); await seedNewVaultDefaults(); // The welcome seed creates entities through the privileged in-process diff --git a/packages/shell/src/main/welcome/welcome-content.test.ts b/packages/shell/src/main/welcome/welcome-content.test.ts index 57a2f319..1ef046dd 100644 --- a/packages/shell/src/main/welcome/welcome-content.test.ts +++ b/packages/shell/src/main/welcome/welcome-content.test.ts @@ -16,6 +16,21 @@ describe("buildWelcomeStarterSet", () => { expect(buildWelcomeStarterSet(NOW)).toEqual(buildWelcomeStarterSet(NOW)); }); + it("gives the journal entry the snippet and word count its preview reads", () => { + const journal = buildWelcomeStarterSet(NOW).find( + (e) => e.type === WelcomeEntityType.JournalEntry, + ); + expect(journal?.properties.body).toBe( + "Your first journal entry. Today you opened Brainstorm for the first time. A good first step: Take the product tour.", + ); + expect(journal?.properties.wordCount).toBe(20); + }); + + it("points the starter bookmark at the project's own docs site", () => { + const bookmark = buildWelcomeStarterSet(NOW).find((e) => e.type === WelcomeEntityType.Bookmark); + expect(bookmark?.properties.url).toBe("https://docs.getbrainstorm.online"); + }); + it("seed version is a positive integer", () => { expect(Number.isInteger(WELCOME_SEED_VERSION)).toBe(true); expect(WELCOME_SEED_VERSION).toBeGreaterThan(0); diff --git a/packages/shell/src/main/welcome/welcome-content.ts b/packages/shell/src/main/welcome/welcome-content.ts index 80ae0067..f95cc02a 100644 --- a/packages/shell/src/main/welcome/welcome-content.ts +++ b/packages/shell/src/main/welcome/welcome-content.ts @@ -124,6 +124,23 @@ function paragraph(children: Inline[]): Block { return { type: "paragraph", version: 1, format: "", indent: 0, direction: null, children }; } +/** The denormalised `body` snippet + `wordCount` an editor autosave would have + * written for these paragraphs. Journal reads its day preview and word count + * from those two properties, never from the Y.Doc, so a planted body without + * them showed "0 words" and "No entry yet." beside the entry itself. */ +function denormalized(paragraphs: Block[]): { body: string; wordCount: number } { + const plain = paragraphs + .map((block) => + block.children + .map((inline) => (inline.type === "mention" ? inline.label : inline.text)) + .join(""), + ) + .join(" ") + .replace(/\s+/g, " ") + .trim(); + return { body: plain, wordCount: plain ? plain.split(" ").length : 0 }; +} + function body(children: Block[]): WelcomeBody { return { root: { type: "root", version: 1, format: "", indent: 0, direction: null, children } }; } @@ -182,7 +199,7 @@ export function buildWelcomeStarterSet(now: number): WelcomeStarterEntity[] { // Database/Graph show it). properties: { title: "Brainstorm — help & docs", - url: "https://brainstorm.app", + url: "https://docs.getbrainstorm.online", savedAt: now, ...ts, }, @@ -193,15 +210,15 @@ export function buildWelcomeStarterSet(now: number): WelcomeStarterEntity[] { // projection keys entries by an `YYYY-MM-DD` title); the UTC derivation // keeps the output byte-identical for a given `now`. const journalDateKey = new Date(now).toISOString().slice(0, 10); + const journalParagraphs = [ + paragraph([text("Your first journal entry. Today you opened Brainstorm for the first time.")]), + paragraph([text("A good first step: "), mention(task, "Take the product tour"), text(".")]), + ]; const journal: WelcomeStarterEntity = { id: JOURNAL_ID, type: WelcomeEntityType.JournalEntry, - properties: { title: journalDateKey, ...ts }, - body: body([ - title(journalDateKey), - paragraph([text("Your first journal entry. Today you opened Brainstorm for the first time.")]), - paragraph([text("A good first step: "), mention(task, "Take the product tour"), text(".")]), - ]), + properties: { title: journalDateKey, ...denormalized(journalParagraphs), ...ts }, + body: body([title(journalDateKey), ...journalParagraphs]), }; // The hub note mentions every other starter entity so Graph paints a diff --git a/packages/shell/src/renderer/welcome/template-gallery.tsx b/packages/shell/src/renderer/welcome/template-gallery.tsx index ab844bbc..76b72095 100644 --- a/packages/shell/src/renderer/welcome/template-gallery.tsx +++ b/packages/shell/src/renderer/welcome/template-gallery.tsx @@ -46,7 +46,7 @@ export function TemplateGallery({

{t("shell.welcome.templates.heading")}

-

{t("shell.welcome.templates.hint")}

+

{t("shell.welcome.templates.hint")}

    {templates.map((template) => { const selected = template.id === selectedId; diff --git a/packages/shell/src/renderer/welcome/welcome-templates.test.tsx b/packages/shell/src/renderer/welcome/welcome-templates.test.tsx index 23a0c105..13170d93 100644 --- a/packages/shell/src/renderer/welcome/welcome-templates.test.tsx +++ b/packages/shell/src/renderer/welcome/welcome-templates.test.tsx @@ -117,6 +117,19 @@ function submit(): void { } describe("Welcome — template gallery", () => { + it("drops the brand hero on the Start step so the actions stay above the fold", async () => { + await act(async () => { + root.render(); + }); + await flush(); + expect(host.querySelector(".welcome__brand")).not.toBeNull(); + act(() => root.unmount()); + root = createRoot(host); + await enterCreateMode(); + expect(host.querySelector(".welcome__brand")).toBeNull(); + expect(host.querySelector(".welcome__step-title")).not.toBeNull(); + }); + it("renders a card per template once the list loads", async () => { await enterCreateMode(); expect(host.querySelector('[data-testid="welcome-templates"]')).not.toBeNull(); diff --git a/packages/shell/src/renderer/welcome/welcome.tsx b/packages/shell/src/renderer/welcome/welcome.tsx index 5b8942e3..22f9c164 100644 --- a/packages/shell/src/renderer/welcome/welcome.tsx +++ b/packages/shell/src/renderer/welcome/welcome.tsx @@ -193,11 +193,18 @@ export function Welcome() { : "welcome__content" } > -
    - -

    {t("shell.welcome.brand")}

    -
    -

    {t("shell.welcome.tagline")}

    + {/* The Start step carries its own heading and the tallest content + (seven template tiles): with the brand hero above it the card + outgrew a default window and "Create vault" sat below the fold. */} + {mode !== WelcomeMode.Start && ( + <> +
    + +

    {t("shell.welcome.brand")}

    +
    +

    {t("shell.welcome.tagline")}

    + + )} {mode === WelcomeMode.Menu && (
    From 5416281fb21b28f1bab3f647a60649939b267d6e Mon Sep 17 00:00:00 2001 From: brainstorm-os Date: Thu, 19 Nov 2026 14:44:12 +0100 Subject: [PATCH 03/42] =?UTF-8?q?fix(polish):=200.15.1=20=E2=80=94=20a=20s?= =?UTF-8?q?creen-by-screen=20pass=20over=20every=20app,=20in=20light=20and?= =?UTF-8?q?=20dark?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Chat: day dividers key on the local calendar day (they keyed on the UTC date while the times beside them were local); both ⋯ buttons use the shared glyph. Books: one live progress value feeds the library row, the inspector and the footer; EPUBs drop the meaningless Pages row; the open inspector no longer covers the footer's next-page control. Browser: a restored blank tab shows the start page; short tab titles fit. Database: numeric header, cells and totals share one edge; type-list names split into words. SDK: an uncoloured chip is visible on light themes; an enabled header ⋯ rests at full strength. Files: Filter shares the toolbar field face; the sort menu fits its content; the selection bar is opaque and the list scrolls clear of it. Tasks: the checkbox ring holds 3:1 on dark themes. Journal, Mailbox: wording and an un-shouted account address. Adds the 0.15.1 What's New entry and bumps both package versions. Co-Authored-By: Claude Fable 5.2 --- apps/books/src/app.test.tsx | 17 ++ apps/books/src/app.tsx | 22 +- apps/books/src/logic/book-properties.test.ts | 22 +- apps/books/src/logic/book-properties.ts | 44 ++- apps/books/src/logic/library.test.ts | 20 ++ apps/books/src/logic/library.ts | 21 ++ apps/books/src/render/pdf-reader.ts | 12 +- apps/books/src/render/reader.test.ts | 29 ++ apps/books/src/render/reader.ts | 14 +- apps/books/src/styles.css | 28 +- apps/books/src/ui/inspector.tsx | 2 +- apps/browser/src/app-features.test.tsx | 34 ++- apps/browser/src/logic/persistence.test.ts | Bin 6335 -> 6763 bytes apps/browser/src/logic/persistence.ts | 8 +- apps/browser/src/styles.css | 5 +- apps/chat/src/app.tsx | 8 +- apps/chat/src/format.test.ts | 39 ++- apps/chat/src/format.ts | 9 +- apps/chat/src/logic/chat.test.ts | 58 +++- apps/chat/src/logic/chat.ts | 12 +- apps/chat/src/styles.css | 4 +- apps/database/src/styles.css | 25 +- apps/files/src/app.tsx | 8 +- apps/files/src/styles-toolbar-row.test.ts | 49 +++ apps/files/src/styles.css | 48 ++- apps/files/src/ui/dialogs.tsx | 4 +- apps/files/src/ui/filter-bar.tsx | 2 +- apps/graph/src/render/node-label.test.ts | 2 +- apps/journal/src/app.tsx | 2 +- apps/journal/src/i18n/de.json | 1 + apps/journal/src/i18n/en.json | 1 + apps/journal/src/i18n/es.json | 1 + apps/journal/src/i18n/fr.json | 1 + apps/journal/src/i18n/it.json | 1 + apps/journal/src/i18n/pt.json | 1 + apps/mailbox/src/styles.css | 4 + apps/tasks/src/styles.css | 4 +- package.json | 2 +- packages/sdk/src/app-theme.css | 6 + .../property-ui/dictionary-helpers.test.ts | 6 +- .../sdk/src/property-ui/dictionary-helpers.ts | 7 +- packages/sdk/src/system-entities.test.ts | 27 +- packages/sdk/src/system-entities.ts | 15 +- packages/shell/changelog/changelog.json | 281 ++++++++++++++++++ packages/shell/package.json | 2 +- 45 files changed, 827 insertions(+), 81 deletions(-) diff --git a/apps/books/src/app.test.tsx b/apps/books/src/app.test.tsx index 287318d8..1069b6f3 100644 --- a/apps/books/src/app.test.tsx +++ b/apps/books/src/app.test.tsx @@ -182,6 +182,23 @@ describe("BooksApp shelf", () => { expect(el.querySelector(".books__page")?.textContent).toContain("Anchor"); }); + // The inspector read the stored fraction as a bare number ("45") while the + // footer showed the live one ("23% read"); both now come from the reader. + it("shows the reader's live progress in the inspector, as a percentage", async () => { + const el = await renderApp(); + const footer = /(\d+)%/.exec(el.querySelector(".books__progress")?.textContent ?? "")?.[1]; + expect(footer).toBeDefined(); + expect(Number(footer)).toBeGreaterThan(0); + expect(el.querySelector(".bs-cell-progress-text")?.textContent).toBe(`${footer}%`); + }); + + it("carries no Pages row for a reflowable book — the footer owns the page count", async () => { + const el = await renderApp(); + const labels = [...el.querySelectorAll(".bs-props__row-label")].map((n) => n.textContent); + expect(labels).toContain("Progress"); + expect(labels).not.toContain("Pages"); + }); + // F-487 — the shared `.bs-props` inspector is `position: fixed`, so it // takes no layout space. Books centres a full-bleed reading measure under // it, so with the panel open every line ran on underneath: paragraphs read diff --git a/apps/books/src/app.tsx b/apps/books/src/app.tsx index da964549..d41b17f4 100644 --- a/apps/books/src/app.tsx +++ b/apps/books/src/app.tsx @@ -65,7 +65,7 @@ import { } from "./logic/book-open"; import { makePositionPersister, removeBookEntity, writeBookPatch } from "./logic/book-writes"; import { parseEpub } from "./logic/epub-parser"; -import { booksFromEntities } from "./logic/library"; +import { type LiveProgress, booksFromEntities, withLiveProgress } from "./logic/library"; import { type PdfInfo, pdfEnrichmentPatch } from "./logic/pdf-metadata"; import { SAMPLE_BOOK_CONTENT, sampleBook } from "./logic/sample-book"; import { type TocEntry, tocFromContent, tocFromPdfOutline } from "./logic/toc"; @@ -283,6 +283,7 @@ export function BooksApp(): ReactElement { const [openNonce, setOpenNonce] = useState(0); const [status, setStatus] = useState(ReaderStatus.Idle); const [toc, setToc] = useState([]); + const [liveProgress, setLiveProgress] = useState(null); const [showLibrary, setShowLibrary] = useState(true); const libraryAutoHide = useAutoHidePanel({ side: PanelSide.Left, collapsed: !showLibrary }); const [showInspector, setShowInspector] = useState(readInspectorPref); @@ -380,10 +381,13 @@ export function BooksApp(): ReactElement { ); const isSample = selectedId === SAMPLE_BOOK_ID; + const shelf = useMemo(() => withLiveProgress(books, liveProgress), [books, liveProgress]); const selectedBook = useMemo(() => { - if (isSample) return sample.current; - return selectedId ? (books.find((b) => b.id === selectedId) ?? null) : null; - }, [isSample, selectedId, books]); + if (isSample) { + return sample.current ? (withLiveProgress([sample.current], liveProgress)[0] ?? null) : null; + } + return selectedId ? (shelf.find((b) => b.id === selectedId) ?? null) : null; + }, [isSample, selectedId, shelf, liveProgress]); // PRES-3 — who's-here on the open book (vault rows only; not the sample). const bookPeers = usePresence( isSample ? null : (selectedBook?.id ?? null), @@ -461,6 +465,7 @@ export function BooksApp(): ReactElement { if (!stage || !controls) return undefined; setToc([]); + setLiveProgress(null); if (selectedId === null) { stage.replaceChildren(); @@ -468,9 +473,14 @@ export function BooksApp(): ReactElement { return undefined; } + const reportProgress = (progress: number): void => { + if (seq === openSeqRef.current) setLiveProgress({ bookId: selectedId, progress }); + }; + if (selectedId === SAMPLE_BOOK_ID) { const handle = mountReader(stage, controls, SAMPLE_BOOK_CONTENT, { bookId: SAMPLE_BOOK_ID, + onProgressChange: reportProgress, }); readerRef.current = { dispose: handle.dispose, goTo: handle.goTo }; setToc(tocFromContent(SAMPLE_BOOK_CONTENT)); @@ -522,6 +532,7 @@ export function BooksApp(): ReactElement { bookId: book.id, initialPosition: book.reading.position, onPositionChange: persist, + onProgressChange: reportProgress, }); readerRef.current = { dispose: handle.dispose, goTo: handle.goTo }; setToc(tocFromContent(content)); @@ -543,6 +554,7 @@ export function BooksApp(): ReactElement { const handle = mountPdfReader(stage, controls, title, port, { initialPosition: book.reading.position, onPositionChange: persist, + onProgressChange: reportProgress, onOpenLink: (url) => { // Route page links through the `open` intent so web URLs land // in the browser app (the registered `https` opener) — Books @@ -880,7 +892,7 @@ export function BooksApp(): ReactElement { data-inspector-open={selectedBook && subject && showInspector ? "true" : "false"} > { - it("maps author / format label / pages / 0-100 progress", () => { + it("maps author / format label / pages / whole-percent progress fraction", () => { const values = bookToValues(fixtureBook()); expect(values[BOOK_PROP_KEY.author]).toBe("Cal Newport"); expect(values[BOOK_PROP_KEY.format]).toBe("PDF"); expect(values[BOOK_PROP_KEY.pages]).toBe(12); - expect(values[BOOK_PROP_KEY.progress]).toBe(42); + expect(values[BOOK_PROP_KEY.progress]).toBe(0.42); }); it("hides the page count for a book without a measured spine", () => { @@ -56,8 +57,23 @@ describe("applyBookPropertyValue", () => { }); describe("bookPropertyDefs", () => { + // The bare-number cell rendered "45" beside a footer reading "45% read". + it("formats progress as a percentage over a 0..1 range", () => { + const def = bookPropertyDefs(BookFormat.Pdf).find((d) => d.key === BOOK_PROP_KEY.progress); + expect(def?.format).toBe(PropertyFormat.Percent); + expect(def?.range).toEqual({ min: 0, max: 1 }); + }); + + // An EPUB's spine length is its chapter-file count; the reflow reader + // paginates to the viewport, so "Pages 1" sat beside "Page 2 of 8". + it("omits the page count for a reflowable EPUB", () => { + const keys = bookPropertyDefs(BookFormat.Epub).map((d) => d.key); + expect(keys).not.toContain(BOOK_PROP_KEY.pages); + expect(keys).toContain(BOOK_PROP_KEY.progress); + }); + it("declares every value-map key exactly once", () => { - const keys = bookPropertyDefs().map((d) => d.key); + const keys = bookPropertyDefs(BookFormat.Pdf).map((d) => d.key); expect([...keys].sort()).toEqual(Object.values(BOOK_PROP_KEY).sort()); const values = bookToValues(fixtureBook()); for (const key of keys) expect(key in values).toBe(true); diff --git a/apps/books/src/logic/book-properties.ts b/apps/books/src/logic/book-properties.ts index 84d447f8..fdb48c5f 100644 --- a/apps/books/src/logic/book-properties.ts +++ b/apps/books/src/logic/book-properties.ts @@ -7,7 +7,12 @@ * position-persistence path, never edited by hand). */ -import { type PropertyDef, PropertyView, ValueType } from "@brainstorm-os/sdk-types"; +import { + type PropertyDef, + PropertyFormat, + PropertyView, + ValueType, +} from "@brainstorm-os/sdk-types"; import { t } from "../i18n"; import { type Book, BookFormat } from "../types/book"; @@ -18,19 +23,32 @@ export const BOOK_PROP_KEY = { progress: "progress", } as const; -export function bookPropertyDefs(): readonly PropertyDef[] { - return [ +/** The rows for a book of `format`. A reflowable EPUB has no page count of + * its own — its `spineLength` counts chapter files and the reader paginates + * to the viewport — so only a fixed-layout PDF carries the Pages row. */ +export function bookPropertyDefs(format: BookFormat): readonly PropertyDef[] { + const defs: PropertyDef[] = [ { key: BOOK_PROP_KEY.author, name: t("prop.author"), icon: null, valueType: ValueType.Text }, { key: BOOK_PROP_KEY.format, name: t("prop.format"), icon: null, valueType: ValueType.Text }, - { key: BOOK_PROP_KEY.pages, name: t("prop.pages"), icon: null, valueType: ValueType.Number }, - { - key: BOOK_PROP_KEY.progress, - name: t("prop.progress"), + ]; + if (format === BookFormat.Pdf) { + defs.push({ + key: BOOK_PROP_KEY.pages, + name: t("prop.pages"), icon: null, valueType: ValueType.Number, - display: { view: PropertyView.ProgressBar }, - }, - ]; + }); + } + defs.push({ + key: BOOK_PROP_KEY.progress, + name: t("prop.progress"), + icon: null, + valueType: ValueType.Number, + format: PropertyFormat.Percent, + range: { min: 0, max: 1 }, + display: { view: PropertyView.ProgressBar }, + }); + return defs; } export const READONLY_BOOK_PROP_KEYS: ReadonlySet = new Set([ @@ -44,14 +62,14 @@ const FORMAT_LABEL_KEYS = { [BookFormat.Epub]: "prop.format.epub", } as const; -/** Cell values for a book. Progress is 0..100 (the ProgressBar cell's - * default range). */ +/** Cell values for a book. Progress is the 0..1 fraction snapped to a whole + * percent, so the cell reads the same number as the shelf row and footer. */ export function bookToValues(book: Book): Record { return { [BOOK_PROP_KEY.author]: book.author, [BOOK_PROP_KEY.format]: t(FORMAT_LABEL_KEYS[book.format]), [BOOK_PROP_KEY.pages]: book.spineLength > 0 ? book.spineLength : null, - [BOOK_PROP_KEY.progress]: Math.round(book.reading.progress * 100), + [BOOK_PROP_KEY.progress]: Math.round(book.reading.progress * 100) / 100, }; } diff --git a/apps/books/src/logic/library.test.ts b/apps/books/src/logic/library.test.ts index 59cfb67d..b6a5e5e4 100644 --- a/apps/books/src/logic/library.test.ts +++ b/apps/books/src/logic/library.test.ts @@ -11,6 +11,7 @@ import { filterLibrary, isInProgress, sortLibrary, + withLiveProgress, } from "./library"; const SECTION_LABELS: LibrarySectionLabels = { @@ -185,3 +186,22 @@ describe("booksFromEntities", () => { expect(books[0]?.format).toBe(BookFormat.Pdf); }); }); + +// The footer measures progress against the LIVE pagination; the stored +// fraction was measured against whatever viewport last turned a page. Shown +// side by side they disagreed ("45%" in the row, "23% read" in the footer). +describe("withLiveProgress", () => { + const shelf = [reading("a", "A", 0.45, 10), reading("b", "B", 0.8, 20)]; + + it("shows the open book at the reader's live progress", () => { + const out = withLiveProgress(shelf, { bookId: "a", progress: 0.23 }); + expect(out.map((b) => b.reading.progress)).toEqual([0.23, 0.8]); + expect(out[0]?.reading.lastReadAt).toBe(10); + }); + + it("returns the same list when nothing is live or nothing differs", () => { + expect(withLiveProgress(shelf, null)).toBe(shelf); + expect(withLiveProgress(shelf, { bookId: "a", progress: 0.45 })).toBe(shelf); + expect(withLiveProgress(shelf, { bookId: "zzz", progress: 0.1 })).toBe(shelf); + }); +}); diff --git a/apps/books/src/logic/library.ts b/apps/books/src/logic/library.ts index 03c539ad..43a51370 100644 --- a/apps/books/src/logic/library.ts +++ b/apps/books/src/logic/library.ts @@ -146,6 +146,27 @@ export function continueReading(books: readonly Book[]): Book[] { return sortLibrary(books.filter(isInProgress), LibrarySort.Recent); } +/** The open book's progress as its mounted reader currently measures it. */ +export type LiveProgress = { bookId: string; progress: number }; + +/** Overlay the open reader's live progress onto the shelf. Progress is + * measured to the END of the visible page, so it depends on the pagination + * (viewport × typography) — the stored fraction is only true for the layout + * that last turned a page. Every surface that shows the open book's progress + * reads through this, so they cannot disagree with the reader footer. */ +export function withLiveProgress( + books: readonly Book[], + live: LiveProgress | null, +): readonly Book[] { + if (live === null) return books; + const index = books.findIndex((b) => b.id === live.bookId); + const book = books[index]; + if (!book || book.reading.progress === live.progress) return books; + const next = [...books]; + next[index] = { ...book, reading: { ...book.reading, progress: live.progress } }; + return next; +} + /** The persistence seam for per-book reading state. The reader calls * `saveReadingPosition` whenever the parked locator changes; the preview * drop leaves it unset (in-memory only), and 9.21.2 wires it to a diff --git a/apps/books/src/render/pdf-reader.ts b/apps/books/src/render/pdf-reader.ts index 2bbb7a83..70961b2e 100644 --- a/apps/books/src/render/pdf-reader.ts +++ b/apps/books/src/render/pdf-reader.ts @@ -75,6 +75,9 @@ export type PdfReaderOptions = { * with the stable locator + progress (0..1), mirroring the reflow * reader's contract so the host persists both modes identically. */ onPositionChange?: (locator: Locator, progress: number) => void; + /** Fired with the progress the footer shows whenever it changes, + * including on mount — mirrors the reflow reader. Display-only. */ + onProgressChange?: (progress: number) => void; /** Fired when a link in the page is clicked — the host opens it (web links * go to the browser via the `open` intent). Absent = links render but do * nothing on click. */ @@ -215,6 +218,8 @@ export function mountPdfReader( return isSpread() ? 2 : 1; } + let reportedProgress: number | null = null; + function paint(): void { if (disposed) return; applyView(); @@ -231,9 +236,14 @@ export function mountPdfReader( : t("reader.pageStatus", { page: String(state.pageIndex + 1), total }); status.textContent = pageStatus; leftCanvas.setAttribute("aria-label", `${title} — ${pageStatus}`); + const fraction = pdfProgress(state); progress.textContent = t("reader.progress", { - percent: String(Math.round(pdfProgress(state) * 100)), + percent: String(Math.round(fraction * 100)), }); + if (fraction !== reportedProgress) { + reportedProgress = fraction; + options.onProgressChange?.(fraction); + } prev.disabled = !canGoPrevPdf(state); next.disabled = !canGoNextPdf(state); if (state.pageCount <= 0) return; diff --git a/apps/books/src/render/reader.test.ts b/apps/books/src/render/reader.test.ts index e71ec2f3..d6732d33 100644 --- a/apps/books/src/render/reader.test.ts +++ b/apps/books/src/render/reader.test.ts @@ -104,6 +104,35 @@ describe("reader render with a measured page box", () => { }); }); +describe("reader live progress", () => { + const footerPercent = (root: HTMLElement): number => + Number(/(\d+)%/.exec(root.querySelector(".books__progress")?.textContent ?? "")?.[1]); + + // The footer was the only surface that knew the live number; the shelf row + // and the inspector showed the stored one until the next page turn. + it("reports the footer's progress on mount, before any page turn", () => { + const { root, right } = scaffold(); + const seen: number[] = []; + handle = mountReader(root, right, SAMPLE_BOOK_CONTENT, { + onProgressChange: (p) => seen.push(p), + }); + expect(seen).toHaveLength(1); + expect(Math.round((seen[0] ?? -1) * 100)).toBe(footerPercent(root)); + }); + + it("reports again when a page turn moves it, and stays in step with the footer", () => { + const { root, right } = scaffold(); + const seen: number[] = []; + handle = mountReader(root, right, SAMPLE_BOOK_CONTENT, { + onProgressChange: (p) => seen.push(p), + }); + root.querySelector(".books__nav-btn:last-of-type")?.click(); + expect(seen).toHaveLength(2); + expect(seen[1]).toBeGreaterThan(seen[0] ?? 1); + expect(Math.round((seen[1] ?? -1) * 100)).toBe(footerPercent(root)); + }); +}); + describe("reader render", () => { it("paints the typography controls + the first page content + page status", () => { const { root, right } = scaffold(); diff --git a/apps/books/src/render/reader.ts b/apps/books/src/render/reader.ts index 2f4bf761..0a140218 100644 --- a/apps/books/src/render/reader.ts +++ b/apps/books/src/render/reader.ts @@ -126,6 +126,11 @@ export type ReaderOptions = { * it. The preview drop leaves it unset (in-memory only). Mirrors * `onTypographyChange`. */ onPositionChange?: (locator: Locator, progress: number) => void; + /** Fired with the progress the footer shows whenever it changes — + * including on mount and on a re-pagination that moves no locator + * (progress is measured to the end of the visible page, so a resize + * changes it). Display-only: nothing is persisted off this. */ + onProgressChange?: (progress: number) => void; /** The book whose highlights these are — the `Highlight/v1.bookId`. */ bookId?: string; /** The highlight persistence seam. 9.21.6 wires these to `Highlight/v1` @@ -231,6 +236,8 @@ export function mountReader( } } + let reportedProgress: number | null = null; + function paint(): void { applyTypographyVars(); const current = currentPage(state); @@ -255,9 +262,14 @@ export function mountReader( page: String(state.pageIndex + 1), total: String(total), }); + const fraction = readingProgress(state); progress.textContent = t("reader.progress", { - percent: String(Math.round(readingProgress(state) * 100)), + percent: String(Math.round(fraction * 100)), }); + if (fraction !== reportedProgress) { + reportedProgress = fraction; + options.onProgressChange?.(fraction); + } prev.disabled = !canGoPrev(state); next.disabled = !canGoNext(state); } diff --git a/apps/books/src/styles.css b/apps/books/src/styles.css index 3b19375e..97d22b42 100644 --- a/apps/books/src/styles.css +++ b/apps/books/src/styles.css @@ -84,14 +84,19 @@ body { * open each line ran on underneath it: every paragraph appeared hard-cut * mid-word at the panel edge, and the glass blur turned the occluded tail of * each line into a grey bar inside the panel that read as a stuck loading - * skeleton beside "Author — Empty". Reserving the panel's width on the stage - * fixes both: the column recentres in the width that is actually visible. */ + * skeleton beside "Author — Empty". Reserving the panel's width fixes both: + * the column recentres in the width that is actually visible. + * + * The reservation sits on the reader ROOT, not the stage: the footer is the + * stage's sibling, so a stage-only inset left it full-bleed and its + * next-page button (the trailing end of a space-between row) under the panel. + * Padding keeps the page background running beneath the glass. */ .books__layout { --books-inspector-width: 320px; } -.books__layout[data-inspector-open="true"] .books__stage { - padding-inline-end: calc(var(--space-4) + var(--books-inspector-width)); +.books__layout[data-inspector-open="true"] .books { + padding-inline-end: var(--books-inspector-width); } .books__content { @@ -387,6 +392,15 @@ body { padding-top: 0; } +/* The shared progress cell is `width: 100%` plus a -4px inline margin (a + * hover bleed for its editable face). As a growing flex item that makes it + * 8px wider than the value column, so its trailing number ran past the + * panel padding. Books' progress is read-only — no hover face to bleed. */ +:root .books__props .bs-cell-progress { + margin-inline: 0; + padding-inline: 0; +} + /* Separate the table of contents from the property rows with the same 1px divider the meta footer uses, so CONTENTS reads as its own section. */ .books__toc { @@ -451,6 +465,9 @@ body { flex-direction: column; flex: 1 1 auto; min-height: 0; + /* Matches the inspector's own slide transition so the column glides to + * its new centre instead of jumping. */ + transition: padding-inline-end var(--motion-duration-normal, 180ms) ease; background: var(--reader-bg, var(--bg)); color: var(--reader-text, var(--text)); --reader-font-family: var(--text-family-body, var(--text-family-ui)); @@ -547,9 +564,6 @@ body { display: flex; justify-content: center; padding: var(--space-6) var(--space-4); - /* Matches the inspector's own slide transition so the column glides to - * its new centre instead of jumping. */ - transition: padding-inline-end var(--motion-duration-normal, 180ms) ease; } .books__page { diff --git a/apps/books/src/ui/inspector.tsx b/apps/books/src/ui/inspector.tsx index 1642412c..2b5434ed 100644 --- a/apps/books/src/ui/inspector.tsx +++ b/apps/books/src/ui/inspector.tsx @@ -56,7 +56,7 @@ export function BookInspector({ onClose, }: BookInspectorProps): ReactElement { const values = bookToValues(book); - const rows: PropertiesPanelRow[] = bookPropertyDefs().map((def) => { + const rows: PropertiesPanelRow[] = bookPropertyDefs(book.format).map((def) => { const locked = readOnly || READONLY_BOOK_PROP_KEYS.has(def.key); const row: PropertiesPanelRow = { def, value: values[def.key], readOnly: locked }; if (!locked) { diff --git a/apps/browser/src/app-features.test.tsx b/apps/browser/src/app-features.test.tsx index 853a7243..0a53028c 100644 --- a/apps/browser/src/app-features.test.tsx +++ b/apps/browser/src/app-features.test.tsx @@ -22,7 +22,7 @@ import { BrowserApp } from "./app"; import { type HistoryVisit, historyRecordToProperties } from "./logic/history"; import { PERSIST_DEBOUNCE_MS, sessionRecordToProperties } from "./logic/persistence"; import type { EntityRecord } from "./runtime"; -import type { BrowsingSessionRecord } from "./types/browsing-session"; +import { type BrowsingSessionRecord, NEW_TAB_URL } from "./types/browsing-session"; const popupStateListeners = vi.hoisted(() => [] as Array<(open: boolean) => void>); vi.mock("@brainstorm-os/sdk/menus", async (importOriginal) => ({ @@ -326,6 +326,38 @@ describe("BrowsingSession/v1 restore", () => { expect(webView.bounds.some((b) => b.tabId === restoredOpen?.tabId)).toBe(true); }); + it("shows the start page when the restored ACTIVE tab is a blank new tab", async () => { + const stored = storedRecord(); + const record: BrowsingSessionRecord = { + ...stored, + tabs: [ + ...stored.tabs, + { + id: "old-3", + url: NEW_TAB_URL, + title: "", + faviconUrl: null, + pinned: false, + history: [], + historyIndex: -1, + }, + ], + activeTabId: "old-3", + }; + install([{ ...sessionRow(), properties: sessionRecordToProperties(record) }]); + await render(); + await act(async () => {}); + + expect(tabTitles()).toEqual(["Tab A", "Tab B", "New tab"]); + expect(container.querySelector(".browser-start")).not.toBeNull(); + const restoredOpen = webView.opened.at(-1); + expect(restoredOpen?.url).toBe(NEW_TAB_URL); + // The native view composites above the chrome, so it must be parked or + // it covers the start page. + const lastBounds = webView.bounds.filter((b) => b.tabId === restoredOpen?.tabId).at(-1); + expect(lastBounds?.rect).toEqual({ x: 0, y: 0, width: 0, height: 0 }); + }); + it("keeps a fresh session when nothing was stored", async () => { await render(); await act(async () => {}); diff --git a/apps/browser/src/logic/persistence.test.ts b/apps/browser/src/logic/persistence.test.ts index 6aa0dd7d4a3dffc74dd6ab5270db83b0f9b5e06a..a29e4cab928cb943a72095d30acc3b9d8bcd000d 100644 GIT binary patch delta 382 zcmYjN%}T>S5Js@n#*+vYdm4IbAgNS~V63PC4+T+ZBO+4jrdg83x*K+W_N2yW4B&Og>F;k3(eX0}_yhleu29{AdJGmTs z$L{c=cdF4Z)gykNlUPLpE#u|pbNO6~2@hXfrIvjd<#w9;D^1R0Ol#3q3&{xClzPPP{s(KZ qsee$(w#v25*5I(IdzeiWE^QRb%sZvWwbjBRd#mmh@%+2`zVrjfUVPL5 delta 59 zcmaEDvfprm8CywZL8?OaXfRXq_lat#1TdSctR8L diff --git a/apps/browser/src/logic/persistence.ts b/apps/browser/src/logic/persistence.ts index fb48a250..eda4efaf 100644 --- a/apps/browser/src/logic/persistence.ts +++ b/apps/browser/src/logic/persistence.ts @@ -16,7 +16,7 @@ import { sanitizeInlineText } from "@brainstorm-os/sdk/sanitize-text"; import type { BrowsingSessionRecord, TabSnapshot } from "../types/browsing-session"; -import { MAX_RECENTLY_CLOSED } from "../types/browsing-session"; +import { MAX_RECENTLY_CLOSED, NEW_TAB_URL } from "../types/browsing-session"; import { isWebUrl } from "./external-open"; /** Per-tab history bound in the persisted record — a long-lived tab's @@ -86,7 +86,11 @@ function parseTab(input: unknown): TabSnapshot | null { : currentEntry !== undefined && isWebUrl(currentEntry) ? Math.max(history.indexOf(currentEntry), 0) : history.length - 1; - const url = isWebUrl(raw.url) ? raw.url : (history[historyIndex] ?? ""); + // A tab with nothing web to restore IS a blank new tab: the chrome keys the + // start page (and parking the native view under it) on `NEW_TAB_URL`, so an + // empty string here restored a tab that showed neither a page nor the start + // page. + const url = isWebUrl(raw.url) ? raw.url : (history[historyIndex] ?? NEW_TAB_URL); return { id: raw.id, url, diff --git a/apps/browser/src/styles.css b/apps/browser/src/styles.css index cbe101a6..ba3d16aa 100644 --- a/apps/browser/src/styles.css +++ b/apps/browser/src/styles.css @@ -63,7 +63,10 @@ body { align-items: center; gap: var(--space-1, var(--space-1)); height: 30px; - min-width: 96px; + /* Floor sized for the chrome (12 + 14 favicon + 8 + 4 + 18 close + 4) plus a + * short title: at 96px only ~36px was left and "New tab" ellipsised to + * "Ne…" beside 190px neighbours. The strip scrolls, so a floor is safe. */ + min-width: 128px; max-width: 220px; padding: 0 var(--space-1, var(--space-1)) 0 var(--space-3, var(--space-3)); border-radius: var(--radius-md); diff --git a/apps/chat/src/app.tsx b/apps/chat/src/app.tsx index 4fd0cbf2..711d0e60 100644 --- a/apps/chat/src/app.tsx +++ b/apps/chat/src/app.tsx @@ -865,7 +865,9 @@ export function ChatApp(): ReactElement { aria-haspopup="menu" onClick={openMore} > - + + +
    @@ -1280,7 +1282,9 @@ function MessageView({ openMessageMenu({ x: r.right, y: r.bottom + 4 }, m, el); }} > - + + + ) : null} {m.richBody ? ( diff --git a/apps/chat/src/format.test.ts b/apps/chat/src/format.test.ts index a4f0ead9..422bee9b 100644 --- a/apps/chat/src/format.test.ts +++ b/apps/chat/src/format.test.ts @@ -1,8 +1,9 @@ -import { describe, expect, it } from "vitest"; +import { afterAll, beforeAll, describe, expect, it } from "vitest"; import { formatDay, formatStamp, formatTime } from "./format"; +import { dayKey } from "./logic/chat"; describe("chat time formatting (Chats-3)", () => { - const now = new Date("2026-06-20T15:00:00.000Z"); + const now = new Date(2026, 5, 20, 15, 0, 0); it("labels today / yesterday by default", () => { expect(formatDay("2026-06-20", false, now)).toBe("Today"); @@ -27,3 +28,37 @@ describe("chat time formatting (Chats-3)", () => { expect(formatDay("nope", true)).toBe("nope"); }); }); + +describe("day divider agrees with the displayed time", () => { + const savedTz = process.env.TZ; + afterAll(() => { + if (savedTz === undefined) Reflect.deleteProperty(process.env, "TZ"); + else process.env.TZ = savedTz; + }); + + describe.each(["Europe/Berlin", "America/Los_Angeles", "Pacific/Auckland"])("in %s", (tz) => { + beforeAll(() => { + process.env.TZ = tz; + }); + + it("labels the divider with the same local date the timestamp shows", () => { + for (const iso of ["2026-08-01T22:53:00.000Z", "2026-08-02T06:30:00.000Z"]) { + const local = new Date(iso); + const expected = local.toLocaleDateString("en", { + weekday: "long", + year: "numeric", + month: "short", + day: "numeric", + }); + expect(formatDay(dayKey(iso), true, local)).toBe(expected); + } + }); + + it("calls a message sent just after local midnight Today, not Yesterday", () => { + const iso = "2026-08-01T22:53:00.000Z"; + const now = new Date(Date.parse(iso) + 60_000); + expect(formatDay(dayKey(iso), false, now)).toBe("Today"); + expect(formatDay(dayKey("2026-07-31T22:53:00.000Z"), false, now)).toBe("Yesterday"); + }); + }); +}); diff --git a/apps/chat/src/format.ts b/apps/chat/src/format.ts index 22d16dea..c6b4e791 100644 --- a/apps/chat/src/format.ts +++ b/apps/chat/src/format.ts @@ -32,10 +32,11 @@ export function formatStamp(iso: string): string { /** A day-divider label: Today / Yesterday / a localized date. With `absolute` * (Chats-3) the relative labels give way to the full date. */ export function formatDay(dayKey: string, absolute = false, now: Date = new Date()): string { - const ms = Date.parse(dayKey); - if (!Number.isFinite(ms)) return dayKey; - const that = new Date(ms); - const startOf = (d: Date) => Date.UTC(d.getUTCFullYear(), d.getUTCMonth(), d.getUTCDate()); + const parts = /^(\d{4})-(\d{2})-(\d{2})$/.exec(dayKey); + if (!parts) return dayKey; + // A bare `YYYY-MM-DD` parses as UTC midnight; the key is a LOCAL day. + const that = new Date(Number(parts[1]), Number(parts[2]) - 1, Number(parts[3])); + const startOf = (d: Date) => Date.UTC(d.getFullYear(), d.getMonth(), d.getDate()); const diffDays = Math.round((startOf(now) - startOf(that)) / 86_400_000); if (!absolute) { if (diffDays === 0) return t("day.today"); diff --git a/apps/chat/src/logic/chat.test.ts b/apps/chat/src/logic/chat.test.ts index 62fb9e3e..c623d301 100644 --- a/apps/chat/src/logic/chat.test.ts +++ b/apps/chat/src/logic/chat.test.ts @@ -6,7 +6,7 @@ import { RosterRole, SenderKind, } from "@brainstorm-os/sdk-types"; -import { describe, expect, it } from "vitest"; +import { afterAll, beforeAll, describe, expect, it } from "vitest"; import { AUTHOR_COLORS, CHANNEL_TYPE, @@ -187,14 +187,14 @@ describe("groupMessages", () => { ref: "p1", name: "Mira", body: "a", - createdAt: "2026-06-20T23:59:00Z", + createdAt: new Date(2026, 5, 20, 23, 59, 0).toISOString(), seq: 0, }), message("m2", "c", { ref: "p1", name: "Mira", body: "b", - createdAt: "2026-06-21T00:00:30Z", + createdAt: new Date(2026, 5, 21, 0, 0, 30).toISOString(), seq: 1, }), ], @@ -207,6 +207,54 @@ describe("groupMessages", () => { }); }); +describe("day grouping follows the local calendar day", () => { + const color = (ref: string) => `color-${ref}`; + const savedTz = process.env.TZ; + beforeAll(() => { + process.env.TZ = "Europe/Berlin"; + }); + afterAll(() => { + if (savedTz === undefined) Reflect.deleteProperty(process.env, "TZ"); + else process.env.TZ = savedTz; + }); + + it("keys a just-after-local-midnight message to the new day", () => { + expect(new Date("2026-08-01T22:53:00.000Z").getHours()).toBe(0); + expect(dayKey("2026-08-01T20:11:00.000Z")).toBe("2026-08-01"); + expect(dayKey("2026-08-01T22:53:00.000Z")).toBe("2026-08-02"); + }); + + it("splits a group at local midnight even inside one UTC day", () => { + const rows = channelMessages( + [ + message("m1", "c", { + ref: "p1", + name: "Mira", + body: "late", + createdAt: "2026-08-01T21:58:00.000Z", + seq: 0, + }), + message("m2", "c", { + ref: "p1", + name: "Mira", + body: "after midnight", + createdAt: "2026-08-01T22:01:00.000Z", + seq: 1, + }), + ], + "c", + ); + expect(groupMessages(rows, color).map((g) => g.dayKey)).toEqual(["2026-08-01", "2026-08-02"]); + }); + + it("keeps one local day together across a UTC date change", () => { + process.env.TZ = "America/Los_Angeles"; + expect(dayKey("2026-08-01T23:30:00.000Z")).toBe("2026-08-01"); + expect(dayKey("2026-08-02T00:30:00.000Z")).toBe("2026-08-01"); + process.env.TZ = "Europe/Berlin"; + }); +}); + describe("membersFromMessages", () => { it("lists distinct authors in first-seen order", () => { const rows = channelMessages( @@ -316,8 +364,8 @@ describe("initials", () => { }); describe("dayKey", () => { - it("returns the UTC date portion of an ISO stamp", () => { - expect(dayKey("2026-06-20T10:00:00.000Z")).toBe("2026-06-20"); + it("returns the local calendar date of an ISO stamp", () => { + expect(dayKey(new Date(2026, 5, 20, 10, 0, 0).toISOString())).toBe("2026-06-20"); expect(dayKey("nope")).toBe("nope"); }); }); diff --git a/apps/chat/src/logic/chat.ts b/apps/chat/src/logic/chat.ts index a358f495..dd40ae25 100644 --- a/apps/chat/src/logic/chat.ts +++ b/apps/chat/src/logic/chat.ts @@ -240,10 +240,16 @@ export function nextSeq(messages: readonly ChatMessage[]): number { // ─────────────────────────────── grouping ─────────────────────────────── -/** `YYYY-MM-DD` (UTC) of an ISO timestamp; "" when unparseable. */ +/** `YYYY-MM-DD` of an ISO timestamp in the viewer's LOCAL calendar — the same + * zone the message time renders in, so a divider can never disagree with the + * stamps beneath it. An unparseable stamp is returned as-is. */ export function dayKey(iso: string): string { - const idx = iso.indexOf("T"); - return idx > 0 ? iso.slice(0, idx) : iso; + const ms = Date.parse(iso); + if (!Number.isFinite(ms)) return iso; + const d = new Date(ms); + const month = String(d.getMonth() + 1).padStart(2, "0"); + const day = String(d.getDate()).padStart(2, "0"); + return `${d.getFullYear()}-${month}-${day}`; } /** Messages within this many ms by the same author collapse into one group. */ diff --git a/apps/chat/src/styles.css b/apps/chat/src/styles.css index e0ed59bd..4637f4a1 100644 --- a/apps/chat/src/styles.css +++ b/apps/chat/src/styles.css @@ -834,7 +834,9 @@ body { height: 24px; opacity: 0; background: var(--bg-elev); - border: 1px solid var(--border-subtle); + border: 1px solid var(--border-strong); + color: var(--text); + gap: var(--space-0_5); } .chat__line-wrap:hover .chat__line-more, diff --git a/apps/database/src/styles.css b/apps/database/src/styles.css index 153b75ed..29278f15 100644 --- a/apps/database/src/styles.css +++ b/apps/database/src/styles.css @@ -1054,6 +1054,10 @@ body.is-resizing .db-inspector__resize { align-items: baseline; gap: var(--space-2); max-width: 100%; + /* The hover face bleeds into the cell padding so the label itself ends on + * the cell's content edge — the edge the header label and the numeric + * values above it share. */ + margin-inline: calc(-1 * var(--space-1)); padding: 0 var(--space-1); border: 0; background: transparent; @@ -1170,6 +1174,17 @@ body.is-resizing .db-inspector__resize { text-align: end; } +/* The shared faces bleed 6px (pill) / 4px (plain) past the cell padding and + * inset their text 2px, which lands a right-aligned value 4px / 2px outside + * the cell's content edge. Trailing bleed = the face's own inset, so the + * numerals end exactly on the edge the header label and footer use. */ +.dbv-grid__cell--numeric > .bs-cell-pill, +.dbv-grid__cell--numeric > .bs-cell-plain, +.dbv-grid__cell--numeric .bs-cell-plain-input, +.dbv-grid__cell--numeric .bs-cell-input { + margin-inline-end: calc(-1 * var(--space-0_5)); +} + .dbv-grid__cell--numeric .bs-cell-plain-input, .dbv-grid__cell--numeric .bs-cell-input { text-align: end; @@ -1382,7 +1397,9 @@ body.is-resizing .db-inspector__resize { background: var(--bg); } -.dbv-grid[data-pinned="true"] .dbv-grid__row:not(.dbv-grid__row--head):hover .dbv-grid__cell[data-col="title"] { +.dbv-grid[data-pinned="true"] + .dbv-grid__row:not(.dbv-grid__row--head):not(.dbv-grid__row--foot):hover + .dbv-grid__cell[data-col="title"] { background: linear-gradient(var(--hover), var(--hover)), var(--bg); } @@ -1395,6 +1412,12 @@ body.is-resizing .db-inspector__resize { background: var(--bg-elev); } +/* The footer band is `--bg-elev` like the header; left on the pinned cell's + * page colour, the title cell read as a grey block set 12px into a white band. */ +.dbv-grid[data-pinned="true"] .dbv-grid__row--foot .dbv-grid__cell[data-col="title"] { + background: var(--bg-elev); +} + /* ── List view ────────────────────────────────────────────────────────── */ .dbv-list { diff --git a/apps/files/src/app.tsx b/apps/files/src/app.tsx index bf82fc0c..9da96926 100644 --- a/apps/files/src/app.tsx +++ b/apps/files/src/app.tsx @@ -59,7 +59,7 @@ import { hasActiveFilters } from "./logic/filters"; import { buildPreviewSiblings } from "./logic/preview-siblings"; import { ScopeFlipAction, SearchScope, flipScope } from "./logic/search"; import type { SmartFolder } from "./logic/smart-folders"; -import { SortDirection, SortKey } from "./logic/sort"; +import { SortKey } from "./logic/sort"; import { ActionId, chordFor } from "./shortcuts"; import { type FilesStore, useFilesStore } from "./store/use-files-store"; import { type Entity, FILE_TYPE, FOLDER_TYPE, readName } from "./types/entity"; @@ -765,11 +765,7 @@ export function FilesApp() { {t("brainstorm.files.sort.label")}: {sortLabel(store.sortKey)} - + className="view-switch" diff --git a/apps/files/src/styles-toolbar-row.test.ts b/apps/files/src/styles-toolbar-row.test.ts index 01a312b9..78ab70b1 100644 --- a/apps/files/src/styles-toolbar-row.test.ts +++ b/apps/files/src/styles-toolbar-row.test.ts @@ -116,3 +116,52 @@ describe("files toolbar is one control row", () => { ).not.toMatch(/bs-(select|input|btn)--sm/); }); }); + +describe("files toolbar end group is one face", () => { + const css = readFileSync(CSS_PATH, "utf8"); + const tsx = readFileSync(APP_PATH, "utf8"); + const filterTsx = readFileSync(join(SRC_DIR, "ui", "filter-bar.tsx"), "utf8"); + + it("the Filter toggle wears the same field face as Sort-by and the view picker", () => { + const className = /className="([^"]*\btoolbar__filter\b[^"]*)"/.exec(filterTsx)?.[1] ?? ""; + expect(className.split(" "), "Filter must wear the shared .bs-select face").toContain( + "bs-select", + ); + expect( + className, + "a transparent .bs-btn--secondary beside two white fields reads as a disabled control", + ).not.toMatch(/bs-btn/); + }); + + it("the Sort-by trigger shows the dropdown caret, never the sort direction", () => { + const trigger = /data-testid="toolbar-sort"[\s\S]*?<\/button>/.exec(tsx)?.[0]; + expect(trigger, "could not locate the sort trigger JSX").toBeDefined(); + expect(trigger).toContain("IconName.CaretDown"); + expect(trigger, "an up caret on a closed menu reads as an open one").not.toContain("CaretUp"); + }); + + it("the view-options menu is not held to the small panel's 480px cap and shows scroll shadows", () => { + const panel = onlyBody(css, ".bs-popover__panel:has(.sort-menu__items)"); + expect(panel).toMatch(/max-height:\s*80vh/); + const body = onlyBody(css, ".bs-popover__body:has(> .sort-menu__items)"); + expect(body, "the covers must scroll with the content").toMatch(/\blocal\b/); + expect(body, "the edge shadows must stay pinned to the scrollport").toMatch(/\bscroll\b/); + }); +}); + +describe("files bulk-action bar does not sit on the content", () => { + const css = readFileSync(CSS_PATH, "utf8"); + + it("the bar is an opaque surface", () => { + const body = onlyBody(css, ".files-bulkbar"); + expect(body).toMatch(/background:\s*var\(--color-background-elevated\)/); + expect(body, "glass lets the tiles underneath read through the labels").not.toMatch( + /glass-background|backdrop-filter/, + ); + }); + + it("the list reserves the bar's footprint while the bar is shown", () => { + const body = onlyBody(css, ".content-list:has(~ .files-bulkbar)"); + expect(body).toMatch(/padding-bottom:/); + }); +}); diff --git a/apps/files/src/styles.css b/apps/files/src/styles.css index 9d753059..b18ef247 100644 --- a/apps/files/src/styles.css +++ b/apps/files/src/styles.css @@ -499,9 +499,17 @@ body[data-nav-open="false"] .sidebar:not([data-bs-autohide]) { padding: var(--space-2) var(--space-2) var(--space-2) var(--space-4); border: var(--border-width) solid var(--color-glass-border, var(--border)); border-radius: var(--radius-full); - background: var(--color-glass-background-strong); + /* Opaque, like the shared popover panel: the bar floats over tiles with no + * scrim, and the glass fill let their titles read through its labels. */ + background: var(--color-background-elevated); box-shadow: var(--shadow-xl); - backdrop-filter: blur(12px); +} + +/* The bar overlays the list's last visible row; while it is up the scroller + * gains the bar's footprint (bottom offset + 46px pill + breathing room) so + * the final row can scroll clear of it. */ +.content-list:has(~ .files-bulkbar) { + padding-bottom: calc(var(--space-4) * 2 + 46px); } .files-bulkbar__count { @@ -615,9 +623,16 @@ body[data-nav-open="false"] .sidebar:not([data-bs-autohide]) { color: var(--accent); } +/* The Filter toggle wears the `.bs-select` field face, like Sort-by and the + * view picker beside it: as a transparent `.bs-btn--secondary` with dim ink it + * read as a disabled control next to two white fields. Pressed (filter row + * open) borrows the face's own open-state edge. */ .toolbar__filter { flex-shrink: 0; - gap: var(--space-1); +} + +.toolbar__filter[aria-pressed="true"] { + border-color: var(--color-accent-default); } /* Property-filter row + the Duplicates summary (9.8.16) — a second band @@ -1678,8 +1693,9 @@ body:not([data-inspector="open"]) .inspector { * picker are the SAME `.bs-select` face (one boxed trigger face per * toolbar; chrome from `@brainstorm-os/sdk/select-menu.css`). The sort button * keeps its own popover (direction + group-by + layout controls, not a plain - * option list) — only its face is the shared one; its caret shows the current - * sort direction instead of the dropdown caret. */ + * option list) — only its face is the shared one, dropdown caret included + * (a direction caret pointed UP on a closed menu beside the view picker's + * down caret; the direction is drawn on the menu's own direction row). */ .toolbar__sort-label { max-width: 220px; @@ -1691,6 +1707,28 @@ body:not([data-inspector="open"]) .inspector { gap: var(--space-0_5); } +/* The view-options menu is longer than the shared small panel's 480px cap + * (sort + direction + group + tile size + columns), so it always scrolls — + * and macOS overlay scrollbars hide the only cue, leaving a row sliced by the + * panel edge. Let the panel use the height the window has, and paint scroll + * shadows: the opaque covers ride the content (`local`) and slide off the + * edge shadows (`scroll`) whenever more rows lie beyond that edge. */ +.bs-popover__panel:has(.sort-menu__items) { + max-height: 80vh; +} + +.bs-popover__body:has(> .sort-menu__items) { + background: + linear-gradient(var(--color-background-elevated) 40%, transparent) top / 100% var(--space-6) + no-repeat local, + linear-gradient(transparent, var(--color-background-elevated) 60%) bottom / 100% var(--space-6) + no-repeat local, + linear-gradient(var(--color-border-strong), transparent) top / 100% var(--space-2) no-repeat + scroll, + linear-gradient(transparent, var(--color-border-strong)) bottom / 100% var(--space-2) no-repeat + scroll; +} + .sort-menu__item { display: flex; align-items: center; diff --git a/apps/files/src/ui/dialogs.tsx b/apps/files/src/ui/dialogs.tsx index 3cb317dc..e8a658da 100644 --- a/apps/files/src/ui/dialogs.tsx +++ b/apps/files/src/ui/dialogs.tsx @@ -170,8 +170,8 @@ export type SortMenuProps = { onClose: () => void; }; -/** The direction row's state glyph: the same caret the toolbar trigger shows, - * so "which way is it sorting" reads identically in both places. */ +/** The direction row's state glyph — the one place the sort direction is + * drawn. The toolbar trigger's caret is the dropdown affordance, not this. */ export function directionIcon(direction: SortDirection): IconName { return direction === SortDirection.Asc ? IconName.CaretUp : IconName.CaretDown; } diff --git a/apps/files/src/ui/filter-bar.tsx b/apps/files/src/ui/filter-bar.tsx index 35669ffa..18bec413 100644 --- a/apps/files/src/ui/filter-bar.tsx +++ b/apps/files/src/ui/filter-bar.tsx @@ -61,7 +61,7 @@ export function FilterToggleButton({ return (
)} + {hostMode === LanHostMode.WhenVaultOpen && peering && !peering.listenerUrl && ( +

+ {t("shell.settings.sync.lan.noAddress")} +

+ )} + {peering && ( <>
From 23f8877ab7f48f1e1cc1ac1054250cb00e82871c Mon Sep 17 00:00:00 2001 From: brainstorm-os Date: Thu, 19 Nov 2026 15:34:14 +0100 Subject: [PATCH 11/42] =?UTF-8?q?fix(sync):=20P2P-1=20=E2=80=94=20land=20t?= =?UTF-8?q?he=20five=20discovery=20findings=20the=20gate=20ledger=20record?= =?UTF-8?q?s=20as=20fixed,=20and=20make=20tag=20rotation=20reach=20the=20w?= =?UTF-8?q?ire?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The security-review and pentest runs of 2026-07-31 failed on five findings and passed on a fix commit that never reached the merged tree: the squash of #377 carried the feature without it, and the probe file the ledger cites did not exist. All five were live. Re-implemented here, with the probes: - The instance name is inside the tag's MAC, in a length-prefixed pre-image. It is the candidate table's de-dup key and is rendered in Settings, so a captured advert could be replayed under any name. - The candidate and cooldown tables are capped, expired records retire on insert, and the live target is pinned by instance. - The advert no longer publishes os.hostname() or an address record for every interface: the SRV host is the per-boot instance name, AAAA is off, and A records are held to the advertised addresses. - A record arriving after the vault closed is refused instead of verified against the secret captured when browsing started. - Loopback is refused in an advert even under a valid tag, and a paired address is re-validated at the coordinator rather than trusted. Two defects found by reading bonjour-service 1.4.4 rather than by a probe: it has no in-place TXT update, so the rotated tag was never announced and a host stopped being discoverable about ten minutes after it started listening; and its find() callback fires on first sighting only. Rotation now withdraws and re-announces under the same name, and the browser also takes txt-update. Wire change: the tag namespace and TXT schema are v2. A 0.15 device and this build do not discover each other; pairing addresses, a typed address and the relay are unaffected. Adds an in-process advertise -> browse -> gated dial -> admission test over a real loopback socket with an in-memory multicast bus. Real multicast is not exercised by anything here. Co-Authored-By: Claude Fable 5.2 --- .../src/main/sync/lan-dial-coordinator.ts | 77 +++- .../main/sync/lan-discover-dial-e2e.test.ts | 342 ++++++++++++++++ .../main/sync/lan-discovery-attacks.test.ts | 381 ++++++++++++++++++ .../main/sync/lan-discovery-service.test.ts | 155 +++++-- .../src/main/sync/lan-discovery-service.ts | 48 ++- .../src/main/sync/lan-discovery-tag.test.ts | 99 ++++- .../shell/src/main/sync/lan-discovery-tag.ts | 38 +- .../shell/src/main/sync/lan-discovery.test.ts | 97 ++++- packages/shell/src/main/sync/lan-discovery.ts | 64 ++- .../src/main/sync/lan-mdns-backend.test.ts | 201 +++++++++ .../shell/src/main/sync/lan-mdns-backend.ts | 120 ++++-- 11 files changed, 1495 insertions(+), 127 deletions(-) create mode 100644 packages/shell/src/main/sync/lan-discover-dial-e2e.test.ts create mode 100644 packages/shell/src/main/sync/lan-discovery-attacks.test.ts create mode 100644 packages/shell/src/main/sync/lan-mdns-backend.test.ts diff --git a/packages/shell/src/main/sync/lan-dial-coordinator.ts b/packages/shell/src/main/sync/lan-dial-coordinator.ts index bec01c21..78a574a6 100644 --- a/packages/shell/src/main/sync/lan-dial-coordinator.ts +++ b/packages/shell/src/main/sync/lan-dial-coordinator.ts @@ -99,6 +99,15 @@ export const LAN_CONNECT_TIMEOUT_MS = 3_000; export const LAN_HEARTBEAT_INTERVAL_MS = 5_000; export const LAN_HEARTBEAT_TIMEOUT_MS = 5_000; +/** Ceiling on remembered peers. One identity's devices on one LAN is a handful; + * the table is reachable from the network, so it is bounded regardless of how + * well the advert gate holds. */ +export const LAN_MAX_CANDIDATES = 16; + +/** Ceiling on remembered failures. Each candidate carries at most + * `LAN_MAX_ADVERT_ADDRESSES` urls, plus the typed and self-host addresses. */ +export const LAN_MAX_COOLDOWNS = 128; + export type LanDialCoordinatorOptions = { /** Fired whenever the effective target changes (including to `null`). The * wiring turns this into `ActiveRelayOrchestrator.reconfigure()`. */ @@ -237,11 +246,12 @@ export class LanDialCoordinator { */ offerCandidate(candidate: LanPeerCandidate, source = LanDialSource.Discovery): void { if (candidate.urls.length === 0) return; - this.#candidates.set(candidate.instance, { - candidate, - seenAtMs: this.#now(), - source, - }); + const now = this.#now(); + // Delete first so a re-sighting moves to the back of insertion order, + // which is what makes "oldest" below mean least recently SEEN. + this.#candidates.delete(candidate.instance); + this.#candidates.set(candidate.instance, { candidate, seenAtMs: now, source }); + this.#retireCandidates(now); this.#reselect(); } @@ -257,16 +267,23 @@ export class LanDialCoordinator { * Stored under a stable synthetic instance so a re-pair replaces it rather * than accumulating. */ - offerPairedPeer(url: string): void { + offerPairedPeer(url: string): boolean { + // Re-validated here rather than trusted from the caller: this is the one + // entry point that takes a raw string, and a payload address that is + // loopback, routable or unparseable must be a refusal, not a throw. + const normalised = parseLanPeerAddress(url, { allowLoopback: false }); + if (!normalised) return false; + const parsed = new URL(normalised); this.offerCandidate( { - instance: `paired:${url}`, - addresses: [new URL(url).hostname], - port: Number(new URL(url).port), - urls: [url], + instance: `paired:${normalised}`, + addresses: [parsed.hostname], + port: Number(parsed.port), + urls: [normalised], }, LanDialSource.Pairing, ); + return true; } /** The dial reached an admitted, live connection. */ @@ -281,7 +298,10 @@ export class LanDialCoordinator { * than closing is the transport's job; picking a different path is ours. */ noteDialUnusable(url: string): void { - this.#cooldown.set(url, this.#now() + this.#cooldownMs); + const now = this.#now(); + this.#cooldown.delete(url); + this.#cooldown.set(url, now + this.#cooldownMs); + this.#retireCooldowns(now); this.#reselect(); } @@ -293,6 +313,41 @@ export class LanDialCoordinator { this.#reselect(); } + /** + * Expired records go on every insert, not only when someone happens to read + * them, and the table is capped. The live target's record is pinned by + * INSTANCE: pinning by url would protect every record of a flood that all + * names one address. A pairing-learned peer outlives discovery noise. + */ + #retireCandidates(now: number): void { + for (const [instance, entry] of this.#candidates) { + if (now - entry.seenAtMs > this.#candidateTtlMs) this.#candidates.delete(instance); + } + const pinned = this.#target?.instance ?? null; + const evictOldest = (source: LanDialSource | null): boolean => { + for (const [instance, entry] of this.#candidates) { + if (instance === pinned) continue; + if (source !== null && entry.source !== source) continue; + this.#candidates.delete(instance); + return true; + } + return false; + }; + while (this.#candidates.size > LAN_MAX_CANDIDATES) { + if (!evictOldest(LanDialSource.Discovery) && !evictOldest(null)) break; + } + } + + #retireCooldowns(now: number): void { + for (const [url, until] of this.#cooldown) { + if (until <= now) this.#cooldown.delete(url); + } + for (const url of this.#cooldown.keys()) { + if (this.#cooldown.size <= LAN_MAX_COOLDOWNS) break; + this.#cooldown.delete(url); + } + } + #reselect(): void { const next = this.#select(); const changed = diff --git a/packages/shell/src/main/sync/lan-discover-dial-e2e.test.ts b/packages/shell/src/main/sync/lan-discover-dial-e2e.test.ts new file mode 100644 index 00000000..f1eccec8 --- /dev/null +++ b/packages/shell/src/main/sync/lan-discover-dial-e2e.test.ts @@ -0,0 +1,342 @@ +/** + * P2P-1 — advertise → browse → gated dial → admitted connection, in one process. + * + * Every layer is the production one except two, and both substitutions are + * about the NETWORK, not about the code under test: + * + * - **The multicast group is an in-memory bus.** `LanDiscoveryService` runs + * unchanged against the `LanMdnsBackend` seam; nothing binds UDP 5353. So + * this proves the rules (what is advertised, what survives a browse, what + * reaches the dialer) and proves NOTHING about `bonjour-service` on a real + * interface. That is `lan-mdns-backend.test.ts` for the adapter's shape and + * a real two-device run for the wire. + * - **The private address is routed to loopback.** The listener binds + * 127.0.0.1 so the test is hermetic, while the advert names a private + * address, because discovery refuses loopback even under a valid tag. The + * `viaLoopback` socket stands in for the LAN delivering that address. + * + * The listener, the channel-bound admission handshake, the roster check, the + * dial coordinator and `WebSocketRelayPort` are all real, over a real socket. + */ + +import { createServer } from "node:net"; +import { afterEach, describe, expect, it } from "vitest"; +import { generateDeviceX25519 } from "../credentials/device-x25519"; +import { ed25519 } from "../test-support/crypto-test-helpers"; +import { openLanChallenge } from "./lan-admission"; +import { + LAN_CONNECT_TIMEOUT_MS, + LanDialCoordinator, + LanDialMode, + LanDialSource, + type LanDialTarget, +} from "./lan-dial-coordinator"; +import type { LanMdnsBackend, LanPublishRequest } from "./lan-discovery"; +import { LanDiscoveryService } from "./lan-discovery-service"; +import { deriveLanDiscoverySecret } from "./lan-discovery-tag"; +import { LanHostController } from "./lan-host-controller"; +import { createLanListener } from "./lan-host-factory"; +import { LanHostMode } from "./lan-host-policy"; +import { + type ActiveDeviceRecord, + createLanSessionAccess, + makeLanClientHandshakeForSession, +} from "./lan-sync-wiring"; +import { + type WebSocketCtor, + WebSocketRelayPort, + WebSocketRelayState, +} from "./websocket-relay-port"; + +const LAN_ADDRESS = "192.168.77.10"; + +const settle = (ms: number) => new Promise((r) => setTimeout(r, ms)); + +async function waitUntil(predicate: () => boolean, timeoutMs = 5_000): Promise { + const deadline = Date.now() + timeoutMs; + while (!predicate() && Date.now() < deadline) await settle(20); + return predicate(); +} + +/** One multicast group. Records stay until withdrawn, and a host that dies + * without withdrawing leaves its record behind, exactly as measured. */ +function mdnsBus() { + const records = new Map }>(); + const browsers = new Set<(record: unknown) => void>(); + const announce = (name: string, txt: Readonly>) => { + const record = { name, txt: { ...txt } }; + records.set(name, record); + for (const deliver of browsers) deliver(record); + }; + const backend = (): LanMdnsBackend => ({ + publish: async (advert: LanPublishRequest) => { + announce(advert.name, advert.txt); + return { + updateTxt: async (txt) => announce(advert.name, txt), + stop: async () => { + records.delete(advert.name); + }, + }; + }, + browse: (_type, onRecord) => { + browsers.add(onRecord); + for (const record of records.values()) onRecord(record); + return { stop: () => void browsers.delete(onRecord) }; + }, + }); + return { backend, announce, records }; +} + +const viaLoopback = class { + constructor(url: string) { + const NativeWebSocket = (globalThis as unknown as { WebSocket: WebSocketCtor }).WebSocket; + // biome-ignore lint/correctness/noConstructorReturn: stands in for LAN routing + return new NativeWebSocket(url.replace(/^ws:\/\/[\d.]+:/, "ws://127.0.0.1:")); + } +} as unknown as WebSocketCtor; + +function makeDevice() { + const ed = ed25519.keygen(); + const x = generateDeviceX25519(); + return { + edPublic: new Uint8Array(ed.publicKey), + edSecret: new Uint8Array(ed.secretKey), + x25519Pub: new Uint8Array(x.publicKey), + x25519Secret: new Uint8Array(x.secretKey), + }; +} +type Device = ReturnType; + +const recordOf = (d: Device): ActiveDeviceRecord => ({ + deviceEd25519Pub: Buffer.from(d.edPublic).toString("base64"), + deviceX25519Pub: Buffer.from(d.x25519Pub).toString("base64"), +}); + +function accessFor(self: Device, roster: () => readonly ActiveDeviceRecord[]) { + return createLanSessionAccess({ + getSession: () => ({ + deviceEd25519: { publicKey: self.edPublic }, + signWithDeviceKey: (m: Uint8Array) => new Uint8Array(ed25519.sign(m, self.edSecret)), + openLanSealedChallenge: (a: { + sealed: Parameters[0]["sealed"]; + hostAccount: string; + clientAccount: string; + }) => openLanChallenge({ ...a, deviceX25519Secret: self.x25519Secret }), + }), + getDevices: () => ({ listActive: roster }), + }); +} + +const cleanups: (() => Promise | void)[] = []; +afterEach(async () => { + for (const cleanup of cleanups.splice(0).reverse()) await cleanup(); +}); + +/** A device that hosts: real listener on loopback, advertised as `LAN_ADDRESS`. */ +async function startHost(args: { + bus: ReturnType; + device: Device; + roster: () => readonly ActiveDeviceRecord[]; + secret: Uint8Array; +}) { + let endpoint: { addresses: readonly string[]; port: number } | null = null; + const controller = new LanHostController({ + readState: () => ({ + mode: LanHostMode.WhenVaultOpen, + hasSession: true, + hasSharedEntities: false, + }), + createListener: () => + createLanListener({ + access: accessFor(args.device, args.roster), + addresses: () => ["127.0.0.1"], + }), + onUrlChanged: (url) => { + endpoint = url ? { addresses: [LAN_ADDRESS], port: Number(new URL(url).port) } : null; + }, + }); + const discovery = new LanDiscoveryService({ + readState: () => ({ advertise: controller.listening, browse: false, listener: endpoint }), + discoverySecret: () => args.secret, + backend: args.bus.backend(), + onCandidate: () => undefined, + }); + await controller.apply(); + await discovery.apply(); + cleanups.push(async () => { + await discovery.dispose(); + await controller.dispose(); + }); + return { controller, discovery, port: () => endpoint?.port ?? 0 }; +} + +/** A device that browses and dials whatever the coordinator selects. */ +async function startDialer(args: { + bus: ReturnType; + device: Device; + roster: () => readonly ActiveDeviceRecord[]; + secret: Uint8Array; +}) { + const targets: (LanDialTarget | null)[] = []; + const ports: WebSocketRelayPort[] = []; + const coordinator = new LanDialCoordinator({ + onTargetChanged: (target) => { + targets.push(target); + if (!target) return; + const port = new WebSocketRelayPort({ + url: target.url, + wsImpl: viaLoopback, + requireAdmission: true, + lanHandshake: makeLanClientHandshakeForSession(accessFor(args.device, args.roster)), + connectTimeoutMs: LAN_CONNECT_TIMEOUT_MS, + }); + ports.push(port); + port.connect(); + }, + }); + coordinator.setMode(LanDialMode.Auto); + const discovery = new LanDiscoveryService({ + readState: () => ({ advertise: false, browse: true, listener: null }), + discoverySecret: () => args.secret, + backend: args.bus.backend(), + onCandidate: (candidate) => coordinator.offerCandidate(candidate, LanDialSource.Discovery), + }); + await discovery.apply(); + cleanups.push(async () => { + for (const port of ports) port.close(); + await discovery.dispose(); + }); + return { coordinator, targets, ports }; +} + +describe("discover → gated dial → admission, over a real socket", () => { + it("a device of the same identity is discovered, dialled and admitted", async () => { + const bus = mdnsBus(); + const secret = deriveLanDiscoverySecret(new Uint8Array(32).fill(21)); + const desktop = makeDevice(); + const laptop = makeDevice(); + const roster = () => [recordOf(desktop), recordOf(laptop)]; + + const host = await startHost({ bus, device: desktop, roster, secret }); + const dialer = await startDialer({ bus, device: laptop, roster, secret }); + + expect(dialer.coordinator.target()).toMatchObject({ + url: `ws://${LAN_ADDRESS}:${host.port()}`, + source: LanDialSource.Discovery, + instance: host.discovery.instance, + }); + const port = dialer.ports[0] as WebSocketRelayPort; + expect(await waitUntil(() => port.gatedAdmission())).toBe(true); + expect(port.state).toBe(WebSocketRelayState.Open); + }); + + it("a discovered device of the same identity that is NOT on the roster is still refused", async () => { + // The tag decides whether we say hello. It never decides admission. + const bus = mdnsBus(); + const secret = deriveLanDiscoverySecret(new Uint8Array(32).fill(22)); + const desktop = makeDevice(); + const revoked = makeDevice(); + + const host = await startHost({ + bus, + device: desktop, + roster: () => [recordOf(desktop)], + secret, + }); + const dialer = await startDialer({ + bus, + device: revoked, + roster: () => [recordOf(desktop), recordOf(revoked)], + secret, + }); + + expect(dialer.coordinator.target()?.url).toBe(`ws://${LAN_ADDRESS}:${host.port()}`); + const port = dialer.ports[0] as WebSocketRelayPort; + // The same setup admits in the test above, so "never admitted" here is + // the roster refusing, not the dial failing to arrive. + expect(await waitUntil(() => port.gatedAdmission(), 1_200)).toBe(false); + }); + + it("an advertiser of another identity is never dialled — no socket, so no hello to harvest", async () => { + const bus = mdnsBus(); + const mine = deriveLanDiscoverySecret(new Uint8Array(32).fill(23)); + const theirs = deriveLanDiscoverySecret(new Uint8Array(32).fill(24)); + const laptop = makeDevice(); + const rogueDevice = makeDevice(); + + let connections = 0; + const rogueSocket = createServer((socket) => { + connections += 1; + socket.destroy(); + }); + await new Promise((resolve) => rogueSocket.listen(0, "127.0.0.1", resolve)); + cleanups.push(() => new Promise((resolve) => rogueSocket.close(() => resolve()))); + + // A complete, well-formed Brainstorm host: real listener, real advert, + // correctly signed for ITS identity. The only thing it lacks is our key. + await startHost({ + bus, + device: rogueDevice, + roster: () => [recordOf(rogueDevice), recordOf(laptop)], + secret: theirs, + }); + // And a bare harvester that copies the honest fields without a key. + const rogueAddress = rogueSocket.address(); + const roguePort = typeof rogueAddress === "object" && rogueAddress ? rogueAddress.port : 0; + bus.announce("brainstorm-00000000000000ee", { + v: "2", + p: String(roguePort), + a: LAN_ADDRESS, + e: String(Math.floor(Date.now() / 300_000)), + t: "A".repeat(22), + }); + + const dialer = await startDialer({ + bus, + device: laptop, + roster: () => [recordOf(rogueDevice), recordOf(laptop)], + secret: mine, + }); + await settle(150); + + expect(dialer.targets).toEqual([]); + expect(dialer.ports).toEqual([]); + expect(dialer.coordinator.knownPeers()).toEqual([]); + expect(connections).toBe(0); + }); + + it("a record left behind by a dead host costs one failed dial, then the relay carries sync", async () => { + const bus = mdnsBus(); + const secret = deriveLanDiscoverySecret(new Uint8Array(32).fill(25)); + const desktop = makeDevice(); + const laptop = makeDevice(); + const roster = () => [recordOf(desktop), recordOf(laptop)]; + + const host = await startHost({ bus, device: desktop, roster, secret }); + const stale = [...bus.records.values()]; + // Killed, not shut down: the listener is gone and nobody said goodbye. + await host.controller.dispose(); + await host.discovery.dispose(); + for (const record of stale) bus.announce(record.name, record.txt); + + const dialer = await startDialer({ bus, device: laptop, roster, secret }); + const url = dialer.coordinator.target()?.url ?? ""; + expect(url).toBe(`ws://${LAN_ADDRESS}:${Number(new URL(url).port)}`); + + const port = dialer.ports[0] as WebSocketRelayPort; + const failed = await waitUntil( + () => port.state !== WebSocketRelayState.Open && port.state !== WebSocketRelayState.Connecting, + LAN_CONNECT_TIMEOUT_MS + 1_500, + ); + expect(failed).toBe(true); + expect(port.gatedAdmission()).toBe(false); + + dialer.coordinator.noteDialUnusable(url); + expect(dialer.coordinator.target()).toBeNull(); + // The record is still on the bus and still verifies: seeing it again + // must not put a cooling address straight back. + for (const record of stale) bus.announce(record.name, record.txt); + expect(dialer.coordinator.target()).toBeNull(); + expect(dialer.ports).toHaveLength(1); + }); +}); diff --git a/packages/shell/src/main/sync/lan-discovery-attacks.test.ts b/packages/shell/src/main/sync/lan-discovery-attacks.test.ts new file mode 100644 index 00000000..b3d2a547 --- /dev/null +++ b/packages/shell/src/main/sync/lan-discovery-attacks.test.ts @@ -0,0 +1,381 @@ +/** + * P2P-1 — adversarial probes against the discover → dial path. + * + * Kept as regression tests. Each probe is something a device on the same LAN + * can do with no key material, or with one captured advert. A red probe here + * means the gate in front of the first unauthenticated message is open. + * + * History worth knowing: the gate ledger records these fixes as landed on + * 2026-07-31, and they were not in the tree. The merge carried the feature + * and dropped the fix commit, so the findings shipped. This file is what + * makes a second loss fail CI instead of going unnoticed. + */ + +import { describe, expect, it, vi } from "vitest"; +import { + DEFAULT_LAN_CANDIDATE_TTL_MS, + DEFAULT_LAN_DIAL_COOLDOWN_MS, + LAN_MAX_CANDIDATES, + LanDialCoordinator, + LanDialMode, + LanDialSource, + type LanDialTarget, +} from "./lan-dial-coordinator"; +import { + LAN_MAX_ADVERT_ADDRESSES, + LanTxtKey, + buildLanAdvertTxt, + parseLanAdvert, +} from "./lan-discovery"; +import { LanDiscoveryService } from "./lan-discovery-service"; +import { + LAN_DISCOVERY_EPOCH_MS, + deriveLanDiscoverySecret, + lanDiscoveryEpoch, +} from "./lan-discovery-tag"; + +const VICTIM_KEY = new Uint8Array(32).fill(0x11); +const SECRET = deriveLanDiscoverySecret(VICTIM_KEY); +const NOW = 1_800_000_000_000; +const PORT = 51_820; +const HOST = "192.168.1.10"; + +const name = (n: number) => `brainstorm-${n.toString(16).padStart(16, "0")}`; + +function genuine( + args: { instance?: string; port?: number; addresses?: string[]; nowMs?: number } = {}, +) { + const instance = args.instance ?? name(1); + return { + name: instance, + txt: buildLanAdvertTxt({ + secret: SECRET, + instance, + port: args.port ?? PORT, + addresses: args.addresses ?? [HOST], + nowMs: args.nowMs ?? NOW, + }), + }; +} + +const parse = (record: unknown, nowMs = NOW) => parseLanAdvert({ record, secret: SECRET, nowMs }); + +function coordinator(start = NOW) { + const clock = { now: start }; + const targets: (LanDialTarget | null)[] = []; + const c = new LanDialCoordinator({ + onTargetChanged: (t) => targets.push(t), + now: () => clock.now, + }); + return { c, clock, targets }; +} + +/** Run a record through the same two steps production does. */ +function offer(c: LanDialCoordinator, record: unknown, nowMs: number): boolean { + const candidate = parseLanAdvert({ record, secret: SECRET, nowMs }); + if (!candidate) return false; + c.offerCandidate(candidate, LanDialSource.Discovery); + return true; +} + +describe("forgery from public material", () => { + it("a tag derived from anything but the sovereign secret does not verify", () => { + const publicLooking = [ + new Uint8Array(32).fill(0x11).map((b, i) => b ^ (i + 1)), + new Uint8Array(32), + new Uint8Array(64).fill(0x11), + ]; + for (const key of publicLooking) { + const forged = buildLanAdvertTxt({ + secret: deriveLanDiscoverySecret(key), + instance: name(1), + port: PORT, + addresses: [HOST], + nowMs: NOW, + }); + expect(parse({ name: name(1), txt: forged })).toBeNull(); + } + }); + + it("an advert that mimics every honest field but the tag produces no candidate", () => { + const honest = genuine(); + for (const tag of ["", "A".repeat(22), "A".repeat(21), "A".repeat(23), "!".repeat(22)]) { + expect(parse({ ...honest, txt: { ...honest.txt, [LanTxtKey.Tag]: tag } })).toBeNull(); + } + }); + + it("flipping any one character of a genuine tag breaks it", () => { + const honest = genuine(); + const tag = honest.txt[LanTxtKey.Tag] ?? ""; + for (let i = 0; i < tag.length; i++) { + const flipped = `${tag.slice(0, i)}${tag[i] === "A" ? "B" : "A"}${tag.slice(i + 1)}`; + expect(parse({ ...honest, txt: { ...honest.txt, [LanTxtKey.Tag]: flipped } })).toBeNull(); + } + }); +}); + +describe("replay of one captured advert", () => { + const captured = genuine(); + + it.each([ + ["a different address", { [LanTxtKey.Addresses]: "192.168.1.66" }], + ["an extra address", { [LanTxtKey.Addresses]: `${HOST},192.168.1.66` }], + ["a different port", { [LanTxtKey.Port]: "4444" }], + ["a later epoch label", { [LanTxtKey.Epoch]: String(lanDiscoveryEpoch(NOW) + 1) }], + ])("is refused when pointed at %s", (_label, override) => { + expect(parse({ ...captured, txt: { ...captured.txt, ...override } })).toBeNull(); + }); + + it("is refused under a DIFFERENT instance name — the name is authenticated", () => { + for (const other of [name(2), name(0xffff), "Andrew's laptop - trusted"]) { + expect(parse({ ...captured, name: other })).toBeNull(); + } + }); + + it("verbatim, dies with its epoch window", () => { + expect(parse(captured, NOW + LAN_DISCOVERY_EPOCH_MS)).not.toBeNull(); + expect(parse(captured, NOW + 2 * LAN_DISCOVERY_EPOCH_MS)).toBeNull(); + expect(parse(captured, NOW - 2 * LAN_DISCOVERY_EPOCH_MS)).toBeNull(); + }); + + it("verbatim, can only ever name the genuine host's own address", () => { + const candidate = parse(captured); + expect(candidate?.urls).toEqual([`ws://${HOST}:${PORT}`]); + }); + + it("under thousands of names cannot grow the candidate table at all", () => { + const { c } = coordinator(); + c.setMode(LanDialMode.Auto); + let accepted = 0; + for (let i = 2; i < 5_002; i++) { + if (offer(c, { ...captured, name: name(i) }, NOW)) accepted += 1; + } + expect(accepted).toBe(0); + expect(c.knownPeers()).toHaveLength(0); + }); +}); + +describe("hostile advert fields", () => { + it.each<[string, unknown]>([ + ["a string", "advert"], + ["an array", []], + ["a txt that is an array", { name: name(1), txt: [] }], + ["non-string txt values", { name: name(1), txt: { v: 2, p: PORT, a: [HOST], e: 1, t: {} } }], + ["an object-valued name", { ...genuine(), name: { toString: () => name(1) } }], + ["a name carrying markup", genuine({ instance: "" })], + ["a name with a dot the responder would rewrite", genuine({ instance: "brainstorm.local" })], + ["an uppercase variant of a minted name", genuine({ instance: name(1).toUpperCase() })], + ["a 10 kB name", genuine({ instance: "b".repeat(10_000) })], + ["a broadcast address", genuine({ addresses: ["255.255.255.255"] })], + ["a multicast address", genuine({ addresses: ["224.0.0.251"] })], + ["a routable address", genuine({ addresses: ["203.0.113.7"] })], + ["CGNAT space", genuine({ addresses: ["100.64.0.1"] })], + ["loopback", genuine({ addresses: ["127.0.0.1"] })], + ["loopback in another spelling", genuine({ addresses: ["127.1.2.3"] })], + ["the wildcard", genuine({ addresses: ["0.0.0.0"] })], + ["a hostname", genuine({ addresses: ["router.local"] })], + ["an IPv6 literal", genuine({ addresses: ["fe80::1"] })], + ["an IPv4-mapped IPv6 literal", genuine({ addresses: ["::ffff:192.168.1.10"] })], + ["a hex octet", genuine({ addresses: ["0xc0.168.1.10"] })], + ["an address with a port smuggled in", genuine({ addresses: ["192.168.1.10:22"] })], + ["an address with a path", genuine({ addresses: ["192.168.1.10/x"] })], + [ + "one address more than the ceiling", + genuine({ + addresses: Array.from({ length: LAN_MAX_ADVERT_ADDRESSES + 1 }, (_, i) => `10.0.0.${i + 1}`), + }), + ], + ["a repeated address", genuine({ addresses: [HOST, HOST] })], + ["port 0", genuine({ port: 0 })], + ["a port past 65535", genuine({ port: 65_536 })], + ])("refuses %s, even when it is correctly signed", (_label, record) => { + expect(parse(record)).toBeNull(); + }); + + it.each([ + ["a non-canonical port", { [LanTxtKey.Port]: "051820" }], + ["a signed port", { [LanTxtKey.Port]: "+51820" }], + ["an exponent port", { [LanTxtKey.Port]: "5e4" }], + ["an absurd epoch", { [LanTxtKey.Epoch]: "9".repeat(16) }], + ["a fractional epoch", { [LanTxtKey.Epoch]: "6000000.5" }], + ["a future schema version", { [LanTxtKey.Version]: "3" }], + ["the retired schema version", { [LanTxtKey.Version]: "1" }], + ])("refuses %s", (_label, override) => { + const honest = genuine(); + expect(parse({ ...honest, txt: { ...honest.txt, ...override } })).toBeNull(); + }); + + it("is not confused by prototype-polluting keys", () => { + const honest = genuine(); + const txt = JSON.parse( + JSON.stringify({ ...honest.txt }).replace("{", '{"__proto__":{"t":"x"},"constructor":"x",'), + ); + expect(parse({ name: honest.name, txt })?.port).toBe(PORT); + expect(({} as Record).t).toBeUndefined(); + }); +}); + +describe("the network cannot steer selection", () => { + it("Off refuses every network source", () => { + const { c, targets } = coordinator(); + for (let i = 1; i <= 50; i++) offer(c, genuine({ instance: name(i) }), NOW); + expect(c.target()).toBeNull(); + expect(targets).toEqual([]); + }); + + it("Manual dials only the typed address, and a flood cannot displace it", () => { + const { c } = coordinator(); + c.setMode(LanDialMode.Manual); + c.setManualUrl("ws://192.168.1.50:9000"); + for (let i = 1; i <= 50; i++) { + offer(c, genuine({ instance: name(i), addresses: [`10.0.0.${i}`] }), NOW); + } + expect(c.target()).toEqual({ url: "ws://192.168.1.50:9000", source: LanDialSource.Manual }); + }); + + it("a vault close drops every network-learned peer", () => { + const { c } = coordinator(); + c.setMode(LanDialMode.Auto); + offer(c, genuine(), NOW); + expect(c.target()).not.toBeNull(); + c.reset(); + expect(c.target()).toBeNull(); + expect(c.knownPeers()).toEqual([]); + }); + + it("a record arriving after the vault closed is refused, not verified with a stale key", async () => { + let secret: Uint8Array | null = SECRET; + let deliver: ((record: unknown) => void) | null = null; + const onCandidate = vi.fn(); + const svc = new LanDiscoveryService({ + readState: () => ({ advertise: false, browse: true, listener: null }), + discoverySecret: () => secret, + backend: { + publish: async () => ({ updateTxt: async () => undefined, stop: async () => undefined }), + browse: (_type, cb) => { + deliver = cb; + return { stop: () => undefined }; + }, + }, + onCandidate, + now: () => NOW, + }); + await svc.apply(); + secret = null; + (deliver as ((record: unknown) => void) | null)?.(genuine()); + expect(onCandidate).not.toHaveBeenCalled(); + await svc.dispose(); + }); +}); + +describe("attacker-reachable tables are bounded", () => { + // Reaching these tables needs a VALID tag per distinct name, which a + // keyless attacker cannot mint. The bound is for the case the gate is + // wrong, and for a user with many devices. + it("the candidate table never exceeds its ceiling", () => { + const { c } = coordinator(); + c.setMode(LanDialMode.Auto); + for (let i = 1; i <= 5_000; i++) { + offer(c, genuine({ instance: name(i), addresses: [`10.${i >> 8}.${i & 255}.9`] }), NOW); + } + expect(c.knownPeers().length).toBeLessThanOrEqual(LAN_MAX_CANDIDATES); + }); + + it("a flood does not evict the peer the link is live on", () => { + const { c } = coordinator(); + c.setMode(LanDialMode.Auto); + offer(c, genuine({ instance: name(1) }), NOW); + const live = c.target(); + expect(live?.instance).toBe(name(1)); + for (let i = 2; i <= 500; i++) { + offer(c, genuine({ instance: name(i), addresses: [`10.9.${i >> 8}.${i & 255}`] }), NOW); + } + expect(c.target()).toEqual(live); + expect(c.knownPeers().some((p) => p.instance === name(1))).toBe(true); + }); + + it("a flood naming the live ADDRESS under other names protects only the live record", () => { + const { c } = coordinator(); + c.setMode(LanDialMode.Auto); + offer(c, genuine({ instance: name(1) }), NOW); + for (let i = 2; i <= 200; i++) offer(c, genuine({ instance: name(i) }), NOW); + expect(c.knownPeers().length).toBeLessThanOrEqual(LAN_MAX_CANDIDATES); + expect(c.target()?.instance).toBe(name(1)); + }); + + it("a discovery flood does not evict a peer learned by pairing", () => { + const { c } = coordinator(); + c.setMode(LanDialMode.Auto); + expect(c.offerPairedPeer("ws://192.168.1.77:5000")).toBe(true); + for (let i = 1; i <= 200; i++) { + offer(c, genuine({ instance: name(i), addresses: [`10.9.${i >> 8}.${i & 255}`] }), NOW); + } + expect(c.knownPeers().some((p) => p.source === LanDialSource.Pairing)).toBe(true); + }); + + it("expired candidates are evicted on insert rather than kept for the process lifetime", () => { + const { c, clock } = coordinator(); + for (let i = 1; i <= LAN_MAX_CANDIDATES; i++) { + offer(c, genuine({ instance: name(i), addresses: [`10.0.0.${i}`] }), clock.now); + } + clock.now += DEFAULT_LAN_CANDIDATE_TTL_MS + 1; + offer(c, genuine({ instance: name(999), nowMs: clock.now }), clock.now); + c.setMode(LanDialMode.Auto); + expect(c.knownPeers().map((p) => p.instance)).toEqual([name(999)]); + // Nothing stale is resurrected by winding the clock back into its window. + clock.now -= DEFAULT_LAN_CANDIDATE_TTL_MS; + expect(c.knownPeers().map((p) => p.instance)).toEqual([name(999)]); + }); + + it("the cooldown table is bounded and sheds expired entries", () => { + const { c, clock } = coordinator(); + c.setMode(LanDialMode.Auto); + for (let i = 0; i < 2_000; i++) c.noteDialUnusable(`ws://10.1.${i >> 8}.${i & 255}:5000`); + const fresh = "ws://10.200.0.1:5000"; + offer(c, genuine({ instance: name(7), addresses: ["10.200.0.1"], port: 5000 }), clock.now); + expect(c.target()?.url).toBe(fresh); + // The earliest failures were shed to hold the bound; that is the safe + // direction (a forgotten failure costs one 3 s dial, not a wedge). + c.noteDialUnusable(fresh); + expect(c.target()).toBeNull(); + clock.now += DEFAULT_LAN_DIAL_COOLDOWN_MS + 1; + c.noteDialUnusable("ws://10.250.0.1:5000"); + offer( + c, + genuine({ instance: name(7), addresses: ["10.200.0.1"], port: 5000, nowMs: clock.now }), + clock.now, + ); + expect(c.target()?.url).toBe(fresh); + }); +}); + +describe("pairing-payload provenance", () => { + it.each([ + "ws://127.0.0.1:8787", + "ws://8.8.8.8:80", + "ws://relay.example.com:443", + "wss://192.168.1.20:443", + "ws://user:pw@192.168.1.20:80", + "ws://192.168.1.20:80/path", + "ws://192.168.1.20:80?x=1", + "ws://192.168.1.20:80#frag", + "ws://192.168.1.20", + "not a url", + "", + "ws://[fe80::1]:80", + ])("refuses %s rather than adopting it or throwing", (url) => { + const { c } = coordinator(); + c.setMode(LanDialMode.Auto); + expect(() => c.offerPairedPeer(url)).not.toThrow(); + expect(c.offerPairedPeer(url)).toBe(false); + expect(c.target()).toBeNull(); + expect(c.knownPeers()).toEqual([]); + }); + + it("adopts a private address, normalised", () => { + const { c } = coordinator(); + c.setMode(LanDialMode.Auto); + expect(c.offerPairedPeer(" 192.168.1.20:51820 ")).toBe(true); + expect(c.target()?.url).toBe("ws://192.168.1.20:51820"); + }); +}); diff --git a/packages/shell/src/main/sync/lan-discovery-service.test.ts b/packages/shell/src/main/sync/lan-discovery-service.test.ts index 3d00a277..68638e37 100644 --- a/packages/shell/src/main/sync/lan-discovery-service.test.ts +++ b/packages/shell/src/main/sync/lan-discovery-service.test.ts @@ -8,14 +8,24 @@ const SECRET = deriveLanDiscoverySecret(new Uint8Array(32).fill(1)); const NOW = 1_800_000_000_000; function fakeBackend() { - const published: { txt: Record; name: string; port: number }[] = []; + const published: { + txt: Record; + name: string; + port: number; + addresses: readonly string[]; + }[] = []; const updates: Record[] = []; let onRecord: ((r: unknown) => void) | null = null; let stopped = 0; let browseStopped = 0; const backend: LanMdnsBackend = { publish: async (advert) => { - published.push({ txt: { ...advert.txt }, name: advert.name, port: advert.port }); + published.push({ + txt: { ...advert.txt }, + name: advert.name, + port: advert.port, + addresses: [...advert.addresses], + }); return { updateTxt: async (txt) => { updates.push({ ...txt }); @@ -70,6 +80,28 @@ function service(args: { return { svc, timers }; } +function peerRecord(args: { + name: string; + port: number; + addresses: string[]; + secret?: Uint8Array; + nowMs?: number; +}) { + return { + name: args.name, + txt: buildLanAdvertTxt({ + secret: args.secret ?? SECRET, + instance: args.name, + port: args.port, + addresses: args.addresses, + nowMs: args.nowMs ?? NOW, + }), + }; +} + +const PEER = "brainstorm-00000000000000aa"; +const ROGUE_PEER = "brainstorm-00000000000000bb"; + const HOSTING: LanDiscoveryState = { advertise: true, browse: true, @@ -83,11 +115,12 @@ describe("LanDiscoveryService", () => { await svc.apply(); expect(fake.published).toHaveLength(1); expect(fake.published[0]?.port).toBe(51_820); + const name = fake.published[0]?.name ?? ""; + expect(name).toBe(svc.instance); expect(fake.published[0]?.txt[LanTxtKey.Tag]).toBe( - buildLanAdvertTxt({ secret: SECRET, port: 51_820, addresses: ["192.168.1.10"], nowMs: NOW })[ - LanTxtKey.Tag - ], + peerRecord({ name, port: 51_820, addresses: ["192.168.1.10"] }).txt[LanTxtKey.Tag], ); + expect(fake.published[0]?.addresses).toEqual(["192.168.1.10"]); await svc.dispose(); }); @@ -130,24 +163,15 @@ describe("LanDiscoveryService", () => { }); await svc.apply(); - fake.emit({ - name: "brainstorm-peer", - txt: buildLanAdvertTxt({ - secret: SECRET, - port: 9000, - addresses: ["192.168.1.20"], - nowMs: NOW, - }), - }); - fake.emit({ - name: "brainstorm-rogue", - txt: buildLanAdvertTxt({ - secret: deriveLanDiscoverySecret(new Uint8Array(32).fill(2)), + fake.emit(peerRecord({ name: PEER, port: 9000, addresses: ["192.168.1.20"] })); + fake.emit( + peerRecord({ + name: ROGUE_PEER, port: 9001, addresses: ["192.168.1.30"], - nowMs: NOW, + secret: deriveLanDiscoverySecret(new Uint8Array(32).fill(2)), }), - }); + ); fake.emit({ name: "junk" }); fake.emit("not an object"); @@ -166,15 +190,7 @@ describe("LanDiscoveryService", () => { await svc.apply(); const own = svc.instance; expect(own).not.toBeNull(); - fake.emit({ - name: own, - txt: buildLanAdvertTxt({ - secret: SECRET, - port: 51_820, - addresses: ["192.168.1.10"], - nowMs: NOW, - }), - }); + fake.emit(peerRecord({ name: own ?? "", port: 51_820, addresses: ["192.168.1.10"] })); expect(seen).toEqual([]); await svc.dispose(); }); @@ -190,6 +206,17 @@ describe("LanDiscoveryService", () => { expect(fake.published).toHaveLength(1); expect(fake.updates).toHaveLength(1); expect(svc.instance).toBe(instance); + // The refreshed TXT is signed for the SAME name in the NEW epoch, so a + // peer verifies it without the name ever changing. + expect(fake.updates[0]).toEqual( + peerRecord({ + name: instance ?? "", + port: 51_820, + addresses: ["192.168.1.10"], + nowMs: now, + }).txt, + ); + expect(fake.updates[0]?.[LanTxtKey.Tag]).not.toBe(fake.published[0]?.txt[LanTxtKey.Tag]); await svc.dispose(); }); @@ -255,12 +282,74 @@ describe("LanDiscoveryService", () => { }); await svc.apply(); expect(() => - fake.emit({ - name: "brainstorm-peer", - txt: buildLanAdvertTxt({ secret: SECRET, port: 9000, addresses: ["10.0.0.5"], nowMs: NOW }), - }), + fake.emit(peerRecord({ name: PEER, port: 9000, addresses: ["10.0.0.5"] })), ).not.toThrow(); expect(onError).toHaveBeenCalled(); await svc.dispose(); }); + + it("refuses a record that arrives after the vault closed, not verifying with a stale key", async () => { + const seen: LanPeerCandidate[] = []; + const fake = fakeBackend(); + let secret: Uint8Array | null = SECRET; + const svc = new LanDiscoveryService({ + readState: () => HOSTING, + discoverySecret: () => secret, + backend: fake.backend, + onCandidate: (c) => seen.push(c), + now: () => NOW, + }); + await svc.apply(); + const record = peerRecord({ name: PEER, port: 9000, addresses: ["10.0.0.5"] }); + fake.emit(record); + expect(seen).toHaveLength(1); + secret = null; + fake.emit(record); + expect(seen).toHaveLength(1); + await svc.apply(); + expect(svc.browsing).toBe(false); + expect(svc.advertising).toBe(false); + await svc.dispose(); + }); + + it("verifies against the identity that is open NOW, not the one browsing started under", async () => { + const seen: LanPeerCandidate[] = []; + const fake = fakeBackend(); + const next = deriveLanDiscoverySecret(new Uint8Array(32).fill(5)); + let secret: Uint8Array = SECRET; + const svc = new LanDiscoveryService({ + readState: () => HOSTING, + discoverySecret: () => secret, + backend: fake.backend, + onCandidate: (c) => seen.push(c), + now: () => NOW, + }); + await svc.apply(); + secret = next; + fake.emit(peerRecord({ name: PEER, port: 9000, addresses: ["10.0.0.5"] })); + expect(seen).toEqual([]); + fake.emit(peerRecord({ name: PEER, port: 9000, addresses: ["10.0.0.5"], secret: next })); + expect(seen).toHaveLength(1); + await svc.dispose(); + }); + + it("treats a key check that throws as no key", async () => { + const onError = vi.fn(); + const fake = fakeBackend(); + const svc = new LanDiscoveryService({ + readState: () => HOSTING, + discoverySecret: () => { + throw new Error("keystore locked"); + }, + backend: fake.backend, + onCandidate: () => undefined, + onError, + now: () => NOW, + }); + await expect(svc.apply()).resolves.toBeUndefined(); + expect(fake.published).toHaveLength(0); + expect(fake.browsing()).toBe(false); + expect(onError).toHaveBeenCalled(); + await svc.dispose(); + }); }); diff --git a/packages/shell/src/main/sync/lan-discovery-service.ts b/packages/shell/src/main/sync/lan-discovery-service.ts index 5cacca49..079f1abe 100644 --- a/packages/shell/src/main/sync/lan-discovery-service.ts +++ b/packages/shell/src/main/sync/lan-discovery-service.ts @@ -24,11 +24,11 @@ import { LAN_SERVICE_TYPE, - type LanAdvertRecord, type LanMdnsBackend, type LanMdnsBrowse, type LanMdnsPublication, type LanPeerCandidate, + type LanPublishRequest, buildLanAdvertTxt, mintLanInstanceName, parseLanAdvert, @@ -131,7 +131,7 @@ export class LanDiscoveryService { return; } const state = this.#options.readState(); - const secret = this.#options.discoverySecret(); + const secret = this.#readSecret(); // No identity ⇒ nothing to prove and nothing we could verify. Refuse // both halves rather than advertising an untagged record. if (!secret) { @@ -140,7 +140,7 @@ export class LanDiscoveryService { return; } - if (state.browse) this.#startBrowse(backend, secret); + if (state.browse) this.#startBrowse(backend); else this.#stopBrowse(); const wantAdvert = state.advertise && state.listener !== null && state.listener.port > 0; @@ -153,13 +153,24 @@ export class LanDiscoveryService { await this.#startOrRefreshAdvert(backend, secret, listener); } - #startBrowse(backend: LanMdnsBackend, secret: Uint8Array): void { + #readSecret(): Uint8Array | null { + try { + return this.#options.discoverySecret(); + } catch (error) { + this.#options.onError?.(error); + return null; + } + } + + #startBrowse(backend: LanMdnsBackend): void { if (this.#browse) return; try { this.#browse = backend.browse(LAN_SERVICE_TYPE, (record) => { - // Re-read the secret per record: a vault that closed between the - // browse starting and this record arriving must not verify. - const live = this.#options.discoverySecret() ?? secret; + // Re-read the secret per record and REFUSE without one: a record + // arriving after the vault closed must not verify against the key + // captured when the browse started. + const live = this.#readSecret(); + if (!live) return; const candidate = parseLanAdvert({ record, secret: live, @@ -196,17 +207,19 @@ export class LanDiscoveryService { listener: { addresses: readonly string[]; port: number }, ): Promise { const key = `${listener.port}|${[...listener.addresses].sort().join(",")}`; - const txt = buildLanAdvertTxt({ - secret, - port: listener.port, - addresses: listener.addresses, - nowMs: this.#now(), - }); - if (this.#publication && this.#advertKey === key) { + const mint = (instance: string) => + buildLanAdvertTxt({ + secret, + instance, + port: listener.port, + addresses: listener.addresses, + nowMs: this.#now(), + }); + if (this.#publication && this.#instance && this.#advertKey === key) { // Same listener, new epoch: refresh the TXT in place rather than // churning the instance name, which peers use as a de-dup key. try { - await this.#publication.updateTxt(txt); + await this.#publication.updateTxt(mint(this.#instance)); } catch (error) { this.#options.onError?.(error); } @@ -215,11 +228,12 @@ export class LanDiscoveryService { } await this.#stopAdvert(); const instance = mintLanInstanceName(this.#randomBytes); - const advert: LanAdvertRecord & { type: string } = { + const advert: LanPublishRequest = { type: LAN_SERVICE_TYPE, name: instance, port: listener.port, - txt, + addresses: [...listener.addresses], + txt: mint(instance), }; try { const publication = await backend.publish(advert); diff --git a/packages/shell/src/main/sync/lan-discovery-tag.test.ts b/packages/shell/src/main/sync/lan-discovery-tag.test.ts index e65a2e62..bb932891 100644 --- a/packages/shell/src/main/sync/lan-discovery-tag.test.ts +++ b/packages/shell/src/main/sync/lan-discovery-tag.test.ts @@ -12,8 +12,11 @@ const SOVEREIGN = new Uint8Array(32).fill(7); const OTHER_SOVEREIGN = new Uint8Array(32).fill(9); const NOW = 1_800_000_000_000; -function input(overrides: Partial<{ epoch: number; port: number; addresses: string[] }> = {}) { +function input( + overrides: Partial<{ instance: string; epoch: number; port: number; addresses: string[] }> = {}, +) { return { + instance: "brainstorm-00112233aabbccdd", epoch: lanDiscoveryEpoch(NOW), port: 51_820, addresses: ["192.168.1.10"], @@ -59,6 +62,78 @@ describe("lanDiscoveryTag", () => { }); }); +describe("rotation and unlinkability", () => { + const mine = deriveLanDiscoverySecret(SOVEREIGN); + const theirs = deriveLanDiscoverySecret(OTHER_SOVEREIGN); + const epoch0 = lanDiscoveryEpoch(NOW); + + it("an old tag is rejected once its epoch has left the window", () => { + const old = lanDiscoveryTag(mine, input()); + const at = (epochs: number) => + verifyLanDiscoveryTag({ + secret: mine, + tag: old, + input: input(), + nowMs: NOW + epochs * LAN_DISCOVERY_EPOCH_MS, + }); + expect([at(0), at(1), at(2), at(50)]).toEqual([true, true, false, false]); + }); + + it("an old tag cannot be re-labelled with the current epoch", () => { + const old = lanDiscoveryTag(mine, input()); + const relabelled = input({ epoch: epoch0 + 3 }); + expect( + verifyLanDiscoveryTag({ + secret: mine, + tag: old, + input: relabelled, + nowMs: NOW + 3 * LAN_DISCOVERY_EPOCH_MS, + }), + ).toBe(false); + }); + + it("neither identity accepts the other's tag, for the same advert, in any epoch", () => { + for (let i = 0; i < 20; i++) { + const at = input({ epoch: epoch0 + i }); + const nowMs = NOW + i * LAN_DISCOVERY_EPOCH_MS; + const a = lanDiscoveryTag(mine, at); + const b = lanDiscoveryTag(theirs, at); + expect(a).not.toBe(b); + expect(verifyLanDiscoveryTag({ secret: theirs, tag: a, input: at, nowMs })).toBe(false); + expect(verifyLanDiscoveryTag({ secret: mine, tag: b, input: at, nowMs })).toBe(false); + } + }); + + it("one identity's tags across epochs share nothing an observer could link", () => { + const tags = Array.from({ length: 64 }, (_, i) => + lanDiscoveryTag(mine, input({ epoch: epoch0 + i })), + ); + expect(new Set(tags).size).toBe(tags.length); + const bits = tags.map((tag) => [...Buffer.from(tag, "base64url")]); + let differing = 0; + for (let i = 1; i < bits.length; i++) { + const [prev, cur] = [bits[i - 1] ?? [], bits[i] ?? []]; + for (let j = 0; j < cur.length; j++) { + let x = (cur[j] ?? 0) ^ (prev[j] ?? 0); + while (x) { + differing += x & 1; + x >>= 1; + } + } + } + // Consecutive tags behave like independent 128-bit strings: ~64 bits + // differ on average. A counter or a shared prefix would sit far below. + const mean = differing / (bits.length - 1); + expect(mean).toBeGreaterThan(54); + expect(mean).toBeLessThan(74); + }); + + it("the tag is not derivable from the PUBLIC half of the identity", () => { + const fromPublic = deriveLanDiscoverySecret(new Uint8Array(32).fill(7).map((b) => b ^ 0xff)); + expect(lanDiscoveryTag(fromPublic, input())).not.toBe(lanDiscoveryTag(mine, input())); + }); +}); + describe("verifyLanDiscoveryTag", () => { const secret = deriveLanDiscoverySecret(SOVEREIGN); @@ -108,6 +183,28 @@ describe("verifyLanDiscoveryTag", () => { ); }); + // The name is the candidate table's de-dup key and is rendered in Settings. + it("refuses a replayed tag presented under a different instance name", () => { + const tag = lanDiscoveryTag(secret, input()); + const renamed = input({ instance: "brainstorm-ffffffffffffffff" }); + expect(verifyLanDiscoveryTag({ secret, tag, input: renamed, nowMs: NOW })).toBe(false); + }); + + it("field shuffling cannot collide two different adverts onto one tag", () => { + const pairs: [ReturnType, ReturnType][] = [ + [input({ instance: "a|1", port: 2 }), input({ instance: "a", port: 12 })], + [ + input({ instance: "n", addresses: ["10.0.0.1", "10.0.0.2"] }), + input({ instance: "n", addresses: ["10.0.0.1,10.0.0.2"] }), + ], + [input({ port: 5, addresses: ["10.0.0.1"] }), input({ port: 51, addresses: ["0.0.0.1"] })], + [input({ instance: "x1:y" }), input({ instance: "x", addresses: ["y"] })], + ]; + for (const [a, b] of pairs) { + expect(lanDiscoveryTag(secret, a)).not.toBe(lanDiscoveryTag(secret, b)); + } + }); + it("refuses a wrong-length tag without doing any work", () => { expect(verifyLanDiscoveryTag({ secret, tag: "short", input: input(), nowMs: NOW })).toBe(false); expect(verifyLanDiscoveryTag({ secret, tag: "x".repeat(500), input: input(), nowMs: NOW })).toBe( diff --git a/packages/shell/src/main/sync/lan-discovery-tag.ts b/packages/shell/src/main/sync/lan-discovery-tag.ts index 61f8bd6f..16c7434d 100644 --- a/packages/shell/src/main/sync/lan-discovery-tag.ts +++ b/packages/shell/src/main/sync/lan-discovery-tag.ts @@ -38,6 +38,11 @@ * * `node:crypto` rather than `@noble/*` on purpose: HKDF and HMAC-SHA256 are * both built in, and this file must not add a dependency to the transport tree. + * + * `v2` because `v1` left the instance name outside the MAC. A `v1` device and a + * `v2` device do not discover each other; pairing addresses, a typed address + * and the relay are unaffected. + * * The file is deliberately NOT named `*relay*` — the relay-blind CI fence scans * that pattern for crypto imports, and this file legitimately has them. */ @@ -46,10 +51,10 @@ import { createHmac, hkdfSync, timingSafeEqual } from "node:crypto"; /** HKDF `info`, and the MAC's domain-separation prefix. Versioned so a future * derivation change is a new namespace rather than a silent reinterpretation. */ -export const LAN_DISCOVERY_INFO = "brainstorm/lan-discovery/v1"; +export const LAN_DISCOVERY_INFO = "brainstorm/lan-discovery/v2"; /** HKDF salt. Fixed and public — the secrecy is entirely in the IKM. */ -const LAN_DISCOVERY_SALT = "brainstorm/lan-discovery/v1/salt"; +const LAN_DISCOVERY_SALT = "brainstorm/lan-discovery/v2/salt"; /** How long one tag stays valid. Five minutes: long enough that a peer that * wakes up mid-epoch still recognises a neighbour without waiting, short @@ -72,8 +77,17 @@ export const LAN_DISCOVERY_EPOCH_SKEW = 1; const SECRET_BYTES = 32; -/** The advert fields the tag commits to. Everything a dialer would act on. */ +/** The advert fields the tag commits to: everything a dialer acts on OR + * displays. */ export type LanDiscoveryTagInput = { + /** + * The advertised instance name. It is the candidate table's de-dup key, the + * freshness key for selection and text rendered in Settings, so an advert + * whose name is outside the MAC can be replayed verbatim under any name an + * attacker chooses: one capture then fills the table and puts arbitrary + * text in a privileged list. + */ + instance: string; /** Epoch the tag was minted in. */ epoch: number; /** The advertised listener port. */ @@ -111,13 +125,21 @@ export function lanDiscoveryEpoch(nowMs: number): number { } /** - * The MAC input. Canonical and unambiguous: every field is length-tagged by the - * separator, addresses are sorted, so two different adverts cannot produce the - * same pre-image by shuffling content across fields. + * The MAC input. Every field is LENGTH-PREFIXED rather than separator-joined: + * the instance name is attacker-influenced free text, so a separator alone + * would let content migrate across a field boundary (a name ending in `|80` + * colliding with a different port). Addresses are sorted so order is not part + * of the commitment. */ function tagPreimage(input: LanDiscoveryTagInput): string { - const addresses = [...input.addresses].sort().join(","); - return `${LAN_DISCOVERY_INFO}|${input.epoch}|${input.port}|${addresses}`; + const fields = [ + LAN_DISCOVERY_INFO, + input.instance, + String(input.epoch), + String(input.port), + ...[...input.addresses].sort(), + ]; + return `${fields.length};${fields.map((field) => `${Buffer.byteLength(field, "utf8")}:${field}`).join("")}`; } /** Mint the tag for one advert in one epoch. */ diff --git a/packages/shell/src/main/sync/lan-discovery.test.ts b/packages/shell/src/main/sync/lan-discovery.test.ts index bf2536a1..ea12ae91 100644 --- a/packages/shell/src/main/sync/lan-discovery.test.ts +++ b/packages/shell/src/main/sync/lan-discovery.test.ts @@ -15,16 +15,27 @@ const NOW = 1_800_000_000_000; const PORT = 51_820; const ADDRESSES = ["192.168.1.10"]; -function advert(overrides: Record = {}, name = "brainstorm-aabbccdd") { +const NAME = "brainstorm-aabbccdd00112233"; + +function signed(args: { secret?: Uint8Array; name?: string; port?: number; addresses?: string[] }) { + const name = args.name ?? NAME; return { name, - txt: { - ...buildLanAdvertTxt({ secret: SECRET, port: PORT, addresses: ADDRESSES, nowMs: NOW }), - ...overrides, - }, + txt: buildLanAdvertTxt({ + secret: args.secret ?? SECRET, + instance: name, + port: args.port ?? PORT, + addresses: args.addresses ?? ADDRESSES, + nowMs: NOW, + }), }; } +function advert(overrides: Record = {}, name = NAME) { + const base = signed({ name }); + return { name, txt: { ...base.txt, ...overrides } }; +} + describe("LAN_SERVICE_TYPE", () => { it("is the type the spike measured on the wire", () => { expect(LAN_SERVICE_TYPE).toBe("brainstorm-sync"); @@ -40,11 +51,9 @@ describe("parseLanAdvert", () => { }); it("refuses an advert tagged by a different identity — the whole point of the gate", () => { - const record = { - name: "brainstorm-deadbeef", - txt: buildLanAdvertTxt({ secret: ROGUE, port: PORT, addresses: ADDRESSES, nowMs: NOW }), - }; + const record = signed({ secret: ROGUE }); expect(parseLanAdvert({ record, secret: SECRET, nowMs: NOW })).toBeNull(); + expect(parseLanAdvert({ record, secret: ROGUE, nowMs: NOW })).not.toBeNull(); }); it("refuses an advert with no tag at all", () => { @@ -64,7 +73,7 @@ describe("parseLanAdvert", () => { record: advert(), secret: SECRET, nowMs: NOW, - ownInstance: "brainstorm-aabbccdd", + ownInstance: NAME, }), ).toBeNull(); }); @@ -77,8 +86,18 @@ describe("parseLanAdvert", () => { ["non-string name", { record: { name: 7, txt: advert().txt } }], ["empty name", { record: advert({}, "") }], ["oversized name", { record: advert({}, "x".repeat(LAN_MAX_INSTANCE_NAME + 1)) }], - ["missing txt", { record: { name: "brainstorm-aabbccdd" } }], - ["wrong txt version", { record: advert({ [LanTxtKey.Version]: "2" }) }], + ["a name this product did not mint", { record: signed({ name: "Trusted laptop" }) }], + ["a name with surrounding whitespace", { record: { ...advert(), name: ` ${NAME} ` } }], + ["missing txt", { record: { name: NAME } }], + ["the retired v1 txt version", { record: advert({ [LanTxtKey.Version]: "1" }) }], + ["loopback, even correctly tagged", { record: signed({ addresses: ["127.0.0.1"] }) }], + [ + "loopback beside a real address, correctly tagged", + { record: signed({ addresses: ["192.168.1.10", "127.0.0.1"] }) }, + ], + ["a padded address, correctly tagged", { record: signed({ addresses: [" 10.0.0.1"] }) }], + ["a non-canonical octet, correctly tagged", { record: signed({ addresses: ["10.0.0.1e0"] }) }], + ["an empty octet, correctly tagged", { record: signed({ addresses: ["10..0.1"] }) }], ["non-numeric port", { record: advert({ [LanTxtKey.Port]: "http" }) }], ["zero port", { record: advert({ [LanTxtKey.Port]: "0" }) }], ["out-of-range port", { record: advert({ [LanTxtKey.Port]: "70000" }) }], @@ -106,7 +125,61 @@ describe("parseLanAdvert", () => { }); }); +describe("stale, duplicate and multi-address records", () => { + it("keeps every advertised address, in advertised order", () => { + const addresses = ["192.168.1.10", "10.0.0.4", "172.16.3.3", "169.254.9.9"]; + const candidate = parseLanAdvert({ record: signed({ addresses }), secret: SECRET, nowMs: NOW }); + expect(candidate?.addresses).toEqual(addresses); + expect(candidate?.urls).toEqual(addresses.map((a) => `ws://${a}:${PORT}`)); + }); + + it("verifies whatever order the responder delivers the address list in", () => { + const record = signed({ addresses: ["192.168.1.10", "10.0.0.4"] }); + const reordered = { + ...record, + txt: { ...record.txt, [LanTxtKey.Addresses]: "192.168.1.10,10.0.0.4" }, + }; + const flipped = { + ...record, + txt: { ...record.txt, [LanTxtKey.Addresses]: "10.0.0.4,192.168.1.10" }, + }; + expect(parseLanAdvert({ record: reordered, secret: SECRET, nowMs: NOW })?.urls[0]).toBe( + `ws://192.168.1.10:${PORT}`, + ); + expect(parseLanAdvert({ record: flipped, secret: SECRET, nowMs: NOW })?.urls[0]).toBe( + `ws://10.0.0.4:${PORT}`, + ); + }); + + it("a duplicate delivery of one record parses to the same candidate", () => { + const record = signed({}); + const a = parseLanAdvert({ record, secret: SECRET, nowMs: NOW }); + const b = parseLanAdvert({ record, secret: SECRET, nowMs: NOW + 1_000 }); + expect(b).toEqual(a); + }); + + it("a record still cached by the responder after its epochs passed is refused", () => { + const record = signed({}); + expect(parseLanAdvert({ record, secret: SECRET, nowMs: NOW + 5 * 60_000 })).not.toBeNull(); + expect(parseLanAdvert({ record, secret: SECRET, nowMs: NOW + 11 * 60_000 })).toBeNull(); + }); + + it("a verbatim replay under a different instance name is refused", () => { + const captured = signed({}); + for (const name of ["brainstorm-ffffffffffffffff", "Andrew's laptop - trusted"]) { + expect(parseLanAdvert({ record: { ...captured, name }, secret: SECRET, nowMs: NOW })).toBeNull(); + } + }); +}); + describe("mintLanInstanceName", () => { + it("mints a name the parser accepts", () => { + const name = mintLanInstanceName(() => new Uint8Array(8).fill(0x5a)); + expect(parseLanAdvert({ record: signed({ name }), secret: SECRET, nowMs: NOW })?.instance).toBe( + name, + ); + }); + it("is random per boot, so the name is not a tracking handle", () => { let n = 0; const seq = () => new Uint8Array(8).fill(n++); diff --git a/packages/shell/src/main/sync/lan-discovery.ts b/packages/shell/src/main/sync/lan-discovery.ts index c385ba16..6ca92b95 100644 --- a/packages/shell/src/main/sync/lan-discovery.ts +++ b/packages/shell/src/main/sync/lan-discovery.ts @@ -29,7 +29,7 @@ * for crypto imports and this module legitimately verifies a MAC. */ -import { isBindableAddress } from "./lan-address"; +import { isBindableAddress, isPrivateIpv4 } from "./lan-address"; import { LAN_DISCOVERY_TAG_CHARS, type LanDiscoveryTagInput, @@ -54,7 +54,7 @@ export enum LanTxtKey { /** TXT schema version. A peer advertising anything else is ignored outright * rather than best-effort parsed. */ -export const LAN_TXT_VERSION = "1"; +export const LAN_TXT_VERSION = "2"; /** How many addresses one advert may carry. A machine with Wi-Fi, Ethernet, a * VPN `utun` and a Docker bridge legitimately has several; four is enough for @@ -65,6 +65,12 @@ export const LAN_MAX_ADVERT_ADDRESSES = 4; * by definition. */ export const LAN_MAX_INSTANCE_NAME = 63; +/** The only shape `mintLanInstanceName` produces. Anything else was not minted + * by this product, so it is refused before any MAC work; it also guarantees + * the name has no dots, which `bonjour-service` would rewrite on publish and + * so break the commitment. */ +const LAN_INSTANCE_NAME_RE = /^brainstorm-[0-9a-f]{16}$/; + /** The advert as it goes on the wire / comes off it. `txt` values are strings * because that is all a TXT record holds. */ export type LanAdvertRecord = { @@ -73,6 +79,17 @@ export type LanAdvertRecord = { txt: Readonly>; }; +/** + * What we ask the responder to publish. `addresses` is the complete set of + * addresses the advert may disclose in ANY record: the responder must not add + * the machine's hostname or its other interfaces on its own initiative, which + * would hand a passive observer a stable handle that outlives the rotation. + */ +export type LanPublishRequest = LanAdvertRecord & { + type: string; + addresses: readonly string[]; +}; + /** A record that passed every check: safe to hand to the dialer. */ export type LanPeerCandidate = { /** The advertised instance name. Ephemeral per boot, so it is a display @@ -91,7 +108,7 @@ export type LanPeerCandidate = { export type LanMdnsBackend = { /** Publish one service instance. Resolves with a handle that can refresh the * TXT in place (tag rotation) and stop the advert. */ - publish(advert: LanAdvertRecord & { type: string }): Promise; + publish(advert: LanPublishRequest): Promise; /** Start browsing. `onRecord` fires per discovered/updated instance. */ browse(type: string, onRecord: (record: unknown) => void): LanMdnsBrowse; }; @@ -108,13 +125,23 @@ export type LanMdnsBrowse = { /** Mint the TXT record for our own advert, tag included. */ export function buildLanAdvertTxt(args: { secret: Uint8Array; + /** The instance name the advert is published under. Authenticated. */ + instance: string; port: number; addresses: readonly string[]; nowMs: number; }): Record { - const addresses = [...args.addresses].sort(); + // Caller order is PREFERENCE order (most reachable first) and is kept on the + // wire; the tag is over the sorted set, so order is a hint an on-path + // attacker could shuffle but only among addresses the host really signed. + const addresses = [...args.addresses]; const epoch = lanDiscoveryEpoch(args.nowMs); - const input: LanDiscoveryTagInput = { epoch, port: args.port, addresses }; + const input: LanDiscoveryTagInput = { + instance: args.instance, + epoch, + port: args.port, + addresses, + }; return { [LanTxtKey.Version]: LAN_TXT_VERSION, [LanTxtKey.Port]: String(args.port), @@ -127,7 +154,7 @@ export function buildLanAdvertTxt(args: { /** * Turn a raw browse result into a candidate, or `null`. * - * `null` for: a non-object, a missing or oversized instance name, a bad TXT + * `null` for: a non-object, an instance name this product did not mint, a bad TXT * version, a port that is not an integer in `1..65535`, an address list that is * empty / oversized / contains anything that is not a private IPv4 literal, a * malformed epoch, a wrong-length tag, or a tag that does not verify. There is @@ -148,8 +175,10 @@ export function parseLanAdvert(args: { const raw = record as { name?: unknown; txt?: unknown }; if (typeof raw.name !== "string") return null; - const instance = raw.name.trim(); - if (instance.length === 0 || instance.length > LAN_MAX_INSTANCE_NAME) return null; + // Not trimmed: the name is authenticated byte-for-byte, and "repairing" it + // first would verify a name the peer never signed. + const instance = raw.name; + if (!LAN_INSTANCE_NAME_RE.test(instance)) return null; if (args.ownInstance && instance === args.ownInstance) return null; if (!raw.txt || typeof raw.txt !== "object") return null; @@ -164,7 +193,7 @@ export function parseLanAdvert(args: { if (!addresses) return null; const epochRaw = txt[LanTxtKey.Epoch]; - if (typeof epochRaw !== "string" || !/^\d{1,15}$/.test(epochRaw)) return null; + if (typeof epochRaw !== "string" || !/^[1-9]\d{0,14}$/.test(epochRaw)) return null; const epoch = Number(epochRaw); if (!Number.isSafeInteger(epoch)) return null; @@ -178,7 +207,7 @@ export function parseLanAdvert(args: { const ok = verifyLanDiscoveryTag({ secret, tag, - input: { epoch, port, addresses: sorted }, + input: { instance, epoch, port, addresses: sorted }, nowMs, }); if (!ok) return null; @@ -192,7 +221,9 @@ export function parseLanAdvert(args: { } function parseBoundedInt(value: unknown, min: number, max: number): number | null { - if (typeof value !== "string" || !/^\d{1,7}$/.test(value)) return null; + // Canonical decimal only: one spelling per value, so two records that + // differ on the wire never verify as the same advert. + if (typeof value !== "string" || !/^[1-9]\d{0,6}$/.test(value)) return null; const n = Number(value); if (!Number.isSafeInteger(n) || n < min || n > max) return null; return n; @@ -205,10 +236,13 @@ function parseAddressList(value: unknown): readonly string[] | null { const out: string[] = []; for (const part of parts) { const address = part.trim(); - // `isBindableAddress` is the same predicate the listener binds by: - // private IPv4 or loopback literal, nothing routable, no hostnames. A - // hostname here would mean a DNS lookup driven by an attacker's advert. - if (!isBindableAddress(address)) return null; + // Private IPv4 literals only. No hostnames (a DNS lookup driven by an + // advert), nothing routable, and NOT loopback even under a valid tag: a + // peer is never on our loopback, so such an advert can only point the + // dial at a local service. + // `isBindableAddress` supplies the strict four-octet literal check; + // `isPrivateIpv4` then removes the loopback it deliberately allows. + if (address !== part || !isBindableAddress(address) || !isPrivateIpv4(address)) return null; if (out.includes(address)) return null; out.push(address); } diff --git a/packages/shell/src/main/sync/lan-mdns-backend.test.ts b/packages/shell/src/main/sync/lan-mdns-backend.test.ts new file mode 100644 index 00000000..b2d8fbee --- /dev/null +++ b/packages/shell/src/main/sync/lan-mdns-backend.test.ts @@ -0,0 +1,201 @@ +/** + * The `bonjour-service` adapter, against a stand-in for the library's class. + * + * No socket: `vi.mock` replaces the package, so this pins what we ASK the + * responder for. One test below additionally builds the library's real + * `Service` (which needs no socket either) to prove the address filter matches + * the record shapes the installed version actually emits. + */ + +import { beforeEach, describe, expect, it, vi } from "vitest"; + +type PublishOptions = { + name: string; + type: string; + port: number; + host?: string; + disableIPv6?: boolean; + probe?: boolean; + txt: Record; +}; + +const fake = vi.hoisted(() => { + const state = { + published: [] as { + opts: PublishOptions; + stopped: boolean; + records: () => { type: string; data?: unknown }[]; + handlers: Map void>; + }[], + browserHandlers: new Map void>(), + onUp: null as ((service: unknown) => void) | null, + browserStopped: 0, + failConstruct: false, + }; + class Bonjour { + constructor() { + if (state.failConstruct) throw new Error("EADDRINUSE 5353"); + } + publish(opts: PublishOptions) { + const service = { + opts, + stopped: false, + handlers: new Map void>(), + records: () => [ + { type: "PTR", data: "x" }, + { type: "SRV", data: { port: opts.port, target: opts.host } }, + { type: "TXT", data: opts.txt }, + { type: "A", data: "192.168.1.10" }, + { type: "A", data: "10.8.0.2" }, + { type: "A", data: "203.0.113.7" }, + { type: "AAAA", data: "2001:db8::1" }, + { type: "AAAA", data: "fe80::1" }, + ], + stop(callback?: () => void) { + service.stopped = true; + callback?.(); + }, + on(event: string, listener: (...args: unknown[]) => void) { + service.handlers.set(event, listener); + }, + }; + state.published.push(service); + return service; + } + find(_opts: { type: string }, onUp: (service: unknown) => void) { + state.onUp = onUp; + return { + stop: () => { + state.browserStopped += 1; + }, + removeAllListeners: () => state.browserHandlers.clear(), + on: (event: string, listener: (...args: unknown[]) => void) => { + state.browserHandlers.set(event, listener); + }, + }; + } + destroy(callback?: () => void) { + callback?.(); + } + } + return { state, Bonjour }; +}); + +vi.mock("bonjour-service", () => ({ Bonjour: fake.Bonjour })); + +import { createLanMdnsBackend, restrictAddressRecords } from "./lan-mdns-backend"; + +const NAME = "brainstorm-0011223344556677"; +const ADVERT = { + type: "brainstorm-sync", + name: NAME, + port: 51_820, + addresses: ["192.168.1.10"], + txt: { v: "2", t: "first" }, +}; + +beforeEach(() => { + fake.state.published.length = 0; + fake.state.browserHandlers.clear(); + fake.state.onUp = null; + fake.state.browserStopped = 0; + fake.state.failConstruct = false; +}); + +async function backend() { + const built = await createLanMdnsBackend(); + if (!built) throw new Error("backend unavailable"); + return built; +} + +describe("createLanMdnsBackend — what the advert discloses", () => { + it("publishes under a per-boot host, never the machine's hostname", async () => { + await (await backend()).publish(ADVERT); + const opts = fake.state.published[0]?.opts; + expect(opts?.host).toBe(`${NAME}.local`); + expect(opts?.disableIPv6).toBe(true); + }); + + it("never disables the probe, which is what lets the record filter land first", async () => { + await (await backend()).publish(ADVERT); + expect(fake.state.published[0]?.opts.probe).toBeUndefined(); + }); + + it("keeps address records to the addresses we chose to advertise", async () => { + await (await backend()).publish(ADVERT); + const records = fake.state.published[0]?.records() ?? []; + expect(records.filter((r) => r.type === "A").map((r) => r.data)).toEqual(["192.168.1.10"]); + expect(records.some((r) => r.type === "AAAA")).toBe(false); + expect(records.map((r) => r.type)).toEqual(["PTR", "SRV", "TXT", "A"]); + }); + + it("filters the records the installed library really builds", async () => { + const { Service } = (await vi.importActual("bonjour-service")) as { + Service: new ( + config: PublishOptions, + ) => { host: string; records(): { type: string; name: string; data?: unknown }[] }; + }; + const service = new Service({ ...ADVERT, host: `${NAME}.local`, disableIPv6: true }); + const raw = service.records(); + const kept = restrictAddressRecords(raw, ["192.168.1.10"]); + expect(raw.some((r) => r.type === "SRV" && r.name.startsWith(NAME))).toBe(true); + expect(kept.filter((r) => r.type === "A").every((r) => r.data === "192.168.1.10")).toBe(true); + expect(kept.some((r) => r.type === "AAAA")).toBe(false); + expect(kept.filter((r) => r.type !== "A").length).toBe( + raw.filter((r) => r.type !== "A" && r.type !== "AAAA").length, + ); + const srv = kept.find((r) => r.type === "SRV"); + expect((srv?.data as { target?: string }).target).toBe(`${NAME}.local`); + expect(kept.filter((r) => r.type === "A").every((r) => r.name === `${NAME}.local`)).toBe(true); + }); +}); + +describe("createLanMdnsBackend — rotation reaches the wire", () => { + it("re-announces the SAME name with the new TXT, withdrawing the old record first", async () => { + const publication = await (await backend()).publish(ADVERT); + await publication.updateTxt({ v: "2", t: "second" }); + expect(fake.state.published).toHaveLength(2); + expect(fake.state.published[0]?.stopped).toBe(true); + expect(fake.state.published[1]?.opts).toMatchObject({ + name: NAME, + host: `${NAME}.local`, + port: 51_820, + txt: { v: "2", t: "second" }, + }); + expect(fake.state.published[1]?.records().filter((r) => r.type === "A")).toHaveLength(1); + await publication.stop(); + expect(fake.state.published[1]?.stopped).toBe(true); + }); +}); + +describe("createLanMdnsBackend — browsing", () => { + it("delivers first sightings and TXT refreshes alike", async () => { + const seen: unknown[] = []; + const browse = (await backend()).browse("brainstorm-sync", (record) => seen.push(record)); + fake.state.onUp?.({ name: "a" }); + fake.state.browserHandlers.get("txt-update")?.({ name: "a", refreshed: true }); + expect(seen).toEqual([{ name: "a" }, { name: "a", refreshed: true }]); + browse.stop(); + expect(fake.state.browserStopped).toBe(1); + }); + + it("absorbs a consumer that throws and a responder that errors", async () => { + const onError = vi.fn(); + const built = await createLanMdnsBackend({ onError }); + built?.browse("brainstorm-sync", () => { + throw new Error("consumer"); + }); + expect(() => fake.state.onUp?.({})).not.toThrow(); + fake.state.browserHandlers.get("error")?.(new Error("socket")); + await built?.publish(ADVERT); + fake.state.published[0]?.handlers.get("error")?.(new Error("probe")); + expect(onError).toHaveBeenCalledTimes(3); + }); + + it("returns null when the platform will not give us a responder (OQ-P2P-3)", async () => { + fake.state.failConstruct = true; + const onError = vi.fn(); + expect(await createLanMdnsBackend({ onError })).toBeNull(); + expect(onError).toHaveBeenCalledOnce(); + }); +}); diff --git a/packages/shell/src/main/sync/lan-mdns-backend.ts b/packages/shell/src/main/sync/lan-mdns-backend.ts index 9d4e2044..71e0ece3 100644 --- a/packages/shell/src/main/sync/lan-mdns-backend.ts +++ b/packages/shell/src/main/sync/lan-mdns-backend.ts @@ -22,10 +22,10 @@ */ import type { - LanAdvertRecord, LanMdnsBackend, LanMdnsBrowse, LanMdnsPublication, + LanPublishRequest, } from "./lan-discovery"; /** The slice of `bonjour-service` this adapter uses, declared structurally so @@ -35,21 +35,51 @@ type BonjourLike = { name: string; type: string; port: number; + host: string; + disableIPv6: boolean; txt: Record; }): BonjourServiceLike; find( opts: { type: string }, onUp: (service: unknown) => void, - ): { stop(): void; removeAllListeners?: () => void }; + ): { + stop(): void; + removeAllListeners?: () => void; + on?(event: string, listener: (...args: unknown[]) => void): void; + }; destroy(callback?: () => void): void; }; type BonjourServiceLike = { stop(callback?: () => void): void; - updateTxt?(txt: Record): void; on?(event: string, listener: (...args: unknown[]) => void): void; + records?: () => DnsRecordLike[]; }; +const BROWSER_TXT_UPDATE_EVENT = "txt-update"; + +type DnsRecordLike = { type: string; data?: unknown }; + +/** + * Keep an advert's address records to the addresses we chose to disclose. + * + * `bonjour-service` builds an A/AAAA record for EVERY non-internal interface + * the OS reports, so left alone an advert publishes the VPN address, a routable + * address and the global-scope IPv6 set next to the one private IPv4 the + * listener is bound to. Exported for the test; the dialer never reads these + * records (it uses the authenticated TXT list), so dropping them costs nothing. + */ +export function restrictAddressRecords( + records: readonly T[], + allowed: readonly string[], +): T[] { + return records.filter((record) => { + if (record.type === "AAAA") return false; + if (record.type !== "A") return true; + return typeof record.data === "string" && allowed.includes(record.data); + }); +} + /** * Build the responder, or `null` when this platform will not give us one. * @@ -71,49 +101,79 @@ export async function createLanMdnsBackend(options?: { } return { - publish(advert: LanAdvertRecord & { type: string }): Promise { - const service = bonjour.publish({ - name: advert.name, - type: advert.type, - port: advert.port, - txt: { ...advert.txt }, - }); - // A responder error after publish must not become an unhandled 'error' - // event on the emitter, which would take the main process down. - service.on?.("error", (error) => options?.onError?.(error)); + publish(advert: LanPublishRequest): Promise { + const announce = (txt: Readonly>): BonjourServiceLike => { + const service = bonjour.publish({ + name: advert.name, + type: advert.type, + port: advert.port, + // Without `host` the SRV target and every A record are owned by + // `os.hostname()`: a stable, human-chosen machine name broadcast + // to the whole LAN, which defeats the per-boot instance name and + // the rotating tag. The instance name is already random per boot. + host: `${advert.name}.local`, + disableIPv6: true, + txt: { ...txt }, + }); + // Patched before the first announce: `publish` probes for ~250 ms + // before it reads `records()`. Never pass `probe: false`, which + // would announce synchronously with the unrestricted set. + const buildRecords = service.records?.bind(service); + if (buildRecords) { + const allowed = [...advert.addresses]; + service.records = () => restrictAddressRecords(buildRecords(), allowed); + } + // A responder error after publish must not become an unhandled + // 'error' event on the emitter, which would take the main process + // down. + service.on?.("error", (error) => options?.onError?.(error)); + return service; + }; + const withdraw = (service: BonjourServiceLike): Promise => + new Promise((resolve) => { + try { + service.stop(() => resolve()); + } catch { + resolve(); + } + // `stop` does not always invoke its callback when the socket is + // already gone; do not hang the reconcile queue on it. + setTimeout(resolve, 1_000).unref?.(); + }); + let service = announce(advert.txt); const publication: LanMdnsPublication = { - updateTxt(txt) { + // `bonjour-service` has no in-place TXT update (1.4.4 registers a + // snapshot of the records at announce), so a rotated tag reaches + // the wire only by withdrawing and re-announcing under the SAME + // name. The goodbye is also what makes a browsing peer see the + // refreshed advert as a new sighting. + async updateTxt(txt) { try { - service.updateTxt?.({ ...txt }); + await withdraw(service); + service = announce(txt); } catch (error) { options?.onError?.(error); } - return Promise.resolve(); - }, - stop() { - return new Promise((resolve) => { - try { - service.stop(() => resolve()); - } catch { - resolve(); - } - // `stop` does not always invoke its callback when the socket is - // already gone; do not hang the reconcile queue on it. - setTimeout(resolve, 1_000).unref?.(); - }); }, + stop: () => withdraw(service), }; return Promise.resolve(publication); }, browse(type: string, onRecord: (record: unknown) => void): LanMdnsBrowse { - const browser = bonjour.find({ type }, (service) => { + const deliver = (service: unknown): void => { try { onRecord(service); } catch (error) { options?.onError?.(error); } - }); + }; + const browser = bonjour.find({ type }, deliver); + // `find`'s callback is the first sighting only. A peer whose responder + // refreshes its TXT in place reports the rotated tag as an update, and + // without this a known peer silently ages out of the candidate table. + browser.on?.(BROWSER_TXT_UPDATE_EVENT, deliver); + browser.on?.("error", (error) => options?.onError?.(error)); return { stop() { try { From 2fc6bf6730fc8ba76a92372e2a7ce2eaf1b8ec9c Mon Sep 17 00:00:00 2001 From: brainstorm-os Date: Thu, 19 Nov 2026 15:36:54 +0100 Subject: [PATCH 12/42] feat(editor): embed provider catalogue with exact-host classifier (B11.20b) One classifier branch plus one catalogue row per provider. Adds Google Maps, OpenStreetMap, Spotify, SoundCloud, CodePen and Miro; X and GitHub Gist stay bookmark cards because both embed through a script injected into the host page. Hardens the existing branches: hosts now match an exact set (the YouTube branch used endsWith, so evilyoutube.com classified), embeds need https with no userinfo or port, and every embed URL is rebuilt from validated tokens on the provider's fixed origin instead of interpolating the pasted URL. Co-Authored-By: Claude Fable 5.2 --- .../notes/src/editor/nodes/web-embed-node.tsx | 8 - packages/editor/src/i18n.tsx | 45 ++ .../src/plugins/embed-providers.test.ts | 483 +++++++++++++- .../editor/src/plugins/embed-providers.ts | 615 ++++++++++++++++-- 4 files changed, 1080 insertions(+), 71 deletions(-) delete mode 100644 apps/notes/src/editor/nodes/web-embed-node.tsx diff --git a/apps/notes/src/editor/nodes/web-embed-node.tsx b/apps/notes/src/editor/nodes/web-embed-node.tsx deleted file mode 100644 index 739344d1..00000000 --- a/apps/notes/src/editor/nodes/web-embed-node.tsx +++ /dev/null @@ -1,8 +0,0 @@ -/** Re-export shim — WebEmbedNode now lives in `@brainstorm-os/editor`. */ -export { - WEB_EMBED_NODE_TYPE, - type SerializedWebEmbedNode, - $createWebEmbedNode, - $isWebEmbedNode, - WebEmbedNode, -} from "@brainstorm-os/editor"; diff --git a/packages/editor/src/i18n.tsx b/packages/editor/src/i18n.tsx index 78e22ed4..5cd50c78 100644 --- a/packages/editor/src/i18n.tsx +++ b/packages/editor/src/i18n.tsx @@ -223,6 +223,29 @@ export type EditorI18nKey = | "editor.embed.chooser.bookmark" | "editor.embed.chooser.embed" | "editor.embed.chooser.link" + | "editor.bookmark.urlPlaceholder.youtube" + | "editor.bookmark.urlPlaceholder.googleMaps" + // Web embeds (WebEmbedNode facade + the shared Embeds slash rows). + | "editor.block.webEmbed" + | "editor.block.webEmbed.description" + | "editor.block.youtube" + | "editor.block.youtube.description" + | "editor.block.googleMaps" + | "editor.block.googleMaps.description" + | "editor.embed.facade.load" + | "editor.embed.facade.notice" + | "editor.embed.frameTitle" + | "editor.embed.provider.youtube" + | "editor.embed.provider.vimeo" + | "editor.embed.provider.loom" + | "editor.embed.provider.figma" + | "editor.embed.provider.codesandbox" + | "editor.embed.provider.googleMaps" + | "editor.embed.provider.openStreetMap" + | "editor.embed.provider.spotify" + | "editor.embed.provider.soundCloud" + | "editor.embed.provider.codePen" + | "editor.embed.provider.miro" // Comments panel (CommentsPanel — B11.9). | "editor.comments.region" | "editor.comments.empty" @@ -487,6 +510,28 @@ export const EDITOR_I18N_DEFAULTS: EditorManifest = Object.freeze({ "editor.embed.chooser.bookmark": "Create bookmark", "editor.embed.chooser.embed": "Embed", "editor.embed.chooser.link": "Plain link", + "editor.bookmark.urlPlaceholder.youtube": "Paste a YouTube link…", + "editor.bookmark.urlPlaceholder.googleMaps": "Paste a Google Maps link…", + "editor.block.webEmbed": "Bookmark or web embed", + "editor.block.webEmbed.description": "A link as a card, or an embed from a supported site", + "editor.block.youtube": "YouTube", + "editor.block.youtube.description": "Embed a YouTube video", + "editor.block.googleMaps": "Google Maps", + "editor.block.googleMaps.description": "Embed a map of a place or an area", + "editor.embed.facade.load": "Load {provider}", + "editor.embed.facade.notice": "Nothing has loaded yet. Loading connects to {host}.", + "editor.embed.frameTitle": "{provider} embed", + "editor.embed.provider.youtube": "YouTube", + "editor.embed.provider.vimeo": "Vimeo", + "editor.embed.provider.loom": "Loom", + "editor.embed.provider.figma": "Figma", + "editor.embed.provider.codesandbox": "CodeSandbox", + "editor.embed.provider.googleMaps": "Google Maps", + "editor.embed.provider.openStreetMap": "OpenStreetMap", + "editor.embed.provider.spotify": "Spotify", + "editor.embed.provider.soundCloud": "SoundCloud", + "editor.embed.provider.codePen": "CodePen", + "editor.embed.provider.miro": "Miro", "editor.comments.region": "Comments", "editor.comments.empty": "No comments yet", "editor.comments.mention.search": "Mention a member", diff --git a/packages/editor/src/plugins/embed-providers.test.ts b/packages/editor/src/plugins/embed-providers.test.ts index 74b5389f..ecd25bd6 100644 --- a/packages/editor/src/plugins/embed-providers.test.ts +++ b/packages/editor/src/plugins/embed-providers.test.ts @@ -1,5 +1,81 @@ import { describe, expect, it } from "vitest"; -import { EmbedKind, classifyUrl, faviconUrl, isLoneUrl, parseHttpUrl } from "./embed-providers"; +import { + EMBED_PROVIDERS, + EmbedAspect, + EmbedKind, + EmbedLoadPolicy, + type EmbedProviderKind, + classifyEmbed, + classifyUrl, + faviconUrl, + isLoneUrl, + parseHttpUrl, + safeExternalHref, +} from "./embed-providers"; + +const YT = "dQw4w9WgXcQ"; +const SPOTIFY_ID = "4cOdK2wGLETKBW3PvgPWqT"; + +/** One canonical share URL per provider → the exact iframe src it must + * produce. Doubles as the fixture for the catalogue invariants below. */ +const CANONICAL: Readonly> = { + [EmbedKind.YouTube]: { + url: `https://www.youtube.com/watch?v=${YT}`, + embedUrl: `https://www.youtube-nocookie.com/embed/${YT}`, + }, + [EmbedKind.Vimeo]: { + url: "https://vimeo.com/123456789", + embedUrl: "https://player.vimeo.com/video/123456789?dnt=1", + }, + [EmbedKind.Loom]: { + url: "https://www.loom.com/share/0281766fa2d04bb788eaf19e65135184", + embedUrl: "https://www.loom.com/embed/0281766fa2d04bb788eaf19e65135184", + }, + [EmbedKind.Figma]: { + url: "https://www.figma.com/design/AbCdEf0123456789abcdef/My-File?node-id=12-34&t=tracking", + embedUrl: + "https://www.figma.com/embed?embed_host=brainstorm&url=https%3A%2F%2Fwww.figma.com%2Fdesign%2FAbCdEf0123456789abcdef%2FMy-File%3Fnode-id%3D12-34", + }, + [EmbedKind.CodeSandbox]: { + url: "https://codesandbox.io/s/cool-sandbox-x1y2z", + embedUrl: "https://codesandbox.io/embed/cool-sandbox-x1y2z", + }, + [EmbedKind.GoogleMaps]: { + url: "https://www.google.com/maps/place/Eiffel+Tower/@48.8583701,2.2944813,17z/data=!3m1!4b1", + embedUrl: "https://www.google.com/maps?q=Eiffel+Tower&z=17&output=embed", + }, + [EmbedKind.OpenStreetMap]: { + url: "https://www.openstreetmap.org/?mlat=52.5&mlon=13.4#map=10/52.5/13.4", + embedUrl: + "https://www.openstreetmap.org/export/embed.html?bbox=12.872656%2C52.25923%2C13.927344%2C52.74077&marker=52.5%2C13.4&layer=mapnik", + }, + [EmbedKind.Spotify]: { + url: `https://open.spotify.com/track/${SPOTIFY_ID}?si=abcdef`, + embedUrl: `https://open.spotify.com/embed/track/${SPOTIFY_ID}`, + }, + [EmbedKind.SoundCloud]: { + url: "https://soundcloud.com/some-artist/some-track?utm_source=clipboard", + embedUrl: + "https://w.soundcloud.com/player/?url=https%3A%2F%2Fsoundcloud.com%2Fsome-artist%2Fsome-track&auto_play=false&hide_related=true&show_comments=false&visual=false", + }, + [EmbedKind.CodePen]: { + url: "https://codepen.io/some-user/pen/abcXYZ", + embedUrl: "https://codepen.io/some-user/embed/abcXYZ?default-tab=result", + }, + [EmbedKind.Miro]: { + url: "https://miro.com/app/board/uXjVO1a2b3c=/?share_link_id=123", + embedUrl: "https://miro.com/app/live-embed/uXjVO1a2b3c=/?embedMode=view_only_without_ui", + }, +}; + +const PROVIDER_KINDS = Object.keys(EMBED_PROVIDERS) as EmbedProviderKind[]; + +function expectBookmark(url: string): void { + const c = classifyUrl(url); + expect(c?.kind ?? EmbedKind.Bookmark, url).toBe(EmbedKind.Bookmark); + expect(c?.embedUrl ?? null, url).toBeNull(); + expect(classifyEmbed(url), url).toBeNull(); +} describe("parseHttpUrl", () => { it("accepts http(s) and infers a missing scheme", () => { @@ -16,6 +92,29 @@ describe("parseHttpUrl", () => { }); }); +describe("safeExternalHref", () => { + it("passes http(s) and nothing else", () => { + expect(safeExternalHref("https://example.com/a?b=1")).toBe("https://example.com/a?b=1"); + for (const hostile of [ + "javascript:alert(1)", + "JaVaScRiPt:alert(1)", + " javascript:alert(1)", + "data:text/html,", + "vbscript:msgbox(1)", + "file:///etc/passwd", + "blob:https://example.com/uuid", + "", + ]) { + expect(safeExternalHref(hostile), hostile).toBeNull(); + } + }); + + it("never yields a script-scheme href even when a scheme-less prefix parses", () => { + const href = safeExternalHref("javascript://example.com/%0aalert(1)"); + expect(href).toBeNull(); + }); +}); + describe("isLoneUrl", () => { it("is true only for a single bare URL", () => { expect(isLoneUrl("https://a.com")).toBe(true); @@ -25,25 +124,209 @@ describe("isLoneUrl", () => { }); }); -describe("classifyUrl", () => { - it("maps YouTube watch / short / youtu.be to a nocookie embed", () => { +describe("classifyUrl — provider mapping", () => { + it("maps each provider's canonical URL to its exact embed URL", () => { + for (const kind of PROVIDER_KINDS) { + const { url, embedUrl } = CANONICAL[kind]; + const c = classifyUrl(url); + expect(c?.kind, url).toBe(kind); + expect(c?.embedUrl, url).toBe(embedUrl); + } + }); + + it("maps YouTube watch / short / youtu.be / shorts / live to a nocookie embed", () => { for (const u of [ - "https://www.youtube.com/watch?v=dQw4w9WgXcQ", - "https://youtu.be/dQw4w9WgXcQ", - "https://www.youtube.com/embed/dQw4w9WgXcQ", - "https://youtube.com/shorts/dQw4w9WgXcQ", + `https://www.youtube.com/watch?v=${YT}`, + `https://youtu.be/${YT}`, + `https://www.youtube.com/embed/${YT}`, + `https://youtube.com/shorts/${YT}`, + `https://m.youtube.com/live/${YT}`, + `https://www.youtube.com/watch?list=PL123&v=${YT}&feature=share`, + `HTTPS://WWW.YouTube.COM/watch?v=${YT}`, + `youtube.com/watch?v=${YT}`, ]) { const c = classifyUrl(u); - expect(c?.kind).toBe(EmbedKind.YouTube); - expect(c?.embedUrl).toBe("https://www.youtube-nocookie.com/embed/dQw4w9WgXcQ"); + expect(c?.kind, u).toBe(EmbedKind.YouTube); + expect(c?.embedUrl, u).toBe(`https://www.youtube-nocookie.com/embed/${YT}`); + } + }); + + it("carries a YouTube start time as a re-serialised integer, nothing else", () => { + expect(classifyUrl(`https://youtu.be/${YT}?t=90s&si=track`)?.embedUrl).toBe( + `https://www.youtube-nocookie.com/embed/${YT}?start=90`, + ); + expect(classifyUrl(`https://youtu.be/${YT}?t=1m30s`)?.embedUrl).toBe( + `https://www.youtube-nocookie.com/embed/${YT}`, + ); + expect(classifyUrl(`https://youtu.be/${YT}?t=9"onload="x`)?.embedUrl).toBe( + `https://www.youtube-nocookie.com/embed/${YT}`, + ); + }); + + it("keeps a Vimeo unlisted hash and always opts out of tracking", () => { + expect(classifyUrl("https://vimeo.com/123456789/abcdef0123")?.embedUrl).toBe( + "https://player.vimeo.com/video/123456789?h=abcdef0123&dnt=1", + ); + expectBookmark("https://vimeo.com/channels/staffpicks"); + expectBookmark("https://vimeo.com/123456789/not-a-hash!"); + }); + + it("maps Google Maps place / search / viewport / ?q URLs to the keyless embed", () => { + const cases: Array<[string, string]> = [ + [ + "https://www.google.com/maps/search/coffee+near+me/@52.52,13.405,14z", + "https://www.google.com/maps?q=coffee+near+me&z=14&output=embed", + ], + [ + "https://www.google.com/maps/@52.52,13.405,12.5z", + "https://www.google.com/maps?q=52.52%2C13.405&z=12&output=embed", + ], + [ + "https://maps.google.com/?q=Brandenburg%20Gate", + "https://www.google.com/maps?q=Brandenburg+Gate&z=15&output=embed", + ], + [ + "https://maps.google.com/maps?q=1600+Amphitheatre+Parkway", + "https://www.google.com/maps?q=1600+Amphitheatre+Parkway&z=15&output=embed", + ], + ]; + for (const [url, embedUrl] of cases) { + const c = classifyUrl(url); + expect(c?.kind, url).toBe(EmbedKind.GoogleMaps); + expect(c?.embedUrl, url).toBe(embedUrl); + } + }); + + it("leaves Google Maps short links, directions and non-map Google pages as bookmarks", () => { + for (const u of [ + "https://maps.app.goo.gl/AbCdEf123", + "https://goo.gl/maps/AbCdEf123", + "https://www.google.com/maps/dir/Berlin/Hamburg", + "https://www.google.com/maps", + "https://www.google.com/search?q=maps", + "https://www.google.com/url?q=https://evil.example", + "https://www.google.com/maps/../url?q=https://evil.example", + "https://www.google.com/maps/@999,999,15z", + "https://www.google.de/maps/place/Berlin", + "https://docs.google.com/maps/place/Berlin", + ]) { + expectBookmark(u); + } + }); + + it("neutralises a hostile Google Maps query into one encoded q value", () => { + const c = classifyUrl( + "https://www.google.com/maps/place/x%26output%3Dsvembed%26pb%3Devil%23frag%0d%0aSet-Cookie:a/@1,2,3z", + ); + expect(c?.kind).toBe(EmbedKind.GoogleMaps); + const embed = new URL(c?.embedUrl ?? ""); + expect(embed.origin).toBe("https://www.google.com"); + expect(embed.pathname).toBe("/maps"); + expect([...embed.searchParams.keys()]).toEqual(["q", "z", "output"]); + expect(embed.searchParams.get("output")).toBe("embed"); + expect(embed.searchParams.get("q")).toBe("x&output=svembed&pb=evil#fragSet-Cookie:a"); + expect(embed.hash).toBe(""); + expectBookmark(`https://www.google.com/maps/place/${"a".repeat(201)}`); + expectBookmark("https://www.google.com/maps/place/%E0%A4%A"); + }); + + it("maps OpenStreetMap map-hash / marker / embed URLs and rejects the rest", () => { + const hashOnly = classifyUrl("https://www.openstreetmap.org/#map=12/48.8566/2.3522"); + expect(hashOnly?.kind).toBe(EmbedKind.OpenStreetMap); + const box = new URL(hashOnly?.embedUrl ?? "").searchParams.get("bbox")?.split(",").map(Number); + expect(box).toHaveLength(4); + const [minLon, minLat, maxLon, maxLat] = box as [number, number, number, number]; + expect(minLon).toBeLessThan(2.3522); + expect(maxLon).toBeGreaterThan(2.3522); + expect(minLat).toBeLessThan(48.8566); + expect(maxLat).toBeGreaterThan(48.8566); + expect(new URL(hashOnly?.embedUrl ?? "").searchParams.has("marker")).toBe(false); + + expect( + classifyUrl("https://www.openstreetmap.org/export/embed.html?bbox=13.3,52.4,13.5,52.6&x=y") + ?.embedUrl, + ).toBe( + "https://www.openstreetmap.org/export/embed.html?bbox=13.3%2C52.4%2C13.5%2C52.6&layer=mapnik", + ); + + const polar = classifyUrl("https://openstreetmap.org/#map=0/89.9/179.9"); + const polarBox = new URL(polar?.embedUrl ?? "").searchParams.get("bbox")?.split(",").map(Number); + for (const [i, limit] of [180, 85.0511, 180, 85.0511].entries()) { + expect(Math.abs(polarBox?.[i] ?? Number.NaN)).toBeLessThanOrEqual(limit); + } + + for (const u of [ + "https://www.openstreetmap.org/", + "https://www.openstreetmap.org/node/123456", + "https://www.openstreetmap.org/#map=12/abc/def", + "https://www.openstreetmap.org/#map=12/91/0", + "https://www.openstreetmap.org/export/embed.html?bbox=1,2,3", + "https://www.openstreetmap.org/export/embed.html?bbox=1,2,3,javascript:alert(1)", + "https://tile.openstreetmap.org/#map=12/48.8/2.3", + ]) { + expectBookmark(u); + } + }); + + it("sizes Spotify and SoundCloud by single item vs list", () => { + expect(classifyUrl(`https://open.spotify.com/track/${SPOTIFY_ID}`)?.aspect).toBe( + EmbedAspect.Audio, + ); + const playlist = classifyUrl(`https://open.spotify.com/intl-de/playlist/${SPOTIFY_ID}`); + expect(playlist?.aspect).toBe(EmbedAspect.AudioList); + expect(playlist?.embedUrl).toBe(`https://open.spotify.com/embed/playlist/${SPOTIFY_ID}`); + expect(classifyUrl("https://soundcloud.com/some-artist/sets/an-album")?.aspect).toBe( + EmbedAspect.AudioList, + ); + expect(classifyUrl("https://soundcloud.com/some-artist/sets/an-album")?.embedUrl).toContain( + "url=https%3A%2F%2Fsoundcloud.com%2Fsome-artist%2Fsets%2Fan-album&", + ); + }); + + it("rejects provider URLs whose shape or id is not the expected one", () => { + for (const u of [ + "https://www.youtube.com/watch?v=short", + `https://www.youtube.com/watch?v=${YT}%22%3E%3Cscript%3E`, + `https://www.youtube.com/embed/${YT}/extra`, + "https://www.youtube.com/redirect?q=https://evil.example", + "https://www.youtube.com/@some-channel", + "https://www.loom.com/share/../../evil", + "https://www.loom.com/looms/videos", + "https://www.figma.com/", + "https://www.figma.com/community/file/123456", + "https://www.figma.com/file/a%2F..%2F..%2Fevil/x", + "https://codesandbox.io/s/a/b/c", + "https://codesandbox.io/dashboard", + `https://open.spotify.com/track/${SPOTIFY_ID}x`, + `https://open.spotify.com/user/${SPOTIFY_ID}`, + `https://open.spotify.com/track/${SPOTIFY_ID}/extra`, + "https://soundcloud.com/discover/sets", + "https://soundcloud.com/some-artist", + "https://soundcloud.com/some-artist/sets", + "https://soundcloud.com/some-artist/some%2Ftrack", + "https://codepen.io/some-user", + "https://codepen.io/some-user/pens/public", + "https://codepen.io/some-user/pen/ab { - expect(classifyUrl("https://vimeo.com/123456789")?.kind).toBe(EmbedKind.Vimeo); - expect(classifyUrl("https://www.loom.com/share/abc123")?.kind).toBe(EmbedKind.Loom); - expect(classifyUrl("https://www.figma.com/file/abc/Design")?.kind).toBe(EmbedKind.Figma); - expect(classifyUrl("https://codesandbox.io/s/cool-sandbox")?.kind).toBe(EmbedKind.CodeSandbox); + it("keeps script-widget providers (X, Gist) and the never-list as bookmark cards", () => { + for (const u of [ + "https://x.com/someone/status/1234567890123456789", + "https://twitter.com/someone/status/1234567890123456789", + "https://platform.twitter.com/embed/Tweet.html?id=1234567890123456789", + "https://gist.github.com/someone/0123456789abcdef0123456789abcdef", + "https://gist.github.com/someone/0123456789abcdef0123456789abcdef.js", + "https://www.instagram.com/p/AbCdEf/", + "https://www.facebook.com/someone/posts/123", + ]) { + expectBookmark(u); + } }); it("falls back to a bookmark for everything else", () => { @@ -62,6 +345,178 @@ describe("classifyUrl", () => { }); }); +describe("classifyUrl — host allowlist attacks", () => { + const watch = `/watch?v=${YT}`; + + it("never embeds a lookalike, suffix, prefix or subdomain host", () => { + for (const host of [ + "youtube.com.evil.example", + "www.youtube.com.evil.example", + "evilyoutube.com", + "notyoutube.com", + "youtube.com-evil.example", + "youtube.co", + "youtube.com.", + "www.youtube.com.", + "evil.www.youtube.com", + "attacker.youtube.com", + "youtube-nocookie.com.evil.example", + "xn--yutube-wqf.com", + "yоutube.com", + "youtubе.com", + "www.youtube.com%2eevil.example", + "127.0.0.1", + "[::1]", + ]) { + expectBookmark(`https://${host}${watch}`); + } + for (const u of [ + "https://evil.figma.com/file/AbCdEf0123456789abcdef/x", + "https://figma.com.evil.example/file/AbCdEf0123456789abcdef/x", + "https://evilvimeo.com/123456789", + "https://codesandbox.io.evil.example/s/abc", + "https://www.google.com.evil.example/maps/place/Berlin", + "https://maps.google.com.evil.example/?q=Berlin", + "https://openstreetmap.org.evil.example/#map=12/48.8/2.3", + `https://open.spotify.com.evil.example/track/${SPOTIFY_ID}`, + `https://evil-open.spotify.com/track/${SPOTIFY_ID}`, + "https://soundcloud.com.evil.example/a/b", + "https://codepen.io.evil.example/u/pen/abcXYZ", + "https://miro.com.evil.example/app/board/uXjVO1a2b3c=/", + ]) { + expectBookmark(u); + } + }); + + it("never embeds through userinfo, a port, or plain http", () => { + for (const u of [ + `https://www.youtube.com@evil.example${watch}`, + `https://www.youtube.com:pw@evil.example${watch}`, + `https://user@www.youtube.com${watch}`, + `https://user:pass@www.youtube.com${watch}`, + `https://evil.example\\@www.youtube.com${watch}`, + `https://evil.example#@www.youtube.com${watch}`, + `https://evil.example?@www.youtube.com${watch}`, + `https://www.youtube.com:8443${watch}`, + `http://www.youtube.com${watch}`, + `http://youtu.be/${YT}`, + `//evil.example/www.youtube.com${watch}`, + ]) { + expectBookmark(u); + } + }); + + it("never classifies a non-http scheme, however it is dressed", () => { + for (const u of [ + `javascript:alert(1)//www.youtube.com${watch}`, + `javascript://www.youtube.com${watch}%0aalert(1)`, + `data:text/html,