fix(webui): enforce workspace authority across queued inputs
Keep workspace changes at the authenticated owner boundary. Queue follow-up inputs separately when their tool scope differs from the active run. Co-authored-by: Tenable Research <152317477+tenbbughunters@users.noreply.github.com>
This commit is contained in:
1 parent
4dd3fbecb6
commit
219dd13fd8
12 files changed
+381
-36
No files matched your search
@@ -164,7 +164,7 @@ class WebUICommandRouter:
|
||||
self._message_locks: dict[tuple[str, str], tuple[asyncio.Lock, int]] = {}
|
||||
|
||||
def workspace_project_selection_available(self, connection: ServerConnection) -> bool:
|
||||
return self._http_router.workspace_project_selection_available(connection)
|
||||
return connection in self._webui_connections
|
||||
|
||||
async def send_webui_protocol_error(
|
||||
self,
|
||||
@@ -286,6 +286,37 @@ class WebUICommandRouter:
|
||||
)
|
||||
return None
|
||||
|
||||
async def dispatch_legacy_message(
|
||||
self,
|
||||
connection: ServerConnection,
|
||||
client_id: str,
|
||||
chat_id: str,
|
||||
content: str,
|
||||
) -> None:
|
||||
"""Authorize legacy text frames through the same workspace boundary."""
|
||||
scope = await self.workspace_scope_or_error(
|
||||
connection,
|
||||
lambda: self._workspaces.scope_for_message(
|
||||
{}, chat_id=chat_id, chat_running=False,
|
||||
can_change_project=self.workspace_project_selection_available(connection),
|
||||
),
|
||||
chat_id=chat_id,
|
||||
)
|
||||
if scope is None:
|
||||
return
|
||||
await self._transport.webui_dispatch_message(
|
||||
sender_id=client_id, chat_id=chat_id, content=content,
|
||||
media=None,
|
||||
metadata={
|
||||
"remote": getattr(connection, "remote_address", None),
|
||||
WORKSPACE_SCOPE_METADATA_KEY: scope.metadata(),
|
||||
},
|
||||
is_dm=False,
|
||||
session_key=None,
|
||||
require_existing_session=False,
|
||||
session_generation=None,
|
||||
)
|
||||
|
||||
async def dispatch(
|
||||
self,
|
||||
connection: ServerConnection,
|
||||
|
||||
@@ -314,15 +314,20 @@ class WebUIWorkspaceController:
|
||||
data = await self._sessions.state.read_metadata(session_key)
|
||||
return self._scope_from_snapshot(data)
|
||||
|
||||
def _scope_from_snapshot(self, data: dict[str, Any] | None) -> WorkspaceScope:
|
||||
def _scope_from_snapshot(
|
||||
self, data: dict[str, Any] | None, *, default_scope: WorkspaceScope | None = None,
|
||||
) -> WorkspaceScope:
|
||||
if default_scope is None:
|
||||
default_scope = self.default_scope()
|
||||
if not isinstance(data, dict):
|
||||
return self.default_scope()
|
||||
return default_scope
|
||||
metadata = data.get("metadata", {})
|
||||
if not isinstance(metadata, dict) or WORKSPACE_SCOPE_METADATA_KEY not in metadata:
|
||||
return self.default_scope()
|
||||
return default_scope
|
||||
metadata_data = cast(dict[str, Any], metadata)
|
||||
return self._scope_from_metadata_value(
|
||||
cast(object, metadata_data.get(WORKSPACE_SCOPE_METADATA_KEY))
|
||||
cast(object, metadata_data.get(WORKSPACE_SCOPE_METADATA_KEY)),
|
||||
default_scope=default_scope,
|
||||
)
|
||||
|
||||
def _validate_existing_project(self, metadata: Mapping[str, Any], scope: WorkspaceScope) -> None:
|
||||
@@ -393,17 +398,23 @@ class WebUIWorkspaceController:
|
||||
session_key: str | None,
|
||||
can_change_project: bool,
|
||||
) -> WorkspaceScope:
|
||||
baseline = default_workspace_scope(
|
||||
self._default_workspace,
|
||||
self._default_restrict_to_workspace,
|
||||
source_channel=_WEBUI_SCOPE_CHANNEL,
|
||||
)
|
||||
default = self.default_scope() if can_change_project else baseline
|
||||
data = (
|
||||
await self._sessions.state.read_metadata(session_key)
|
||||
if self._sessions is not None and session_key else None
|
||||
)
|
||||
if data is not None:
|
||||
current = self._scope_from_snapshot(data)
|
||||
current = self._scope_from_snapshot(data, default_scope=default)
|
||||
elif session_key and (draft := self._draft_scopes.get(session_key)) is not None:
|
||||
self._draft_scopes.move_to_end(session_key)
|
||||
current = draft
|
||||
else:
|
||||
current = self.default_scope()
|
||||
current = default
|
||||
raw = envelope.get(WORKSPACE_SCOPE_METADATA_KEY)
|
||||
if raw is None:
|
||||
scope = current
|
||||
@@ -416,7 +427,9 @@ class WebUIWorkspaceController:
|
||||
)
|
||||
if data is not None:
|
||||
self._validate_existing_project(data["metadata"], scope)
|
||||
if not can_change_project and not _scope_change_is_non_escalating(current, scope):
|
||||
# An owner's draft, saved scope or UI preference cannot authorize a client.
|
||||
# Its ceiling is operator config, including deliberately unrestricted use.
|
||||
if not can_change_project and not _scope_change_is_non_escalating(baseline, scope):
|
||||
raise WorkspaceScopeError(
|
||||
"project selection is unavailable for this connection",
|
||||
status=403,
|
||||
|
||||
@@ -448,15 +448,6 @@ class GatewayHTTPHandler:
|
||||
mcp_oauth_redirect_uri=self._mcp_oauth_redirect_uri,
|
||||
)
|
||||
|
||||
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 check_api_token(self, request: WsRequest) -> bool:
|
||||
if getattr(request, "_nanobot_trusted_proxy_authenticated", False):
|
||||
@@ -1726,15 +1717,13 @@ class GatewayHTTPHandler:
|
||||
return _http_error(401, "Unauthorized")
|
||||
return _http_json_response(
|
||||
self.workspaces.payload(
|
||||
can_change_project=self.workspace_project_selection_available(connection),
|
||||
can_change_project=True, # This HTTP route requires owner API authentication.
|
||||
)
|
||||
)
|
||||
|
||||
def _handle_workspace_favorite(self, connection: Any, request: WsRequest) -> Response:
|
||||
if not self.check_api_token(request):
|
||||
return _http_error(401, "Unauthorized")
|
||||
if not self.workspace_project_selection_available(connection):
|
||||
return _http_error(403, "project selection is unavailable for this connection")
|
||||
payload = _mutation_payload(request) or {}
|
||||
try:
|
||||
favorites = self.workspaces.set_favorite_project(payload.get("path"), payload.get("pinned"))
|
||||
@@ -1747,8 +1736,6 @@ class GatewayHTTPHandler:
|
||||
async def _handle_workspace_resolve_project(self, connection: Any, request: WsRequest) -> Response:
|
||||
if not self.check_api_token(request):
|
||||
return _http_error(401, "Unauthorized")
|
||||
if not self.workspace_project_selection_available(connection):
|
||||
return _http_error(403, "project selection is unavailable for this connection")
|
||||
payload = _mutation_payload(request) or {}
|
||||
try:
|
||||
project = self.workspaces.resolve_project(payload.get("path"))
|
||||
@@ -1759,8 +1746,6 @@ class GatewayHTTPHandler:
|
||||
async def _handle_workspace_directories(self, connection: Any, request: WsRequest) -> Response:
|
||||
if not self.check_api_token(request):
|
||||
return _http_error(401, "Unauthorized")
|
||||
if not self.workspace_project_selection_available(connection):
|
||||
return _http_error(403, "project selection is unavailable for this connection")
|
||||
query = _parse_query(request.path)
|
||||
try:
|
||||
payload = await asyncio.to_thread(
|
||||
|
||||
Reference in new issue
Block a user