Skip to content

Build your personal knowledge base with Trilium Notes

fix(protected_session): re-encrypt becca when the session expires by timeout

Elian Doran93de6bfcc20b
diff --git a/packages/trilium-core/src/becca/becca_loader.spec.ts b/packages/trilium-core/src/becca/becca_loader.spec.ts
index dc055cdc9e..f446d57859 100644
--- a/packages/trilium-core/src/becca/becca_loader.spec.ts
+++ b/packages/trilium-core/src/becca/becca_loader.spec.ts
@@ -150,6 +150,35 @@ describe("becca_loader", () => {
});
});
+ describe("LEAVE_PROTECTED_SESSION listener", () => {
+ it("reloads becca from disk, purging decrypted titles from memory and the flat-text index", () => {
+ // Simulate the post-unlock state: a protected note whose plaintext title lives
+ // only in becca memory (on disk the title is ciphertext, here it has no row at all).
+ const note = new BNote();
+ note.updateFromRow({
+ noteId: "loaderLeaveProtected",
+ title: "swordfish plaintext",
+ isProtected: true,
+ type: "text",
+ mime: "text/html",
+ blobId: ""
+ });
+ note.init();
+ note.isDecrypted = true;
+
+ // The flat-text search index serves the plaintext title (this is the #10115
+ // leak surface: title-word search matching protected notes without a session).
+ expect(becca.getFlatTextIndex().flatTexts.some((text) => text.includes("swordfish"))).toBe(true);
+
+ getContext().init(() => eventService.emit(eventService.LEAVE_PROTECTED_SESSION));
+
+ // Full reload from disk: the memory-only decrypted state is discarded...
+ expect(becca.getNote("loaderLeaveProtected")).toBeNull();
+ // ...and the rebuilt search index no longer matches the plaintext.
+ expect(becca.getFlatTextIndex().flatTexts.some((text) => text.includes("swordfish"))).toBe(false);
+ });
+ });
+
describe("load()", () => {
it("loads etapi tokens into becca", () => {
const etapiTokenId = "loaderEtapiToken1";
diff --git a/packages/trilium-core/src/services/scheduler.spec.ts b/packages/trilium-core/src/services/scheduler.spec.ts
index 5ddc39fdef..86fd5e120b 100644
--- a/packages/trilium-core/src/services/scheduler.spec.ts
+++ b/packages/trilium-core/src/services/scheduler.spec.ts
@@ -4,6 +4,7 @@ import becca from "../becca/becca.js";
import { buildNote } from "../test/becca_easy_mocking.js";
import attributeService from "./attributes.js";
import config from "./config.js";
+import events from "./events.js";
import hiddenSubtreeService from "./hidden_subtree.js";
import options from "./options.js";
import protected_session from "./protected_session.js";
@@ -147,12 +148,16 @@ describe("scheduler", () => {
// Non-zero (the source guards on the date being truthy) but far in the past.
getLastProtectedSessionOperationDate.mockReturnValue(1);
getOptionInt.mockReturnValue(10); // 10s timeout
+ const emit = vi.spyOn(events, "emit").mockImplementation(() => {});
startScheduler();
await settleDbReady();
await vi.advanceTimersByTimeAsync(30 * SECOND);
expect(resetDataKey).toHaveBeenCalled();
+ // The event triggers the becca reload that re-encrypts in-memory titles;
+ // manual logout emits it too, and the two paths must stay symmetric.
+ expect(emit).toHaveBeenCalledWith(events.LEAVE_PROTECTED_SESSION);
expect(reloadFrontend).toHaveBeenCalledWith(expect.any(String));
});
diff --git a/packages/trilium-core/src/services/scheduler.ts b/packages/trilium-core/src/services/scheduler.ts
index b4e4b900dc..cb59f4befa 100644
--- a/packages/trilium-core/src/services/scheduler.ts
+++ b/packages/trilium-core/src/services/scheduler.ts
@@ -2,6 +2,7 @@ import type BNote from "../becca/entities/bnote.js";
import attributeService from "../services/attributes.js";
import config from "./config.js";
import * as cls from "./context.js";
+import events from "./events.js";
import hiddenSubtreeService from "./hidden_subtree.js";
import { getLog } from "./log.js";
import options from "./options.js";
@@ -69,7 +70,10 @@ export function startScheduler() {
7 * 3600 * 1000
);
- setInterval(() => checkProtectedSessionExpiration(), 30000);
+ setInterval(
+ cls.wrap(() => checkProtectedSessionExpiration()),
+ 30000
+ );
});
}
@@ -78,6 +82,10 @@ function checkProtectedSessionExpiration() {
const lastProtectedSessionOperationDate = protected_session.getLastProtectedSessionOperationDate();
if (protected_session.isProtectedSessionAvailable() && lastProtectedSessionOperationDate && Date.now() - lastProtectedSessionOperationDate > protectedSessionTimeout * 1000) {
protected_session.resetDataKey();
+ // Mirror logoutFromProtectedSession(): without this event, becca — and the flat-text
+ // search index derived from it — would keep the decrypted titles in memory, letting
+ // title-word searches match protected notes after the session ended.
+ events.emit(events.LEAVE_PROTECTED_SESSION);
getLog().info("Expiring protected session");
ws.reloadFrontend("leaving protected session");
}