Skip to content

Build your personal knowledge base with Trilium Notes

fix(mindmap): sanitize HTML before rendering it

Elian Doran15da3ff626c2
diff --git a/apps/client/src/widgets/type_widgets/MindMap.spec.ts b/apps/client/src/widgets/type_widgets/MindMap.spec.ts
new file mode 100644
index 0000000000..ff55cb2e25
--- /dev/null
+++ b/apps/client/src/widgets/type_widgets/MindMap.spec.ts
@@ -0,0 +1,80 @@
+import { describe, expect, it, vi } from "vitest";
+
+// The node-menu plugin touches the DOM at import time, which fails under happy-dom
+// and is irrelevant to the pure sanitization logic under test here.
+vi.mock("@mind-elixir/node-menu", () => ({ default: {} }));
+
+import { sanitizeMindMapData } from "./MindMap.js";
+
+describe("sanitizeMindMapData", () => {
+ it("strips XSS/RCE vectors from dangerouslySetInnerHTML (GHSA-rj57-j38v-3577)", () => {
+ const data = {
+ nodeData: {
+ id: "root",
+ topic: "root",
+ dangerouslySetInnerHTML: `<img src=x onerror="require('child_process').exec('calc')">`,
+ children: []
+ }
+ };
+
+ const sanitized = sanitizeMindMapData(data);
+ const html = sanitized.nodeData.dangerouslySetInnerHTML;
+
+ expect(html).not.toContain("onerror");
+ expect(html).not.toContain("child_process");
+ // The benign part of the markup is preserved rather than dropped wholesale.
+ expect(html).toContain("<img");
+ });
+
+ it("removes <script> payloads while keeping harmless markup", () => {
+ const data = { nodeData: { dangerouslySetInnerHTML: `<b>hi</b><script>alert(1)</script>` } };
+
+ const html = sanitizeMindMapData(data).nodeData.dangerouslySetInnerHTML;
+
+ expect(html).toContain("<b>hi</b>");
+ expect(html).not.toContain("<script");
+ });
+
+ it("sanitizes the property anywhere in the tree, including nested children", () => {
+ const data = {
+ nodeData: {
+ id: "root",
+ topic: "root",
+ children: [
+ { id: "a", topic: "a" },
+ {
+ id: "b",
+ topic: "b",
+ dangerouslySetInnerHTML: `<svg><script>alert(1)</script></svg>`,
+ children: [
+ { id: "c", dangerouslySetInnerHTML: `<a href="javascript:alert(1)">x</a>` }
+ ]
+ }
+ ]
+ }
+ };
+
+ sanitizeMindMapData(data);
+
+ const b = data.nodeData.children[1];
+ expect(b.dangerouslySetInnerHTML).not.toContain("<script");
+ expect(b.children?.[0].dangerouslySetInnerHTML).not.toContain("javascript:");
+ });
+
+ it("mutates in place and returns the same reference", () => {
+ const data = { nodeData: { topic: "root" } };
+ expect(sanitizeMindMapData(data)).toBe(data);
+ });
+
+ it("leaves content without the property untouched", () => {
+ const data = { nodeData: { id: "root", topic: "hello", children: [] } };
+ sanitizeMindMapData(data);
+ expect(data).toEqual({ nodeData: { id: "root", topic: "hello", children: [] } });
+ });
+
+ it("ignores a non-string dangerouslySetInnerHTML value", () => {
+ const data = { nodeData: { dangerouslySetInnerHTML: 123 } };
+ sanitizeMindMapData(data);
+ expect(data.nodeData.dangerouslySetInnerHTML).toBe(123);
+ });
+});
diff --git a/apps/client/src/widgets/type_widgets/MindMap.tsx b/apps/client/src/widgets/type_widgets/MindMap.tsx
index bf1f3382b1..1a80000d39 100644
--- a/apps/client/src/widgets/type_widgets/MindMap.tsx
+++ b/apps/client/src/widgets/type_widgets/MindMap.tsx
@@ -11,6 +11,7 @@ import type { LangPack } from "mind-elixir/i18n";
import { HTMLAttributes, RefObject } from "preact";
import { useCallback, useEffect, useRef } from "preact/hooks";
+import { sanitizeNoteContentHtml } from "../../services/sanitize_content";
import utils from "../../services/utils";
import { useColorScheme, useEditorSpacedUpdate, useEffectiveReadOnly, useSyncedRef, useTriliumEvent, useTriliumEvents, useTriliumOption } from "../react/hooks";
import { refToJQuerySelector } from "../react/react_utils";
@@ -18,6 +19,51 @@ import { TypeWidgetProps } from "./type_widget";
const NEW_TOPIC_NAME = "";
+/**
+ * Recursively sanitizes a parsed Mind Elixir data structure in place, neutralizing any
+ * `dangerouslySetInnerHTML` payloads found anywhere in the tree.
+ *
+ * Mind Elixir nodes support an (undocumented in its README) `dangerouslySetInnerHTML`
+ * property which replaces the node's content with raw HTML via `element.innerHTML`
+ * (see mind-elixir `utils/dom.ts`). Trilium's UI never produces this property, but a
+ * malicious note can embed it in the stored JSON. Because Safe Import only sanitizes
+ * notes of `type === "text"` and skips `mindMap` notes, such a payload reaches the
+ * client unsanitized and is injected into the DOM when the map is rendered, yielding
+ * stored XSS (and historically RCE on the Electron desktop client) — GHSA-rj57-j38v-3577.
+ *
+ * Rather than dropping the field, we run every occurrence through the same DOMPurify
+ * sanitizer used for text notes, so any legitimate markup is preserved while script
+ * execution vectors are stripped.
+ *
+ * @param data the parsed Mind Elixir content (mutated in place).
+ * @returns the same object, for convenience.
+ */
+export function sanitizeMindMapData<T>(data: T): T {
+ sanitizeMindMapNode(data);
+ return data;
+}
+
+function sanitizeMindMapNode(value: unknown): void {
+ if (Array.isArray(value)) {
+ for (const item of value) {
+ sanitizeMindMapNode(item);
+ }
+ return;
+ }
+
+ if (value && typeof value === "object") {
+ const record = value as Record<string, unknown>;
+
+ if (typeof record.dangerouslySetInnerHTML === "string") {
+ record.dangerouslySetInnerHTML = sanitizeNoteContentHtml(record.dangerouslySetInnerHTML);
+ }
+
+ for (const key of Object.keys(record)) {
+ sanitizeMindMapNode(record[key]);
+ }
+ }
+}
+
interface MindElixirProps {
apiRef?: RefObject<MindElixirInstance>;
containerProps?: Omit<HTMLAttributes<HTMLDivElement>, "ref">;
@@ -84,7 +130,7 @@ export default function MindMap({ note, ntxId, noteContext }: TypeWidgetProps) {
if (content) {
try {
- newContent = JSON.parse(content) as MindElixirData;
+ newContent = sanitizeMindMapData(JSON.parse(content) as MindElixirData);
delete newContent.theme; // The theme is managed internally by the widget, so we remove it from the loaded content to avoid inconsistencies.
} catch (e) {
console.warn(e);