From 1168103a4382c984bb8f460f8c9a4fd62ead9fd0 Mon Sep 17 00:00:00 2001 From: larryluozhang Date: Mon, 28 Sep 2026 13:19:09 +0800 Subject: [PATCH] fix(learner): allow own-scope settings and workspace writes via route templates MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Follow-up to #1323 (which merged as the prefix surface map): three learner-blocking gaps remained, and all three sit on URL prefixes that also host admin-grade operations, so they are matched on the resolved route template — the same pattern as _LEARNER_KB_READ_ROUTES. - PUT /api/settings/ui — theme/language save into the caller's own per-user interface.json; learners currently get 403 changing language. - PUT/DELETE /api/settings/draft — the settings UI saves through a per-user draft document; catalog apply stays admin-gated in the endpoint itself. - /api/settings/workspace self-management — PUT root (select current binding), POST /validate, POST /registrations, PATCH /registrations/{id}. Data migration/export/root-migration/snapshot routes under the same prefix stay default-denied: they move data across scopes and carry no internal admin gate. --- deeptutor/api/routers/auth.py | 28 +++++++ tests/multi_user/test_learning_surface_map.py | 84 +++++++++++++++++++ 2 files changed, 112 insertions(+) diff --git a/deeptutor/api/routers/auth.py b/deeptutor/api/routers/auth.py index 5764c3819..e783205c3 100644 --- a/deeptutor/api/routers/auth.py +++ b/deeptutor/api/routers/auth.py @@ -607,6 +607,28 @@ _LEARNER_KB_READ_ROUTES = frozenset( ) +_LEARNER_SETTINGS_WRITE_ROUTES = frozenset( + { + # UI preferences resolve through the per-user path service into the + # caller's OWN workspace (interface.json) — saving theme/language is + # safe for learning accounts. + ("PUT", "/api/settings/ui"), + # Per-user settings draft document. Applying a draft to the runtime + # catalog stays admin-gated in the endpoint itself. + ("PUT", "/api/settings/draft"), + ("DELETE", "/api/settings/draft"), + # Workspace self-management: the caller's own registrations inside + # their own scope directory. Data migration/export/root-migration + # routes under the same prefix stay denied — they move data across + # scopes and carry no internal admin gate. + ("PUT", "/api/settings/workspace"), + ("POST", "/api/settings/workspace/validate"), + ("POST", "/api/settings/workspace/registrations"), + ("PATCH", "/api/settings/workspace/registrations/{workspace_id}"), + } +) + + def _learning_surface_for_path( path: str, method: str = "GET", *, route_path: str | None = None ) -> str: @@ -635,6 +657,12 @@ def _learning_surface_for_path( and route_path in _LEARNER_KB_READ_ROUTES ): return "reading" + # Learner-safe settings/workspace writes, matched on the resolved route + # template like the KB reads above: the same URL prefixes also host + # admin-grade operations (catalog apply, data migration), so a plain + # prefix match would open too much. + if route_path and (method.upper(), route_path) in _LEARNER_SETTINGS_WRITE_ROUTES: + return "chat" return "" diff --git a/tests/multi_user/test_learning_surface_map.py b/tests/multi_user/test_learning_surface_map.py index f1bdb0233..d1ee1b129 100644 --- a/tests/multi_user/test_learning_surface_map.py +++ b/tests/multi_user/test_learning_surface_map.py @@ -112,6 +112,90 @@ def test_learner_kb_get_allowlist(path: str, route_path: str, expected: str) -> assert _learning_surface_for_path(path, "GET") == "" +@pytest.mark.parametrize( + ("path", "method", "route_path", "expected"), + [ + # UI preferences and the per-user settings draft. + ("/api/settings/ui", "PUT", "/api/settings/ui", "chat"), + ("/api/settings/draft", "PUT", "/api/settings/draft", "chat"), + ("/api/settings/draft", "DELETE", "/api/settings/draft", "chat"), + # Workspace self-management. + ("/api/settings/workspace", "PUT", "/api/settings/workspace", "chat"), + ( + "/api/settings/workspace/validate", + "POST", + "/api/settings/workspace/validate", + "chat", + ), + ( + "/api/settings/workspace/registrations", + "POST", + "/api/settings/workspace/registrations", + "chat", + ), + ( + "/api/settings/workspace/registrations/ws_1", + "PATCH", + "/api/settings/workspace/registrations/{workspace_id}", + "chat", + ), + # Admin-grade neighbors under the same prefixes stay denied. + ("/api/settings/ui", "POST", "/api/settings/ui", ""), + ("/api/settings/draft", "GET", "/api/settings/draft", ""), + ( + "/api/settings/workspace/registrations", + "GET", + "/api/settings/workspace/registrations", + "", + ), + ( + "/api/settings/workspace/data/migrate", + "POST", + "/api/settings/workspace/data/migrate", + "", + ), + ( + "/api/settings/workspace/data/export", + "POST", + "/api/settings/workspace/data/export", + "", + ), + ( + "/api/settings/workspace/registrations/migrate-root", + "POST", + "/api/settings/workspace/registrations/migrate-root", + "", + ), + ( + "/api/settings/workspace/registrations/ws_1/migrate", + "POST", + "/api/settings/workspace/registrations/{workspace_id}/migrate", + "", + ), + ( + "/api/settings/workspace/registrations/system-snapshot", + "POST", + "/api/settings/workspace/registrations/system-snapshot", + "", + ), + ( + "/api/settings/workspace/knowledge-bases/move", + "POST", + "/api/settings/workspace/knowledge-bases/move", + "", + ), + ], +) +def test_learner_settings_write_allowlist( + path: str, method: str, route_path: str, expected: str +) -> None: + assert _learning_surface_for_path(path, method, route_path=route_path) == expected + if expected: + # Without the resolved route template the guard must stay denied: + # prefix matching alone cannot tell these from admin operations. + assert _learning_surface_for_path(path, method) == "" + + def test_route_template_is_passed_to_surface_guard(monkeypatch) -> None: import asyncio from types import SimpleNamespace