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.
Python artificial-intelligencedagdata-sciencedata-visualizationdataflowdeveloper-toolsmachine-learningnotebookspipelinepythonreactivesqlweb-app
fix(plugins): JSON-escape > and < in web-component attrs (#9595)
Kiran Gadhavec4702c346353
diff --git a/frontend/src/plugins/core/__test__/sanitize.test.ts b/frontend/src/plugins/core/__test__/sanitize.test.tsindex dfd5517395..6b311850f5 100644--- a/frontend/src/plugins/core/__test__/sanitize.test.ts+++ b/frontend/src/plugins/core/__test__/sanitize.test.ts@@ -557,4 +557,34 @@ describe("sanitizeHtml", () => {const html = "<some-custom-element>Content</some-custom-element>";expect(sanitizeHtml(html)).toMatchInlineSnapshot(`"Content"`);});++ // DOMPurify SAFE_FOR_XML strips attribute values containing `]>`, `-->`,+ // `--!>`, or `</(style|script|...)`. `_build_attr` JSON-escapes `<`/`>`+ // (`<`/`>`) before HTML-escaping so the parsed attribute value+ // never matches. These tests pin that contract.+ test("preserves data-* attribute with JSON-escaped ]> payload", () => {+ const html =+ "<marimo-table data-data='[{"x":"]\u003e"}]'></marimo-table>";+ expect(sanitizeHtml(html)).toMatchInlineSnapshot(+ `"<marimo-table data-data="[{"x":"]\\u003e"}]"></marimo-table>"`,+ );+ });++ test("preserves data-* attribute with JSON-escaped </script> payload", () => {+ const html =+ "<marimo-table data-data='[{"x":"\u003c/script\u003e"}]'></marimo-table>";+ expect(sanitizeHtml(html)).toMatchInlineSnapshot(+ `"<marimo-table data-data="[{"x":"\\u003c/script\\u003e"}]"></marimo-table>"`,+ );+ });++ // Regression sentinel: confirms DOMPurify still strips an UN-escaped `]>`,+ // which is precisely the condition `_build_attr` exists to prevent.+ test("strips data-* attribute when raw ]> leaks through", () => {+ const html =+ "<marimo-table data-data='[{"x":"]>"}]'></marimo-table>";+ expect(sanitizeHtml(html)).toMatchInlineSnapshot(+ `"<marimo-table></marimo-table>"`,+ );+ });});diff --git a/marimo/_plugins/core/web_component.py b/marimo/_plugins/core/web_component.pyindex 018ea52b91..2dfbda575f 100644--- a/marimo/_plugins/core/web_component.py+++ b/marimo/_plugins/core/web_component.py@@ -38,7 +38,16 @@ S = TypeVar("S", bound=JSONType)def _build_attr(name: str, value: JSONType) -> str:- processed = escape(encode_json_str(value))+ encoded = encode_json_str(value)+ # Encode `<` and `>` as JSON `\uXXXX` escapes so the parsed attribute+ # value (post HTML unescaping) contains no literal `<` or `>`.+ # DOMPurify's SAFE_FOR_XML rule strips any attribute whose value matches+ # `]>` (or `</style>` etc.); without this, cell strings like `]>` or+ # `]>>` (atom-mapped reaction SMILES) would silently drop plugin args.+ # `JSON.parse` decodes `<`/`>` natively, so the frontend sees+ # the original characters.+ encoded = encoded.replace(">", "\\u003e").replace("<", "\\u003c")+ processed = escape(encoded)# manual escapes for things html.escape doesn't escape## - backslashes, when unescaped can lead to problemsdiff --git a/tests/_islands/snapshots/island-mimetypes.txt b/tests/_islands/snapshots/island-mimetypes.txtindex 507f4b8f0b..53369d3476 100644--- a/tests/_islands/snapshots/island-mimetypes.txt+++ b/tests/_islands/snapshots/island-mimetypes.txt@@ -4,7 +4,7 @@data-reactive="true"><marimo-cell-output>- <marimo-mime-renderer data-mime='"text/markdown"' data-data='"<span class=\"markdown prose dark:prose-invert contents\"><span class=\"paragraph\">Hello, islands!</span></span>"'></marimo-mime-renderer>+ <marimo-mime-renderer data-mime='"text/markdown"' data-data='"\u003cspan class=\"markdown prose dark:prose-invert contents\"\u003e\u003cspan class=\"paragraph\"\u003eHello, islands!\u003c/span\u003e\u003c/span\u003e"'></marimo-mime-renderer></marimo-cell-output><marimo-cell-code hidden>mo.md('Hello%2C%20islands!')</marimo-cell-code></marimo-island>@@ -34,7 +34,7 @@data-reactive="true"><marimo-cell-output>- <marimo-json-output data-json-data='[1,"text/markdown:<span class=\"markdown prose dark:prose-invert contents\"><span class=\"paragraph\">Hello</span></span>"]' data-value-types='"python"'></marimo-json-output>+ <marimo-json-output data-json-data='[1,"text/markdown:\u003cspan class=\"markdown prose dark:prose-invert contents\"\u003e\u003cspan class=\"paragraph\"\u003eHello\u003c/span\u003e\u003c/span\u003e"]' data-value-types='"python"'></marimo-json-output></marimo-cell-output><marimo-cell-code hidden>%5B1%2C%20mo.md('Hello')%5D</marimo-cell-code></marimo-island>\ No newline at end of filediff --git a/tests/_islands/snapshots/island-no-code.txt b/tests/_islands/snapshots/island-no-code.txtindex 8707d5dc0e..9a1ad67288 100644--- a/tests/_islands/snapshots/island-no-code.txt+++ b/tests/_islands/snapshots/island-no-code.txt@@ -4,7 +4,7 @@data-reactive="true"><marimo-cell-output>- <marimo-mime-renderer data-mime='"text/markdown"' data-data='"<span class=\"markdown prose dark:prose-invert contents\"><span class=\"paragraph\">Hello, islands!</span></span>"'></marimo-mime-renderer>+ <marimo-mime-renderer data-mime='"text/markdown"' data-data='"\u003cspan class=\"markdown prose dark:prose-invert contents\"\u003e\u003cspan class=\"paragraph\"\u003eHello, islands!\u003c/span\u003e\u003c/span\u003e"'></marimo-mime-renderer></marimo-cell-output><marimo-cell-code hidden>mo.md('Hello%2C%20islands!')</marimo-cell-code></marimo-island>\ No newline at end of filediff --git a/tests/_islands/snapshots/island.txt b/tests/_islands/snapshots/island.txtindex 8707d5dc0e..9a1ad67288 100644--- a/tests/_islands/snapshots/island.txt+++ b/tests/_islands/snapshots/island.txt@@ -4,7 +4,7 @@data-reactive="true"><marimo-cell-output>- <marimo-mime-renderer data-mime='"text/markdown"' data-data='"<span class=\"markdown prose dark:prose-invert contents\"><span class=\"paragraph\">Hello, islands!</span></span>"'></marimo-mime-renderer>+ <marimo-mime-renderer data-mime='"text/markdown"' data-data='"\u003cspan class=\"markdown prose dark:prose-invert contents\"\u003e\u003cspan class=\"paragraph\"\u003eHello, islands!\u003c/span\u003e\u003c/span\u003e"'></marimo-mime-renderer></marimo-cell-output><marimo-cell-code hidden>mo.md('Hello%2C%20islands!')</marimo-cell-code></marimo-island>\ No newline at end of filediff --git a/tests/_islands/snapshots/markdown.txt b/tests/_islands/snapshots/markdown.txtindex 7855486385..48c1a29f07 100644--- a/tests/_islands/snapshots/markdown.txt+++ b/tests/_islands/snapshots/markdown.txt@@ -4,7 +4,7 @@data-reactive="true"><marimo-cell-output>- <marimo-mime-renderer data-mime='"text/markdown"' data-data='"<span class=\"markdown prose dark:prose-invert contents\"><h1 id=\"hello-markdown\">Hello, Markdown!</h1>\n<span class=\"paragraph\">This is a paragsraph.</span>\n<div class=\"language-python3 codehilite\"><pre><span></span><code><span class=\"n\">mo</span><span class=\"o\">.</span><span class=\"n\">md</span><span class=\"p\">(</span>\n<span class=\"w\"> </span><span class=\"sd\">&#39;&#39;&#39;</span>\n<span class=\"sd\"> # Hello, Markdown!</span>\n\n<span class=\"sd\"> This is a paragraph.</span>\n<span class=\"sd\"> &#39;&#39;&#39;</span>\n<span class=\"p\">)</span>\n</code></pre></div></span>"'></marimo-mime-renderer>+ <marimo-mime-renderer data-mime='"text/markdown"' data-data='"\u003cspan class=\"markdown prose dark:prose-invert contents\"\u003e\u003ch1 id=\"hello-markdown\"\u003eHello, Markdown!\u003c/h1\u003e\n\u003cspan class=\"paragraph\"\u003eThis is a paragsraph.\u003c/span\u003e\n\u003cdiv class=\"language-python3 codehilite\"\u003e\u003cpre\u003e\u003cspan\u003e\u003c/span\u003e\u003ccode\u003e\u003cspan class=\"n\"\u003emo\u003c/span\u003e\u003cspan class=\"o\"\u003e.\u003c/span\u003e\u003cspan class=\"n\"\u003emd\u003c/span\u003e\u003cspan class=\"p\"\u003e(\u003c/span\u003e\n\u003cspan class=\"w\"\u003e \u003c/span\u003e\u003cspan class=\"sd\"\u003e&#39;&#39;&#39;\u003c/span\u003e\n\u003cspan class=\"sd\"\u003e # Hello, Markdown!\u003c/span\u003e\n\n\u003cspan class=\"sd\"\u003e This is a paragraph.\u003c/span\u003e\n\u003cspan class=\"sd\"\u003e &#39;&#39;&#39;\u003c/span\u003e\n\u003cspan class=\"p\"\u003e)\u003c/span\u003e\n\u003c/code\u003e\u003c/pre\u003e\u003c/div\u003e\u003c/span\u003e"'></marimo-mime-renderer></marimo-cell-output><marimo-cell-code hidden>%0Aimport%20marimo%20as%20mo%0Amo.md(%0A%20%20%20%20%22%22%22%0A%20%20%20%20%23%20Hello%2C%20Markdown!%0A%0A%20%20%20%20This%20is%20a%20paragsraph.%0A%0A%20%20%20%20%60%60%60python3%0A%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20'''%0A%20%20%20%20%20%20%20%20%23%20Hello%2C%20Markdown!%0A%0A%20%20%20%20%20%20%20%20This%20is%20a%20paragraph.%0A%20%20%20%20%20%20%20%20'''%0A%20%20%20%20)%0A%20%20%20%20%60%60%60%0A%20%20%20%20%22%22%22%0A)%0A</marimo-cell-code></marimo-island>\ No newline at end of filediff --git a/tests/_plugins/core/test_web_component.py b/tests/_plugins/core/test_web_component.pyindex f182047cfc..60b6eac13d 100644--- a/tests/_plugins/core/test_web_component.py+++ b/tests/_plugins/core/test_web_component.py@@ -49,3 +49,57 @@ def test_embed_dollar_sign_in_md() -> None:markdown = md(as_html(["$", "$"]).text)# don't parse dollar in data as latexassert "arithmatex" not in markdown.text+++# DOMPurify's SAFE_FOR_XML rule strips any attribute whose value matches+# `]>`, `-->`, `--!>`, or `</(style|script|title|xmp|textarea|noscript|iframe+# |noembed|noframes)`. `_build_attr` must emit JSON-escaped `<`/`>` so the+# parsed attribute value never contains those literals.+SAFE_FOR_XML_TRIGGERS = [+ "]>",+ "]>>",+ "[Cl:31]>>[c:1]",+ "<script>alert(1)</script>",+ "<!-- comment -->",+ "<!--!>",+ "</style>",+ "</textarea>",+ "</iframe>",+]+++def _attr_value(html: str, name: str) -> str:+ import html as _html_mod++ match = re.search(f"data-{name}='(.*?)'", html)+ assert match is not None+ return _html_mod.unescape(match.groups()[0])+++def test_build_attr_escapes_angle_brackets() -> None:+ for raw in SAFE_FOR_XML_TRIGGERS:+ html = build_ui_plugin(+ "tag-name", initial_value=raw, label=None, args={"data": raw}+ )+ for attr_name in ("initial-value", "data"):+ parsed = _attr_value(html, attr_name)+ assert ">" not in parsed, (raw, attr_name, parsed)+ assert "<" not in parsed, (raw, attr_name, parsed)+++def test_initial_value_roundtrip_with_angle_brackets() -> None:+ for raw in SAFE_FOR_XML_TRIGGERS:+ html = build_ui_plugin(+ "tag-name", initial_value=raw, label=None, args={}+ )+ assert parse_initial_value(html) == raw+++def test_build_attr_nested_data_escapes_angle_brackets() -> None:+ nested = {"rows": [{"x": raw} for raw in SAFE_FOR_XML_TRIGGERS]}+ html = build_ui_plugin(+ "tag-name", initial_value=None, label=None, args={"data": nested}+ )+ parsed = _attr_value(html, "data")+ assert ">" not in parsed+ assert "<" not in parseddiff --git a/tests/_plugins/stateless/test_mermaid.py b/tests/_plugins/stateless/test_mermaid.pyindex 2dbd58ce2d..8e04bb2257 100644--- a/tests/_plugins/stateless/test_mermaid.py+++ b/tests/_plugins/stateless/test_mermaid.py@@ -10,7 +10,7 @@ from marimo._plugins.stateless.mermaid import mermaiddef test_mo_mermaid_diagram_only() -> None:assert (mermaid("graph TD\nA --> B").text- == "<marimo-mermaid data-diagram='"graph TD\nA --> B"'></marimo-mermaid>"+ == "<marimo-mermaid data-diagram='"graph TD\nA --\u003e B"'></marimo-mermaid>")@@ -24,7 +24,7 @@ def test_mo_mermaid_theme_and_theme_variables() -> None:"lineColor": "#475569",},).text- == "<marimo-mermaid data-diagram='"graph TD\nA --> B"' data-theme='"base"' data-theme_variables='{"primaryColor":"#E8EEF5","lineColor":"#475569"}'></marimo-mermaid>"+ == "<marimo-mermaid data-diagram='"graph TD\nA --\u003e B"' data-theme='"base"' data-theme_variables='{"primaryColor":"#E8EEF5","lineColor":"#475569"}'></marimo-mermaid>")@@ -34,7 +34,7 @@ def test_mo_mermaid_theme_defaults_to_base_with_theme_variables() -> None:"graph TD\nA --> B",theme_variables={"primaryColor": "#E8EEF5"},).text- == "<marimo-mermaid data-diagram='"graph TD\nA --> B"' data-theme='"base"' data-theme_variables='{"primaryColor":"#E8EEF5"}'></marimo-mermaid>"+ == "<marimo-mermaid data-diagram='"graph TD\nA --\u003e B"' data-theme='"base"' data-theme_variables='{"primaryColor":"#E8EEF5"}'></marimo-mermaid>")@@ -52,5 +52,5 @@ def test_mo_mermaid_rejects_theme_variables_with_non_base_theme() -> None:def test_mo_mermaid_accepts_any_theme_string() -> None:assert (mermaid("graph TD\nA --> B", theme=cast(Any, "custom-new-theme")).text- == "<marimo-mermaid data-diagram='"graph TD\nA --> B"' data-theme='"custom-new-theme"'></marimo-mermaid>"+ == "<marimo-mermaid data-diagram='"graph TD\nA --\u003e B"' data-theme='"custom-new-theme"'></marimo-mermaid>")