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(share): do not serve protected notes or their attachments publicly
Elian Doran75a2be00f2a6
diff --git a/apps/server/src/share/routes.spec.ts b/apps/server/src/share/routes.spec.tsindex 67703028e4..4c0bc2657a 100644--- a/apps/server/src/share/routes.spec.ts+++ b/apps/server/src/share/routes.spec.ts@@ -59,6 +59,37 @@ describe("Share API test", () => {expect(cannotSetHeadersCount).toBe(0);});+ // A protected note cannot be shared (GHSA-xmv9-3v98-7gq8). The integration+ // fixture contains "Protected shared note" — a protected note placed under+ // the "Shared Notes" subtree that owns a protected file attachment.+ const PROTECTED_SHARED_NOTE_ID = "uOCKdcqOhDF5";+ const PROTECTED_SHARED_ATTACHMENT_ID = "vC6a1DskeJNh";++ it("does not serve a protected note's content over the public share routes (GHSA-xmv9-3v98-7gq8)", async () => {+ // Every route that streams raw note content must refuse a protected note.+ for (const path of [+ `/share/api/notes/${PROTECTED_SHARED_NOTE_ID}/download`,+ `/share/api/notes/${PROTECTED_SHARED_NOTE_ID}/view`,+ `/share/api/images/${PROTECTED_SHARED_NOTE_ID}/image.png`+ ]) {+ await supertest(app).get(path).expect(404);+ }++ expect(cannotSetHeadersCount).toBe(0);+ });++ it("does not serve a protected note's attachments over the public share routes (GHSA-xmv9-3v98-7gq8)", async () => {+ await supertest(app)+ .get(`/share/api/attachments/${PROTECTED_SHARED_ATTACHMENT_ID}/download`)+ .expect(404);++ await supertest(app)+ .get(`/share/api/attachments/${PROTECTED_SHARED_ATTACHMENT_ID}/image/secret`)+ .expect(404);++ expect(cannotSetHeadersCount).toBe(0);+ });+it("renders custom share template", async () => {// Custom EJS templates require scripting to be enabledconst originalEnabled = config.Security.backendScriptingEnabled;diff --git a/apps/server/src/share/routes.ts b/apps/server/src/share/routes.tsindex 84ba0c7c37..99564e3d2c 100644--- a/apps/server/src/share/routes.ts+++ b/apps/server/src/share/routes.ts@@ -39,8 +39,17 @@ function checkAttachmentAccess(attachmentId: string, req: Request, res: Responseconst note = checkNoteAccess(attachment.ownerId, req, res);- // truthy note means the user has access, and we can return the attachment- return note ? attachment : false;+ if (!note) {+ return false;+ }++ // Protected notes cannot be shared, and neither can their attachments+ // (GHSA-xmv9-3v98-7gq8).+ if (rejectProtected(note, res, `Attachment '${attachmentId}' not found.`)) {+ return false;+ }++ return attachment;}function checkNoteAccess(noteId: string, req: Request, res: Response) {@@ -83,6 +92,35 @@ function checkNoteAccess(noteId: string, req: Request, res: Response) {return false;}+// Like checkNoteAccess, but additionally refuses protected notes. Used by the+// routes that stream a note's raw content/images: a protected note still shows+// up as "[protected]" in the shared tree (handled by content_renderer.ts), but+// its actual bytes must never be served anonymously (GHSA-xmv9-3v98-7gq8).+function checkNoteContentAccess(noteId: string, req: Request, res: Response) {+ const note = checkNoteAccess(noteId, req, res);++ if (!note) {+ return false;+ }++ if (rejectProtected(note, res, `Note '${noteId}' not found.`)) {+ return false;+ }++ return note;+}++// Sends a 404 and returns true when the note is protected, so callers can bail.+function rejectProtected(note: SNote, res: Response, message: string) {+ if (!note.isProtected) {+ return false;+ }++ res.status(404).json({ message });++ return true;+}+function renderImageAttachment(image: SNote, res: Response, attachmentName: string) {let svgString = "<svg/>";const attachment = image.getAttachmentByTitle(attachmentName);@@ -144,6 +182,15 @@ function register(router: Router) {addNoIndexHeader(note, res);if (note.isLabelTruthy("shareRaw") || typeof req.query.raw !== "undefined") {+ // A protected note is shown as a placeholder by renderNoteContent()+ // below, but the raw branch streams note.getContent() directly, so it+ // must refuse protected notes (GHSA-xmv9-3v98-7gq8).+ if (note.isProtected) {+ render404(res);++ return;+ }+// For SVG content, add a restrictive Content-Security-Policy to prevent// stored XSS via script execution (CWE-79). HTML is intentionally served// unrestricted here: serving raw HTML requires the `#shareRaw` attribute (or an@@ -207,7 +254,7 @@ function register(router: Router) {let note: SNote | boolean;- if (!(note = checkNoteAccess(req.params.noteId, req, res))) {+ if (!(note = checkNoteContentAccess(req.params.noteId, req, res))) {return;}@@ -229,7 +276,7 @@ function register(router: Router) {let image: SNote | boolean;- if (!(image = checkNoteAccess(req.params.noteId, req, res))) {+ if (!(image = checkNoteContentAccess(req.params.noteId, req, res))) {return;}@@ -318,7 +365,7 @@ function register(router: Router) {let note: SNote | boolean;- if (!(note = checkNoteAccess(req.params.noteId, req, res))) {+ if (!(note = checkNoteContentAccess(req.params.noteId, req, res))) {return;}diff --git a/apps/server/src/share/shaca/shaca_loader.ts b/apps/server/src/share/shaca/shaca_loader.tsindex c6a054f432..896be78e3b 100644--- a/apps/server/src/share/shaca/shaca_loader.ts+++ b/apps/server/src/share/shaca/shaca_loader.ts@@ -76,6 +76,14 @@ function load() {AND ownerId IN (${noteIdStr})`);for (const row of rawAttachmentRows) {+ // Defense-in-depth for GHSA-xmv9-3v98-7gq8: protected notes cannot be+ // shared, so their attachments must never enter the share cache. The+ // note was loaded above (attachments are scoped to the same subtree).+ const [, ownerId] = row;+ if (shaca.notes[ownerId]?.isProtected) {+ continue;+ }+new SAttachment(row);}diff --git a/packages/trilium-core/src/test/fixtures/document.db b/packages/trilium-core/src/test/fixtures/document.dbindex 80661dc2f6..273ae05fe0 100644Binary files a/packages/trilium-core/src/test/fixtures/document.db and b/packages/trilium-core/src/test/fixtures/document.db differ