Skip to content

The agent that grows with you

fix: report malformed installed plists without leaking parser failures

Teknium1b55d9646aa7
diff --git a/hermes_cli/gateway_runtime_service.py b/hermes_cli/gateway_runtime_service.py
index c3ee2cbc892..673ada67e34 100644
--- a/hermes_cli/gateway_runtime_service.py
+++ b/hermes_cli/gateway_runtime_service.py
@@ -15,6 +15,7 @@ import plistlib
import subprocess
import sys
import time
+from xml.parsers.expat import ExpatError
from gateway.runtime_contract import RuntimeState
from hermes_cli._subprocess_compat import windows_hide_flags
@@ -61,10 +62,10 @@ def _run(argv: list[str] | tuple[str, ...], deadline: float, *, encoding: str |
creationflags=windows_hide_flags(), timeout=remaining(deadline))
-def _verify_binding(verify, *args, **kwargs) -> None:
+def _verify_binding(verify, *args, **kwargs):
try:
- verify(*args, **kwargs)
- except (ValueError, TypeError, KeyError, AttributeError) as exc:
+ return verify(*args, **kwargs)
+ except (ValueError, TypeError, KeyError, AttributeError, ExpatError) as exc:
reason = str(exc)
raise RuntimeStartError(reason if reason in {
"profile_mismatch", "service_account_mismatch", "service_disabled"
@@ -158,7 +159,7 @@ def _launchd(home: Path, deadline: float) -> ExistingService | None:
_verify_binding(verify_launchd_loaded, loaded[0], home, uid=os.getuid(), username=account.pw_name) # windows-footgun: ok — native launchd only
return found[0]
if installed:
- definition = plistlib.loads(read_definition(plist))
+ definition = _verify_binding(plistlib.loads, read_definition(plist))
_verify_binding(verify_launchd_plist, definition, label, home)
# Load ONLY the existing file, never bootout/rewrite or kickstart -k.
return ExistingService("launchd", ("launchctl", "bootstrap", domains[0], str(plist)))
diff --git a/tests/hermes_cli/test_gateway_runtime_service.py b/tests/hermes_cli/test_gateway_runtime_service.py
index f4148a606a1..d0af0042a65 100644
--- a/tests/hermes_cli/test_gateway_runtime_service.py
+++ b/tests/hermes_cli/test_gateway_runtime_service.py
@@ -22,7 +22,6 @@ import pytest
def test_ensure_checks_effective_service_binding_before_start(tmp_path, monkeypatch, case, reason):
from hermes_cli import gateway_runtime as runtime
from hermes_cli.gateway_runtime_service import service_suffix
- import pwd
monkeypatch.setattr(Path, "home", lambda: tmp_path)
home = tmp_path / (".hermes" if case == "default" else "custom root")
@@ -38,6 +37,13 @@ def test_ensure_checks_effective_service_binding_before_start(tmp_path, monkeypa
configured = home
if case in {"wrong_home", "dropin_home"}:
configured = tmp_path / "unrequested"
+ if case == "wrong_home":
+ unit.write_text(f'[Service]\nEnvironment="HERMES_HOME={configured}"\n', encoding="utf-8")
+ original = unit.read_bytes()
+ if case == "dropin_home":
+ override = unit.parent / (unit.name + '.d') / 'override.conf'
+ override.parent.mkdir()
+ override.write_text(f'[Service]\nEnvironment="HERMES_HOME={configured}"\n', encoding="utf-8")
if case == "alias":
configured = tmp_path / "alias"
configured.symlink_to(home, target_is_directory=True)
@@ -72,10 +78,8 @@ def test_ensure_checks_effective_service_binding_before_start(tmp_path, monkeypa
# An older default install may rely on the user manager's HOME.
props['Environment'] = 'HERMES_SUPERVISED_CHILD=1'
manager_env = f'HOME={tmp_path}\n'
- if case == "system":
- props['User'] = pwd.getpwuid(os.getuid()).pw_name
definition = tmp_path / "effective.json"
- definition.write_text(json.dumps({'props': props, 'system': case == 'system', 'env': manager_env}), encoding="utf-8")
+ definition.write_text(json.dumps({'props': props, 'system': False, 'env': manager_env}), encoding="utf-8")
calls = tmp_path / 'calls.jsonl'
executable = tmp_path / 'inert-supervisor'
executable.write_text('#!' + sys.executable + '\nimport json,sys\nfrom pathlib import Path\n'
@@ -96,8 +100,7 @@ def test_ensure_checks_effective_service_binding_before_start(tmp_path, monkeypa
starts = [cmd for cmd in commands if 'start' in cmd]
assert len(starts) == (1 if reason == 'deadline' else 0)
assert all('--no-ask-password' in cmd for cmd in starts)
- if case != 'system':
- assert unit.read_bytes() == original
+ assert unit.read_bytes() == original
assert not (home / 'logs').exists()
diff --git a/tests/hermes_cli/test_gateway_runtime_service_roots.py b/tests/hermes_cli/test_gateway_runtime_service_roots.py
index d73bd51e802..f44e17fe09f 100644
--- a/tests/hermes_cli/test_gateway_runtime_service_roots.py
+++ b/tests/hermes_cli/test_gateway_runtime_service_roots.py
@@ -27,3 +27,12 @@ def test_service_start_does_not_decode_irrelevant_supervisor_output():
from hermes_cli.gateway_runtime_service import ExistingService, start_existing_gateway_service
service = ExistingService('systemd', (sys.executable, '-c', 'import sys; sys.stdout.buffer.write(bytes([255]))'))
start_existing_gateway_service(service, deadline=time.monotonic() + 5)
+
+
+def test_malformed_installed_plist_has_a_bounded_refusal():
+ import plistlib
+ from hermes_cli.gateway_runtime_service import _verify_binding, RuntimeStartError
+ with pytest.raises(RuntimeStartError, match='service_identity_unverified'):
+ _verify_binding(plistlib.loads, b'<?xml version="1.0"?><plist><dict>')
+ definition = {'Label': 'ai.hermes.gateway'}
+ assert _verify_binding(plistlib.loads, plistlib.dumps(definition)) == definition