From b34edd6b019887363a792e058fd86f0670f173a5 Mon Sep 17 00:00:00 2001 From: Artur Hapantsou Date: Sun, 23 Aug 2026 16:55:50 -0700 Subject: [PATCH] fix: execute_code and argv-list payloads no longer bypass the gateway lifecycle guard (#68289) execute_code lacked the lifecycle guard entirely, and Python argv-list forms (subprocess.run([...])) separated command words with brackets and commas the shell-shaped pattern could not see. Mirror the terminal_tool guard in execute_code (ownership-gated per #92560) and strip argv-list punctuation in the token-join re-scan. Salvaged from PR #68289 by @arcimun, adapted to the ownership gate and current guard structure. --- contributors/emails/szsyyk@163.com | 1 + cron/lifecycle_guard.py | 15 +++++++++++- tests/hermes_cli/test_gateway_restart_loop.py | 23 ++++++++++++++++++- tools/code_execution_tool.py | 17 ++++++++++++++ 4 files changed, 54 insertions(+), 2 deletions(-) create mode 100644 contributors/emails/szsyyk@163.com diff --git a/contributors/emails/szsyyk@163.com b/contributors/emails/szsyyk@163.com new file mode 100644 index 0000000000..11e29824a8 --- /dev/null +++ b/contributors/emails/szsyyk@163.com @@ -0,0 +1 @@ +arcimun diff --git a/cron/lifecycle_guard.py b/cron/lifecycle_guard.py index 349e977ebb..a3960c0efc 100644 --- a/cron/lifecycle_guard.py +++ b/cron/lifecycle_guard.py @@ -115,6 +115,12 @@ _GATEWAY_LIFECYCLE_PATTERN = re.compile( # across genuinely separate lines. _SHELL_LINE_CONTINUATION = re.compile(r"\\\r?\n[ \t]*") +# Python argv-list punctuation (#68289): `subprocess.run(["launchctl", +# "bootout", ...])` separates the words the OS will exec with brackets and +# commas rather than spaces. Stripped before the token-join re-scan only — +# never from the raw text, so prose stays governed by the primary pattern. +_ARGV_LIST_PUNCTUATION = re.compile(r"[\[\],]+") + # Branch A2 (#78028): the same foot-gun written with an explicit profile # selector — `hermes -p gateway restart|stop` / `--profile ` @@ -252,11 +258,18 @@ def contains_gateway_lifecycle_command(text: str) -> bool: # Token-aware second pass (#80269): re-run the pattern on shell-tokenized # segments where quotes/escapes are resolved, closing splice bypasses # like `kick"start"`. Runs after the profile-flag check so both passes - # apply independently. + # apply independently. Tokens are additionally re-joined with Python + # argv-list punctuation ([ ] ,) stripped (#68289): the same command + # reaches this guard as `subprocess.run(["launchctl", "bootout", ...])` + # from execute_code, where commas and brackets — not spaces — separate + # the argv words the OS will actually see. for segment in _iter_command_segments(normalized): joined = " ".join(segment) if joined and _GATEWAY_LIFECYCLE_PATTERN.search(joined): return True + stripped = _ARGV_LIST_PUNCTUATION.sub(" ", joined) + if stripped != joined and _GATEWAY_LIFECYCLE_PATTERN.search(stripped): + return True # Order-independent launchctl pass (#77083): a shell loop can build the # gateway label from a variable defined in an earlier `;`-separated # segment (`label=${item%%:*}; launchctl bootout "gui/$uid/$label"`), so diff --git a/tests/hermes_cli/test_gateway_restart_loop.py b/tests/hermes_cli/test_gateway_restart_loop.py index 3a779d98c8..4f8b27c10b 100644 --- a/tests/hermes_cli/test_gateway_restart_loop.py +++ b/tests/hermes_cli/test_gateway_restart_loop.py @@ -232,6 +232,24 @@ class TestGatewayLifecyclePattern: text = 'echo "unbalanced\nhermes gateway restart' assert _contains_gateway_lifecycle_command(text), f"Should match: {text!r}" + @pytest.mark.parametrize("text", [ + # #68289: execute_code payloads carry the argv as a Python list — + # brackets/commas separate the words the OS will exec. + 'import subprocess\nsubprocess.run(["launchctl", "bootout", "gui/501/ai.hermes.gateway"])', + 'subprocess.run(["hermes", "gateway", "restart"])', + 'os.system("launchctl kickstart -k gui/501/ai.hermes.gateway")', + ]) + def test_python_argv_list_forms_blocked(self, text): + assert _contains_gateway_lifecycle_command(text), f"Should match: {text!r}" + + @pytest.mark.parametrize("text", [ + # Argv-punctuation stripping must not create prose false positives. + 'print("checking gateway restart docs")', + 'data = ["hermes", "notes"] # unrelated list', + ]) + def test_python_argv_stripping_stays_narrow(self, text): + assert not _contains_gateway_lifecycle_command(text), f"Should NOT match: {text!r}" + class TestProfileFlagGatewayLifecycle: """#78028: `hermes -p gateway restart|stop` bypasses Branch A's @@ -416,7 +434,10 @@ class TestGatewaySelfTargetingGuard: assert exc_info.value.code == 1 def test_uninstall_refuses_inside_gateway(self, monkeypatch): - monkeypatch.setenv("_HERMES_GATEWAY", "1") + from tools import process_registry + monkeypatch.setattr( + process_registry, "_is_supervised_gateway_process", lambda: True + ) from hermes_cli.gateway import gateway_command args = Namespace(gateway_command="uninstall", system=False) diff --git a/tools/code_execution_tool.py b/tools/code_execution_tool.py index 4c6ec73b36..81cdbcf745 100644 --- a/tools/code_execution_tool.py +++ b/tools/code_execution_tool.py @@ -1286,6 +1286,23 @@ def execute_code( "terminal(command=...) instead." ) + # Hard-block gateway-lifecycle commands, mirroring the terminal_tool + # guard (#68289): without this, execute_code is a straight bypass — the + # terminal() path refuses `launchctl bootout ai.hermes.gateway`, but the + # identical command inside `os.system(...)` / `subprocess.run([...])` + # here sailed through and SIGTERM'd the gateway mid-task. Gated on + # PID-file ownership, not the inherited env marker (#92560). + from tools.process_registry import _is_supervised_gateway_process + if _is_supervised_gateway_process(): + from cron.lifecycle_guard import contains_gateway_lifecycle_command + if contains_gateway_lifecycle_command(code): + return tool_error( + "Blocked: cannot restart or stop the gateway from inside the " + "gateway process. The gateway would kill this script before " + "it could complete (SIGTERM propagates to child processes). " + "Run the lifecycle command from a shell outside the gateway." + ) + # Dispatch: remote backends use file-based RPC, local uses UDS from tools.terminal_tool import _get_env_config, _docker_has_host_access _env_config = _get_env_config()