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.
This commit is contained in:
liuhao1024
2026-06-02 17:23:26 +08:00
committed by Teknium
parent 66666f6e2e
commit 73f8fb74e0
2 changed files with 85 additions and 1 deletions
+73
View File
@@ -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."""
+12 -1
View File
@@ -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):