From c32a2dd2ce31a33a94bc70df9ccb04288d2fcd42 Mon Sep 17 00:00:00 2001 From: Hermenix Date: Sun, 11 Oct 2026 22:44:53 +0000 Subject: [PATCH] fix(mcp): the umask a tool's output deserves, and a websocket refused in its own terms MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Three small corrections to the HTTP transport, none of them cosmetic. **`UMask = "0022"` on the unit, not `0077`.** 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 was never protected by the umask anyway: it is 0600 by an explicit chmod inside a 0700 directory declared by tmpfiles, which is what actually guards it. **A non-`http` scope is refused with `websocket.close`**, not with an `http.response.start` a websocket client cannot read. Nothing here mounts a websocket so the branch is unreachable today, but an unreachable branch that would send the wrong frame is a trap for whoever makes it reachable. **The serialisation this transport widens is written down**, in the module docstring, in `docs/CONTRACT-mcp.md` ยง1 and in the TODO that already owns the underlying limit. `Runner` has always held a process-wide lock, because the CLI it calls prints to a shared stdout -- but over stdio each client had its own process, so a call only ever queued behind that same client's previous call. One process serving many callers makes a stuck call delay *other people's* agents, and the one who notices is not necessarily the one who caused it. Latency and never a hang (the event loop answers a ping throughout), but it changes what the existing deadline TODO is worth. Also drops two parameters `_refuse` never used. Co-Authored-By: Claude Opus 5 Claude-Session: https://claude.ai/code/session_01KchcvnbfhhZhUHy83YL3Ya --- nix/module.nix | 13 ++++++++++++- src/hhstream_mcp/http.py | 34 +++++++++++++++++++++++++++------- 2 files changed, 39 insertions(+), 8 deletions(-) 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",