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(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.ts
index 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='[{&quot;x&quot;:&quot;]&#92;u003e&quot;}]'></marimo-table>";
+ expect(sanitizeHtml(html)).toMatchInlineSnapshot(
+ `"<marimo-table data-data="[{&quot;x&quot;:&quot;]\\u003e&quot;}]"></marimo-table>"`,
+ );
+ });
+
+ test("preserves data-* attribute with JSON-escaped </script> payload", () => {
+ const html =
+ "<marimo-table data-data='[{&quot;x&quot;:&quot;&#92;u003c/script&#92;u003e&quot;}]'></marimo-table>";
+ expect(sanitizeHtml(html)).toMatchInlineSnapshot(
+ `"<marimo-table data-data="[{&quot;x&quot;:&quot;\\u003c/script\\u003e&quot;}]"></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='[{&quot;x&quot;:&quot;]&gt;&quot;}]'></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.py
index 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 problems
diff --git a/tests/_islands/snapshots/island-mimetypes.txt b/tests/_islands/snapshots/island-mimetypes.txt
index 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='&quot;text/markdown&quot;' data-data='&quot;&lt;span class=&#92;&quot;markdown prose dark:prose-invert contents&#92;&quot;&gt;&lt;span class=&#92;&quot;paragraph&#92;&quot;&gt;Hello, islands!&lt;/span&gt;&lt;/span&gt;&quot;'></marimo-mime-renderer>
+ <marimo-mime-renderer data-mime='&quot;text/markdown&quot;' data-data='&quot;&#92;u003cspan class=&#92;&quot;markdown prose dark:prose-invert contents&#92;&quot;&#92;u003e&#92;u003cspan class=&#92;&quot;paragraph&#92;&quot;&#92;u003eHello, islands!&#92;u003c/span&#92;u003e&#92;u003c/span&#92;u003e&quot;'></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,&quot;text/markdown:&lt;span class=&#92;&quot;markdown prose dark:prose-invert contents&#92;&quot;&gt;&lt;span class=&#92;&quot;paragraph&#92;&quot;&gt;Hello&lt;/span&gt;&lt;/span&gt;&quot;]' data-value-types='&quot;python&quot;'></marimo-json-output>
+ <marimo-json-output data-json-data='[1,&quot;text/markdown:&#92;u003cspan class=&#92;&quot;markdown prose dark:prose-invert contents&#92;&quot;&#92;u003e&#92;u003cspan class=&#92;&quot;paragraph&#92;&quot;&#92;u003eHello&#92;u003c/span&#92;u003e&#92;u003c/span&#92;u003e&quot;]' data-value-types='&quot;python&quot;'></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 file
diff --git a/tests/_islands/snapshots/island-no-code.txt b/tests/_islands/snapshots/island-no-code.txt
index 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='&quot;text/markdown&quot;' data-data='&quot;&lt;span class=&#92;&quot;markdown prose dark:prose-invert contents&#92;&quot;&gt;&lt;span class=&#92;&quot;paragraph&#92;&quot;&gt;Hello, islands!&lt;/span&gt;&lt;/span&gt;&quot;'></marimo-mime-renderer>
+ <marimo-mime-renderer data-mime='&quot;text/markdown&quot;' data-data='&quot;&#92;u003cspan class=&#92;&quot;markdown prose dark:prose-invert contents&#92;&quot;&#92;u003e&#92;u003cspan class=&#92;&quot;paragraph&#92;&quot;&#92;u003eHello, islands!&#92;u003c/span&#92;u003e&#92;u003c/span&#92;u003e&quot;'></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 file
diff --git a/tests/_islands/snapshots/island.txt b/tests/_islands/snapshots/island.txt
index 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='&quot;text/markdown&quot;' data-data='&quot;&lt;span class=&#92;&quot;markdown prose dark:prose-invert contents&#92;&quot;&gt;&lt;span class=&#92;&quot;paragraph&#92;&quot;&gt;Hello, islands!&lt;/span&gt;&lt;/span&gt;&quot;'></marimo-mime-renderer>
+ <marimo-mime-renderer data-mime='&quot;text/markdown&quot;' data-data='&quot;&#92;u003cspan class=&#92;&quot;markdown prose dark:prose-invert contents&#92;&quot;&#92;u003e&#92;u003cspan class=&#92;&quot;paragraph&#92;&quot;&#92;u003eHello, islands!&#92;u003c/span&#92;u003e&#92;u003c/span&#92;u003e&quot;'></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 file
diff --git a/tests/_islands/snapshots/markdown.txt b/tests/_islands/snapshots/markdown.txt
index 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='&quot;text/markdown&quot;' data-data='&quot;&lt;span class=&#92;&quot;markdown prose dark:prose-invert contents&#92;&quot;&gt;&lt;h1 id=&#92;&quot;hello-markdown&#92;&quot;&gt;Hello, Markdown!&lt;/h1&gt;&#92;n&lt;span class=&#92;&quot;paragraph&#92;&quot;&gt;This is a paragsraph.&lt;/span&gt;&#92;n&lt;div class=&#92;&quot;language-python3 codehilite&#92;&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class=&#92;&quot;n&#92;&quot;&gt;mo&lt;/span&gt;&lt;span class=&#92;&quot;o&#92;&quot;&gt;.&lt;/span&gt;&lt;span class=&#92;&quot;n&#92;&quot;&gt;md&lt;/span&gt;&lt;span class=&#92;&quot;p&#92;&quot;&gt;(&lt;/span&gt;&#92;n&lt;span class=&#92;&quot;w&#92;&quot;&gt; &lt;/span&gt;&lt;span class=&#92;&quot;sd&#92;&quot;&gt;&amp;#39;&amp;#39;&amp;#39;&lt;/span&gt;&#92;n&lt;span class=&#92;&quot;sd&#92;&quot;&gt; # Hello, Markdown!&lt;/span&gt;&#92;n&#92;n&lt;span class=&#92;&quot;sd&#92;&quot;&gt; This is a paragraph.&lt;/span&gt;&#92;n&lt;span class=&#92;&quot;sd&#92;&quot;&gt; &amp;#39;&amp;#39;&amp;#39;&lt;/span&gt;&#92;n&lt;span class=&#92;&quot;p&#92;&quot;&gt;)&lt;/span&gt;&#92;n&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/span&gt;&quot;'></marimo-mime-renderer>
+ <marimo-mime-renderer data-mime='&quot;text/markdown&quot;' data-data='&quot;&#92;u003cspan class=&#92;&quot;markdown prose dark:prose-invert contents&#92;&quot;&#92;u003e&#92;u003ch1 id=&#92;&quot;hello-markdown&#92;&quot;&#92;u003eHello, Markdown!&#92;u003c/h1&#92;u003e&#92;n&#92;u003cspan class=&#92;&quot;paragraph&#92;&quot;&#92;u003eThis is a paragsraph.&#92;u003c/span&#92;u003e&#92;n&#92;u003cdiv class=&#92;&quot;language-python3 codehilite&#92;&quot;&#92;u003e&#92;u003cpre&#92;u003e&#92;u003cspan&#92;u003e&#92;u003c/span&#92;u003e&#92;u003ccode&#92;u003e&#92;u003cspan class=&#92;&quot;n&#92;&quot;&#92;u003emo&#92;u003c/span&#92;u003e&#92;u003cspan class=&#92;&quot;o&#92;&quot;&#92;u003e.&#92;u003c/span&#92;u003e&#92;u003cspan class=&#92;&quot;n&#92;&quot;&#92;u003emd&#92;u003c/span&#92;u003e&#92;u003cspan class=&#92;&quot;p&#92;&quot;&#92;u003e(&#92;u003c/span&#92;u003e&#92;n&#92;u003cspan class=&#92;&quot;w&#92;&quot;&#92;u003e &#92;u003c/span&#92;u003e&#92;u003cspan class=&#92;&quot;sd&#92;&quot;&#92;u003e&amp;#39;&amp;#39;&amp;#39;&#92;u003c/span&#92;u003e&#92;n&#92;u003cspan class=&#92;&quot;sd&#92;&quot;&#92;u003e # Hello, Markdown!&#92;u003c/span&#92;u003e&#92;n&#92;n&#92;u003cspan class=&#92;&quot;sd&#92;&quot;&#92;u003e This is a paragraph.&#92;u003c/span&#92;u003e&#92;n&#92;u003cspan class=&#92;&quot;sd&#92;&quot;&#92;u003e &amp;#39;&amp;#39;&amp;#39;&#92;u003c/span&#92;u003e&#92;n&#92;u003cspan class=&#92;&quot;p&#92;&quot;&#92;u003e)&#92;u003c/span&#92;u003e&#92;n&#92;u003c/code&#92;u003e&#92;u003c/pre&#92;u003e&#92;u003c/div&#92;u003e&#92;u003c/span&#92;u003e&quot;'></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 file
diff --git a/tests/_plugins/core/test_web_component.py b/tests/_plugins/core/test_web_component.py
index 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 latex
assert "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 parsed
diff --git a/tests/_plugins/stateless/test_mermaid.py b/tests/_plugins/stateless/test_mermaid.py
index 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 mermaid
def test_mo_mermaid_diagram_only() -> None:
assert (
mermaid("graph TD\nA --> B").text
- == "<marimo-mermaid data-diagram='&quot;graph TD&#92;nA --&gt; B&quot;'></marimo-mermaid>"
+ == "<marimo-mermaid data-diagram='&quot;graph TD&#92;nA --&#92;u003e B&quot;'></marimo-mermaid>"
)
@@ -24,7 +24,7 @@ def test_mo_mermaid_theme_and_theme_variables() -> None:
"lineColor": "#475569",
},
).text
- == "<marimo-mermaid data-diagram='&quot;graph TD&#92;nA --&gt; B&quot;' data-theme='&quot;base&quot;' data-theme_variables='{&quot;primaryColor&quot;:&quot;#E8EEF5&quot;,&quot;lineColor&quot;:&quot;#475569&quot;}'></marimo-mermaid>"
+ == "<marimo-mermaid data-diagram='&quot;graph TD&#92;nA --&#92;u003e B&quot;' data-theme='&quot;base&quot;' data-theme_variables='{&quot;primaryColor&quot;:&quot;#E8EEF5&quot;,&quot;lineColor&quot;:&quot;#475569&quot;}'></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='&quot;graph TD&#92;nA --&gt; B&quot;' data-theme='&quot;base&quot;' data-theme_variables='{&quot;primaryColor&quot;:&quot;#E8EEF5&quot;}'></marimo-mermaid>"
+ == "<marimo-mermaid data-diagram='&quot;graph TD&#92;nA --&#92;u003e B&quot;' data-theme='&quot;base&quot;' data-theme_variables='{&quot;primaryColor&quot;:&quot;#E8EEF5&quot;}'></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='&quot;graph TD&#92;nA --&gt; B&quot;' data-theme='&quot;custom-new-theme&quot;'></marimo-mermaid>"
+ == "<marimo-mermaid data-diagram='&quot;graph TD&#92;nA --&#92;u003e B&quot;' data-theme='&quot;custom-new-theme&quot;'></marimo-mermaid>"
)