diff --git a/nix/module.nix b/nix/module.nix index ac32ecb..51baf7d 100644 --- a/nix/module.nix +++ b/nix/module.nix @@ -1534,7 +1534,18 @@ in { SupplementaryGroups = [cfg.sessions.group]; WorkingDirectory = cfg.mcp.stateDir; - UMask = "0077"; + # The CLI's own umask and not a tighter one, deliberately. This unit's + # tools write files an operator then reads: `shot --output` and + # `record` produce exactly the files the same verbs produce from a + # shell, and a unit that made them 0600 would be a surprising + # difference between the CLI and the tool that wraps it -- the sort + # that ends with a web server unable to read a capture and nothing + # saying why. + # + # The authorization table is not protected by this. It is `0600` by an + # explicit chmod, inside a `0700` directory declared by the tmpfiles + # entry above, which is what actually guards it. + UMask = "0022"; Restart = "on-failure"; RestartSec = 5; diff --git a/src/hhstream_mcp/http.py b/src/hhstream_mcp/http.py index 115ee02..52e1a2f 100644 --- a/src/hhstream_mcp/http.py +++ b/src/hhstream_mcp/http.py @@ -51,6 +51,23 @@ The deliberate choices in the request path :class:`~hhstream_live.server.RateLimiter` rather than growing a second one. 256-bit tokens make guessing hopeless already; this is so an attacker cannot even try at line speed and so the attempt is visible in one place. + +One operational property worth knowing before enabling this +--------------------------------------------------------- + +**Tool calls are serialised across every caller, not per session.** +:class:`hhstream_mcp.invoke.Runner` holds a process-wide lock because the CLI +it calls prints to a stdout this process shares, and that was invisible while +each client had a process of its own. It is visible here: two agents on two +tokens queue behind each other, and a slow call (a long ``record``, a ``grim`` +wedged on a compositor that has stopped answering) delays everyone's next call +rather than only its own caller's. + +The event loop stays responsive throughout -- a ``ping`` is answered, a +resource read is still off the loop in its own thread -- so this is latency and +never a hang. It is the same limit +``docs/TODO/mcp-in-process-has-no-call-deadline.md`` already describes, with +one more consequence now that the transport is shared. """ from __future__ import annotations @@ -175,26 +192,31 @@ class BearerAuth: async def __call__(self, scope: dict[str, Any], receive: Any, send: Any) -> None: # `lifespan` is the server talking to itself; only `http` carries a - # caller. A `websocket` scope would reach here if a route grew one, - # and falls into the same refusal as an unauthenticated request. + # caller. if scope["type"] == "lifespan": await self.app(scope, receive, send) return if scope["type"] != "http": - await self._refuse(scope, receive, send) + # Nothing here mounts a websocket, so this is unreachable today -- + # but it is refused in that protocol's own terms rather than with + # an `http.response.start` a websocket client cannot read. An + # unreachable branch that would send the wrong frame is a trap for + # whoever makes it reachable. + log.warning("refusing a %r scope: this server is http only", scope["type"]) + await send({"type": "websocket.close", "code": 1008}) return client = (scope.get("client") or ("unknown", 0))[0] header = _header(scope, b"authorization") token = bearer_token(header) if header else None if token is None: - await self._refuse(scope, receive, send, client=client, reason="no bearer token") + await self._refuse(send, client=client, reason="no bearer token") return # A file read, so off the event loop: small, but the discipline is the # one the rest of this package keeps about blocking work. grant = await anyio.to_thread.run_sync(lambda: self.store.verify(token)) if grant is None: - await self._refuse(scope, receive, send, client=client, reason="token refused") + await self._refuse(send, client=client, reason="token refused") return scope[SCOPE_KEY] = grant log.debug("request authorized by token %s", grant.id) @@ -202,8 +224,6 @@ class BearerAuth: async def _refuse( self, - scope: dict[str, Any], - receive: Any, send: Any, *, client: str = "unknown",