feat(cli): attach terminal clients to the selected Desktop [skip ci]

NAN-104 requires terminal chat and browser attachment without granting a client ownership of Desktop's gateway. Resolve bounded short-lived credentials over authenticated current-user IPC, pin both Desktop and gateway identities, and reject replacement instances or incompatible clients before any mutation.

Reuse the existing TUI artifact resolver behind a narrow Desktop-only entrypoint, keep credentials out of argv/environment/files, and preserve Python-mode reconnect and gateway leases. Add protocol, real-gateway, and anonymous IPC regression coverage; matching native-host and Windows acceptance remain release gates.
This commit is contained in:
Xubin Ren committed 2026-09-08 10:04:15 +08:00
1 parent b373a95f5c
commit 48e7c4f697
20 files changed
+797 -52

No files matched your search

+6
View File
@@ -60,6 +60,12 @@ class WebUIGatewayEndpoint:
got, query = parse_request_path(request.path)
expected_ws = normalize_config_path(self._config.path)
if got == expected_ws and is_websocket_upgrade(request):
terminal_version = query_first(query, "terminal_protocol")
if terminal_version is not None and (
terminal_version != "1"
or query_first(query, "terminal_instance") != self._tokens.instance_id
):
return connection.respond(409, "Terminal instance or protocol changed")
client_id = query_first(query, "client_id") or ""
if len(client_id) > 128:
client_id = client_id[:128]
+2
View File
@@ -4,6 +4,7 @@ from __future__ import annotations
import secrets
import time
import uuid
from dataclasses import dataclass, field
from typing import Any, Literal
@@ -19,6 +20,7 @@ class GatewayTokenStore:
"""Own short-lived WebSocket and WebUI API tokens for one gateway process."""
max_tokens: int = 10_000
instance_id: str = field(default_factory=lambda: str(uuid.uuid4()), init=False)
issued_tokens: dict[str, float] = field(default_factory=dict)
issued_token_audiences: dict[str, IssuedTokenAudience] = field(default_factory=dict)
api_tokens: dict[str, float] = field(default_factory=dict)
+11 -1
View File
@@ -511,6 +511,8 @@ class GatewayHTTPHandler:
# Bootstrap
if got == "/webui/bootstrap":
return self._handle_bootstrap(connection, request)
if got == "/webui/terminal":
return self._handle_bootstrap(connection, request, terminal_probe=True)
# Settings routes (delegated)
response = await self.settings_routes.dispatch(connection, request, got)
@@ -601,7 +603,9 @@ class GatewayHTTPHandler:
# -- Bootstrap ----------------------------------------------------------
def _handle_bootstrap(self, connection: Any, request: Any) -> Response:
def _handle_bootstrap(
self, connection: Any, request: Any, *, terminal_probe: bool = False,
) -> Response:
secret = self.config.token_issue_secret.strip() or self.config.token.strip()
is_local_browser = _is_local_browser_request(connection, request.headers)
is_proxy_authenticated = _is_trusted_proxy_authenticated_request(
@@ -616,6 +620,11 @@ class GatewayHTTPHandler:
elif not is_local_browser:
return _http_error(403, "bootstrap is localhost-only")
terminal = {"protocolVersion": 1, "gatewayId": self.tokens.instance_id}
if terminal_probe:
# Capability probing does not allocate credentials or acquire client leases.
return _http_json_response(terminal, extra_headers=_NO_STORE_HEADERS)
if is_proxy_authenticated:
payload = {
"ws_path": _normalize_config_path(self.config.path),
@@ -651,6 +660,7 @@ class GatewayHTTPHandler:
expected_path = _normalize_config_path(self.config.path)
payload = {
"token": token,
"terminal": terminal,
"ws_path": expected_path,
"ws_url": ws_url,
"expires_in": self.config.token_ttl_s,