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
Decouple skew protection from agent-facing endpoints (#8993)
Trevor Manz70bdbe4d843c
diff --git a/marimo/_server/api/endpoints/health.py b/marimo/_server/api/endpoints/health.pyindex 09c5e14ae5..6b65e1c0c4 100644--- a/marimo/_server/api/endpoints/health.py+++ b/marimo/_server/api/endpoints/health.py@@ -100,24 +100,10 @@ class SessionInfo(TypedDict):@router.get("/api/sessions", include_in_schema=False)@requires("edit")async def list_sessions(request: Request) -> JSONResponse:- """List active session IDs and their notebook paths.-- Only available when the server was started without an auth token- (``--no-token``) and without skew protection- (``--no-skew-protection``).- """+ """List active session IDs and their notebook paths."""state = AppState(request)- if state.enable_auth or state.skew_protection:- return JSONResponse(- {- "error": "Session listing requires --no-token and "- "--no-skew-protection"- },- status_code=403,- )-sessions = {session_id: SessionInfo(filename=session.app_file_manager.filename,diff --git a/marimo/_server/api/lifespans.py b/marimo/_server/api/lifespans.pyindex 7ec81054ee..00a754b1b7 100644--- a/marimo/_server/api/lifespans.py+++ b/marimo/_server/api/lifespans.py@@ -213,9 +213,8 @@ async def server_registry(app: Starlette) -> AsyncIterator[None]:"""Register this server in the local registry for discovery.Only servers started **without** an auth token (``--no-token``)- **and** without skew protection (``--no-skew-protection``) are- registered. This ensures only servers that have explicitly opted- into relaxed local access are discoverable.+ are registered. This ensures only servers that have explicitly+ opted into relaxed local access are discoverable."""from marimo._server.server_registry import (ServerRegistryEntry,@@ -225,12 +224,13 @@ async def server_registry(app: Starlette) -> AsyncIterator[None]:state = AppState.from_app(app)# Guard: only register when the user has opted into relaxed local- # access (no auth token, no skew protection).- if state.enable_auth or state.skew_protection:+ # access (no auth token). Skew protection is irrelevant here —+ # it guards against frontend/server version mismatch and should+ # not prevent agent-oriented discovery.+ if state.enable_auth:LOGGER.debug(- "Skipping server registry: auth=%s, skew_protection=%s",+ "Skipping server registry: auth=%s",state.enable_auth,- state.skew_protection,)yieldreturndiff --git a/marimo/_server/api/middleware.py b/marimo/_server/api/middleware.pyindex dbe2158bca..4db2bf6313 100644--- a/marimo/_server/api/middleware.py+++ b/marimo/_server/api/middleware.py@@ -136,6 +136,9 @@ class SkewProtectionMiddleware:"application/x-www-form-urlencoded"):return await self.app(scope, receive, send)+ # If /api/kernel/execute, skip (agent-only endpoint)+ if request.url.path.rstrip("/").endswith("/api/kernel/execute"):+ return await self.app(scope, receive, send)# If ws, skipif request.url.path.startswith("/ws") or request.url.path.endswith("/ws"diff --git a/marimo/_server/server_registry.py b/marimo/_server/server_registry.pyindex 72c57a221b..c32e06b72c 100644--- a/marimo/_server/server_registry.py+++ b/marimo/_server/server_registry.py@@ -8,9 +8,8 @@ read these files to discover running instances — no marimo import orHTTP endpoint is required.SECURITY: Only instances that have opted into relaxed local access —-started **without** an auth token (``--no-token``) **and** without-skew protection (``--no-skew-protection``) — should be registered.-See the guard in ``lifespans.server_registry``.+started **without** an auth token (``--no-token``) — should be+registered. See the guard in ``lifespans.server_registry``."""from __future__ import annotationsdiff --git a/tests/_server/api/test_middleware.py b/tests/_server/api/test_middleware.pyindex 6dd267be4b..fe5220c17a 100644--- a/tests/_server/api/test_middleware.py+++ b/tests/_server/api/test_middleware.py@@ -139,6 +139,23 @@ def with_server(app: Starlette) -> Starlette:return app+def test_skew_protection_skips_execute(edit_app: Starlette) -> None:+ """POST /api/kernel/execute is exempt from skew protection."""+ client = TestClient(edit_app, raise_server_exceptions=False)++ # An invalid skew token would normally return 401, but the execute+ # endpoint is exempt. The endpoint itself will fail (no session),+ # but it should NOT be blocked by skew protection (401).+ response = client.post(+ "/api/kernel/execute",+ headers=token_header("fake-token", "wrong-skew-id"),+ json={"code": "1+1"},+ )+ assert response.status_code != 401, (+ "skew protection should not gate /api/kernel/execute"+ )++def test_skew_protection_disabled() -> None:"""Test that skew protection can be disabled via CLI flag"""app = create_starlette_app(base_url="", skew_protection=False)