Skip to content

Build your personal knowledge base with Trilium Notes

fix(search): re-index protected note titles on unlock so search finds them (closes #10406)

Elian Doranfe94e9fc6d04
diff --git a/packages/trilium-core/src/becca/entities/bnote.ts b/packages/trilium-core/src/becca/entities/bnote.ts
index 0b8e474706..d0c1b77065 100644
--- a/packages/trilium-core/src/becca/entities/bnote.ts
+++ b/packages/trilium-core/src/becca/entities/bnote.ts
@@ -1519,6 +1519,10 @@ class BNote extends AbstractBeccaEntity<BNote> {
try {
this.title = protectedSessionService.decryptString(this.title) || "";
this.__flatTextCache = null;
+ // The pre-built flat text search index still holds this note's encrypted
+ // title, so schedule a refresh — otherwise the note stays unsearchable by
+ // title even after the protected session is unlocked (issue #10406).
+ this.becca.dirtyNoteFlatText(this.noteId);
this.isDecrypted = true;
} catch (e: any) {
diff --git a/packages/trilium-core/src/becca/entities/bnote_content.spec.ts b/packages/trilium-core/src/becca/entities/bnote_content.spec.ts
index 48136e3e2c..85c98c8fcf 100644
--- a/packages/trilium-core/src/becca/entities/bnote_content.spec.ts
+++ b/packages/trilium-core/src/becca/entities/bnote_content.spec.ts
@@ -428,6 +428,28 @@ describe("BNote content / misc getters", () => {
expect(note.title).toBe("");
expect(note.isDecrypted).toBe(true);
});
+
+ it("refreshes the flat text search index so a decrypted note is searchable by title (issue #10406)", () => {
+ protectedSessionService.setDataKey(PROTECTED_KEY);
+ const note = createNote({ isProtected: true });
+
+ const cipher = protectedSessionService.encrypt("topsecret");
+ note.title = cipher ?? "";
+ note.isDecrypted = false;
+ note.invalidateThisCache();
+
+ // Build the index while the title is still encrypted — it must not contain the plaintext yet.
+ const idxBefore = becca.getFlatTextIndex().noteIdToIdx.get(note.noteId);
+ expect(idxBefore).toBeDefined();
+ expect(becca.getFlatTextIndex().flatTexts[idxBefore ?? -1]).not.toContain("topsecret");
+
+ note.decrypt();
+ expect(note.isDecrypted).toBe(true);
+
+ // decrypt() must schedule an index refresh; after it the plaintext title is indexed.
+ const idxAfter = becca.getFlatTextIndex().noteIdToIdx.get(note.noteId);
+ expect(becca.getFlatTextIndex().flatTexts[idxAfter ?? -1]).toContain("topsecret");
+ });
});
describe("saveRevision / eraseExcessRevisionSnapshots (lines 1551-1620)", () => {