Files
hermes-agent/tests/gateway/test_webhook_route_toolsets.py
teknium1 b0aa41586e Merge commit '09fbc27a4a93' into fix/106742-thread-resolution
# Conflicts:
#	apps/desktop/electron/backend-dial-claim.test.ts
#	apps/desktop/electron/gateway-file-download-transport.test.ts
#	apps/desktop/electron/pool-limits.test.ts
#	apps/desktop/electron/pool-spawn-coordinator.test.ts
#	apps/desktop/electron/quit-guard.test.ts
#	apps/desktop/src/app/chat/composer/controls.test.tsx
#	apps/desktop/src/components/notifications.test.tsx
#	apps/desktop/src/plugins/hermes-bots/group-chat-view.inline-code.test.tsx
#	apps/desktop/src/sdk/profile-routing.test.ts
#	apps/desktop/src/store/gateway-spawn-priority.test.ts
#	apps/desktop/src/store/profile.test.ts
#	gateway/run.py
#	gateway/run_turn.py
#	gateway/status.py
#	hermes_cli/dashboard_auth/ws_tickets.py
#	hermes_cli/web_routers/sessions.py
#	tests/agent/test_secret_scope_tier1_migration.py
#	tests/cron/test_bot_chat_delivery_child_env.py
#	tests/cron/test_bot_chat_warning_replay.py
#	tests/cron/test_codex_execution_paths.py
#	tests/cron/test_cron_bot_chat_delivery.py
#	tests/cron/test_cron_empty_payload.py
#	tests/cron/test_cron_mcp_toolset_empty_block.py
#	tests/cron/test_cron_memory_contract.py
#	tests/cron/test_cron_no_agent.py
#	tests/cron/test_cron_request_overrides.py
#	tests/cron/test_cron_workdir.py
#	tests/cron/test_run_one_job.py
#	tests/cron/test_scheduler.py
#	tests/cron/test_sessiondb_init_hang.py
#	tests/desktop/test_bots_chat_live_append.py
#	tests/gateway/test_api_server_runs_extraction.py
#	tests/gateway/test_approval_prompt_redaction.py
#	tests/gateway/test_auto_reset_clean_context.py
#	tests/gateway/test_auto_reset_evicts_cached_agent.py
#	tests/gateway/test_compression_deferred_soft_result.py
#	tests/gateway/test_compression_session_id_persistence.py
#	tests/gateway/test_control_socket_windows_live.py
#	tests/gateway/test_fallback_chain_reload.py
#	tests/gateway/test_model_switch_after_auto_reset.py
#	tests/gateway/test_session_hygiene.py
#	tests/gateway/test_warning_wiring_conservation.py
#	tests/hermes_cli/test_backup.py
#	tests/hermes_cli/test_chat_q_exit_clear.py
#	tests/hermes_cli/test_cli_preloaded_skills.py
#	tests/hermes_cli/test_cron.py
#	tests/hermes_cli/test_ensure_gateway_service.py
#	tests/hermes_cli/test_kanban_gateway_restart_handoff.py
#	tests/hermes_cli/test_observational_sessiondb_modes.py
#	tests/hermes_cli/test_profiles_sidebar_cache.py
#	tests/hermes_cli/test_sessions_export_md_cli.py
#	tests/hermes_cli/test_single_query_session_finalize.py
#	tests/hermes_cli/test_web_server.py
#	tests/hermes_state/test_hermes_state.py
#	tests/tools/test_bot_live_owner_delivery.py
#	tests/tools/test_bot_mode_dm.py
#	tests/tools/test_bot_retry_policy.py
#	tests/tools/test_bot_turn_lock.py
#	tests/tui_gateway/test_bot_live_owner_delivery.py
#	tests/tui_gateway/test_compute_host_turn_protocol.py
#	tui_gateway/server.py
#	ui-tui/src/__tests__/createSlashHandler.test.ts
#	ui-tui/src/__tests__/submissionCore.test.ts
#	ui-tui/src/app/useSessionLifecycle.ts
2026-09-23 07:47:09 -07:00

