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
feat(markdown): parse Markdown and purify the headings
Elian Doran1d397176993b
diff --git a/apps/client/src/widgets/type_widgets/code/Markdown.spec.ts b/apps/client/src/widgets/type_widgets/code/Markdown.spec.tsindex a7231a880f..af211f82fe 100644--- a/apps/client/src/widgets/type_widgets/code/Markdown.spec.ts+++ b/apps/client/src/widgets/type_widgets/code/Markdown.spec.ts@@ -108,4 +108,54 @@ describe("renderWithSourceLines", () => {{ id: "md-heading-2", level: 3, text: "Sub", line: 7 }]);});++ it("renders inline markdown formatting in heading text", () => {+ const src = [+ "## **Bold heading**",+ "",+ "## *Italic heading*",+ "",+ "## `Code in heading`",+ "",+ "## Heading with **bold** and `code`",+ "",+ "## Heading with ~~strikethrough~~",+ "",+ "## Heading with [a link](https://example.com)"+ ].join("\n");++ const { headings } = renderWithSourceLines(src);+ expect(headings[0].text).toBe("<strong>Bold heading</strong>");+ expect(headings[1].text).toBe("<em>Italic heading</em>");+ expect(headings[2].text).toBe("<code>Code in heading</code>");+ expect(headings[3].text).toBe("Heading with <strong>bold</strong> and <code>code</code>");+ expect(headings[4].text).toBe("Heading with <del>strikethrough</del>");+ expect(headings[5].text).toBe('Heading with <a href="https://example.com">a link</a>');+ });++ it("sanitizes XSS vectors in heading text", () => {+ const src = [+ "## <script>alert('XSS via script tag')</script>",+ "",+ '## <button onclick="alert(\'clicked!\')">Click me</button>',+ "",+ '## <img src="x" onerror="alert(\'img onerror XSS\')">',+ "",+ '## <a href="javascript:alert(\'javascript: URL\')">Innocent link</a>',+ "",+ '## <svg onload="alert(\'SVG onload XSS\')"><rect width="100" height="100"/></svg>',+ "",+ "## <details><summary>Collapsible</summary><script>alert('inside details')</script></details>"+ ].join("\n");++ const { headings } = renderWithSourceLines(src);++ for (const h of headings) {+ expect(h.text).not.toMatch(/<script/i);+ expect(h.text).not.toMatch(/onerror/i);+ expect(h.text).not.toMatch(/onclick/i);+ expect(h.text).not.toMatch(/onload/i);+ expect(h.text).not.toMatch(/javascript:/i);+ }+ });});diff --git a/apps/client/src/widgets/type_widgets/code/Markdown.tsx b/apps/client/src/widgets/type_widgets/code/Markdown.tsxindex e7f3ad7479..70f734b689 100644--- a/apps/client/src/widgets/type_widgets/code/Markdown.tsx+++ b/apps/client/src/widgets/type_widgets/code/Markdown.tsx@@ -725,10 +725,12 @@ export function renderWithSourceLines(src: string): { html: string; headings: Maline += (token.raw.match(/\n/g) ?? []).length;if (!NON_RENDERED_TOKENS.has(token.type)) lines.push(startLine);if (token.type === "heading") {+ const rawText = token.text ?? "";+ const inlineHtml = DOMPurify.sanitize(marked.parseInline(rawText) as string);headings.push({id: `md-heading-${headingIdx++}`,level: (token as { depth: number }).depth,- text: token.text ?? "",+ text: inlineHtml,line: startLine});}