Skip to content

DeepTutor: Lifelong Personalized Tutoring. https://deeptutor.info/.

fix(learner): allow own-scope settings and workspace writes via route templates

larryluozhang1168103a4382
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") == ""
+ ("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