Merge pull request #1662 from larryluozhang/fix/learner-workspace-surface

fix(learner): allow own-scope settings and workspace writes via route templates
This commit is contained in:
Bingxi Zhao
2026-10-04 14:17:07 +08:00
committed by GitHub
2 changed files with 112 additions and 0 deletions

View File

@@ -608,6 +608,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:
@@ -640,6 +662,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 ""

View File

@@ -122,6 +122,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