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(export): reject single-export of unhandled note types instead of a zero-byte .undefined file
Elian Doran653e1562d884
diff --git a/packages/trilium-core/src/routes/api/export.spec.ts b/packages/trilium-core/src/routes/api/export.spec.tsindex 82a85bbf96..09e55d4850 100644--- a/packages/trilium-core/src/routes/api/export.spec.ts+++ b/packages/trilium-core/src/routes/api/export.spec.ts@@ -71,4 +71,19 @@ describe("Export API (core)", () => {expect(res.status).toBe(500);expect(res.headers["Content-Type"]).toBe("text/plain");});++ it("rejects single-export of an unhandled note type instead of emitting a zero-byte .undefined file", async () => {+ // Regression: note types mapByNoteType doesn't handle (book, webView, mindMap, …) used to fall+ // through to res.send(undefined), producing a "<title>.undefined", zero-byte download. The safety+ // net now rejects them with a clear error rather than a corrupt file.+ const created = await api.post<{ branch: { branchId: string } }>(+ "/api/notes/root/children?target=into",+ { body: { title: "My book", type: "book", content: "" } }+ );+ expect(created.status).toBe(200);++ const res = await api.get<string>(`/api/branches/${created.body.branch.branchId}/export/single/html/exportTask`);+ expect(res.status).toBe(500);+ expect(res.headers["Content-Type"]).toBe("text/plain");+ });});diff --git a/packages/trilium-core/src/services/export/single.ts b/packages/trilium-core/src/services/export/single.tsindex 7cd93ee25d..21bcf69a10 100644--- a/packages/trilium-core/src/services/export/single.ts+++ b/packages/trilium-core/src/services/export/single.ts@@ -13,19 +13,27 @@ import mdService from "./markdown.js";import { stripListItemIds } from "./strip_list_item_ids.js";import { ExportFormat } from "../../meta.js";import { encodeBase64 } from "../utils/binary.js";+import { ValidationError } from "../../errors.js";function exportSingleNote(taskContext: TaskContext<"export">, branch: BBranch, format: ExportFormat, res: Response) {const note = branch.getNote();if (note.type === "image" || note.type === "file") {- return [400, `Note type '${note.type}' cannot be exported as single file.`];+ throw new ValidationError(`Note type '${note.type}' cannot be exported as a single file.`);}if (format !== "html" && format !== "markdown") {- return [400, `Unrecognized format '${format}'`];+ throw new ValidationError(`Unrecognized format '${format}'`);}const { payload, extension, mime } = mapByNoteType(note, note.getContent(), format);++ // Safety net: any note type mapByNoteType doesn't handle leaves these undefined. Reject with a+ // clear error rather than sending `undefined` — which produced a "<title>.undefined", zero-byte file.+ if (payload === undefined || extension === undefined) {+ throw new ValidationError(`Note type '${note.type}' cannot be exported as a single file.`);+ }+const fileName = `${note.title}.${extension}`;res.setHeader("Content-Disposition", getContentDisposition(fileName));