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
test(import): cover front matter attribute-name sanitization
Elian Doranb9381831a90d
diff --git a/packages/trilium-core/src/services/import/frontmatter.spec.ts b/packages/trilium-core/src/services/import/frontmatter.spec.tsindex 52dcb572dd..ac8efb172d 100644--- a/packages/trilium-core/src/services/import/frontmatter.spec.ts+++ b/packages/trilium-core/src/services/import/frontmatter.spec.ts@@ -42,6 +42,18 @@ describe("extractFrontmatter", () => {expect(attributes).toEqual([]);});+ it("sanitizes dangerous-looking property names to plain alphanumeric label names", () => {+ // A relation/script-trigger or promoted-definition syntax in a key is neutralized by toAttributeName:+ // the `~`, `:` and markup characters are stripped, so no special attribute can be smuggled in.+ const { attributes } = extractFrontmatter('---\n"~runOnNoteCreation": x\n"label:evil": y\n"<script>": z\n"~template": w\n---\n');+ expect(attributes).toEqual([+ { name: "runonnotecreation", value: "x" },+ { name: "labelEvil", value: "y" },+ { name: "script", value: "z" },+ { name: "template", value: "w" }+ ]);+ });+it("keeps the note intact when the front matter YAML is malformed", () => {const md = "---\nkey: [unclosed\n---\nBody.";const { body, attributes } = extractFrontmatter(md);diff --git a/packages/trilium-core/src/services/import/single.spec.ts b/packages/trilium-core/src/services/import/single.spec.tsindex 66464c8b52..a67b1e6248 100644--- a/packages/trilium-core/src/services/import/single.spec.ts+++ b/packages/trilium-core/src/services/import/single.spec.ts@@ -117,6 +117,15 @@ describe("processNoteContent", () => {expect(importedNote.getOwnedLabelValues("tags")).toEqual(["Tag", "AnotherTag"]);});+ it("only creates labels from front matter — never relations (no script triggers)", async () => {+ const md = '---\n"~runOnNoteCreation": evil\ntemplate: x\n---\nBody.';+ const { importedNote } = await testImport("Safe.md", "text/markdown", Buffer.from(md));+ // Front matter applies via addLabel, so the only script-execution / template vector (a relation)+ // can never appear — even a key that looks like one lands as a harmless label.+ expect(importedNote.getOwnedAttributes().filter((a) => a.type === "relation")).toHaveLength(0);+ expect(importedNote.getOwnedAttributes().every((a) => a.type === "label")).toBe(true);+ });+it("supports excalidraw note", async () => {const { importedNote } = await testImport("New note.excalidraw", "application/json");expect(importedNote.mime).toBe("application/json");