Skip to content

The agent that grows with you

fix(bot-screen): lease acquire/release only honour a viewer id this connection minted

teknium115184c5f3fe4
diff --git a/tests/tui_gateway/test_display_methods.py b/tests/tui_gateway/test_display_methods.py
index b8435a0e289..1c3bd858614 100644
--- a/tests/tui_gateway/test_display_methods.py
+++ b/tests/tui_gateway/test_display_methods.py
@@ -65,16 +65,19 @@ def test_thumbnail_is_suppressed_while_a_human_holds_the_lease(monkeypatch, _fre
assert _call(server, "display.thumbnail", {})["result"]["data_url"].endswith("SECRET")
-def test_release_without_viewer_id_cannot_yank_another_viewers_lease(_fresh_lease):
+def test_release_without_viewer_id_cannot_yank_another_viewers_lease(monkeypatch, tmp_path, _fresh_lease):
"""lease.release(None) skips the holder check, so a client that lost its viewer id (or a bare RPC)
- must be refused unless it forces; a matching viewer id and force keep working."""
+ must be refused unless it forces; the holder's own (minted) viewer id and force keep working."""
import tui_gateway.server as server
+ from tools.bot_desktop import runtime
- _fresh_lease.acquire("viewer-1")
+ monkeypatch.setattr(runtime, "rfb_socket_path", lambda: tmp_path / "rfb.sock")
+ mine = _call(server, "display.observe", {})["result"]["viewer_id"]
+ _fresh_lease.acquire(mine)
refused = _call(server, "display.lease.release", {})
assert refused["error"]["data"]["code"] == "viewer_mismatch"
assert _fresh_lease.get().holder == _fresh_lease.HUMAN
- assert _call(server, "display.lease.release", {"viewer_id": "viewer-1"})["result"]["lease"]["holder"] == _fresh_lease.AGENT
+ assert _call(server, "display.lease.release", {"viewer_id": mine})["result"]["lease"]["holder"] == _fresh_lease.AGENT
_fresh_lease.acquire("viewer-2")
assert _call(server, "display.lease.release", {"force": True})["result"]["lease"]["holder"] == _fresh_lease.AGENT
@@ -140,3 +143,33 @@ def test_stop_cannot_kill_the_screen_under_a_human_without_force(monkeypatch, _f
forced = _call(server, "display.stop", {"force": True})["result"]
assert forced["stopped"] is True and stops == [1]
assert _fresh_lease.get().holder == _fresh_lease.AGENT
+
+
+def test_lease_acquire_and_release_only_honour_an_id_this_connection_minted(monkeypatch, tmp_path, _fresh_lease):
+ """The minted identity is worthless if acquire takes any string: a caller could acquire under a
+ made-up id (evicting the human) or release with a guessed one. Both must insist on an id that
+ display.observe minted for THIS connection."""
+ import tui_gateway.server as server
+ from tools.bot_desktop import runtime
+
+ monkeypatch.setattr(runtime, "rfb_socket_path", lambda: tmp_path / "rfb.sock")
+
+ class _Peer:
+ def write(self, obj):
+ return True
+
+ def rpc(peer, method, params):
+ return server.dispatch({"jsonrpc": "2.0", "id": 3, "method": method, "params": params}, peer)
+
+ mine, other = _Peer(), _Peer()
+ minted = rpc(mine, "display.observe", {})["result"]["viewer_id"]
+ forged = rpc(other, "display.lease.acquire", {"viewer_id": "made-up"})
+ assert forged["error"]["data"]["code"] == "viewer_mismatch"
+ assert _fresh_lease.get().holder == _fresh_lease.AGENT
+ ok = rpc(mine, "display.lease.acquire", {"viewer_id": minted})["result"]
+ assert ok["lease"]["holder"] == _fresh_lease.HUMAN
+ # another connection presenting the holder's id (read off the wire) cannot release it either
+ stolen = rpc(other, "display.lease.release", {"viewer_id": minted})
+ assert stolen["error"]["data"]["code"] == "viewer_mismatch"
+ assert _fresh_lease.get().holder == _fresh_lease.HUMAN
+ assert rpc(mine, "display.lease.release", {"viewer_id": minted})["result"]["lease"]["holder"] == _fresh_lease.AGENT
diff --git a/tui_gateway/methods_display.py b/tui_gateway/methods_display.py
index 2f9dca3ab80..79d15e4eb3e 100644
--- a/tui_gateway/methods_display.py
+++ b/tui_gateway/methods_display.py
@@ -116,16 +116,23 @@ def _(rid, params: dict) -> dict:
# viewer ids minted per connection (keyed by the transport that asked), so a reconnecting pane can
# keep its identity — and its lease — while nobody can claim an id minted for another connection.
_minted_viewer_ids: "weakref.WeakKeyDictionary[object, set[str]]" = weakref.WeakKeyDictionary()
+# Transports that cannot be weakly referenced (stdio, slotted, or none bound at all) are one
+# connection per process — the TUI's own pipe — so their minted ids share one process-wide set.
+_unweakable_minted_ids: set[str] = set()
+
+
+def _minted_for_this_connection() -> set[str]:
+ try:
+ return _minted_viewer_ids.setdefault(current_transport(), set())
+ except TypeError:
+ return _unweakable_minted_ids
def _mint_viewer_id(requested: str) -> str:
"""Server-minted viewer identity. ``requested`` is honoured only when THIS connection minted it
earlier; anything else (including a holder id read off display.status) gets a fresh id."""
import secrets
- try:
- mine = _minted_viewer_ids.setdefault(current_transport(), set())
- except TypeError: # stdio / slotted transports cannot be weakly referenced: always mint
- mine = set()
+ mine = _minted_for_this_connection()
if requested in mine:
return requested
viewer_id = secrets.token_urlsafe(16)
@@ -133,6 +140,16 @@ def _mint_viewer_id(requested: str) -> str:
return viewer_id
+def _foreign_viewer_id(rid, viewer_id: str):
+ """The error for a viewer id this connection never minted, or None. acquire/release take the id
+ as a capability, so one a caller invented or read off the wire must be refused — otherwise the
+ minting only shapes the honest path and a made-up id still evicts or releases the human."""
+ if viewer_id in _minted_for_this_connection():
+ return None
+ return _err(rid, _DISPLAY_ERR, "viewer_id was not minted for this connection; call display.observe first",
+ data={"code": "viewer_mismatch"})
+
+
@method("display.observe")
@_profile_scoped
def _(rid, params: dict) -> dict:
@@ -204,6 +221,8 @@ def _(rid, params: dict) -> dict:
viewer_id = str(params.get("viewer_id") or "").strip()
if not viewer_id:
return _err(rid, _DISPLAY_ERR, "viewer_id required")
+ if (refused := _foreign_viewer_id(rid, viewer_id)) is not None:
+ return refused
lease = _bd_lease.acquire(viewer_id, reason=str(params.get("reason") or ""))
return _ok(rid, {"lease": _lease_view(lease)})
@@ -218,6 +237,8 @@ def _(rid, params: dict) -> dict:
if viewer_id is None and not params.get("force") and _bd_lease.human_holds():
return _err(rid, _DISPLAY_ERR, "viewer_id required to release another viewer's lease (or pass force: true)",
data={"code": "viewer_mismatch"})
+ if viewer_id is not None and not params.get("force") and (refused := _foreign_viewer_id(rid, viewer_id)) is not None:
+ return refused
lease = _bd_lease.release(viewer_id)
return _ok(rid, {"lease": _lease_view(lease)})