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
feat(sharing): add MARIMO_RESTRICT_SHARING env var machine-wide (#9756)
Florian Verdonckdc35cdfecf4f
diff --git a/marimo/_config/manager.py b/marimo/_config/manager.pyindex 63755ce357..eadda8c281 100644--- a/marimo/_config/manager.py+++ b/marimo/_config/manager.py@@ -17,6 +17,7 @@ from marimo._config.config import (MarimoConfig,PartialMarimoConfig,RuntimeConfig,+ SharingConfig,SqlOutputType,Theme,WidthType,@@ -36,6 +37,7 @@ from marimo._config.secrets import (mask_secrets_partial,remove_secret_placeholders,)+from marimo._config.settings import GLOBAL_SETTINGSfrom marimo._config.utils import (get_or_create_user_config_path,)@@ -65,6 +67,8 @@ def get_default_config_manager(ProjectConfigManager(current_path),ScriptConfigManager(current_path),EnvConfigManager(),+ # Always merged last; see SecurityConfigManager.+ SecurityConfigManager(),)@@ -127,7 +131,16 @@ class MarimoConfigManager(MarimoConfigReader):*partials: PartialMarimoConfigReader,) -> None:self.user_config_mgr = user_config_mgr- self.partials = partials+ # Security partials are pulled out and always merged last (after every+ # other partial, including any added by with_overrides()), so the+ # enforcements they apply cannot be re-enabled by a lower-priority+ # config source. See SecurityConfigManager.+ self.partials = tuple(+ p for p in partials if not isinstance(p, SecurityConfigManager)+ )+ self.security_partials = tuple(+ p for p in partials if isinstance(p, SecurityConfigManager)+ )def get_user_config(self, *, hide_secrets: bool = True) -> MarimoConfig:"""Get the user configuration"""@@ -136,13 +149,18 @@ class MarimoConfigManager(MarimoConfigReader):def get_config_overrides(self, *, hide_secrets: bool = True) -> PartialMarimoConfig:- """Get the configuration overrides"""- if not self.partials:+ """Get the configuration overrides++ Security partials are merged last, so the enforcements they apply+ cannot be overridden by any other config source.+ """+ all_partials = (*self.partials, *self.security_partials)+ if not all_partials:return {}- if len(self.partials) == 1:- return self.partials[0].get_config(hide_secrets=hide_secrets)+ if len(all_partials) == 1:+ return all_partials[0].get_config(hide_secrets=hide_secrets)result: MarimoConfig = cast(MarimoConfig, {})- for partial in self.partials:+ for partial in all_partials:result = merge_config(result, partial.get_config(hide_secrets=hide_secrets))@@ -164,11 +182,16 @@ class MarimoConfigManager(MarimoConfigReader):def with_overrides(self, overrides: PartialMarimoConfig) -> MarimoConfigManager:- """Get a new config manager with the given overrides"""+ """Get a new config manager with the given overrides++ The new override is appended after the existing partials but before the+ security partials, which the constructor keeps last so they always win.+ """return MarimoConfigManager(self.user_config_mgr,*self.partials,MarimoConfigReaderWithOverrides(overrides),+ *self.security_partials,)@@ -336,6 +359,36 @@ class EnvConfigManager(PartialMarimoConfigReader):return project_config+class SecurityConfigManager(PartialMarimoConfigReader):+ """Machine-wide security enforcements that always take precedence.++ `MarimoConfigManager` merges partials of this type last, after every other+ config source (including any added by `with_overrides()`), so the+ enforcements they apply cannot be re-enabled by a user, project, script,+ env, or runtime override. Intended for restrictions set by infra admins+ outside the user's control, e.g. in a devpod or container spec.++ This is the surface area for future required enforcements; today it only+ handles `MARIMO_RESTRICT_SHARING`.+ """++ def get_config(self, *, hide_secrets: bool = True) -> PartialMarimoConfig:+ del hide_secrets # no secrets are produced here+ config: PartialMarimoConfig = {}+ if GLOBAL_SETTINGS.RESTRICT_SHARING:+ # Hide every external code-sharing affordance (shareable WASM+ # links, molab, HTML publishing) across all notebook sessions.+ # See GLOBAL_SETTINGS.RESTRICT_SHARING. Derive the keys from the+ # schema so any future sharing target is disabled automatically;+ # every key in SharingConfig is a boolean that hides the affordance+ # when False.+ config["sharing"] = cast(+ SharingConfig,+ {key: False for key in SharingConfig.__annotations__},+ )+ return config++class ScriptConfigManager(PartialMarimoConfigReader):"""Read the script configuration following PEP 723diff --git a/marimo/_config/settings.py b/marimo/_config/settings.pyindex 7ab62613da..f0dd7aa529 100644--- a/marimo/_config/settings.py+++ b/marimo/_config/settings.py@@ -27,6 +27,20 @@ class GlobalSettings:DISABLE_AUTH_ON_VIRTUAL_FILES: bool = os.getenv("_MARIMO_DISABLE_AUTH_ON_VIRTUAL_FILES", "false") in ("true", "1")+ # Hide all external code-sharing affordances (shareable WASM links, molab,+ # HTML sharing) across every notebook session. Enforced by+ # SecurityConfigManager, which MarimoConfigManager merges after all other+ # config sources, so it cannot be re-enabled by any config file or runtime+ # override. Intended for machine-wide enforcement set by infra admins in a+ # devpod or container spec.+ #+ # Note: this is a UI-hiding/policy control, not a server-side security+ # boundary -- exported HTML still embeds source and endpoints still serve+ # code. Pair with network egress filtering for defence-in-depth.+ RESTRICT_SHARING: bool = os.getenv("MARIMO_RESTRICT_SHARING", "false") in (+ "true",+ "1",+ )GLOBAL_SETTINGS = GlobalSettings()diff --git a/tests/_config/test_manager.py b/tests/_config/test_manager.pyindex 7c57c0e6de..067d2da65f 100644--- a/tests/_config/test_manager.py+++ b/tests/_config/test_manager.py@@ -14,9 +14,11 @@ from marimo._config.manager import (MarimoConfigManager,MarimoConfigReaderWithOverrides,ScriptConfigManager,+ SecurityConfigManager,UserConfigManager,get_default_config_manager,)+from marimo._config.settings import GLOBAL_SETTINGSif TYPE_CHECKING:from pathlib import Path@@ -660,3 +662,74 @@ def test_env_config_manager_no_env_vars() -> None:manager = EnvConfigManager()config = manager.get_config(hide_secrets=False)assert config == {}+++RESTRICTED_SHARING = {"wasm": False, "html": False, "molab": False}+++def test_restrict_sharing_clamps_config_overrides(+ monkeypatch: pytest.MonkeyPatch,+) -> None:+ """MARIMO_RESTRICT_SHARING forces sharing off in the served overrides.++ The editor serves get_config_overrides() to the frontend, so the+ enforcement must be visible there for the Share affordances to be hidden.+ """+ monkeypatch.setattr(GLOBAL_SETTINGS, "RESTRICT_SHARING", True)+ manager = MarimoConfigManager(+ UserConfigManager(), EnvConfigManager(), SecurityConfigManager()+ )+ overrides = manager.get_config_overrides(hide_secrets=False)+ assert overrides["sharing"] == RESTRICTED_SHARING+++def test_restrict_sharing_overrides_user_config(+ monkeypatch: pytest.MonkeyPatch,+) -> None:+ """The restriction wins over a user config that enables sharing."""+ monkeypatch.setattr(GLOBAL_SETTINGS, "RESTRICT_SHARING", True)+ user = UserConfigManager()+ monkeypatch.setattr(+ user,+ "_load_config",+ lambda: merge_default_config(+ {"sharing": {"wasm": True, "html": True, "molab": True}}+ ),+ )+ manager = MarimoConfigManager(+ user, EnvConfigManager(), SecurityConfigManager()+ )+ assert manager.get_config(hide_secrets=False)["sharing"] == (+ RESTRICTED_SHARING+ )+++def test_restrict_sharing_beats_later_override(+ monkeypatch: pytest.MonkeyPatch,+) -> None:+ """A later with_overrides() cannot re-enable sharing.++ with_overrides() appends its partial after EnvConfigManager, but+ MarimoConfigManager keeps the SecurityConfigManager last, so the+ enforcement still wins over the later override.+ """+ monkeypatch.setattr(GLOBAL_SETTINGS, "RESTRICT_SHARING", True)+ manager = MarimoConfigManager(+ UserConfigManager(), EnvConfigManager(), SecurityConfigManager()+ ).with_overrides({"sharing": {"wasm": True, "html": True, "molab": True}})+ assert manager.get_config_overrides(hide_secrets=False)["sharing"] == (+ RESTRICTED_SHARING+ )+++def test_restrict_sharing_disabled_keeps_user_config(+ monkeypatch: pytest.MonkeyPatch,+) -> None:+ """With the flag off, an explicit sharing config is left untouched."""+ monkeypatch.setattr(GLOBAL_SETTINGS, "RESTRICT_SHARING", False)+ manager = MarimoConfigManager(+ UserConfigManager(), EnvConfigManager(), SecurityConfigManager()+ ).with_overrides({"sharing": {"wasm": True}})+ assert manager.get_config_overrides(hide_secrets=False)["sharing"] == {+ "wasm": True+ }