Files
hermes-agent/tests/hermes_cli/test_shared_session_attach.py
Maciej Lisiewski 6981d9318e test(cli): give the runtime dir our group before setting setgid
macOS clears S_ISGID on chmod when the caller is not in the file's group;
CI's basetemp sits under root:wheel /private/var/tmp, so the test never
had setgid to strip.

Co-authored-by: BearHuddleston <196404565+BearHuddleston@users.noreply.github.com>
2026-11-13 16:54:41 +01:00

552 lines
22 KiB
Python

"""Local owner discovery must fence profile and lease identity."""
import json
import os
import stat
import threading
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
import pytest
from hermes_cli.active_sessions import try_acquire_active_session
def test_discovery_uses_exact_profile_and_owner_handshake(tmp_path):
import hashlib
import hmac as hmac_mod
from urllib.parse import parse_qs, urlsplit
from hermes_cli.shared_session_attach import discover_attach_url
home = tmp_path / "profile"
other = tmp_path / "other"
reply = {}
requests = []
class Handler(BaseHTTPRequestHandler):
def do_GET(self):
requests.append(self.path)
params = parse_qs(urlsplit(self.path).query)
proof = hmac_mod.new(
lease.lease_id.encode(),
(f"hermes-session-attach:{params['session_id'][0]}:{params['nonce'][0]}:"
f"{reply['profile_home']}:{reply['websocket_url']}").encode(),
hashlib.sha256,
).hexdigest()
payload = json.dumps(
{**reply, "nonce": params["nonce"][0], "attach_proof": proof}
).encode()
self.send_response(200)
self.end_headers()
self.wfile.write(payload)
def log_message(self, *args):
pass
server = ThreadingHTTPServer(("127.0.0.1", 0), Handler)
thread = threading.Thread(target=server.serve_forever, daemon=True)
thread.start()
origin = f"http://127.0.0.1:{server.server_port}"
lease, error = try_acquire_active_session(
session_id="same-id", surface="desktop", config={}, registry_home=home,
metadata={"live_session_id": "live", "shared_runtime_url": origin},
)
assert error is None
reply.update(session_id="same-id",
profile_home=str(home.resolve()), websocket_url=origin.replace("http:", "ws:") + "/api/ws?token=real-token")
try:
assert discover_attach_url("same-id", registry_home=other) is None
assert requests == []
assert discover_attach_url("same-id", registry_home=home) == reply["websocket_url"]
assert len(requests) == 1
from hermes_cli.shared_session_attach import configure_tui_attachment
env = {"HERMES_TUI_GATEWAY_URL": " "}
configure_tui_attachment(env, "same-id", registry_home=home)
assert env["HERMES_TUI_GATEWAY_URL"] == reply["websocket_url"]
reply["profile_home"] = str(other.resolve())
with pytest.raises(ValueError, match="identity"):
discover_attach_url("same-id", registry_home=home)
reply["profile_home"] = str(home.resolve())
reply["websocket_url"] = "ws://example.com/api/ws?token=secret"
with pytest.raises(ValueError, match="endpoint"):
discover_attach_url("same-id", registry_home=home)
finally:
lease.release()
server.shutdown()
server.server_close()
thread.join()
def test_discovery_refuses_unsupported_owner_without_releasing_lease(tmp_path, monkeypatch):
from hermes_cli.shared_session_attach import discover_attach_url
from hermes_cli.active_sessions import active_session_registry_snapshot
lease, error = try_acquire_active_session(
session_id="old", surface="desktop", config={}, registry_home=tmp_path,
)
assert error is None
try:
with pytest.raises(ValueError, match="not available in this build") as caught:
discover_attach_url("old", registry_home=tmp_path)
first, details = str(caught.value).splitlines()
assert "hermes --resume old" in first
assert details.startswith("Details: ")
assert active_session_registry_snapshot(tmp_path)[0]["lease_id"] == lease.lease_id
registry = tmp_path / "runtime" / "active_sessions.json"
with monkeypatch.context() as patch:
# Our own pid is never probed (#108005), so model a FOREIGN owner whose inspection is denied.
from hermes_cli.active_sessions import _read_entries, _write_entries
entries = _read_entries(registry)
entries[0]["pid"] = os.getpid() + 2**22
_write_entries(registry, entries)
before = registry.read_bytes()
def denied(pid):
raise PermissionError("process inspection denied")
patch.setattr("gateway.status._pid_exists", denied)
with pytest.raises(RuntimeError, match="liveness is unknown"):
discover_attach_url("old", registry_home=tmp_path)
assert registry.read_bytes() == before
finally:
lease.release()
def test_discovery_failure_message_names_state_and_resume_path(tmp_path):
"""A refused handshake keeps the lease and points at the working alternative."""
from hermes_cli.shared_session_attach import discover_attach_url
class Handler(BaseHTTPRequestHandler):
def do_GET(self):
self.send_response(500)
self.end_headers()
def log_message(self, *args):
pass
server = ThreadingHTTPServer(("127.0.0.1", 0), Handler)
thread = threading.Thread(target=server.serve_forever, daemon=True)
thread.start()
origin = f"http://127.0.0.1:{server.server_port}"
lease, error = try_acquire_active_session(
session_id="held", surface="desktop", config={}, registry_home=tmp_path,
metadata={"live_session_id": "live", "shared_runtime_url": origin},
)
assert error is None
try:
with pytest.raises(ValueError, match="just failed") as caught:
discover_attach_url("held", registry_home=tmp_path)
first, details = str(caught.value).splitlines()
assert "hermes --resume held" in first
assert details.startswith("Details: ")
finally:
lease.release()
server.shutdown()
server.server_close()
thread.join()
def test_discovery_refuses_a_listener_that_only_echoes_the_request(tmp_path):
"""A rogue loopback listener can echo every value the client sent; the handshake
must demand proof of a secret the listener cannot learn from the request."""
from urllib.parse import parse_qs, urlsplit
from hermes_cli.shared_session_attach import discover_attach_url
extra = {}
class RogueHandler(BaseHTTPRequestHandler):
def do_GET(self):
params = parse_qs(urlsplit(self.path).query)
body = json.dumps({
**{k: v[0] for k, v in params.items()},
**extra,
"websocket_url": (
f"ws://127.0.0.1:{self.server.server_port}/api/ws?token=stolen"
),
}).encode()
self.send_response(200)
self.end_headers()
self.wfile.write(body)
def log_message(self, *args):
pass
server = ThreadingHTTPServer(("127.0.0.1", 0), RogueHandler)
thread = threading.Thread(target=server.serve_forever, daemon=True)
thread.start()
origin = f"http://127.0.0.1:{server.server_port}"
lease, error = try_acquire_active_session(
session_id="prey", surface="desktop", config={}, registry_home=tmp_path,
metadata={"live_session_id": "live", "shared_runtime_url": origin},
)
assert error is None
try:
with pytest.raises(ValueError, match="identity"):
discover_attach_url("prey", registry_home=tmp_path)
# A forged proof still refuses, and a non-ASCII one cannot crash the check.
extra["attach_proof"] = "0" * 64
with pytest.raises(ValueError, match="identity"):
discover_attach_url("prey", registry_home=tmp_path)
extra["attach_proof"] = "zéro forgé"
with pytest.raises(ValueError, match="identity"):
discover_attach_url("prey", registry_home=tmp_path)
finally:
lease.release()
server.shutdown()
server.server_close()
thread.join()
def test_the_request_never_discloses_the_lease_id(tmp_path):
"""The lease id is the handshake secret: it must not travel in the request."""
import hashlib
import hmac as hmac_mod
from urllib.parse import parse_qs, urlsplit
from hermes_cli.shared_session_attach import discover_attach_url
requests = []
reply = {}
class Handler(BaseHTTPRequestHandler):
def do_GET(self):
requests.append(self.path)
params = parse_qs(urlsplit(self.path).query)
proof = hmac_mod.new(
lease.lease_id.encode(),
(f"hermes-session-attach:{params['session_id'][0]}:{params['nonce'][0]}:"
f"{reply['profile_home']}:{reply['websocket_url']}").encode(),
hashlib.sha256,
).hexdigest()
body = json.dumps({
**reply, "nonce": params["nonce"][0], "attach_proof": proof,
}).encode()
self.send_response(200)
self.end_headers()
self.wfile.write(body)
def log_message(self, *args):
pass
server = ThreadingHTTPServer(("127.0.0.1", 0), Handler)
thread = threading.Thread(target=server.serve_forever, daemon=True)
thread.start()
home = tmp_path / "profile"
origin = f"http://127.0.0.1:{server.server_port}"
lease, error = try_acquire_active_session(
session_id="quiet", surface="desktop", config={}, registry_home=home,
metadata={"live_session_id": "live", "shared_runtime_url": origin},
)
assert error is None
reply.update(session_id="quiet", profile_home=str(home.resolve()),
websocket_url=origin.replace("http:", "ws:") + "/api/ws?token=real-token")
try:
assert discover_attach_url("quiet", registry_home=home) == reply["websocket_url"]
sent = parse_qs(urlsplit(requests[0]).query)
assert "lease_id" not in sent
assert lease.lease_id not in requests[0]
assert len(sent["client_proof"][0]) == 64 # hex MAC: proof without disclosure
# The documented request MAC, so the server side (#113486) can verify it.
assert sent["client_proof"][0] == hmac_mod.new(
lease.lease_id.encode(),
f"hermes-session-attach-request:quiet:{sent['nonce'][0]}".encode(),
hashlib.sha256,
).hexdigest()
finally:
lease.release()
server.shutdown()
server.server_close()
thread.join()
def _rogue_server():
"""A listener that answers /api/session-attach by echoing the request's own
parameters and offering its own websocket: the registry's port, hijacked."""
from urllib.parse import parse_qs, urlsplit
class RogueHandler(BaseHTTPRequestHandler):
def do_GET(self):
params = parse_qs(urlsplit(self.path).query)
body = json.dumps({
**{k: v[0] for k, v in params.items()},
"websocket_url": (
f"ws://127.0.0.1:{self.server.server_port}/api/ws?token=stolen"
),
}).encode()
self.send_response(200)
self.end_headers()
self.wfile.write(body)
def log_message(self, *args):
pass
server = ThreadingHTTPServer(("127.0.0.1", 0), RogueHandler)
thread = threading.Thread(target=server.serve_forever, daemon=True)
thread.start()
return server, thread, f"http://127.0.0.1:{server.server_port}"
def test_e2e_tui_attachment_refuses_a_rogue_owner_and_keeps_the_env(tmp_path):
"""configure_tui_attachment is the exact seam _launch_tui calls: a rogue
listener must leave HERMES_TUI_GATEWAY_URL unset and raise."""
from hermes_cli.shared_session_attach import configure_tui_attachment
server, thread, origin = _rogue_server()
lease, error = try_acquire_active_session(
session_id="prey", surface="desktop", config={}, registry_home=tmp_path,
metadata={"live_session_id": "live", "shared_runtime_url": origin},
)
assert error is None
try:
env = {}
with pytest.raises(ValueError, match="identity"):
configure_tui_attachment(env, "prey", registry_home=tmp_path)
assert "HERMES_TUI_GATEWAY_URL" not in env
finally:
lease.release()
server.shutdown()
server.server_close()
thread.join()
def test_e2e_launch_tui_exits_cleanly_against_a_rogue_listener(tmp_path, monkeypatch, capsys):
"""hermes --resume against a hijacked port: the refusal reaches the user as
Error + exit 1, before any TUI child is spawned."""
monkeypatch.setenv("HERMES_HOME", str(tmp_path))
server, thread, origin = _rogue_server()
lease, error = try_acquire_active_session(
session_id="prey", surface="desktop", config={}, registry_home=tmp_path,
metadata={"live_session_id": "live", "shared_runtime_url": origin},
)
assert error is None
try:
from hermes_cli.main_tui_launch import _launch_tui
with pytest.raises(SystemExit) as caught:
_launch_tui(resume_session_id="prey")
assert caught.value.code == 1
assert "identity" in capsys.readouterr().err
finally:
lease.release()
server.shutdown()
server.server_close()
thread.join()
@pytest.mark.platforms("posix") # POSIX file modes
def test_registry_entries_are_never_readable_by_other_users(tmp_path):
"""The registry carries the lease ids that key the attach MACs, so every
write must leave the file and its directory closed to other users."""
from hermes_cli.active_sessions import _read_entries, _write_entries
lease, error = try_acquire_active_session(
session_id="locked", surface="desktop", config={}, registry_home=tmp_path,
)
assert error is None
try:
registry = tmp_path / "runtime" / "active_sessions.json"
assert stat.S_IMODE(os.stat(registry).st_mode) & 0o007 == 0
assert stat.S_IMODE(os.stat(registry.parent).st_mode) & 0o007 == 0
# An already-loose registry is tightened again on the next write.
entries = _read_entries(registry, strict=True) # reading strips the bits too
os.chmod(registry, 0o644)
_write_entries(registry, entries)
assert stat.S_IMODE(os.stat(registry).st_mode) & 0o007 == 0
finally:
lease.release()
@pytest.mark.platforms("posix") # POSIX file modes
def test_a_loose_registry_is_closed_on_read_without_a_write(tmp_path):
"""Attach discovery only reads: a registry an older build left world-readable
must not stay readable until some later acquire/release rewrites it."""
from hermes_cli.active_sessions import active_session_registry_snapshot
lease, error = try_acquire_active_session(
session_id="old-build", surface="desktop", config={}, registry_home=tmp_path,
)
assert error is None
try:
registry = tmp_path / "runtime" / "active_sessions.json"
os.chmod(registry, 0o644)
os.chmod(registry.parent, 0o755)
assert active_session_registry_snapshot(tmp_path)[0]["lease_id"] == lease.lease_id
assert stat.S_IMODE(os.stat(registry).st_mode) == 0o640
assert stat.S_IMODE(os.stat(registry.parent).st_mode) == 0o750
finally:
lease.release()
@pytest.mark.platforms("posix") # POSIX file modes
def test_shared_group_home_keeps_group_access_and_setgid(tmp_path):
"""The NixOS module runs Hermes with UMask 0007 under a setgid 2770 HERMES_HOME so
hermes-group users share session fencing. Hardening against other users must only
strip the 'other' bits: squashing to owner-only locks group members out of
active_sessions.lock, and their CLI/gateway then run with no lease at all."""
from hermes_cli.active_sessions import _read_entries, _write_entries
home = tmp_path / "shared"
home.mkdir()
os.chmod(home, 0o2770)
previous = os.umask(0o007)
try:
lease, error = try_acquire_active_session(
session_id="team", surface="cli", config={}, registry_home=home,
)
finally:
os.umask(previous)
assert error is None
try:
runtime = home / "runtime"
registry = runtime / "active_sessions.json"
runtime_mode = os.stat(runtime).st_mode
registry_mode = stat.S_IMODE(os.stat(registry).st_mode)
assert stat.S_IMODE(runtime_mode) & 0o070 == 0o070
assert stat.S_IMODE(runtime_mode) & 0o007 == 0
assert registry_mode & 0o060 == 0o060
assert registry_mode & 0o007 == 0
# Linux hands a setgid parent's bit to a new subdirectory; macOS does not (its
# directories take the parent's group anyway). Set it so both hosts test the strip,
# under our own group: macOS drops setgid on chmod when the owner isn't in the group
# (a CI basetemp under root:wheel /private/var/tmp).
entries = _read_entries(registry, strict=True)
os.chown(runtime, -1, os.getgid())
os.chmod(runtime, 0o2777)
_write_entries(registry, entries)
assert stat.S_IMODE(os.stat(runtime).st_mode) == 0o2770
finally:
lease.release()
def test_discovery_refuses_a_relay_that_swaps_the_websocket(tmp_path):
"""The proof covers the returned socket: a relay that obtained a genuine
proof from the owner cannot substitute its own websocket_url."""
import hashlib
import hmac as hmac_mod
from urllib.parse import parse_qs, urlsplit
from hermes_cli.shared_session_attach import discover_attach_url
home = tmp_path / "profile"
swap = {"on": False}
class RelayHandler(BaseHTTPRequestHandler):
def do_GET(self):
params = parse_qs(urlsplit(self.path).query)
port = self.server.server_port
genuine = f"ws://127.0.0.1:{port}/api/ws?token=real"
proof = hmac_mod.new(
lease.lease_id.encode(),
(f"hermes-session-attach:{params['session_id'][0]}:{params['nonce'][0]}:"
f"{params['profile_home'][0]}:{genuine}").encode(),
hashlib.sha256,
).hexdigest()
body = json.dumps({
"session_id": params["session_id"][0], "nonce": params["nonce"][0],
"profile_home": params["profile_home"][0], "attach_proof": proof,
"websocket_url": (f"ws://127.0.0.1:{port}/api/ws?token=stolen"
if swap["on"] else genuine),
}).encode()
self.send_response(200)
self.end_headers()
self.wfile.write(body)
def log_message(self, *args):
pass
server = ThreadingHTTPServer(("127.0.0.1", 0), RelayHandler)
thread = threading.Thread(target=server.serve_forever, daemon=True)
thread.start()
lease, error = try_acquire_active_session(
session_id="relayed", surface="desktop", config={}, registry_home=home,
metadata={"live_session_id": "live",
"shared_runtime_url": f"http://127.0.0.1:{server.server_port}"},
)
assert error is None
try:
assert discover_attach_url("relayed", registry_home=home).endswith("token=real")
swap["on"] = True
with pytest.raises(ValueError, match="identity"):
discover_attach_url("relayed", registry_home=home)
finally:
lease.release()
server.shutdown()
server.server_close()
thread.join()
@pytest.mark.platforms("posix") # POSIX file modes
def test_discovery_refuses_a_listener_that_reads_the_registry_as_another_user(tmp_path):
"""The informed-listener case: a rogue that forges its proof by reading the
registry is only as dangerous as the registry's file modes let it be. With
the registry closed to other users, a listener confined to what other users can read
(the kernel's rule for a non-owner, simulated here) cannot learn the lease
id and its forged proof is refused."""
import hashlib
import hmac as hmac_mod
from urllib.parse import parse_qs, urlsplit
from hermes_cli.shared_session_attach import discover_attach_url
home = tmp_path / "profile"
def read_lease_id_as_other_user():
"""Apply the kernel's read rule for a non-owner: reading is refused
unless the file and the traversed directory grant other-user access."""
registry = home / "runtime" / "active_sessions.json"
for part in (registry, registry.parent):
if stat.S_IMODE(os.stat(part).st_mode) & 0o007 == 0:
raise PermissionError(f"other users cannot read {part}")
return json.loads(registry.read_text(encoding="utf-8"))["entries"][0]["lease_id"]
class InformedRogueHandler(BaseHTTPRequestHandler):
def do_GET(self):
params = parse_qs(urlsplit(self.path).query)
stolen = f"ws://127.0.0.1:{self.server.server_port}/api/ws?token=stolen"
try:
lease_id = read_lease_id_as_other_user()
except PermissionError:
lease_id = None
if lease_id is None:
proof = "0" * 64 # a stranger can only guess: must be refused
else:
proof = hmac_mod.new(
lease_id.encode(),
(f"hermes-session-attach:{params['session_id'][0]}:{params['nonce'][0]}:"
f"{params['profile_home'][0]}:{stolen}").encode(),
hashlib.sha256,
).hexdigest()
body = json.dumps({
"session_id": params["session_id"][0],
"nonce": params["nonce"][0],
"profile_home": params["profile_home"][0],
"attach_proof": proof,
"websocket_url": stolen,
}).encode()
self.send_response(200)
self.end_headers()
self.wfile.write(body)
def log_message(self, *args):
pass
server = ThreadingHTTPServer(("127.0.0.1", 0), InformedRogueHandler)
thread = threading.Thread(target=server.serve_forever, daemon=True)
thread.start()
origin = f"http://127.0.0.1:{server.server_port}"
lease, error = try_acquire_active_session(
session_id="prey", surface="desktop", config={}, registry_home=home,
metadata={"live_session_id": "live", "shared_runtime_url": origin},
)
assert error is None
try:
registry = home / "runtime" / "active_sessions.json"
assert stat.S_IMODE(os.stat(registry).st_mode) & 0o007 == 0
with pytest.raises(ValueError, match="identity"):
discover_attach_url("prey", registry_home=home)
finally:
lease.release()
server.shutdown()
server.server_close()
thread.join()