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: Allow black/white list in extensions (#5271)
Dylan Madisetti0a748d531b85
diff --git a/marimo/_entrypoints/registry.py b/marimo/_entrypoints/registry.pyindex f923312e10..f85239b481 100644--- a/marimo/_entrypoints/registry.py+++ b/marimo/_entrypoints/registry.py@@ -1,13 +1,16 @@# Copyright 2025 Marimo. All rights reserved.+import osfrom importlib.metadata import entry_pointsfrom typing import TYPE_CHECKING, Generic, Optional, TypeVar, cast+from marimo import _loggersfrom marimo._entrypoints.ids import KnownEntryPointif TYPE_CHECKING:from importlib.metadata import EntryPointsT = TypeVar("T")+LOGGER = _loggers.marimo_logger()class EntryPointRegistry(Generic[T]):@@ -17,6 +20,14 @@ class EntryPointRegistry(Generic[T]):1. Through an explicit call to `.register(name, value)`2. By looking for Python packages that provide a setuptools entry point group+ The registry can be configured with allowlists and denylists through environment variables:+ - MARIMO_{GROUP}_ALLOWLIST: Comma-separated list of allowed extensions+ - MARIMO_{GROUP}_DENYLIST: Comma-separated list of denied extensions++ Example:+ MARIMO_CELL_EXECUTOR_ALLOWLIST=my-executor,another-executor+ MARIMO_CELL_EXECUTOR_DENYLIST=denied-executor+Usage:registry = EntryPointRegistry[MyType]("my_entrypoint_group")"""@@ -30,13 +41,47 @@ class EntryPointRegistry(Generic[T]):self.entry_point_group: KnownEntryPoint = entry_point_groupself._plugins: dict[str, T] = {}+ # Convert entry point group to env var format (e.g. marimo.cell.executor -> MARIMO_CELL_EXECUTOR)+ self._env_prefix = entry_point_group.replace(".", "_").upper()++ def _is_allowed(self, name: str) -> bool:+ """Check if an extension name is allowed based on environment variables.++ Args:+ name: The name of the extension to check.++ Returns:+ True if the extension is allowed, False otherwise.+ """+ # Check denylist first+ denylist_var = f"{self._env_prefix}_DENYLIST"+ if denylist_var in os.environ:+ denylist = {+ n.strip().lower() for n in os.environ[denylist_var].split(",")+ }+ if name.lower() in denylist:+ return False++ # Then check allowlist+ allowlist_var = f"{self._env_prefix}_ALLOWLIST"+ if allowlist_var in os.environ:+ allowlist = {+ n.strip().lower() for n in os.environ[allowlist_var].split(",")+ }+ return name.lower() in allowlist++ return True+def register(self, name: str, value: T) -> None:- """Register a plugin by name and value.+ """Register a plugin by name and value if it is allowed.Args:name: The name of the plugin.value: The actual plugin object to register."""+ if not self._is_allowed(name):+ LOGGER.debug("Extension ignored %s", name)+ returnself._plugins[name] = valuedef unregister(self, name: str) -> Optional[T]:@@ -59,7 +104,8 @@ class EntryPointRegistry(Generic[T]):registered = list(self._plugins.keys())entry_points_list = get_entry_points(self.entry_point_group)entry_point_names = [ep.name for ep in entry_points_list]- return sorted(set(registered + entry_point_names))+ all_names = set(registered + entry_point_names)+ return sorted(name for name in all_names if self._is_allowed(name))def get(self, name: str) -> T:"""Get a plugin by name, loading it from entry points if necessary.@@ -72,7 +118,12 @@ class EntryPointRegistry(Generic[T]):Raises:KeyError: If the plugin cannot be found.+ ValueError: If the plugin is not allowed by allowlist/denylist."""+ if not self._is_allowed(name):+ LOGGER.debug("Extension ignored %s", name)+ raise ValueError(f"Extension '{name}' is not allowed")+if name in self._plugins:return self._plugins[name]diff --git a/tests/test_entrypoints.py b/tests/test_entrypoints.pyindex d3d0946c82..c39a29748a 100644--- a/tests/test_entrypoints.py+++ b/tests/test_entrypoints.py@@ -1,3 +1,4 @@+import osfrom typing import castfrom unittest.mock import MagicMock, patch@@ -11,7 +12,9 @@ from marimo._runtime.executor import ExecutionConfig, Executor, get_executorclass TestEntryPointRegistry:@pytest.fixturedef registry(self) -> EntryPointRegistry[str]:- return EntryPointRegistry[str](cast(KnownEntryPoint, "plugins"))+ return EntryPointRegistry[str](+ cast(KnownEntryPoint, "marimo.test.group")+ )def test_register_and_get(self, registry: EntryPointRegistry[str]) -> None:registry.register("test", "value")@@ -37,6 +40,79 @@ class TestEntryPointRegistry:assert "EntryPointRegistry" in repr(registry)assert "test" in repr(registry)+ def test_allowlist(self, registry: EntryPointRegistry[str]) -> None:+ with patch.dict(+ os.environ, {"MARIMO_TEST_GROUP_ALLOWLIST": "test1,test2"}+ ):+ # Allowed extension+ registry.register("test1", "value1")+ assert registry.get("test1") == "value1"++ # Not allowed extension - should be silently ignored+ registry.register("test3", "value3")+ assert "test3" not in registry.names()++ # Not allowed extension - should raise on get+ with pytest.raises(ValueError, match="not allowed"):+ registry.get("test3")++ def test_denylist(self, registry: EntryPointRegistry[str]) -> None:+ with patch.dict(+ os.environ, {"MARIMO_TEST_GROUP_DENYLIST": "test2,test3"}+ ):+ # Allowed extension+ registry.register("test1", "value1")+ assert registry.get("test1") == "value1"++ # Denied extension - should be silently ignored+ registry.register("test2", "value2")+ assert "test2" not in registry.names()++ # Denied extension - should raise on get+ with pytest.raises(ValueError, match="not allowed"):+ registry.get("test2")++ def test_allowlist_and_denylist(+ self, registry: EntryPointRegistry[str]+ ) -> None:+ with patch.dict(+ os.environ,+ {+ "MARIMO_TEST_GROUP_ALLOWLIST": "test1,test2",+ "MARIMO_TEST_GROUP_DENYLIST": "test2,test3",+ },+ ):+ # Allowed extension+ registry.register("test1", "value1")+ assert registry.get("test1") == "value1"++ # Denied extension - should be silently ignored even if in allowlist+ registry.register("test2", "value2")+ assert "test2" not in registry.names()++ # Not in allowlist - should be silently ignored+ registry.register("test4", "value4")+ assert "test4" not in registry.names()++ def test_case_insensitive(self, registry: EntryPointRegistry[str]) -> None:+ with patch.dict(+ os.environ,+ {+ "MARIMO_TEST_GROUP_ALLOWLIST": "Test1,TEST2",+ "MARIMO_TEST_GROUP_DENYLIST": "TEST3,test4",+ },+ ):+ # Case-insensitive allowlist match+ registry.register("test1", "value1")+ registry.register("TEST2", "value2")+ assert set(registry.names()) == {"test1", "TEST2"}++ # Case-insensitive denylist match+ registry.register("Test3", "value3")+ registry.register("TEST4", "value4")+ assert "Test3" not in registry.names()+ assert "TEST4" not in registry.names()+@patch("marimo._entrypoints.registry.entry_points")def test_get_entry_points_modern(self, mock_entry_points: MagicMock