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): keep locked notes and share credentials out of a hosted view's notes
Elian Doranc063a4cd8f5b
diff --git a/packages/trilium-core/src/share/content_renderer.spec.ts b/packages/trilium-core/src/share/content_renderer.spec.tsindex 371846bb8f..251a985831 100644--- a/packages/trilium-core/src/share/content_renderer.spec.ts+++ b/packages/trilium-core/src/share/content_renderer.spec.ts@@ -1744,6 +1744,20 @@ describe("content_renderer pages", () => {expect(textPage.querySelector("#content > h1#title") === null).toBe(false);});+ it("embeds in a hosted view neither the credentials nor the notes the visitor has not presented them for", () => {+ const board = buildSitePage({+ id: "lockedBoard", type: "book", content: "", "#viewType": "board", "#shareCredentials": "user:secret",+ children: [+ { id: "openCard", title: "Open", content: "<p>Open</p>" },+ { id: "lockedCard", title: "Locked", content: "<p>Locked</p>", "#shareCredentials": "other:secret" }+ ]+ });+ const page = parse(String(renderNoteContent(board, (note) => note.noteId !== "lockedCard")));+ const payload = JSON.parse(page.querySelector("script.share-froca")?.textContent ?? "{}");+ expect(payload.notes.map((note: { noteId: string }) => note.noteId)).toEqual([ "lockedBoard", "openCard" ]);+ expect(payload.attributes.map((attribute: { name: string }) => attribute.name)).toEqual([ "viewType" ]);+ });+it("lists the subpages of a collection whose template makes it a list", () => {buildNote({ id: "_template_testList", title: "List template", "#viewType": "list" });const list = buildSitePage({diff --git a/packages/trilium-core/src/share/content_renderer.ts b/packages/trilium-core/src/share/content_renderer.tsindex c11202099c..aca5bd9bdb 100644--- a/packages/trilium-core/src/share/content_renderer.ts+++ b/packages/trilium-core/src/share/content_renderer.ts@@ -499,25 +499,25 @@ export function getContent(note: SNote | BNote, options: ShareRenderOptions = {}renderCode(result, note.mime);} else if (note.type === "mermaid") {renderMermaid(result, note);- hostNoteView(result, note);+ hostNoteView(result, note, options.canAccessEmbed);} else if ([ "image", "canvas", "mindMap" ].includes(note.type)) {renderImage(result, note);- hostNoteView(result, note);+ hostNoteView(result, note, options.canAccessEmbed);} else if (note.type === "file") {renderFile(note, result);} else if (note.type === "book" && !(note instanceof BNote)&& HOSTED_VIEW_TYPES.has(getViewType(note) ?? "")) {- renderCollectionView(result, note);+ renderCollectionView(result, note, options.canAccessEmbed);} else if (note.type === "book") {result.isEmpty = true;} else if (note.type === "webView") {renderWebView(note, result);} else if ([ "noteMap", "relationMap", "render" ].includes(note.type)) {result.content = "";- hostNoteView(result, note);+ hostNoteView(result, note, options.canAccessEmbed);} else if (note.type === "spreadsheet") {renderSpreadsheet(result);- hostNoteView(result, note);+ hostNoteView(result, note, options.canAccessEmbed);} else {result.content = `<p>${t("content_renderer.note-cannot-be-displayed")}</p>`;}@@ -929,9 +929,9 @@ function isSubtreeHidden(note: ShareNote) {* Renders a collection the share theme shows with the app's own view, through its note list: an* element to mount it into, beside the notes and the display options it starts from.*/-function renderCollectionView(result: Result, note: SNote) {+function renderCollectionView(result: Result, note: SNote, canAccess: CanAccessEmbed | undefined) {result.content = "";- hostInAppView(result, note, "share-collection");+ hostInAppView(result, note, "share-collection", canAccess);}/**@@ -939,9 +939,9 @@ function renderCollectionView(result: Result, note: SNote) {* rendered here for a visitor without scripts. The static export, binary content and an empty note* keep the content alone.*/-function hostNoteView(result: Result, note: SNote | BNote) {+function hostNoteView(result: Result, note: SNote | BNote, canAccess: CanAccessEmbed | undefined) {if (!(note instanceof BNote) && typeof result.content === "string" && !result.isEmpty) {- hostInAppView(result, note, "share-note-view");+ hostInAppView(result, note, "share-note-view", canAccess);}}@@ -950,9 +950,9 @@ function hostNoteView(result: Result, note: SNote | BNote) {* notes and the display options the view starts from. The content stays for a visitor without* scripts until the view replaces it.*/-function hostInAppView(result: Result, note: SNote, container: string) {+function hostInAppView(result: Result, note: SNote, container: string, canAccess: CanAccessEmbed | undefined) {const payload = {- ...buildFrocaPayload(note),+ ...buildFrocaPayload(note, canAccess),options: Object.fromEntries(SHARED_OPTIONS.map((name) => [ name, options.getOptionOrNull(name) ])),// The app's assets, such as its translations, from a page directly below `/share/`.assetPath: `../${utils.isDev() ? `${assetUrlFragment}/src` : assetUrlFragment}`,diff --git a/packages/trilium-core/src/share/froca_payload.ts b/packages/trilium-core/src/share/froca_payload.tsindex 54997d769f..f85568120a 100644--- a/packages/trilium-core/src/share/froca_payload.ts+++ b/packages/trilium-core/src/share/froca_payload.ts@@ -17,9 +17,12 @@ export interface FrocaRows {links: Record<string, string>;}-/** The notes a collection view reads, embedded in its page so the view draws without a request. */-export function buildFrocaPayload(note: SNote) {- return buildFrocaRows([ note ]);+/**+ * The notes a collection view reads, embedded in its page so the view draws without a request, of+ * those that `canAccess` lets the visitor read.+ */+export function buildFrocaPayload(note: SNote, canAccess?: (note: SNote) => boolean) {+ return buildFrocaRows([ note ], canAccess);}/**@@ -28,6 +31,7 @@ export function buildFrocaPayload(note: SNote) {* holds only shared notes, so a relation to a note outside the share is left out, as is every note* that is protected or that `canAccess` refuses. A built-in template such as `_template_calendar`* is read from becca instead, as it lives in the hidden subtree, so the notes inherit its labels.+ * `#shareCredentials` is always left out, as it holds the password of the notes.*/export function buildFrocaRows(roots: SNote[], canAccess: (note: SNote) => boolean = () => true): FrocaRows {const notes = new Map<string, SNote>();@@ -79,6 +83,9 @@ export function buildFrocaRows(roots: SNote[], canAccess: (note: SNote) => boole}}for (const attribute of included.ownedAttributes) {+ if (attribute.name === "shareCredentials") {+ continue;+ }if (attribute.type === "label" || notes.has(attribute.value) || templates.has(attribute.value)) {rows.attributes.push(attribute.getPojo());}