Skip to content

The agent that grows with you

fix(gateway): no legacy python -m inside the osascript wrapper; POSIX refuses a foreign-owned mailbox

Tekniumf873d668f293
diff --git a/hermes_cli/gateway_runtime_service_identity.py b/hermes_cli/gateway_runtime_service_identity.py
index 32772c85913..ef3b3f4714d 100644
--- a/hermes_cli/gateway_runtime_service_identity.py
+++ b/hermes_cli/gateway_runtime_service_identity.py
@@ -55,7 +55,7 @@ def verify_home(home: Path, configured: str) -> None:
raise ValueError("profile_mismatch")
-def _launch_module(argv: list[str]) -> tuple[str, list[str]] | None:
+def _launch_module(argv: list[str], *, legacy: bool = True) -> tuple[str, list[str]] | None:
"""``(module, its argv)`` for the module launches the installer persists: ``python -m``, or
``_launchers.installation_command``'s two forms for THIS install root (gateway.PROJECT_ROOT): the
store ``<root>/.hermes/bin/hermes --run-module`` launcher, or ``runtime_command``'s inline bootstrap
@@ -65,7 +65,8 @@ def _launch_module(argv: list[str]) -> tuple[str, list[str]] | None:
root = Path(gateway.PROJECT_ROOT).resolve()
executable = Path(argv[0]).name.lower() if argv else ""
if re.fullmatch(r"python(?:w|\d+(?:\.\d+)*)?(?:\.exe)?", executable):
- if argv[1:2] == ["-m"] and len(argv) > 2:
+ # ``python -m`` is the legacy unwrapped plist form; no installer put it inside the osascript wrapper.
+ if legacy and argv[1:2] == ["-m"] and len(argv) > 2:
return argv[2], argv[3:]
if argv[1:3] == ["-I", "-c"] and len(argv) > 3:
module = next((name for name in ("hermes_cli.stderr_timestamp", "hermes_cli.main")
@@ -83,15 +84,16 @@ def _own_store_launcher(executable: str) -> bool:
return os.path.normcase(str(Path(executable).resolve())) == os.path.normcase(str(launcher))
-def verify_gateway_argv(argv: list[str], home: Path, *, wrapped: bool = False) -> None:
+def verify_gateway_argv(argv: list[str], home: Path, *, wrapped: bool = False, bound: bool = False) -> None:
"""``wrapped``: a launcher wrapper was already stripped. A ``hermes`` launcher a wrapper runs must be
the one ``installation_command`` writes for THIS root (``<root>/.hermes/bin/hermes``), never another
- checkout's. A bare unwrapped ``hermes`` keeps verifying by name, as older systemd/schtasks/launchd
+ checkout's. ``bound``: the osascript wrapper was stripped, so only this root's installer forms count
+ (no legacy ``python -m``). A bare unwrapped ``hermes`` keeps verifying by name, as older systemd/schtasks/launchd
definitions persisted it; a wrapped ``python`` is held to ``_launch_module`` as before."""
from gateway.status import looks_like_gateway_command_line
# The installer wraps launchd stderr with this fixed, non-shell module.
- wrapper = _launch_module(argv)
+ wrapper = _launch_module(argv, legacy=not bound)
if wrapper is not None and wrapper[0] == "hermes_cli.stderr_timestamp":
args = wrapper[1]
if len(args) < 4 or args[0] != "--error-log" or args[2] != "--":
@@ -101,7 +103,7 @@ def verify_gateway_argv(argv: list[str], home: Path, *, wrapped: bool = False) -
_unverified()
executable = Path(argv[0]).name.lower()
if re.fullmatch(r"python(?:w|\d+(?:\.\d+)*)?(?:\.exe)?", executable):
- entry = _launch_module(argv)
+ entry = _launch_module(argv, legacy=not bound)
if entry is None or entry[0] != "hermes_cli.main":
_unverified()
args = entry[1]
@@ -214,7 +216,7 @@ _LAUNCHD_JXA = re.compile(
def _verify_launchd_argv(argv: list[str], home: Path) -> None:
command = _launchd_command(argv)
- verify_gateway_argv(command, home, wrapped=command is not argv)
+ verify_gateway_argv(command, home, wrapped=command is not argv, bound=command is not argv)
def _launchd_command(argv: list[str]) -> list[str]:
diff --git a/tests/hermes_cli/test_gateway_runtime_service.py b/tests/hermes_cli/test_gateway_runtime_service.py
index 50c685d1ab9..b814accb7f4 100644
--- a/tests/hermes_cli/test_gateway_runtime_service.py
+++ b/tests/hermes_cli/test_gateway_runtime_service.py
@@ -158,6 +158,7 @@ def _launchd_gateway_command(tmp_path, monkeypatch, *, store, profile=""):
("other_home", "foreign_root"), ("osascript_non_gateway", "service_identity_unverified"),
("own_wrapper_foreign_inner", "service_identity_unverified"),
("osascript_foreign_bare", "service_identity_unverified"),
+ ("osascript_legacy_module", "service_identity_unverified"),
])
def test_launchd_verifies_the_osascript_wrapped_command_the_installer_writes(case, reason, tmp_path, monkeypatch):
"""The Local Network wrapper (#71206) is unwrapped only as its exact template, and the command inside
@@ -186,6 +187,11 @@ def test_launchd_verifies_the_osascript_wrapped_command_the_installer_writes(cas
command = foreign if case == "osascript_foreign_bare" else installation_command(
gw.PROJECT_ROOT, ["--error-log", str(tmp_path / "logs" / "gateway.error.log"), "--", *foreign],
module="hermes_cli.stderr_timestamp", python=sys.executable)
+ if case == "osascript_legacy_module":
+ # The legacy ``python -m`` form from any interpreter: accepted unwrapped, never written inside osascript.
+ command = [sys.executable, "-m", "hermes_cli.stderr_timestamp", "--error-log",
+ str(tmp_path / "logs" / "gateway.error.log"), "--",
+ sys.executable, "-m", "hermes_cli.main", "gateway", "run", "--external-supervisor"]
if case == "osascript_non_gateway":
command = ["/bin/sh", "-c", "id"]
if case == "tampered_bootstrap":
diff --git a/tests/tools/test_bot_live_owner_delivery.py b/tests/tools/test_bot_live_owner_delivery.py
index c7f96eeb121..ea06b22087b 100644
--- a/tests/tools/test_bot_live_owner_delivery.py
+++ b/tests/tools/test_bot_live_owner_delivery.py
@@ -286,3 +286,23 @@ def test_receipt_wait_polls_the_local_receipt_and_outlives_an_unreachable_owner(
settled = mailbox.await_delivery(tmp_path, key, 5)
assert (settled["status"], settled["reply"]) == ("settled", "PONG")
assert dials == []
+
+
[email protected]("linux", "macos")
+def test_mailbox_lock_refuses_a_dir_another_uid_owns_when_chmod_is_denied(tmp_path, monkeypatch):
+ """On POSIX chmod fails only for a dir this uid doesn't own: that mailbox is refused, not used."""
+ root = tmp_path / "runtime" / mailbox.DELIVERY_DIR_NAME
+ root.mkdir(parents=True)
+ real_chmod = Path.chmod
+
+ def deny(target, mode, *, follow_symlinks=True):
+ if Path(target) == root:
+ raise PermissionError(1, "Operation not permitted", str(target))
+ return real_chmod(target, mode, follow_symlinks=follow_symlinks)
+
+ monkeypatch.setattr(Path, "chmod", deny)
+ monkeypatch.setattr(os, "getuid", lambda: root.stat().st_uid + 1)
+ with pytest.raises(PermissionError):
+ with mailbox._locked(tmp_path):
+ pass
+
diff --git a/tools/bot_live_delivery.py b/tools/bot_live_delivery.py
index a94efebc7c0..64cd6c669d5 100644
--- a/tools/bot_live_delivery.py
+++ b/tools/bot_live_delivery.py
@@ -127,6 +127,9 @@ def _locked(home: Path | str):
try:
root.chmod(0o700)
except OSError as exc:
+ # On POSIX chmod fails only when another uid owns the dir: refuse a mailbox someone else controls.
+ if os.name != "nt" and root.stat().st_uid != os.getuid(): # windows-footgun: ok — POSIX only
+ raise
# A foreign Windows ACL can deny FILE_WRITE_ATTRIBUTES even when this process can
# still use the existing mailbox. Hardening must not abort recovery. The poller
# re-enters twice a second, so WARNING once per dir and DEBUG after.