Skip to content

fix: harden trust-bearing window globals and gate script loading#9330

Merged
mscolnick merged 2 commits intomainfrom
ms/object-freeze
Apr 24, 2026
Merged

fix: harden trust-bearing window globals and gate script loading#9330
mscolnick merged 2 commits intomainfrom
ms/object-freeze

Conversation

@mscolnick
Copy link
Copy Markdown
Contributor

Follow-up to #9318. Extends the frozen first-party marker pattern to
__MARIMO_STATIC__ (backs the virtual-file allowlist) and
__MARIMO_MOUNT_CONFIG__, and adds a shared hasTrustedNotebookContext()
predicate so RenderHTML.replaceSrcScripts refuses notebook-authored
<script src> in untrusted edit mode before any user interaction.

Follow-up to #9318. Extends the frozen first-party marker pattern to
`__MARIMO_STATIC__` (backs the virtual-file allowlist) and
`__MARIMO_MOUNT_CONFIG__`, and adds a shared `hasTrustedNotebookContext()`
predicate so `RenderHTML.replaceSrcScripts` refuses notebook-authored
`<script src>` in untrusted edit mode before any user interaction.
@vercel
Copy link
Copy Markdown

vercel Bot commented Apr 22, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
marimo-docs Ready Ready Preview, Comment Apr 23, 2026 7:39pm

Request Review

@github-actions github-actions Bot added the bash-focus Area to focus on during release bug bash label Apr 22, 2026
@mscolnick mscolnick requested review from manzt and peter-gy April 22, 2026 19:41
@dmadisetti dmadisetti added the bug Something isn't working label Apr 22, 2026
Comment on lines +12 to +29
function isMarimoStaticState(
value: unknown,
): value is Readonly<MarimoStaticState> {
if (typeof value !== "object" || value === null) {
return false;
}
const candidate = value as MarimoStaticState;
if (typeof candidate.files !== "object" || candidate.files === null) {
return false;
}
if (
candidate.modelNotifications !== undefined &&
!Array.isArray(candidate.modelNotifications)
) {
return false;
}
return true;
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we also validate that files is a string-to-string map, instead of only checking if it's an object?

@mscolnick mscolnick marked this pull request as ready for review April 23, 2026 18:50
Copilot AI review requested due to automatic review settings April 23, 2026 18:50
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR hardens marimo’s trust-bearing window globals used by exports/static notebooks and adds a shared trust predicate to prevent notebook-authored <script src> from being appended in untrusted edit mode prior to user interaction.

Changes:

  • Emit __MARIMO_STATIC__ and __MARIMO_MOUNT_CONFIG__ via Object.defineProperty(..., { writable: false, configurable: false }) and Object.freeze(...) to prevent post-emission mutation/redefinition.
  • Add hasTrustedNotebookContext() (run-cell OR trusted export marker OR auto-instantiate OR read/app mode) and use it to gate script loading behavior.
  • Add/adjust frontend tests and update export template snapshots accordingly.

Reviewed changes

Copilot reviewed 17 out of 17 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
tests/_server/templates/snapshots/export1.txt Snapshot update reflecting frozen __MARIMO_STATIC__ and __MARIMO_MOUNT_CONFIG__.
tests/_server/templates/snapshots/export2.txt Snapshot update reflecting frozen __MARIMO_STATIC__ and __MARIMO_MOUNT_CONFIG__.
tests/_server/templates/snapshots/export3.txt Snapshot update reflecting frozen __MARIMO_STATIC__ and __MARIMO_MOUNT_CONFIG__.
tests/_server/templates/snapshots/export4.txt Snapshot update reflecting frozen __MARIMO_STATIC__ and __MARIMO_MOUNT_CONFIG__.
tests/_server/templates/snapshots/export5.txt Snapshot update reflecting frozen __MARIMO_STATIC__ and __MARIMO_MOUNT_CONFIG__.
tests/_server/templates/snapshots/export6.txt Snapshot update reflecting frozen __MARIMO_STATIC__ and __MARIMO_MOUNT_CONFIG__.
tests/_server/templates/data/index.html Makes __MARIMO_MOUNT_CONFIG__ non-writable/non-configurable and frozen in the HTML fixture.
marimo/_server/templates/templates.py Adds _static_state_block() to emit hardened __MARIMO_STATIC__ and uses it in static_notebook_template.
frontend/src/plugins/core/trusted-url.ts Clarifies trust gating rationale for accepting safe data: virtual-file payloads.
frontend/src/plugins/core/sanitize.ts Aligns sanitization trust signals with the trusted export marker (avoid sanitizing in trusted export contexts).
frontend/src/plugins/core/test/RenderHTML.test.ts Adds tests asserting <script src> is dropped in untrusted edit mode and allowed in trusted contexts.
frontend/src/plugins/core/RenderHTML.tsx Gates <script src> appending on hasTrustedNotebookContext().
frontend/src/core/static/static-state.ts Makes __MARIMO_STATIC__ readonly + adds runtime shape validation before treating the page as “static”.
frontend/src/core/static/export-context.ts Adds hasTrustedNotebookContext() combining run-cell/export marker/auto-instantiate/read-mode trust signals.
frontend/src/core/static/tests/static-state.test.ts Tests static-state shape validation logic.
frontend/src/core/static/tests/export-context.test.ts Tests export-context shape validation and hasTrustedNotebookContext() behavior.
frontend/index.html Makes __MARIMO_MOUNT_CONFIG__ non-writable/non-configurable and frozen in the main frontend template.

Comment thread frontend/src/core/static/static-state.ts Outdated
Comment thread frontend/src/plugins/core/RenderHTML.tsx Outdated
Comment thread frontend/src/plugins/core/RenderHTML.tsx
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 17 out of 17 changed files in this pull request and generated no new comments.

@mscolnick mscolnick merged commit 3b9995a into main Apr 24, 2026
48 checks passed
@mscolnick mscolnick deleted the ms/object-freeze branch April 24, 2026 13:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bash-focus Area to focus on during release bug bash bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants