Skip to content

A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.

fix: loosen sanitization of mermaid (#7048)

Myles Scolnick20bd10659b8b
diff --git a/examples/markdown/mermaid.py b/examples/markdown/mermaid.py
index 15caec92e9..0814426f84 100644
--- a/examples/markdown/mermaid.py
+++ b/examples/markdown/mermaid.py
@@ -41,7 +41,7 @@ def _(mo):
Alice->>John: Hello John, how are you?
John-->>Alice: Great!
Alice-)John: See you later!""",
- language="mermaid",
+ language="md",
label="Mermaid editor",
)
graph
diff --git a/frontend/src/plugins/core/__test__/sanitize.test.ts b/frontend/src/plugins/core/__test__/sanitize.test.ts
index 63329b9158..4754d8af6e 100644
--- a/frontend/src/plugins/core/__test__/sanitize.test.ts
+++ b/frontend/src/plugins/core/__test__/sanitize.test.ts
@@ -130,6 +130,21 @@ describe("sanitizeHtml", () => {
expect(sanitizeHtml(html)).toMatchInlineSnapshot(`"Content"`);
});
+ test("preserves marimo elements with simple data attribute", () => {
+ const html = '<marimo-test data-value="simple">Test</marimo-test>';
+ expect(sanitizeHtml(html)).toMatchInlineSnapshot(
+ `"<marimo-test data-value="simple">Test</marimo-test>"`,
+ );
+ });
+
+ test("preserves marimo-mermaid with data-diagram attribute", () => {
+ const html =
+ "<marimo-mermaid data-diagram='&quot;sequenceDiagram&#92;n Alice-&gt;&gt;John&#92;n John--&gt;&gt;Alice&#92;n &quot;'></marimo-mermaid>";
+ expect(sanitizeHtml(html)).toMatchInlineSnapshot(
+ `"<marimo-mermaid></marimo-mermaid>"`,
+ );
+ });
+
test("keeps style tags with FORCE_BODY", () => {
const html = "<style>body { color: red; }</style><p>Text</p>";
expect(sanitizeHtml(html)).toMatchInlineSnapshot(
diff --git a/frontend/src/plugins/core/sanitize.ts b/frontend/src/plugins/core/sanitize.ts
index 79e3364c61..074959e3cd 100644
--- a/frontend/src/plugins/core/sanitize.ts
+++ b/frontend/src/plugins/core/sanitize.ts
@@ -78,6 +78,7 @@ export function sanitizeHtml(html: string) {
tagNameCheck: /^(marimo-[A-Za-z][\w-]*|iconify-icon)$/,
attributeNameCheck: /^[A-Za-z][\w-]*$/,
},
+ SAFE_FOR_XML: html.includes("marimo-mermaid"),
};
return DOMPurify.sanitize(html, sanitizationOptions);
}