fix(mcp): the umask a tool's output deserves, and a websocket refused in its own terms
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 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01KchcvnbfhhZhUHy83YL3Ya
This commit is contained in:
1 parent
1fab00f2bf
commit
c32a2dd2ce
2 files changed
+39
-8
No files matched your search
+12
-1
@@ -1534,7 +1534,18 @@ in {
|
|||||||
SupplementaryGroups = [cfg.sessions.group];
|
SupplementaryGroups = [cfg.sessions.group];
|
||||||
|
|
||||||
WorkingDirectory = cfg.mcp.stateDir;
|
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";
|
Restart = "on-failure";
|
||||||
RestartSec = 5;
|
RestartSec = 5;
|
||||||
|
|||||||
@@ -51,6 +51,23 @@ The deliberate choices in the request path
|
|||||||
:class:`~hhstream_live.server.RateLimiter` rather than growing a second one.
|
:class:`~hhstream_live.server.RateLimiter` rather than growing a second one.
|
||||||
256-bit tokens make guessing hopeless already; this is so an attacker cannot
|
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.
|
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
|
from __future__ import annotations
|
||||||
@@ -175,26 +192,31 @@ class BearerAuth:
|
|||||||
|
|
||||||
async def __call__(self, scope: dict[str, Any], receive: Any, send: Any) -> None:
|
async def __call__(self, scope: dict[str, Any], receive: Any, send: Any) -> None:
|
||||||
# `lifespan` is the server talking to itself; only `http` carries a
|
# `lifespan` is the server talking to itself; only `http` carries a
|
||||||
# caller. A `websocket` scope would reach here if a route grew one,
|
# caller.
|
||||||
# and falls into the same refusal as an unauthenticated request.
|
|
||||||
if scope["type"] == "lifespan":
|
if scope["type"] == "lifespan":
|
||||||
await self.app(scope, receive, send)
|
await self.app(scope, receive, send)
|
||||||
return
|
return
|
||||||
if scope["type"] != "http":
|
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
|
return
|
||||||
|
|
||||||
client = (scope.get("client") or ("unknown", 0))[0]
|
client = (scope.get("client") or ("unknown", 0))[0]
|
||||||
header = _header(scope, b"authorization")
|
header = _header(scope, b"authorization")
|
||||||
token = bearer_token(header) if header else None
|
token = bearer_token(header) if header else None
|
||||||
if token is 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
|
return
|
||||||
# A file read, so off the event loop: small, but the discipline is the
|
# A file read, so off the event loop: small, but the discipline is the
|
||||||
# one the rest of this package keeps about blocking work.
|
# one the rest of this package keeps about blocking work.
|
||||||
grant = await anyio.to_thread.run_sync(lambda: self.store.verify(token))
|
grant = await anyio.to_thread.run_sync(lambda: self.store.verify(token))
|
||||||
if grant is None:
|
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
|
return
|
||||||
scope[SCOPE_KEY] = grant
|
scope[SCOPE_KEY] = grant
|
||||||
log.debug("request authorized by token %s", grant.id)
|
log.debug("request authorized by token %s", grant.id)
|
||||||
@@ -202,8 +224,6 @@ class BearerAuth:
|
|||||||
|
|
||||||
async def _refuse(
|
async def _refuse(
|
||||||
self,
|
self,
|
||||||
scope: dict[str, Any],
|
|
||||||
receive: Any,
|
|
||||||
send: Any,
|
send: Any,
|
||||||
*,
|
*,
|
||||||
client: str = "unknown",
|
client: str = "unknown",
|
||||||
|
|||||||
Reference in new issue
Block a user