The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(webhook): per-route toolsets bind to the authenticated route, not a split of chat_id (GHSA-2fmg-cjqm-hhrj)
emozilla9345c67854f6
diff --git a/gateway/platforms/webhook.py b/gateway/platforms/webhook.pyindex c8005a0df32..a9c6fa514d4 100644--- a/gateway/platforms/webhook.py+++ b/gateway/platforms/webhook.py@@ -323,11 +323,13 @@ class WebhookAdapter(BasePlatformAdapter):def toolsets_for_source(self, source) -> Optional[List[str]]:"""Per-route ``toolsets`` override (config.yaml or a manual key in webhook_subscriptions.json —deliberately NOT settable via `hermes webhook subscribe`, so an agent-created subscription- cannot self-grant tools)."""- parts = str(getattr(source, "chat_id", "") or "").split(":", 2)- if len(parts) < 2 or parts[0] != "webhook":+ cannot self-grant tools). Keyed on ``user_id`` (exactly ``webhook:{route}`` as authenticated), not+ ``chat_id``, whose caller-supplied delivery id and ``:``-bearing route names make any split ambiguous+ (GHSA-2fmg-cjqm-hhrj)."""+ user_id = str(getattr(source, "user_id", "") or "")+ if not user_id.startswith("webhook:"):return None- route_config = self._routes.get(parts[1])+ route_config = self._routes.get(user_id[len("webhook:"):])toolsets = route_config.get("toolsets") if isinstance(route_config, dict) else Noneif not isinstance(toolsets, list):return Nonediff --git a/tests/gateway/test_webhook_route_toolsets.py b/tests/gateway/test_webhook_route_toolsets.pyindex d1a19f07f36..a830444f47b 100644--- a/tests/gateway/test_webhook_route_toolsets.py+++ b/tests/gateway/test_webhook_route_toolsets.py@@ -5,8 +5,22 @@ platform-level ``platform_toolsets.webhook`` resolution for runs triggered bythat route only. The gateway validates the override through the same``_get_platform_tools`` path as platform config, so restricted/unknown namesbehave 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 PlatformConfigfrom gateway.platforms.base import BasePlatformAdapterfrom gateway.platforms.webhook import WebhookAdapterfrom gateway.run import GatewayRunner@@ -16,6 +30,8 @@ from hermes_cli.tools_config import _get_platform_toolsclass _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 Nonedef _make_adapter(routes):@@ -135,3 +151,48 @@ class TestGatewayResolveEnabledToolsetsForSource: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()+ 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