Skip to content

Build your personal knowledge base with Trilium Notes

fix(etapi): mask protected attachment titles in the attachment mapper

Elian Doran70c5902ea6a6
diff --git a/apps/server/spec/etapi/get-note-protected.spec.ts b/apps/server/spec/etapi/get-note-protected.spec.ts
deleted file mode 100644
index ceac333292..0000000000
--- a/apps/server/spec/etapi/get-note-protected.spec.ts
+++ /dev/null
@@ -1,61 +0,0 @@
-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/spec/etapi/protected-metadata.spec.ts b/apps/server/spec/etapi/protected-metadata.spec.ts
new file mode 100644
index 0000000000..333f20efc0
--- /dev/null
+++ b/apps/server/spec/etapi/protected-metadata.spec.ts
@@ -0,0 +1,120 @@
+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";
+const ATTACHMENT_TITLE = "passport-scan.png";
+
+describe("etapi protected metadata", () => {
+ let noteId: string;
+ let attachmentId: 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);
+
+ const attachmentResponse = await supertest(app)
+ .post("/etapi/attachments")
+ .auth(USER, token, { type: "basic" })
+ .send({
+ ownerId: noteId,
+ role: "file",
+ mime: "image/png",
+ title: ATTACHMENT_TITLE,
+ position: 10,
+ content: "not a real png"
+ })
+ .expect(201);
+ attachmentId = attachmentResponse.body.attachmentId;
+ expect(attachmentId).toBeTruthy();
+
+ const dataKey = await password_encryption.getDataKey("demo1234");
+ if (!(dataKey instanceof Uint8Array)) {
+ throw new Error("Expected a data key from the fixture password.");
+ }
+
+ protectFixture(dataKey, () => {
+ const note = becca.getNoteOrThrow(noteId);
+ note.title = PLAINTEXT_TITLE;
+ note.isProtected = true;
+ note.save();
+
+ // `protectNote()` flips the owner note and its attachments together, so a
+ // protected note never has unprotected attachments in practice.
+ for (const attachment of note.getAttachments()) {
+ attachment.isProtected = true;
+ attachment.save();
+ }
+ });
+ });
+
+ afterAll(() => {
+ protected_session.resetDataKey();
+ });
+
+ it("does not disclose the protected note 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);
+ });
+
+ it("does not disclose protected attachment titles when listing a note's attachments", async () => {
+ expect(protected_session.default.isProtectedSessionAvailable()).toBe(false);
+
+ const response = await supertest(app)
+ .get(`/etapi/notes/${noteId}/attachments`)
+ .auth(USER, token, { type: "basic" })
+ .expect(200);
+
+ const attachment = response.body.find((a: { attachmentId: string }) => a.attachmentId === attachmentId);
+ expect(attachment).toBeTruthy();
+ expect(attachment.title).toStrictEqual("[protected]");
+ expect(attachment.title).not.toContain(ATTACHMENT_TITLE);
+ });
+
+ it("does not disclose a protected attachment title when read by id", async () => {
+ expect(protected_session.default.isProtectedSessionAvailable()).toBe(false);
+
+ const response = await supertest(app)
+ .get(`/etapi/attachments/${attachmentId}`)
+ .auth(USER, token, { type: "basic" })
+ .expect(200);
+
+ expect(response.body.title).toStrictEqual("[protected]");
+ expect(response.body.title).not.toContain(ATTACHMENT_TITLE);
+ });
+});
+
+/**
+ * Runs `fn` with the protected session unlocked, then locks it again the way
+ * `logoutFromProtectedSession()` does. The LEAVE event reloads becca, which is what
+ * drops the decrypted note title from memory.
+ */
+function protectFixture(dataKey: Uint8Array, fn: () => void) {
+ cls.init(() => {
+ protected_session.default.setDataKey(dataKey);
+ try {
+ fn();
+ } finally {
+ protected_session.resetDataKey();
+ events.emit(events.LEAVE_PROTECTED_SESSION);
+ }
+ });
+}
diff --git a/apps/server/src/assets/etapi.openapi.yaml b/apps/server/src/assets/etapi.openapi.yaml
index 6df4607c4c..fd53734a60 100644
--- a/apps/server/src/assets/etapi.openapi.yaml
+++ b/apps/server/src/assets/etapi.openapi.yaml
@@ -1184,6 +1184,9 @@ components:
type: string
title:
type: string
+ description: |
+ Title of the attachment. Returned as "[protected]" for protected attachments,
+ since ETAPI cannot open a protected session.
position:
type: integer
format: int32
diff --git a/apps/server/src/etapi/mappers.ts b/apps/server/src/etapi/mappers.ts
index e418cbcfef..0740a85bb0 100644
--- a/apps/server/src/etapi/mappers.ts
+++ b/apps/server/src/etapi/mappers.ts
@@ -51,7 +51,7 @@ function mapAttachmentToPojo(attachment: BAttachment) {
ownerId: attachment.ownerId,
role: attachment.role,
mime: attachment.mime,
- title: attachment.title,
+ title: attachment.getTitleOrProtected(),
position: attachment.position,
blobId: attachment.blobId,
dateModified: attachment.dateModified,