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: restrict head_html to run (#9137)

Dylan Madisettie1adaa07f572
diff --git a/marimo/_server/templates/templates.py b/marimo/_server/templates/templates.py
index f4f3209755..891a969f32 100644
--- a/marimo/_server/templates/templates.py
+++ b/marimo/_server/templates/templates.py
@@ -324,8 +324,10 @@ def notebook_page_template(
if html_head:
html = html.replace("</head>", f"{html_head}</head>")
- # Add per-notebook HTML head file contents if specified
- if app_config.html_head_file:
+ # html_head_file is blocked in edit mode: it can contain arbitrary scripts
+ # and markup that could exfiltrate data or redress the UI. CSS-only styling
+ # (css_file) is permitted. Run mode is unaffected.
+ if mode == SessionMode.RUN and app_config.html_head_file:
head_contents = read_html_head_file(
app_config.html_head_file, filename=filepath or filename
)
diff --git a/tests/_server/templates/test_templates.py b/tests/_server/templates/test_templates.py
index 6cae18ec9d..fd2c90c1f7 100644
--- a/tests/_server/templates/test_templates.py
+++ b/tests/_server/templates/test_templates.py
@@ -477,6 +477,91 @@ class TestNotebookPageTemplate(unittest.TestCase):
assert 'crossorigin="anonymous"' in result
_assert_no_leftover_replacements(result)
+ def test_edit_mode_allows_css_file_injection(self) -> None:
+ """In edit mode, css_file is still injected (CSS-only, no scripts)."""
+ css = "/* custom styling */"
+ css_file = self.filename.parent / "style.css"
+ css_file.write_text(css)
+
+ 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="style.css"),
+ filename=str(self.filename),
+ mode=SessionMode.EDIT,
+ )
+
+ head_section = result.split("</head>", 1)[0]
+ assert css in head_section
+ _assert_no_leftover_replacements(result)
+
+ def test_edit_mode_blocks_html_head_file_injection(self) -> None:
+ """In edit mode, html_head_file content must not appear in <head>."""
+ head = '<script src="https://evil.example.com/keylogger.js"></script>'
+ head_file = self.filename.parent / "head.html"
+ head_file.write_text(head)
+
+ 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(html_head_file="head.html"),
+ filename=str(self.filename),
+ mode=SessionMode.EDIT,
+ )
+
+ head_section = result.split("</head>", 1)[0]
+ assert head not in head_section
+ _assert_no_leftover_replacements(result)
+
+ def test_run_mode_injects_html_head_file(self) -> None:
+ """In run mode, html_head_file must be injected into <head>."""
+ head = (
+ '<script src="https://analytics.example.com/tracker.js"></script>'
+ )
+ head_file = self.filename.parent / "head.html"
+ head_file.write_text(head)
+
+ 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(html_head_file="head.html"),
+ filename=str(self.filename),
+ mode=SessionMode.RUN,
+ )
+
+ head_section = result.split("</head>", 1)[0]
+ assert head in head_section
+ _assert_no_leftover_replacements(result)
+
+ def test_global_html_head_not_blocked_in_edit_mode(self) -> None:
+ """The operator-level html_head param is not blocked in edit mode."""
+ global_head = '<meta name="robots" content="noindex">'
+
+ 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=self.app_config,
+ filename=str(self.filename),
+ mode=SessionMode.EDIT,
+ html_head=global_head,
+ )
+
+ head_section = result.split("</head>", 1)[0]
+ assert global_head in head_section
+ _assert_no_leftover_replacements(result)
+
class TestHomePageTemplate(unittest.TestCase):
def setUp(self) -> None: