The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(webapp): create the port-claim lock and its folder user-only
unsupportedpastelse22287fe77c0
diff --git a/hermes_cli/webapp_access.py b/hermes_cli/webapp_access.pyindex c8be3c620e4..63b1c78775c 100644--- a/hermes_cli/webapp_access.py+++ b/hermes_cli/webapp_access.py@@ -209,8 +209,10 @@ def _port_to_serve(home: Path, host: str) -> tuple[int, Optional[int]]:profile = named_profile_home(home)if _own_port(home) is not None or profile is None:return webapp_settings(home).port, None+ lock = profile.parent.parent / _PORT_CLAIM_LOCK+ _create_user_only(lock)try:- with _file_lock(profile.parent.parent / _PORT_CLAIM_LOCK, _PORT_CLAIM_HOLDER, _PORT_CLAIM_TIMEOUT_SECONDS,+ with _file_lock(lock, _PORT_CLAIM_HOLDER, _PORT_CLAIM_TIMEOUT_SECONDS,"Timed out waiting for another Hermes profile to choose its Webapp port"):claimed = _claimed_ports(home)for port in range(DEFAULT_PORT + 1, DEFAULT_PORT + 1 + _PORT_SEARCH):@@ -223,6 +225,17 @@ def _port_to_serve(home: Path, host: str) -> tuple[int, Optional[int]]:f"Set webapp.port in {home / 'config.yaml'} to a free port, then start again.")+def _create_user_only(lock: Path) -> None:+ """Create *lock* (0600) and its folder (0700) before :func:`_file_lock` opens it, which would create+ both with the umask's mode: the folder also holds the Webapp's credentials. A folder other users can+ reach is closed to them again, as the credential writers do (POSIX)."""+ from gateway.webapp_listener import _close_to_other_users++ lock.parent.mkdir(mode=0o700, parents=True, exist_ok=True)+ _close_to_other_users(lock.parent)+ os.close(os.open(lock, os.O_WRONLY | os.O_CREAT, 0o600))++def _save_port(port: int) -> None:"""Claim *port* as the ``webapp.port`` of the home in scope, changing nothing else."""from hermes_cli.config import load_config, save_configdiff --git a/tests/hermes_cli/test_webapp_access_ports.py b/tests/hermes_cli/test_webapp_access_ports.pyindex 5266b191ea4..2153dfaff94 100644--- a/tests/hermes_cli/test_webapp_access_ports.py+++ b/tests/hermes_cli/test_webapp_access_ports.py@@ -10,6 +10,7 @@ No test binds a real port: what listens is faked, and the probe itself is checke"""import osimport socket+import statfrom pathlib import Pathimport pytest@@ -144,6 +145,23 @@ def test_a_start_refused_for_no_nous_account_saves_no_port(machine, mode, url):assert _webapp_block(machine["solo"]) == {} and machine["probed"] == [][email protected]("posix")[email protected]("existing", [None, 0o775])+def test_choosing_a_port_keeps_the_credential_folder_user_only(machine, existing):+ """The port-claim lock lives in the root's ``webapp`` folder, beside the Webapp's credentials."""+ folder = machine["root"] / "webapp"+ if existing is not None:+ folder.mkdir()+ folder.chmod(existing)+ previous = os.umask(0o002)+ try:+ webapp_access.plan("lan")+ finally:+ os.umask(previous)+ assert stat.S_IMODE(folder.stat().st_mode) == 0o700+ assert stat.S_IMODE((folder / "port-claim.lock").stat().st_mode) == 0o600++#: How long one Start waits for the other to reach its step in the test below before it fails._SYNC_SECONDS = 20