Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
fix(sync): don't crash on notes whose mime hasn't synced yet
Elian Doranbace691a1572
diff --git a/apps/client/src/entities/fnote.spec.ts b/apps/client/src/entities/fnote.spec.tsindex 6ec00cc6d3..97e956a3ff 100644--- a/apps/client/src/entities/fnote.spec.ts+++ b/apps/client/src/entities/fnote.spec.ts@@ -1060,6 +1060,24 @@ describe("FNote system-note predicates", () => {expect(makeNote({ mime: "text/plain" }).isTriliumScript()).toBe(false);});+ it("mime predicates survive a note row that arrives without a mime", () => {+ const note = makeNote({ type: "code", mime: "application/javascript;env=backend" });+ expect(note.isTriliumScript()).toBe(true);++ // What froca_updater does with a ws payload: `note.update(ec.entity as FNoteRow)`. A server+ // that builds that payload from a becca skeleton note (a note referenced by a branch that+ // synced ahead of it) has `mime`/`title` undefined, and JSON.stringify drops the keys+ // entirely - so the cast hides a row that is missing them. froca is the last line of+ // defence here since it cannot tell such a row apart from a complete one.+ note.update({ noteId: note.noteId, blobId: "blob1", isProtected: false } as unknown as FNoteRow);++ expect(() => note.isTriliumScript()).not.toThrow();+ expect(note.isTriliumScript()).toBe(false);+ expect(note.isJavaScript()).toBe(false);+ expect(note.isTriliumSqlite()).toBe(false);+ expect(note.getScriptEnv()).toBeNull();+ });+it("getScriptEnv returns frontend / backend / null", () => {expect(makeNote({ type: "code", mime: "text/html" }).getScriptEnv()).toBe("frontend");expect(makeNote({ type: "code", mime: "application/javascript;env=frontend" }).getScriptEnv()).toBe("frontend");diff --git a/apps/client/src/entities/fnote.ts b/apps/client/src/entities/fnote.tsindex 7546b02a5c..78353e12cd 100644--- a/apps/client/src/entities/fnote.ts+++ b/apps/client/src/entities/fnote.ts@@ -96,7 +96,11 @@ export default class FNote {this.isProtected = !!row.isProtected;this.type = row.type;- this.mime = row.mime;+ // The server can send a row without a mime: becca materialises "skeleton" notes for+ // entities that arrive out of order during sync, and their undefined mime/title are dropped+ // outright by JSON serialisation. Defaulting keeps the mime predicates (isTriliumScript(),+ // isJavaScript(), ...) from throwing until the real row syncs in.+ this.mime = row.mime ?? "";this.blobId = row.blobId;}diff --git a/apps/server/src/services/utils.ts b/apps/server/src/services/utils.tsindex 6cf8cfe11b..37dc7dfdc9 100644--- a/apps/server/src/services/utils.ts+++ b/apps/server/src/services/utils.ts@@ -85,7 +85,7 @@ export function getContentDisposition(filename: string) {}/** @deprecated */-export function isStringNote(type: string | undefined, mime: string) {+export function isStringNote(type: string | undefined, mime: string | undefined) {return coreUtils.isStringNote(type, mime);}diff --git a/packages/trilium-core/src/services/sync_skeleton_notes.spec.ts b/packages/trilium-core/src/services/sync_skeleton_notes.spec.tsnew file mode 100644index 0000000000..810f76cb18--- /dev/null+++ b/packages/trilium-core/src/services/sync_skeleton_notes.spec.ts@@ -0,0 +1,187 @@+import type { EntityChange, EntityChangeRecord, EntityRow, NoteRow } from "@triliumnext/commons";+import type { Request } from "express";+import { beforeAll, describe, expect, it } from "vitest";++import becca from "../becca/becca.js";+import "../becca/becca_loader.js";+import treeRoute from "../routes/api/tree.js";+import * as cls from "./context.js";+import { initMessaging } from "./messaging/index.js";+import { getSql } from "./sql/index.js";+import syncUpdateService from "./sync_update.js";+import dateUtils from "./utils/date.js";+import { randomString } from "./utils/index.js";+import ws from "./ws.js";++/**+ * Entities arrive from a sync partner in entity-change-id order, so a branch (or an attribute) can+ * be applied before the `notes` row it points at. becca covers that by materialising a "skeleton"+ * `BNote` carrying nothing but a noteId (see `BBranch.childNote` / `BAttribute.init`), meant to be+ * filled in once the real row shows up.+ *+ * Until it is, that skeleton is a fully typed `BNote` whose `mime`, `type` and `title` are all+ * `undefined`, and the two frontend-facing payloads (the ws frontend-update and the tree API) copy+ * it out verbatim. `JSON.stringify` then drops the undefined values, so the client receives a note+ * row with no `mime` key at all and cannot tell it apart from a complete one.+ *+ * Consumers are therefore the ones that have to hold: `isStringNote()` treats an unknown mime as+ * binary and `FNote.update()` defaults it to `""`, so the predicates degrade instead of throwing+ * `Cannot read properties of undefined (reading 'startsWith')`. These tests pin both the leak (so+ * it is not mistaken for correct behaviour) and the guards that make it survivable.+ */+describe("sync: skeleton notes created by out-of-order entities", () => {+ beforeAll(() => {+ initMessaging(fakeMessagingProvider);+ ws.init();+ });++ it("keeps becca's note predicates working on a branch synced ahead of its note", () => {+ const { noteId } = syncBranchAheadOfItsNote();++ const skeleton = becca.getNoteOrThrow(noteId);+ // mime/type/title are all unknown at this point; predicates must degrade, not throw.+ expect(() => skeleton.hasStringContent()).not.toThrow();+ expect(skeleton.hasStringContent()).toBe(false);+ expect(skeleton.isJavaScript()).toBe(false);+ expect(skeleton.getScriptEnv()).toBeNull();+ });++ it("ships the skeleton in the ws frontend-update as a row whose mime key is dropped entirely", () => {+ const { noteId } = syncBranchAheadOfItsNote();+ // Any transaction that queues an entity change for that noteId before the note row arrives+ // broadcasts whatever becca holds - `fillInAdditionalProperties` reads becca first.+ const entityChangeId = insertNoteEntityChange(noteId);++ const message = cls.init(() => {+ cls.set("entityChangeIds", [entityChangeId]);+ ws.sendTransactionEntityChangesToAllClients();+ return sentMessages[sentMessages.length - 1];+ });++ if (message.type !== "frontend-update") throw new Error("expected a frontend-update message");+ const change = message.data.entityChanges.find((ec) => ec.entityName === "notes" && ec.entityId === noteId);++ // Known leak: the payload goes out half-filled. Serialisation is what makes it undetectable+ // client-side - the row is not `{ mime: undefined }`, the key is simply not there.+ expect(change?.entity).toBeTruthy();+ const serialized = JSON.parse(JSON.stringify(change?.entity));+ expect(serialized.noteId).toBe(noteId);+ expect("mime" in serialized).toBe(false);+ expect("title" in serialized).toBe(false);+ });++ it("serves the skeleton from the tree API as a row whose mime key is dropped entirely", () => {+ const { noteId } = syncBranchAheadOfItsNote();++ // froca does exactly this when a synced branch references a note it doesn't have yet - so+ // this is the row `FNote.update()` has to survive.+ const response = treeRoute.load({ body: { noteIds: [noteId] } } as Request);+ const noteRow = response.notes.find((note: NoteRow) => note.noteId === noteId);++ expect(noteRow).toBeTruthy();+ const serialized = JSON.parse(JSON.stringify(noteRow));+ expect(serialized.noteId).toBe(noteId);+ expect("mime" in serialized).toBe(false);+ expect("title" in serialized).toBe(false);+ });++ it("fills the skeleton in once the note row arrives in a later batch", () => {+ const { noteId } = syncBranchAheadOfItsNote();++ applySync([{ entityChange: buildEC({ entityName: "notes", entityId: noteId }), entity: remoteNoteRow(noteId) }]);++ const filled = becca.getNoteOrThrow(noteId);+ expect(filled.mime).toBe("application/javascript;env=backend");+ expect(filled.hasStringContent()).toBe(true);++ // And now it is servable.+ const response = treeRoute.load({ body: { noteIds: [noteId] } } as Request);+ expect(response.notes.find((note: NoteRow) => note.noteId === noteId)?.mime).toBe("application/javascript;env=backend");+ });+});++const REMOTE_INSTANCE_ID = "skeletonSpecRemote";++const sentMessages: any[] = [];++const fakeMessagingProvider = {+ sendMessageToAllClients: (message: any) => void sentMessages.push(message),+ sendMessageToClient: () => true,+ setClientMessageHandler: () => {}+};++let counter = 0;++/** Applies a sync batch containing only a branch, whose child note is not in becca yet. */+function syncBranchAheadOfItsNote() {+ counter++;+ const noteId = `skelSpecNote${counter}`;+ const branchId = `skelSpecBranch${counter}`;++ expect(becca.getNote(noteId)).toBeNull();+ applySync([{ entityChange: buildEC({ entityName: "branches", entityId: branchId }), entity: remoteBranchRow(branchId, noteId) }]);++ return { noteId, branchId };+}++function applySync(records: EntityChangeRecord[]) {+ return cls.init(() => syncUpdateService.updateEntities(records, REMOTE_INSTANCE_ID));+}++function buildEC(overrides: Partial<EntityChange>): EntityChange {+ return {+ entityName: "notes",+ entityId: "x",+ hash: `remote-hash-${randomString(6)}`,+ isErased: false,+ isSynced: true,+ changeId: randomString(12),+ utcDateChanged: "2099-01-01 00:00:00.000Z",+ ...overrides+ } as EntityChange;+}++function remoteBranchRow(branchId: string, noteId: string): EntityRow {+ const now = dateUtils.utcNowDateTime();++ return {+ branchId,+ noteId,+ parentNoteId: "root",+ notePosition: 10,+ prefix: null,+ isExpanded: 0,+ isDeleted: 0,+ utcDateModified: now+ } as unknown as EntityRow;+}++function remoteNoteRow(noteId: string): EntityRow {+ const now = dateUtils.utcNowDateTime();++ return {+ noteId,+ title: "arrived late",+ isProtected: 0,+ type: "code",+ mime: "application/javascript;env=backend",+ blobId: null,+ isDeleted: 0,+ dateCreated: now,+ dateModified: now,+ utcDateCreated: now,+ utcDateModified: now+ } as unknown as EntityRow;+}++function insertNoteEntityChange(noteId: string): number {+ return cls.init(() => {+ getSql().execute(+ /*sql*/`INSERT INTO entity_changes (entityName, entityId, hash, isErased, changeId, componentId, instanceId, isSynced, utcDateChanged)+ VALUES ('notes', ?, 'hash', 0, ?, 'NA', ?, 1, ?)`,+ [noteId, randomString(12), REMOTE_INSTANCE_ID, dateUtils.utcNowDateTime()]+ );++ return getSql().getValue<number>("SELECT last_insert_rowid()") ?? 0;+ });+}diff --git a/packages/trilium-core/src/services/utils/index.ts b/packages/trilium-core/src/services/utils/index.tsindex e8b81199da..ec6e69f5fe 100644--- a/packages/trilium-core/src/services/utils/index.ts+++ b/packages/trilium-core/src/services/utils/index.ts@@ -26,8 +26,13 @@ export function md5(content: string | Uint8Array) {return Array.from(bytes).map((b) => b.toString(16).padStart(2, "0")).join("");}-export function isStringNote(type: string | undefined, mime: string) {- return (type && STRING_NOTE_TYPES.has(type)) || mime.startsWith("text/") || STRING_MIME_TYPES.has(mime);+/**+ * `mime` is optional because becca materialises "skeleton" notes for entities that arrive out of+ * order during sync (see `BBranch.childNote`), and those carry neither a type nor a mime until the+ * real row lands. Such a note is reported as binary rather than crashing the caller.+ */+export function isStringNote(type: string | undefined, mime: string | undefined) {+ return (type && STRING_NOTE_TYPES.has(type)) || (!!mime && (mime.startsWith("text/") || STRING_MIME_TYPES.has(mime)));}export function randomString(length: number) {