fix(webui): support remote project paths and honor picker capabilities (#5673)

* fix(webui): honor remote workspace picker capability

* feat(webui): allow remote project path selection

* test(webui): isolate native picker availability from host platform
This commit is contained in:
chengyongru authored and GitHub committed 2026-09-14 13:14:00 +08:00
1 parent 15976ac559
commit ddd6b302c9
9 files changed
+358 -44

No files matched your search

@@ -2066,7 +2066,10 @@ async def test_remote_access_reduction_rejects_stale_in_flight_message_scope(
@pytest.mark.asyncio
async def test_webui_scope_rejects_non_loopback_custom_scope(bus: MagicMock, tmp_path) -> None:
async def test_remote_webui_scope_rejects_full_access_for_custom_scope(
bus: MagicMock,
tmp_path,
) -> None:
default_workspace = tmp_path / "default"
project = tmp_path / "project"
default_workspace.mkdir()
@@ -2096,7 +2099,7 @@ async def test_webui_scope_rejects_non_loopback_custom_scope(bus: MagicMock, tmp
payload = json.loads(conn.send.await_args.args[0])
assert payload["event"] == "error"
assert payload["detail"] == "workspace_scope_rejected"
assert payload["reason"] == "workspace controls are localhost-only"
assert payload["reason"] == "full workspace access is unavailable for this connection"
assert payload["chat_id"] == "chat-remote"
assert sessions.read_session_file("websocket:chat-remote") is None
@@ -2468,6 +2468,76 @@ async def test_webui_automation_result_is_authenticated_and_returns_only_selecte
await server_task
@pytest.mark.asyncio
async def test_remote_new_chat_accepts_server_project_path_but_rejects_remote_full_access(
bus: MagicMock, tmp_path: Path
) -> None:
project = tmp_path / "remote-project"
other_project = tmp_path / "other-project"
project.mkdir()
other_project.mkdir()
channel = _ch(bus, workspace_path=tmp_path, port=_free_port())
connection = AsyncMock()
connection.remote_address = ("192.168.1.5", 50123)
connection.request = _FakeReq(
{
"Host": "nas.example",
"X-Forwarded-For": "203.0.113.42",
}
)
await channel._dispatch_envelope(
connection,
"remote-webui",
{
"type": "new_chat",
"workspace_scope": {
"project_path": str(project),
"access_mode": "restricted",
},
},
)
attached = next(
payload
for payload in (
json.loads(call.args[0]) for call in connection.send.await_args_list
)
if payload.get("event") == "attached"
)
key = f"websocket:{attached['chat_id']}"
scope = channel.gateway.workspaces.scope_for_session_key(key)
assert scope.project_path == project.resolve()
assert scope.access_mode == "restricted"
connection.send.reset_mock()
await channel._dispatch_envelope(
connection,
"remote-webui",
{
"type": "new_chat",
"workspace_scope": {
"project_path": str(other_project),
"access_mode": "full",
},
},
)
rejected = [
json.loads(call.args[0])
for call in connection.send.await_args_list
if json.loads(call.args[0]).get("event") == "error"
]
assert rejected == [
{
"event": "error",
"detail": "workspace_scope_rejected",
"reason": "full workspace access is unavailable for this connection",
}
]
@pytest.mark.asyncio
async def test_webui_automations_route_lists_all_jobs_and_allows_user_actions(
bus: MagicMock, tmp_path: Path
) -> None:
@@ -3535,6 +3605,54 @@ async def test_workspace_folder_picker_is_local_authenticated_mutation(
pick_folder.assert_awaited_once_with()
@pytest.mark.parametrize(
("connection", "headers", "can_use_full_access", "can_pick_folder"),
[
(
_REMOTE,
{"Host": "nas.example", "X-Forwarded-For": "203.0.113.42"},
False,
False,
),
(
_LOCAL,
{"Host": "nas.example", "X-Forwarded-For": "203.0.113.42"},
False,
False,
),
(_LOCAL, {"Host": "127.0.0.1:8765"}, True, True),
],
)
@pytest.mark.parametrize("native_picker_available", [True, False])
def test_workspace_payload_separates_remote_project_selection_from_full_access(
bus: MagicMock,
monkeypatch: pytest.MonkeyPatch,
native_picker_available: bool,
connection: _FakeConn,
headers: dict[str, str],
can_use_full_access: bool,
can_pick_folder: bool,
) -> None:
monkeypatch.setattr(
"nanobot.webui.ws_http.native_folder_picker_available",
lambda: native_picker_available,
)
channel = _ch(bus)
token = channel.gateway.tokens.issue_api_token(300)
request = _FakeReq(
{"Authorization": f"Bearer {token}", **headers},
path="/api/workspaces",
)
response = channel.gateway.http._handle_workspaces(connection, request)
assert response.status_code == 200
controls = json.loads(response.body.decode())["controls"]
assert controls["can_change_project"] is True
assert controls["can_use_full_access"] is can_use_full_access
assert controls["can_pick_folder"] is (can_pick_folder and native_picker_available)
@pytest.mark.asyncio
async def test_workspace_folder_picker_rejects_direct_http(
bus: MagicMock,
+11 -5
View File
@@ -151,8 +151,11 @@ class WebUICommandRouter:
self.request_operations: dict[str, WebUIRequestOperation] = {}
self.request_locks: dict[ServerConnection, asyncio.Lock] = {}
def workspace_controls_available(self, connection: ServerConnection) -> bool:
return self._http_router.workspace_controls_available(connection)
def workspace_project_selection_available(self, connection: ServerConnection) -> bool:
return self._http_router.workspace_project_selection_available(connection)
def workspace_full_access_available(self, connection: ServerConnection) -> bool:
return self._http_router.workspace_full_access_available(connection)
async def send_webui_protocol_error(
self,
@@ -296,7 +299,8 @@ class WebUICommandRouter:
connection,
lambda: self._workspaces.scope_for_new_chat(
envelope,
controls_available=self.workspace_controls_available(connection),
can_change_project=self.workspace_project_selection_available(connection),
can_use_full_access=self.workspace_full_access_available(connection),
),
)
if scope is None:
@@ -436,7 +440,8 @@ class WebUICommandRouter:
envelope,
chat_id=chat_id,
chat_running=websocket_turn_wall_started_at(chat_id) is not None,
controls_available=self.workspace_controls_available(connection),
can_change_project=self.workspace_project_selection_available(connection),
can_use_full_access=self.workspace_full_access_available(connection),
),
chat_id=chat_id,
)
@@ -580,7 +585,8 @@ class WebUICommandRouter:
envelope,
chat_id=chat_id,
chat_running=websocket_turn_wall_started_at(chat_id) is not None,
controls_available=self.workspace_controls_available(connection),
can_change_project=self.workspace_project_selection_available(connection),
can_use_full_access=self.workspace_full_access_available(connection),
)
),
chat_id=chat_id,
+39 -14
View File
@@ -151,7 +151,8 @@ def workspaces_payload(
*,
default_workspace: Path,
default_restrict_to_workspace: bool,
controls_available: bool,
can_change_project: bool,
can_use_full_access: bool,
folder_picker_available: bool = False,
) -> dict[str, Any]:
default_access_mode = read_webui_default_access_mode()
@@ -169,8 +170,8 @@ def workspaces_payload(
"default_access_mode": default_access_mode,
"default_scope": default_scope.payload(),
"controls": {
"can_change_project": controls_available,
"can_use_full_access": controls_available,
"can_change_project": can_change_project,
"can_use_full_access": can_use_full_access,
"can_pick_folder": folder_picker_available,
},
}
@@ -254,13 +255,15 @@ class WebUIWorkspaceController:
def payload(
self,
*,
controls_available: bool,
can_change_project: bool,
can_use_full_access: bool,
folder_picker_available: bool = False,
) -> dict[str, Any]:
return workspaces_payload(
default_workspace=self._default_workspace,
default_restrict_to_workspace=self._default_restrict_to_workspace,
controls_available=controls_available,
can_change_project=can_change_project,
can_use_full_access=can_use_full_access,
folder_picker_available=folder_picker_available,
)
@@ -269,7 +272,8 @@ class WebUIWorkspaceController:
envelope: dict[str, Any],
*,
session_key: str | None,
controls_available: bool,
can_change_project: bool,
can_use_full_access: bool,
) -> WorkspaceScope:
current = self.scope_for_session_key(session_key) if session_key else self.default_scope()
raw = envelope.get(WORKSPACE_SCOPE_METADATA_KEY)
@@ -282,20 +286,37 @@ class WebUIWorkspaceController:
default_restrict_to_workspace=self._default_restrict_to_workspace,
source_channel=_WEBUI_SCOPE_CHANNEL,
)
if not controls_available and not _scope_change_is_non_escalating(current, scope):
raise WorkspaceScopeError("workspace controls are localhost-only", status=403)
if not can_change_project and not _scope_change_is_non_escalating(current, scope):
raise WorkspaceScopeError(
"project selection is unavailable for this connection",
status=403,
)
if (
not can_use_full_access
and scope.access_mode == "full"
and (
current.access_mode != "full"
or scope.project_path != current.project_path
)
):
raise WorkspaceScopeError(
"full workspace access is unavailable for this connection",
status=403,
)
return scope
def scope_for_new_chat(
self,
envelope: dict[str, Any],
*,
controls_available: bool,
can_change_project: bool,
can_use_full_access: bool,
) -> WorkspaceScope:
return self.scope_from_envelope(
envelope,
session_key=None,
controls_available=controls_available,
can_change_project=can_change_project,
can_use_full_access=can_use_full_access,
)
def scope_for_set_request(
@@ -304,14 +325,16 @@ class WebUIWorkspaceController:
*,
chat_id: str,
chat_running: bool,
controls_available: bool,
can_change_project: bool,
can_use_full_access: bool,
) -> WorkspaceScope:
if chat_running:
raise WorkspaceScopeError("chat_running", status=409)
return self.scope_from_envelope(
envelope,
session_key=webui_session_key(chat_id),
controls_available=controls_available,
can_change_project=can_change_project,
can_use_full_access=can_use_full_access,
)
def scope_for_message(
@@ -320,12 +343,14 @@ class WebUIWorkspaceController:
*,
chat_id: str,
chat_running: bool,
controls_available: bool,
can_change_project: bool,
can_use_full_access: bool,
) -> WorkspaceScope:
scope = self.scope_from_envelope(
envelope,
session_key=webui_session_key(chat_id),
controls_available=controls_available,
can_change_project=can_change_project,
can_use_full_access=can_use_full_access,
)
if (
WORKSPACE_SCOPE_METADATA_KEY in envelope
+35 -3
View File
@@ -407,8 +407,36 @@ class GatewayHTTPHandler:
mcp_oauth_redirect_uri=self._mcp_oauth_redirect_uri,
)
def workspace_controls_available(self, connection: Any) -> bool:
return self._runtime_surface == "native" or _is_localhost(connection)
def workspace_project_selection_available(self, connection: Any) -> bool:
"""Return whether an authenticated WebUI may submit a server path.
Project paths are interpreted by the gateway, not by the browser. The
WebUI route and WebSocket handshake already authenticate the caller,
so remote clients can use the manual server-path picker as well.
"""
return True
def workspace_full_access_available(
self,
connection: Any,
headers: Any | None = None,
) -> bool:
"""Return whether this request may opt into Full Access.
A reverse proxy can make a remote browser look like a localhost TCP
peer. Inspect the original browser headers before granting this
capability so proxy-local requests keep the remote restriction.
"""
if self._runtime_surface == "native":
return True
if headers is None:
request = getattr(connection, "request", None)
headers = getattr(request, "headers", None)
if not isinstance(headers, Mapping):
# Lightweight in-process test connections do not carry a
# handshake request. Real WebSocket connections do.
return _is_localhost(connection)
return _is_local_browser_request(connection, headers)
def workspace_folder_picker_available(
self,
@@ -1439,7 +1467,11 @@ class GatewayHTTPHandler:
return _http_error(401, "Unauthorized")
return _http_json_response(
self.workspaces.payload(
controls_available=self.workspace_controls_available(connection),
can_change_project=self.workspace_project_selection_available(connection),
can_use_full_access=self.workspace_full_access_available(
connection,
request.headers,
),
folder_picker_available=self.workspace_folder_picker_available(
connection,
request,