From 73f8fb74e022c8134215dc36cc8ea90f6d1f4c0a Mon Sep 17 00:00:00 2001 From: liuhao1024 Date: Tue, 2 Jun 2026 17:23:26 +0800 Subject: [PATCH] fix(approval): exclude webhook sessions from gateway approval context Webhook sessions trigger the gateway approval branch because HERMES_SESSION_PLATFORM is set, but the webhook adapter has no send_exec_approval and no way to receive /approve replies. This blocks the session for the full approval timeout (60-300 s) with no human who can resolve it. Fix: _is_gateway_approval_context() now returns False when the session platform is 'webhook', falling through to the non-interactive path (auto-approve with warning, or deny if cron). Regression tests added for webhook, non-webhook gateway, cron, and no-platform scenarios. --- tests/tools/test_approval.py | 73 ++++++++++++++++++++++++++++++++++++ tools/approval.py | 13 ++++++- 2 files changed, 85 insertions(+), 1 deletion(-) diff --git a/tests/tools/test_approval.py b/tests/tools/test_approval.py index 97f158b356..683337bc7f 100644 --- a/tests/tools/test_approval.py +++ b/tests/tools/test_approval.py @@ -702,6 +702,79 @@ class TestGatewayProtection: assert dangerous is False + + +class TestWebhookApprovalExclusion: + """Webhook sessions must NOT be treated as gateway approval contexts. + + The webhook adapter has no ``send_exec_approval`` method and no way to + receive ``/approve`` replies. If a webhook session triggers a dangerous + command and falls through to the gateway approval branch, the session + blocks for the full timeout (60-300 s) with no human who can resolve it. + + Fix: ``_is_gateway_approval_context()`` returns ``False`` when the + session platform is ``"webhook"``. + """ + + def test_webhook_platform_returns_false(self, monkeypatch): + """Webhook sessions are not gateway approval contexts.""" + from tools.approval import _is_gateway_approval_context + + monkeypatch.delenv("HERMES_CRON_SESSION", raising=False) + monkeypatch.delenv("HERMES_GATEWAY_SESSION", raising=False) + monkeypatch.setenv("HERMES_SESSION_PLATFORM", "webhook") + + assert _is_gateway_approval_context() is False + + def test_non_webhook_gateway_session_returns_true(self, monkeypatch): + """Non-webhook gateway sessions (e.g. Telegram) are still gateway contexts.""" + from tools.approval import _is_gateway_approval_context + + monkeypatch.delenv("HERMES_CRON_SESSION", raising=False) + monkeypatch.setenv("HERMES_GATEWAY_SESSION", "1") + monkeypatch.setenv("HERMES_SESSION_PLATFORM", "telegram") + + assert _is_gateway_approval_context() is True + + def test_cron_session_returns_false_regardless_of_platform(self, monkeypatch): + """Cron sessions are never gateway approval contexts.""" + from tools.approval import _is_gateway_approval_context + + monkeypatch.setenv("HERMES_CRON_SESSION", "1") + monkeypatch.setenv("HERMES_SESSION_PLATFORM", "telegram") + + assert _is_gateway_approval_context() is False + + def test_no_platform_returns_false(self, monkeypatch): + """No session platform means not a gateway context.""" + from tools.approval import _is_gateway_approval_context + + monkeypatch.delenv("HERMES_CRON_SESSION", raising=False) + monkeypatch.delenv("HERMES_GATEWAY_SESSION", raising=False) + monkeypatch.delenv("HERMES_SESSION_PLATFORM", raising=False) + + assert _is_gateway_approval_context() is False + + def test_webhook_dangerous_command_auto_approves(self, monkeypatch): + """Webhook sessions that trigger dangerous commands auto-approve with a warning. + + This is the non-interactive, non-gateway path - safe because the + command does not execute in an interactive context where the user + could be tricked. + """ + from tools.approval import check_all_command_guards + + monkeypatch.delenv("HERMES_CRON_SESSION", raising=False) + monkeypatch.delenv("HERMES_GATEWAY_SESSION", raising=False) + monkeypatch.delenv("HERMES_INTERACTIVE", raising=False) + monkeypatch.setenv("HERMES_SESSION_PLATFORM", "webhook") + monkeypatch.setenv("HERMES_SESSION_KEY", "test-webhook-session") + + result = check_all_command_guards("sudo systemctl restart nginx", "local") + # Should auto-approve (non-interactive, non-gateway path) + assert result["approved"] is True + + class TestNormalizationBypass: """Obfuscation techniques must not bypass dangerous command detection.""" diff --git a/tools/approval.py b/tools/approval.py index 298cfe4820..b0e6661e5c 100644 --- a/tools/approval.py +++ b/tools/approval.py @@ -303,12 +303,23 @@ def _is_gateway_approval_context() -> bool: ``approvals.cron_mode`` config, not interactive resolve — letting cron fall through to the gateway branch would submit a pending approval with no listener and block the job indefinitely. + + Webhook sessions are excluded for the same reason: the webhook adapter + has no ``send_exec_approval`` and no way to receive ``/approve`` + replies. Submitting a pending approval in a webhook context blocks + the session for the full approval timeout (60-300 s) with no human + who can resolve it. Webhook dangerous-command handling falls through + to the non-interactive path (auto-approve with warning, or deny if + running inside a cron context). """ if _is_cron_approval_context(): return False + platform = _get_session_platform() + if platform == "webhook": + return False if env_var_enabled("HERMES_GATEWAY_SESSION"): return True - return bool(_get_session_platform()) + return bool(platform) def _resolve_cli_approval_callback(approval_callback=None):