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.

Accept editor authentication for notebook operations (#11500)

Trevor Manz78d299304094
diff --git a/marimo/_server/api/auth.py b/marimo/_server/api/auth.py
index 5dba8f26d0..bca00fcc54 100644
--- a/marimo/_server/api/auth.py
+++ b/marimo/_server/api/auth.py
@@ -45,7 +45,7 @@ def validate_auth(
# Validate the cookie. The cookie stores a keyed hash of the token,
# never the token itself.
- if hmac.compare_digest(
+ if _matches(
cookie_session.get_access_token(), hash_access_token(auth_token)
):
return True # Success
@@ -53,9 +53,7 @@ def validate_auth(
# Check for access_token
if TOKEN_QUERY_PARAM in conn.query_params:
# Validate the access_token
- if hmac.compare_digest(
- conn.query_params[TOKEN_QUERY_PARAM], auth_token
- ):
+ if _matches(conn.query_params[TOKEN_QUERY_PARAM], auth_token):
LOGGER.debug("Validated access_token from query param")
# Set the cookie
cookie_session.set_access_token(auth_token)
@@ -65,7 +63,7 @@ def validate_auth(
if form_dict is not None:
# Validate the access_token
password = form_dict.get("password")
- if password and hmac.compare_digest(password, auth_token):
+ if password and _matches(password, auth_token):
LOGGER.debug("Validated access_token from form data")
# Set the cookie
cookie_session.set_access_token(auth_token)
@@ -80,20 +78,14 @@ def validate_auth(
scheme, _, credentials = auth.partition(" ")
scheme_lower = scheme.lower()
- if scheme_lower == "bearer" and hmac.compare_digest(
- credentials, auth_token
- ):
+ if scheme_lower == "bearer" and _matches(credentials, auth_token):
LOGGER.debug("Validated bearer token from header")
cookie_session.set_access_token(auth_token)
return True # Success
if scheme_lower == "basic":
username, password = _parse_basic_auth_credentials(credentials)
- if (
- username
- and password
- and hmac.compare_digest(password, auth_token)
- ):
+ if username and password and _matches(password, auth_token):
LOGGER.debug("Validated basic auth from header")
cookie_session.set_access_token(auth_token)
cookie_session.set_username(username)
@@ -103,6 +95,11 @@ def validate_auth(
return False
+def _matches(given: str, expected: str) -> bool:
+ # compare_digest rejects str with non-ASCII characters.
+ return hmac.compare_digest(given.encode(), expected.encode())
+
+
def _parse_basic_auth_credentials(
credentials: str,
) -> tuple[str | None, str | None]:
diff --git a/marimo/_server/api/middleware.py b/marimo/_server/api/middleware.py
index be420fdc91..87d8b2bc84 100644
--- a/marimo/_server/api/middleware.py
+++ b/marimo/_server/api/middleware.py
@@ -124,8 +124,8 @@ class SkewProtectionMiddleware:
request = Request(scope)
state = AppState.from_app(request.app)
- # If not POST request, then skip
- if request.method != "POST":
+ # Only requests that change state need the token
+ if request.method in ("GET", "HEAD", "OPTIONS"):
return await self.app(scope, receive, send)
# Skip the login form submission. The login page is a server-rendered
# plain HTML form and does not attach the server token header.
diff --git a/marimo/_server/api/router.py b/marimo/_server/api/router.py
index 8833d46ad8..0173a9c936 100644
--- a/marimo/_server/api/router.py
+++ b/marimo/_server/api/router.py
@@ -32,7 +32,7 @@ from marimo._server.api.endpoints.sql import router as sql_router
from marimo._server.api.endpoints.storage import router as storage_router
from marimo._server.api.endpoints.terminal import router as terminal_router
from marimo._server.api.endpoints.ws_endpoint import router as ws_router
-from marimo._server.host.api import editor_events_route
+from marimo._server.host.api import editor_routes
from marimo._server.router import APIRouter
if TYPE_CHECKING:
@@ -87,7 +87,7 @@ def build_routes(base_url: str = "") -> list[BaseRoute]:
)
app_router.include_router(lsp_router, prefix="/api/lsp", name="lsp")
# Register before the catch-all mount shared by health, ws, and assets.
- app_router.routes.append(editor_events_route(base_url))
+ app_router.routes.extend(editor_routes(base_url))
app_router.include_router(health_router, name="health")
app_router.include_router(ws_router, name="ws")
app_router.include_router(assets_router, name="assets")
diff --git a/marimo/_server/host/api.py b/marimo/_server/host/api.py
index 1cc42ada38..30d21a1523 100644
--- a/marimo/_server/host/api.py
+++ b/marimo/_server/host/api.py
@@ -44,6 +44,7 @@ from marimo._server.host.notebook_api import (
_context,
_decode,
_mutation,
+ editor_notebook_routes,
notebook_routes,
problem,
)
@@ -56,6 +57,7 @@ if TYPE_CHECKING:
from anyio import CancelScope
from starlette.requests import HTTPConnection, Request
+ from starlette.routing import BaseRoute
from marimo._core.notebook import Notebook
from marimo._core.stream import RoutedEvent
@@ -191,12 +193,15 @@ async def _notebook_events(request: Request) -> Response:
)
-def editor_events_route(base_url: str = "") -> WebSocketRoute:
- """Expose the notebook event socket through editor authentication."""
- return WebSocketRoute(
- f"{base_url}{HOST_API_PATH}/notebooks/{{notebook_id}}/events/ws",
- _editor_events_ws,
- )
+def editor_routes(base_url: str = "") -> list[BaseRoute]:
+ """Expose the notebook event socket and operations to the editor."""
+ prefix = f"{base_url}{HOST_API_PATH}"
+ return [
+ WebSocketRoute(
+ f"{prefix}/notebooks/{{notebook_id}}/events/ws", _editor_events_ws
+ ),
+ *editor_notebook_routes(prefix),
+ ]
async def _editor_events_ws(websocket: WebSocket) -> None:
diff --git a/marimo/_server/host/middleware.py b/marimo/_server/host/middleware.py
index 0554dce4ea..3d2ae605e3 100644
--- a/marimo/_server/host/middleware.py
+++ b/marimo/_server/host/middleware.py
@@ -3,7 +3,8 @@
Host bearer requests must come from loopback and address a loopback name
on the server's port. The bearer token comes from the host discovery record.
-The notebook event socket also accepts the editor's authentication.
+The notebook event socket and operations also accept the editor's
+authentication.
"""
from __future__ import annotations
@@ -20,7 +21,7 @@ from starlette.websockets import WebSocket
from marimo import _loggers
from marimo._server.host import HOST_API_PATH
-from marimo._server.host.api import build_app, editor_events_route
+from marimo._server.host.api import build_app, editor_routes
from marimo._server.host.notebook_api import problem
if TYPE_CHECKING:
@@ -42,7 +43,7 @@ class HostApiMiddleware:
self.app = app
self.prefix = base_url + HOST_API_PATH
self.host_app = build_app()
- self.editor_socket = editor_events_route(base_url)
+ self.editor_routes = editor_routes(base_url)
async def __call__(
self, scope: Scope, receive: Receive, send: Send
@@ -63,9 +64,7 @@ class HostApiMiddleware:
response = problem(
404, "not-found", "App sessions do not expose a host catalog"
)
- elif self.editor_socket.matches(scope)[
- 0
- ] is Match.FULL and not _bearer_matches(
+ elif self._for_editor(scope) and not _bearer_matches(
request.headers.get("Authorization"), context.token
):
# Let the editor middleware load the cookie before authenticating.
@@ -123,6 +122,20 @@ class HostApiMiddleware:
def _is_host_path(self, path: str) -> bool:
return path == self.prefix or path.startswith(self.prefix + "/")
+ def _for_editor(self, scope: Scope) -> bool:
+ if scope.get("method") == "OPTIONS":
+ # Match the operation requested by the CORS preflight.
+ scope = {
+ **scope,
+ "method": HTTPConnection(scope).headers.get(
+ "Access-Control-Request-Method", ""
+ ),
+ }
+ return any(
+ route.matches(scope)[0] is Match.FULL
+ for route in self.editor_routes
+ )
+
async def _respond(
response: Response, scope: Scope, receive: Receive, send: Send
diff --git a/marimo/_server/host/notebook_api.py b/marimo/_server/host/notebook_api.py
index 4b9aa86d64..08d5b41a27 100644
--- a/marimo/_server/host/notebook_api.py
+++ b/marimo/_server/host/notebook_api.py
@@ -140,3 +140,32 @@ def notebook_routes() -> list[Route]:
Route(kind.PATH, _endpoint(kind), methods=[kind.METHOD])
for kind in CALLS
]
+
+
+def editor_notebook_routes(prefix: str) -> list[Route]:
+ """Serve each notebook operation below `prefix` to the editor."""
+ return [
+ Route(
+ prefix + kind.PATH,
+ _editor_endpoint(_endpoint(kind)),
+ methods=[kind.METHOD],
+ )
+ for kind in CALLS
+ ]
+
+
+def _editor_endpoint(
+ serve: Callable[[Request], Awaitable[Response]],
+) -> Callable[[Request], Awaitable[Response]]:
+ async def handle(request: Request) -> Response:
+ # The editor's middleware has already authenticated the request.
+ scopes = request.auth.scopes
+ if "edit" in scopes:
+ return await serve(request)
+ if scopes:
+ return problem(
+ 403, "forbidden", "This editor cannot edit notebooks"
+ )
+ return problem(401, "unauthorized", "Sign in to the editor")
+
+ return handle
diff --git a/tests/_server/api/test_auth.py b/tests/_server/api/test_auth.py
index 668a05f944..5fa2df6275 100644
--- a/tests/_server/api/test_auth.py
+++ b/tests/_server/api/test_auth.py
@@ -260,6 +260,31 @@ async def test_validate_auth_with_missing_password_in_basic_auth(
assert validate_auth(conn) is False
+ ("headers", "query"),
+ [
+ ({"Authorization": "Bearer \xe9"}, []),
+ (
+ {
+ "Authorization": f"Basic {base64.b64encode('u:é'.encode()).decode()}"
+ },
+ [],
+ ),
+ ({}, [("access_token", "é")]),
+ ],
+)
+async def test_validate_auth_rejects_non_ascii_credentials(
+ app: Starlette, headers: dict[str, str], query: list[tuple[str, str]]
+):
+ conn = create_connection(app)
+ # Run all middleware
+ await app.build_middleware_stack()(conn.scope, mock_receive, mock_send)
+ conn._headers = Headers(headers)
+ conn._query_params = QueryParams(query)
+
+ assert validate_auth(conn) is False
+
+
async def test_validate_auth_with_no_auth(app: Starlette):
conn = create_connection(app)
# Run all middleware
diff --git a/tests/_server/host/test_host_api.py b/tests/_server/host/test_host_api.py
index 2df6b1b45c..4d29c2b82c 100644
--- a/tests/_server/host/test_host_api.py
+++ b/tests/_server/host/test_host_api.py
@@ -288,22 +288,23 @@ def test_latin1_superscripts_are_not_a_port(port: str) -> None:
("Bearer", 401),
("Bearer ", 401),
("Basic record-token", 401),
- ("bearer record-token ", 202),
+ # An empty update reaches the route only with the host's bearer.
+ ("bearer record-token ", 400),
],
)
async def test_malformed_bearers_are_401_problems(
harness: Harness, authorization: str, status: int
) -> None:
+ # The notebook record answers only the host's bearer, never the editor.
response = await finished(
harness.app,
- "PUT",
- f"/notebooks/{harness.notebook_id}/runtime",
- headers={"Authorization": authorization, "Idempotency-Key": "start"},
+ "PATCH",
+ f"/notebooks/{harness.notebook_id}",
+ headers={"Authorization": authorization, "Idempotency-Key": "update"},
body=b"{}",
)
assert response.status == status
- if status != 202:
- assert response.headers["content-type"] == "application/problem+json"
+ assert response.headers["content-type"] == "application/problem+json"
# A bare integer is a cursor from a host before this one.
@@ -2175,7 +2176,7 @@ def test_websocket_rejects_invalid_authentication(
("notebooks.example:1234", ("192.0.2.1", 50000), 403),
],
)
-def test_editor_authentication_is_limited_to_the_event_socket(
+def test_editor_authentication_is_limited_to_notebook_events_and_operations(
harness: Harness,
base_url: str,
credential: str,
@@ -2231,10 +2232,31 @@ def test_editor_authentication_is_limited_to_the_event_socket(
},
}
assert harness.backend().calls == []
- for endpoint in (path, f"{path}/events"):
- assert (
- client.get(f"{endpoint}{query}").status_code == rejected_status
+ edited = client.post(
+ f"{path}/edits{query}",
+ headers={
+ "Idempotency-Key": "editor-1",
+ "Marimo-Server-Token": "skew-id-1",
+ },
+ content=edit_body(
+ {
+ "kind": "put_cell",
+ "base_etag": None,
+ "cell": {"id": "fresh", "code": "z = 3"},
+ }
+ ),
+ )
+ assert edited.status_code == 200, edited.text
+ assert set(edited.json()["cells"]) == {"fresh"}
+ for method, endpoint in (
+ ("GET", f"{path}/events"),
+ ("PATCH", path),
+ ("POST", f"{base_url}{HOST_API_PATH}/notebooks"),
+ ):
+ response = client.request(
+ method, f"{endpoint}{query}", json={"path": "other.py"}
)
+ assert response.status_code == rejected_status
with pytest.raises(WebSocketDisconnect) as exc:
with client.websocket_connect(
f"ws://{authority}{base_url}{HOST_API_PATH}/events/ws{query}"
@@ -2243,6 +2265,89 @@ def test_editor_authentication_is_limited_to_the_event_socket(
assert exc.value.code == 1008
+ ("method", "operation"),
+ [("POST", "/edits"), ("PUT", "/runtime"), ("DELETE", "/runtime")],
+)
+ ("query", "headers"),
+ [
+ # A cross-site form cannot send the server token.
+ ("?access_token=fake-token", {}),
+ ("", {"Marimo-Server-Token": "skew-id-1"}),
+ ],
+)
+def test_editor_operations_need_credentials_and_the_server_token(
+ harness: Harness,
+ method: str,
+ operation: str,
+ query: str,
+ headers: dict[str, str],
+) -> None:
+ client = TestClient(
+ with_client_address(harness.app, ("192.0.2.1", 50000)),
+ base_url="http://notebooks.example:1234",
+ )
+ path = f"{HOST_API_PATH}/notebooks/{harness.notebook_id}{operation}"
+ response = client.request(
+ method,
+ f"{path}{query}",
+ headers={**headers, "Idempotency-Key": "editor-1"},
+ content=b"{}",
+ )
+ assert response.status_code == 401
+ assert harness.backends == {}
+
+
[email protected]("base_url", ["", "/app"])
+ ("method", "operation", "origin", "status"),
+ [
+ ("POST", "/edits", "http://editor.example", 200),
+ ("PUT", "/runtime", "http://editor.example", 200),
+ ("DELETE", "/runtime", "http://editor.example", 200),
+ ("PUT", "/runtime", "http://other.example", 400),
+ ("GET", "/edits", "http://editor.example", 403),
+ ("PATCH", "", "http://editor.example", 403),
+ ],
+)
+def test_editor_operation_preflights(
+ harness: Harness,
+ base_url: str,
+ method: str,
+ operation: str,
+ origin: str,
+ status: int,
+) -> None:
+ app = create_starlette_app(
+ base_url=base_url, allow_origins=("http://editor.example",)
+ )
+ get_starlette_server_state_init(
+ session_manager=harness.app.state.session_manager, base_url=base_url
+ ).apply(app.state)
+ app.state.host_context = harness.app.state.host_context
+ with TestClient(
+ with_client_address(app, ("192.0.2.1", 50000)),
+ base_url="http://notebooks.example:1234",
+ ) as client:
+ path = f"{base_url}{HOST_API_PATH}/notebooks/{harness.notebook_id}"
+ response = client.options(
+ f"{path}{operation}",
+ headers={
+ "Origin": origin,
+ "Access-Control-Request-Method": method,
+ "Access-Control-Request-Headers": (
+ "content-type,idempotency-key,marimo-server-token"
+ ),
+ },
+ )
+ assert (
+ response.status_code,
+ response.headers.get("access-control-allow-origin"),
+ ) == (status, origin if status == 200 else None)
+ assert harness.backends == {}
+
+
@pytest.mark.parametrize("token", ["wrong", TOKEN])
def test_event_socket_query_requires_the_editor_token(
harness: Harness, token: str