192 lines
7.8 KiB
Python

"""Per-route webhook toolset overrides (adapter.toolsets_for_source).
A webhook route config may carry a ``toolsets`` list that replaces the
platform-level ``platform_toolsets.webhook`` resolution for runs triggered by
that route only. The gateway validates the override through the same
``_get_platform_tools`` path as platform config, so restricted/unknown names
behave identically to a manually configured platform toolset list.
The grant must bind to the route whose secret authenticated the request
(GHSA-2fmg-cjqm-hhrj): route names may contain ``:`` and the delivery id in the
session key is caller-supplied, so nothing may be recovered by splitting ``chat_id``.
"""
import asyncio
import hashlib
import hmac
import json
import pytest
from aiohttp import web
from aiohttp.test_utils import TestClient, TestServer
from gateway.config import PlatformConfig
from gateway.platforms.webhook import WebhookAdapter
from gateway.run import GatewayRunner
from hermes_cli.tools_config import _get_platform_tools
class _Src:
def __init__(self, chat_id):
self.chat_id = chat_id
# What _dispatch_agent_run stamps: exactly the authenticated route, no delivery id.
self.user_id = chat_id.rsplit(":", 1)[0] if chat_id.startswith("webhook:") else None
def _make_adapter(routes):
wa = object.__new__(WebhookAdapter)
wa._routes = routes
return wa
def _make_runner(adapter):
gr = object.__new__(GatewayRunner)
gr._delivery_adapter_for = lambda source: adapter
return gr
BASE_CONFIG = {"platform_toolsets": {"webhook": ["web", "vision", "clarify"]}}
class TestWebhookAdapterToolsetsForSource:
def test_route_with_toolsets_returns_list(self):
wa = _make_adapter({"mon": {"secret": "x", "toolsets": ["terminal", "file"]}})
assert wa.toolsets_for_source(_Src("webhook:mon:d1")) == ["terminal", "file"]
def test_unknown_route_returns_none(self):
wa = _make_adapter({})
assert wa.toolsets_for_source(_Src("webhook:ghost:d1")) is None
def test_non_webhook_chat_id_returns_none(self):
wa = _make_adapter({"mon": {"secret": "x", "toolsets": ["terminal"]}})
assert wa.toolsets_for_source(_Src("telegram:123")) is None
def test_empty_or_non_list_toolsets_returns_none(self):
wa = _make_adapter(
{
"empty": {"secret": "x", "toolsets": []},
"str": {"secret": "x", "toolsets": "terminal"},
"blank": {"secret": "x", "toolsets": [" ", ""]},
}
)
assert wa.toolsets_for_source(_Src("webhook:empty:d")) is None
assert wa.toolsets_for_source(_Src("webhook:str:d")) is None
assert wa.toolsets_for_source(_Src("webhook:blank:d")) is None
class TestGatewayResolveEnabledToolsetsForSource:
def test_override_replaces_platform_resolution(self):
wa = _make_adapter(
{"mon": {"secret": "x", "toolsets": ["terminal", "file", "web"]}}
)
gr = _make_runner(wa)
res = GatewayRunner._resolve_enabled_toolsets_for_source(
gr, BASE_CONFIG, _Src("webhook:mon:d"), "webhook"
)
assert "terminal" in res and "file" in res and "web" in res
assert "vision" not in res # platform list fully replaced, not merged
def test_override_validated_like_platform_config(self):
# Contract: resolving with an override is byte-identical to resolving
# the same list configured as platform_toolsets.webhook.
override = ["terminal", "file", "web", "discord_admin"]
wa = _make_adapter({"mon": {"secret": "x", "toolsets": override}})
gr = _make_runner(wa)
res = GatewayRunner._resolve_enabled_toolsets_for_source(
gr, BASE_CONFIG, _Src("webhook:mon:d"), "webhook"
)
expected = sorted(
_get_platform_tools(
{"platform_toolsets": {"webhook": list(override)}}, "webhook"
)
)
assert res == expected
# discord_admin is platform-restricted to discord — must be dropped.
assert "discord_admin" not in res
def test_no_override_uses_platform_resolution(self):
wa = _make_adapter({"plain": {"secret": "x"}})
gr = _make_runner(wa)
res = GatewayRunner._resolve_enabled_toolsets_for_source(
gr, BASE_CONFIG, _Src("webhook:plain:d"), "webhook"
)
assert res == sorted(_get_platform_tools(BASE_CONFIG, "webhook"))
assert "terminal" not in res
def test_adapter_exception_falls_back_to_platform_resolution(self):
wa = _make_adapter({})
wa.toolsets_for_source = lambda source: (_ for _ in ()).throw(
RuntimeError("boom")
)
gr = _make_runner(wa)
res = GatewayRunner._resolve_enabled_toolsets_for_source(
gr, BASE_CONFIG, _Src("webhook:mon:d"), "webhook"
)
assert res == sorted(_get_platform_tools(BASE_CONFIG, "webhook"))
def test_missing_adapter_falls_back_to_platform_resolution(self):
gr = _make_runner(None)
res = GatewayRunner._resolve_enabled_toolsets_for_source(
gr, BASE_CONFIG, _Src("webhook:mon:d"), "webhook"
)
assert res == sorted(_get_platform_tools(BASE_CONFIG, "webhook"))
def test_original_config_not_mutated(self):
cfg = {"platform_toolsets": {"webhook": ["web"]}}
wa = _make_adapter({"mon": {"secret": "x", "toolsets": ["terminal"]}})
gr = _make_runner(wa)
GatewayRunner._resolve_enabled_toolsets_for_source(
gr, cfg, _Src("webhook:mon:d"), "webhook"
)
assert cfg["platform_toolsets"]["webhook"] == ["web"]
class TestToolsetsBindToAuthenticatedRoute:
"""Regression for GHSA-2fmg-cjqm-hhrj, end to end through the real HTTP handler."""
ROUTES = {
"build": {"secret": "strong-secret", "toolsets": ["terminal", "file"]},
"build:external": {"secret": "weak-secret", "toolsets": ["web"], "prompt": "{text}"},
}
async def _dispatch(self, path, secret, delivery_id):
adapter = WebhookAdapter(PlatformConfig(
enabled=True, extra={"host": "127.0.0.1", "port": 0, "routes": self.ROUTES}))
captured = []
async def _capture(event):
captured.append(event.source)
adapter.handle_message = _capture
app = web.Application()
# Admission is durable: the route handler acks only through a session authority.
from tests.gateway.fixtures.webhook_route_authority import mount_authority
mount_authority(app, adapter)
app.router.add_post("/webhooks/{route_name}", adapter._handle_webhook)
body = json.dumps({"text": "hi"}).encode()
sig = "sha256=" + hmac.new(secret.encode(), body, hashlib.sha256).hexdigest()
async with TestClient(TestServer(app)) as cli:
resp = await cli.post(path, data=body, headers={
"X-Hub-Signature-256": sig, "Content-Type": "application/json",
"X-GitHub-Delivery": delivery_id})
assert resp.status == 202
await asyncio.sleep(0.05)
assert len(captured) == 1
gr = _make_runner(adapter)
return GatewayRunner._resolve_enabled_toolsets_for_source(gr, BASE_CONFIG, captured[0], "webhook")
@pytest.mark.asyncio
async def test_colon_route_gets_its_own_toolsets_not_its_prefix_routes(self):
res = await self._dispatch("/webhooks/build:external", "weak-secret", "d1")
assert "web" in res
assert "terminal" not in res and "file" not in res
@pytest.mark.asyncio
async def test_caller_supplied_delivery_id_cannot_name_another_route(self):
# Delivery id is attacker-controlled; a right-split would read "build:external" here.
res = await self._dispatch("/webhooks/build", "strong-secret", "external:d1")
assert "terminal" in res and "file" in res
assert "web" not in res