Skip to content

Build your personal knowledge base with Trilium Notes

fix(core): don't crash startup when hidden subtree contains protected notes (#10549)

Elian Dorane2b28f378a20
diff --git a/apps/server/src/services/hidden_subtree.spec.ts b/apps/server/src/services/hidden_subtree.spec.ts
index 08f69d3fc2..736a60dd06 100644
--- a/apps/server/src/services/hidden_subtree.spec.ts
+++ b/apps/server/src/services/hidden_subtree.spec.ts
@@ -1,10 +1,9 @@
import { deferred, LOCALES } from "@triliumnext/commons";
-import { becca_loader, hidden_subtree as hiddenSubtreeService, i18n, note_service as notes } from "@triliumnext/core";
+import { becca, becca_loader, binary_utils, cls, hidden_subtree as hiddenSubtreeService, i18n, note_service as notes, TaskContext } from "@triliumnext/core";
+import protectedSessionService from "@triliumnext/core/src/services/protected_session.js";
import { beforeAll, describe, expect, it } from "vitest";
-import { becca } from "@triliumnext/core";
import branches from "./branches.js";
-import { cls } from "@triliumnext/core";
import sql_init from "./sql_init.js";
describe("Hidden Subtree", () => {
@@ -190,6 +189,38 @@ describe("Hidden Subtree", () => {
expect(llmNote).toBeFalsy();
});
+ it("leaves the content of a protected template alone when no protected session is available (#10549)", () => {
+ const dataKey = binary_utils.encodeUtf8("0123456789abcdef"); // exactly 16 bytes
+ const note = becca.getNoteOrThrow("_template_presentation_first");
+ const originalContent = note.getContent();
+ expect(originalContent).toBeTruthy();
+
+ // Simulate a user who protected the hidden template while a protected session was active...
+ protectedSessionService.setDataKey(dataKey);
+ try {
+ cls.init(() => notes.protectNoteRecursively(note, true, false, new TaskContext("test-protect", "protectNotes", { protect: true })));
+ expect(note.isProtected).toBe(true);
+
+ // ...and then the server restarts without a protected session — the boot state of #10549.
+ protectedSessionService.resetDataKey();
+ expect(note.isContentAvailable()).toBe(false);
+
+ // The check must neither throw (it used to crash the server on startup) nor touch the content.
+ expect(() => cls.init(() => hiddenSubtreeService.checkHiddenSubtree(true))).not.toThrow();
+
+ // With the session restored, the content still decrypts to the original.
+ protectedSessionService.setDataKey(dataKey);
+ expect(note.getContent()).toBe(originalContent);
+
+ // Clean up: unprotect the note again so the remaining tests see the default state.
+ cls.init(() => notes.protectNoteRecursively(note, false, false, new TaskContext("test-unprotect", "protectNotes", { protect: false })));
+ expect(note.isProtected).toBe(false);
+ expect(note.getContent()).toBe(originalContent);
+ } finally {
+ protectedSessionService.resetDataKey();
+ }
+ });
+
it("fixes attribute of wrong type", () => {
const template = becca.getNoteOrThrow("_template_table");
cls.init(() => {
diff --git a/packages/trilium-core/src/services/hidden_subtree.ts b/packages/trilium-core/src/services/hidden_subtree.ts
index 90fc215d9e..de67b7886e 100644
--- a/packages/trilium-core/src/services/hidden_subtree.ts
+++ b/packages/trilium-core/src/services/hidden_subtree.ts
@@ -439,9 +439,22 @@ function checkHiddenSubtreeRecursively(parentNoteId: string, item: HiddenSubtree
// Existing item, check if it's in the right state.
branch = note.getParentBranches().find((branch) => branch.parentNoteId === parentNoteId);
- if (item.content && note.getContent() !== item.content) {
- log.info(`Updating content of ${item.id}.`);
- note.setContent(item.content);
+ if (item.content && !note.isContentAvailable()) {
+ // The note was protected by the user. Without a protected session the content can neither be
+ // read (getContent() returns "") nor written — attempting to write would throw and take down
+ // the whole hidden subtree check (see #10549), so leave the content alone.
+ log.info(`Skipping content update of ${item.id} since it is protected and no protected session is available.`);
+ } else if (item.content) {
+ try {
+ if (note.getContent() !== item.content) {
+ log.info(`Updating content of ${item.id}.`);
+ note.setContent(item.content);
+ }
+ } catch (e) {
+ // Unreadable content of a single built-in note (e.g. a missing blob row in a damaged
+ // database) must not abort — and thereby roll back — the entire hidden subtree check.
+ log.error(`Failed to update content of ${item.id}: ${e instanceof Error ? (e.stack ?? e.message) : String(e)}`);
+ }
}
if (item.enforceBranches || item.id.startsWith("_help")) {
diff --git a/packages/trilium-core/src/services/one_time_timer.spec.ts b/packages/trilium-core/src/services/one_time_timer.spec.ts
index 63949e9c24..c659ffa666 100644
--- a/packages/trilium-core/src/services/one_time_timer.spec.ts
+++ b/packages/trilium-core/src/services/one_time_timer.spec.ts
@@ -67,6 +67,25 @@ describe("oneTimeTimer", () => {
expect(cbB).toHaveBeenCalledTimes(1);
});
+ it("contains a throwing callback and frees the name for rescheduling", () => {
+ const boom = vi.fn(() => {
+ throw new Error("callback failure");
+ });
+ const next = vi.fn();
+
+ oneTimeTimer.scheduleExecution("throwing", 100, boom);
+
+ // The throw must not escape the timer callback — an uncaught exception here
+ // would crash the whole process (#10549).
+ expect(() => vi.advanceTimersByTime(100)).not.toThrow();
+ expect(boom).toHaveBeenCalledTimes(1);
+
+ // The failure still releases the slot so the task can run again later.
+ oneTimeTimer.scheduleExecution("throwing", 100, next);
+ vi.advanceTimersByTime(100);
+ expect(next).toHaveBeenCalledTimes(1);
+ });
+
it("allows re-scheduling the same name from within the callback", () => {
const inner = vi.fn();
const outer = vi.fn(() => {
diff --git a/packages/trilium-core/src/services/one_time_timer.ts b/packages/trilium-core/src/services/one_time_timer.ts
index f2f514568a..d06e2c84a7 100644
--- a/packages/trilium-core/src/services/one_time_timer.ts
+++ b/packages/trilium-core/src/services/one_time_timer.ts
@@ -1,3 +1,5 @@
+import { getLog } from "./log.js";
+
const scheduledExecutions: Record<string, boolean> = {};
/**
@@ -16,7 +18,19 @@ function scheduleExecution(name: string, milliseconds: number, cb: () => void) {
setTimeout(() => {
delete scheduledExecutions[name];
- cb();
+ try {
+ cb();
+ } catch (e: unknown) {
+ // A synchronous throw here would surface as an uncaughtException and kill the process
+ // (see #10549), so contain it — a scheduled maintenance task must never take the app down.
+ const message = `Scheduled execution '${name}' failed: ${e instanceof Error ? (e.stack ?? e.message) : String(e)}`;
+ try {
+ getLog().error(message);
+ } catch {
+ // The log service may not be initialized yet; this handler must never throw.
+ console.error(message);
+ }
+ }
}, milliseconds);
}