Files
hermes-agent/tests/gateway/test_native_plugin_ws_auth.py
unsupportedpastels 38ceae226d fix(plugins): authenticate Desktop plugin sockets on a local gateway
A local Desktop talks to its gateway with authMode "native" and no static
token, so pluginSocket dialed /api/plugins/<id>/... with an empty ?token=.
The canonical WS gate (web_server_chat._ws_auth_reason) refused every such
upgrade: Kanban stayed on its 60 s poll, third-party ctx.socket was dead, and
the reconnect loop re-dialed every <=30 s forever, keeping an idle-exit
gateway awake.

The plugin socket now rides the credential its REST twin already uses on a
local gateway: the one-use native-http grant in X-Hermes-Gateway-Ticket.

- Gateway: GatewayRuntimeAPI stamps the raw socket peer on WebSocket scopes
  too. PluginApiWebSocketGate redeems the grant once, ahead of routing
  (native_http.redeem_native_ws: the HTTP seam's exact checks - single header,
  no Origin, loopback peer, ready/served profiles, native-http purpose and
  capability - plus the profile selectors, /api/plugins/ paths only), binds
  the grant profile's home and secret scope for the whole connection, and
  requires both that profile's and the launch profile's plugin consent before
  accept. The route's credential check reads that principal and never
  redeems. /api/ws and other core streams keep their purpose-bound credentials.
- Desktop main: hermes:plugin:ws-url mints the grant for the routed profile
  and reserves a one-use dial bound to its origin (scheme, host, port) and
  path. The path is checked one decoded segment at a time (dot segments, an
  encoded slash or backslash, double encoding and bad escapes are refused),
  then parsed with the real URL parser and required to stay under
  /api/plugins/<id>/, and the dial records that parsed path. The webRequest
  hook strips Origin and attaches the header only to an upgrade that matches
  that dial, so the grant can't reach another host or another plugin's
  path. The grant never enters a renderer URL. The redial/mint/prepare
  branch shared by both gateway-socket IPCs moves to
  local-gateway.localGatewayDial (main.ts stays under its line cap).
- Renderer: pluginSocket asks main for the URL when authMode is native, with
  a deadline (a main process that never answers counts as a refused dial),
  and stops after 5 dials in a row that never stay open, including a socket
  that opens and closes at once (polling fallback stays).

Refs #134889 (PLG-1)
Refs #139523
2026-10-16 12:45:38 +00:00

53 lines
2.4 KiB
Python

"""Desktop plugin WebSockets on a local gateway ride the private native-HTTP grant (PLG-1).
A local Desktop has no static dashboard token (``authMode: native``), so the legacy ``?token=``
upgrade always failed and Kanban fell back to polling. The plugin socket presents the same
one-use ``native-http`` grant its REST twin sends, in the same header, under the same peer/Origin
rules; anything else stays refused.
"""
import asyncio
import json
import pytest
from websockets.asyncio.client import connect
from websockets.exceptions import InvalidStatus
from tests.gateway.test_native_http_auth import daemon, headers, ticket
async def _upgrade(origin, path, extra=None):
url = origin.replace('http:', 'ws:') + path
try:
async with connect(url, additional_headers=extra or {}, open_timeout=10):
return 'open'
except InvalidStatus as exc:
return exc.response.status_code
@pytest.mark.platforms("linux")
def test_plugin_socket_accepts_native_grant_only(tmp_path):
with daemon(tmp_path) as (home, descriptor):
origin = descriptor['api_origin']
events = '/api/plugins/kanban/events'
def up(path=events, extra=None):
return asyncio.run(_upgrade(origin, path, extra))
assert up() != 'open'
assert up(events + '?token=') != 'open'
grant = ticket(home, descriptor)
assert up(extra=headers(grant)) == 'open'
assert up(extra=headers(grant)) != 'open' # one use
assert up(extra=headers('invalid')) != 'open'
for purpose in ('interactive', 'worker-adoption'):
assert up(extra=headers(ticket(home, descriptor, purpose))) != 'open', purpose
for browser_origin in ('https://evil.example', 'null'):
assert up(extra={**headers(ticket(home, descriptor)), 'Origin': browser_origin}) != 'open'
assert up(events + '?profile=other', headers(ticket(home, descriptor))) != 'open'
assert up(events + '?profile=default', headers(ticket(home, descriptor))) == 'open'
# The grant is not a query credential, and opens plugin sockets only.
assert up(events + '?ticket=' + ticket(home, descriptor)) != 'open'
for core in ('/api/ws', '/api/pty', '/api/audio/speak-stream'):
assert up(core, headers(ticket(home, descriptor))) != 'open', core
print(json.dumps({'plugin_ws_native_grant': 'accepted once; replay/purpose/origin/profile refused'}))