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: sanitize custom.css (#9131)
Myles Scolnick248219c96c7c
diff --git a/marimo/_server/templates/templates.py b/marimo/_server/templates/templates.pyindex f65dff4369..f4f3209755 100644--- a/marimo/_server/templates/templates.py+++ b/marimo/_server/templates/templates.py@@ -4,6 +4,7 @@ from __future__ import annotationsimport htmlimport jsonimport os+import refrom pathlib import Pathfrom textwrap import dedentfrom typing import TYPE_CHECKING, Any, Literal, cast@@ -577,10 +578,24 @@ def get_version() -> str:)+# HTML5 terminates a <style> raw-text element at the first "</style"+# (case-insensitive, optional whitespace). Neutralize that sequence so+# notebook-controlled CSS cannot break out of the block and inject HTML.+_STYLE_END_RE = re.compile(r"</(?=\s*style)", re.IGNORECASE)+++def _sanitize_css_for_style_block(css_contents: str) -> str:+ # "\/" is a valid CSS escape for "/", so the CSS tokenizer still sees+ # a (harmlessly invalid) token, while the HTML parser no longer+ # matches the end-tag.+ return _STYLE_END_RE.sub(r"<\\/", css_contents)++def _custom_css_block(css_contents: str) -> str:# marimo-custom is used by the frontend to identify this stylesheet# comes from marimo- return f"<style title='marimo-custom'>{css_contents}</style>"+ safe = _sanitize_css_for_style_block(css_contents)+ return f"<style title='marimo-custom'>{safe}</style>"def _inject_custom_css_for_config(diff --git a/tests/_server/templates/test_templates.py b/tests/_server/templates/test_templates.pyindex 90862f247c..6cae18ec9d 100644--- a/tests/_server/templates/test_templates.py+++ b/tests/_server/templates/test_templates.py@@ -177,6 +177,45 @@ class TestNotebookPageTemplate(unittest.TestCase):assert css in result_assert_no_leftover_replacements(result)+ def test_notebook_page_template_custom_css_escapes_style_breakout(+ self,+ ) -> None:+ # Regression: a notebook-controlled css_file must not be able to+ # break out of the <style> block and inject script/html. See the+ # pre-execution XSS originally reported against marimo 0.23.0.+ payload = "</style><script>window.__xss__ = 1;</script><style>"+ css_file = self.filename.parent / "custom.css"+ css_file.write_text(payload)++ result = templates.notebook_page_template(+ html=self.html,+ base_url=self.base_url,+ user_config=self.user_config,+ config_overrides=self.config_overrides,+ server_token=self.server_token,+ app_config=_AppConfig(css_file="custom.css"),+ filename=str(self.filename),+ mode=SessionMode.EDIT,+ )++ # The escaped form must be present — proving the sanitizer ran+ # rather than the attacker's payload having been silently dropped.+ assert "<\\/style>" in result+ # And the raw breakout sequence from the payload must not survive.+ # The only literal "</style" the HTML parser should see inside our+ # injected block is the legitimate closer emitted by+ # _custom_css_block itself.+ marker = "<style title='marimo-custom'>"+ assert marker in result+ body_after_marker = result.split(marker, 1)[1]+ # Up to the first real closer, the content is our sanitized CSS;+ # the attacker's payload should be present only in escaped form+ # (so the browser stays in <style> raw-text mode the whole time).+ injected = body_after_marker.split("</style>", 1)[0]+ assert "<\\/style>" in injected+ assert "</style" not in injected # case-sensitive: no raw end-tag+ _assert_no_leftover_replacements(result)+def test_notebook_page_template_custom_css_workspace_relative(self,) -> None: