Skip to content

Build your personal knowledge base with Trilium Notes

fix(import/zip): book content not sanitized

Elian Doranabb01f09b3ad
diff --git a/packages/trilium-core/src/services/import/zip.spec.ts b/packages/trilium-core/src/services/import/zip.spec.ts
index 10ee1f4f38..83f8a76faa 100644
--- a/packages/trilium-core/src/services/import/zip.spec.ts
+++ b/packages/trilium-core/src/services/import/zip.spec.ts
@@ -18,10 +18,8 @@ async function testImport(fileName: string) {
return testImportBuffer(buffer);
}
-async function testImportBuffer(buffer: Buffer) {
- const taskContext = TaskContext.getInstance("import-mdx", "importNotes", {
- textImportedAsText: true
- });
+async function testImportBuffer(buffer: Buffer, taskId = "import-mdx", taskData: Record<string, unknown> = { textImportedAsText: true }) {
+ const taskContext = TaskContext.getInstance(taskId, "importNotes", taskData);
return new Promise<{ importedNote: BNote; rootNote: BNote }>((resolve, reject) => {
getContext().init(async () => {
@@ -116,6 +114,37 @@ describe("processNoteContent", () => {
expect(content).toStrictEqual(`{"view":{"center":{"lat":49.19598332223546,"lng":-2.1414576506668808},"zoom":12}}`);
});
+ it("sanitizes book note HTML content on safe import (GHSA-h7w4-cjfg-cvj8)", async () => {
+ const metaFile = {
+ formatVersion: 2,
+ appVersion: "0.0.0",
+ files: [{
+ noteId: "bookXssNote1",
+ title: "Book Payload",
+ type: "book",
+ mime: "text/html",
+ dataFileName: "Book Payload.html",
+ attributes: [],
+ attachments: []
+ }]
+ };
+
+ const payload = `<img src=x onerror="require('child_process').exec('calc')"><b>safe</b>`;
+ const zipBuffer = await createZipBuffer({
+ "!!!meta.json": JSON.stringify(metaFile),
+ "Book Payload.html": payload
+ });
+
+ const { importedNote } = await testImportBuffer(zipBuffer, "import-book-safe", { textImportedAsText: true, safeImport: true });
+ const content = importedNote.getContent() as string;
+
+ expect(importedNote.type).toBe("book");
+ expect(content).not.toContain("onerror");
+ expect(content).not.toContain("child_process");
+ // Benign markup survives sanitization.
+ expect(content).toContain("safe");
+ });
+
it("rewrites relative attachment paths in markdown code notes on import", async () => {
const metaFile = {
formatVersion: 2,
diff --git a/packages/trilium-core/src/services/import/zip.ts b/packages/trilium-core/src/services/import/zip.ts
index a4fe308db5..a99c69b120 100644
--- a/packages/trilium-core/src/services/import/zip.ts
+++ b/packages/trilium-core/src/services/import/zip.ts
@@ -399,7 +399,12 @@ async function importZip(taskContext: TaskContext<"importNotes">, fileBuffer: Ui
content = markdownService.renderToHtml(content, noteTitle);
}
- if (type === "text" && typeof content === "string") {
+ // `book` notes are rendered as rich HTML through the same `renderText()` path as
+ // `text` notes (see content_renderer), so their content must receive the same
+ // import processing — crucially the Safe Import HTML sanitization. Otherwise a
+ // malicious `book` note bypasses sanitization and achieves stored XSS/RCE when its
+ // content is previewed in a grid/list view.
+ if ((type === "text" || type === "book") && typeof content === "string") {
content = processTextNoteContent(content, noteTitle, filePath, noteMeta);
}