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(etapi): mask protected note titles in the note mapper
Elian Doranc5502f4ea228
diff --git a/apps/server/spec/etapi/get-note-protected.spec.ts b/apps/server/spec/etapi/get-note-protected.spec.tsnew file mode 100644index 0000000000..ceac333292--- /dev/null+++ b/apps/server/spec/etapi/get-note-protected.spec.ts@@ -0,0 +1,61 @@+import { becca, cls, events, password_encryption, protected_session } from "@triliumnext/core";+import { Application } from "express";+import supertest from "supertest";+import { afterAll, beforeAll, describe, expect, it } from "vitest";+import config from "../../src/services/config.js";+import { createNote, login } from "./utils.js";++let app: Application;+let token: string;++const USER = "etapi";+const PLAINTEXT_TITLE = "Bank account recovery codes";++describe("etapi/get-note protected", () => {+ let noteId: string;++ beforeAll(async () => {+ config.General.noAuthentication = false;+ const buildApp = (await import("../../src/app.js")).default;+ app = await buildApp();+ token = await login(app);+ noteId = await createNote(app, token);++ // Protect the note the way the UI does: unlock, flip the flag so the title is+ // written back encrypted, then leave the protected session. The LEAVE event+ // reloads becca, which is what drops the decrypted title from memory.+ const dataKey = await password_encryption.getDataKey("demo1234");+ if (!(dataKey instanceof Uint8Array)) {+ throw new Error("Expected a data key from the fixture password.");+ }++ cls.init(() => {+ protected_session.default.setDataKey(dataKey);++ const note = becca.getNoteOrThrow(noteId);+ note.title = PLAINTEXT_TITLE;+ note.isProtected = true;+ note.save();++ protected_session.resetDataKey();+ events.emit(events.LEAVE_PROTECTED_SESSION);+ });+ });++ afterAll(() => {+ protected_session.resetDataKey();+ });++ it("does not disclose the protected title while the protected session is locked", async () => {+ expect(protected_session.default.isProtectedSessionAvailable()).toBe(false);++ const response = await supertest(app)+ .get(`/etapi/notes/${noteId}`)+ .auth(USER, token, { type: "basic" })+ .expect(200);++ expect(response.body.isProtected).toBe(true);+ expect(response.body.title).toStrictEqual("[protected]");+ expect(response.body.title).not.toContain(PLAINTEXT_TITLE);+ });+});diff --git a/apps/server/src/assets/etapi.openapi.yaml b/apps/server/src/assets/etapi.openapi.yamlindex 99e5a98c70..6df4607c4c 100644--- a/apps/server/src/assets/etapi.openapi.yaml+++ b/apps/server/src/assets/etapi.openapi.yaml@@ -1081,6 +1081,9 @@ components:readOnly: truetitle:type: string+ description: |+ Title of the note. Returned as "[protected]" for protected notes, since ETAPI+ cannot open a protected session.type:type: stringenum:diff --git a/apps/server/src/etapi/mappers.ts b/apps/server/src/etapi/mappers.tsindex c6c9c15b0f..e418cbcfef 100644--- a/apps/server/src/etapi/mappers.ts+++ b/apps/server/src/etapi/mappers.ts@@ -4,7 +4,7 @@ function mapNoteToPojo(note: BNote) {return {noteId: note.noteId,isProtected: note.isProtected,- title: note.title,+ title: note.getTitleOrProtected(),type: note.type,mime: note.mime,blobId: note.blobId,