The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(relay): a relayed Discord interaction carries the reader's authenticated provenance
Tekniumac6c2ec18da8
diff --git a/gateway/relay/adapter_discord_interaction.py b/gateway/relay/adapter_discord_interaction.pyindex aecc4cb53ed..8d7fb87bd8c 100644--- a/gateway/relay/adapter_discord_interaction.py+++ b/gateway/relay/adapter_discord_interaction.py@@ -100,7 +100,9 @@ class RelayDiscordInteractionMixin:src = event.sourcesrc.chat_name, src.chat_topic = await self._discord_chat_labels_for(str(src.scope_id or ""), src.chat_id)- await self.handle_message(event)+ from gateway.session_ingress_context import relay_passthrough_event+ with relay_passthrough_event(self._transport, forward, event):+ await self.handle_message(event)returnlogger.info("relay passthrough_forward dropped (no handler): platform=%s method=%s path=%s",diff --git a/gateway/relay/ws_transport.py b/gateway/relay/ws_transport.pyindex d3e99284057..3410fb97fd2 100644--- a/gateway/relay/ws_transport.py+++ b/gateway/relay/ws_transport.py@@ -968,8 +968,12 @@ class WebSocketRelayTransport:# Edge-ACKed passthrough request riding the same WS (no public inbound# port needed); bufferId (§5.3) is passed through for ack.if self._passthrough_handler is not None:+ from gateway.session_ingress_context import relay_callbackfwd = _passthrough_from_wire(frame.get("forward", {}))- await self._passthrough_handler(fwd, frame.get("bufferId"))+ # Same authenticated-reader proof as _on_inbound, bound to the forward; the+ # adapter moves it onto the event it decodes from this forward.+ with relay_callback(self, fwd):+ await self._passthrough_handler(fwd, frame.get("bufferId"))_FRAME_HANDLERS = {"descriptor": _on_descriptor,diff --git a/gateway/session_ingress_context.py b/gateway/session_ingress_context.pyindex aef3ebf7842..86f3eb6af91 100644--- a/gateway/session_ingress_context.py+++ b/gateway/session_ingress_context.py@@ -24,6 +24,18 @@ def relay_callback(transport, event):_relay_callback.reset(token)+@contextmanager+def relay_passthrough_event(transport, forward, event):+ # A passthrough frame is decoded after the reader bound its proof to the raw forward; move+ # that proof onto the decoded event. Without the reader's binding nothing is minted.+ bound = _relay_callback.get()+ if bound is None or bound[0] is not transport or bound[1] is not forward:+ yield+ return+ with relay_callback(transport, event):+ yield++def _relay_connector(adapter, source):from gateway.relay.ws_transport import WebSocketRelayTransporttransport = getattr(adapter, '_transport', None)diff --git a/tests/gateway/relay/test_relay_passthrough_authority.py b/tests/gateway/relay/test_relay_passthrough_authority.pynew file mode 100644index 00000000000..1cadcca6ddf--- /dev/null+++ b/tests/gateway/relay/test_relay_passthrough_authority.py@@ -0,0 +1,66 @@+"""A relayed Discord interaction reaches the default native session authority only through the+authenticated WS reader: real frame dispatcher -> relay adapter -> GatewayRunner/SQLite authority."""+import base64+import json+from unittest.mock import AsyncMock, MagicMock++import pytest++_MODAL = {"id": "i1", "type": 5, "channel_id": "c1", "guild_id": "g1",+ "member": {"user": {"id": "u1", "username": "ben"}},+ "data": {"custom_id": "feedback", "components": [+ {"type": 1, "components": [{"type": 4, "custom_id": "title", "value": "MODAL_MARKER"}]}]}}+_FRAME = {"type": "passthrough_forward", "bufferId": "b1", "forward": {+ "platform": "discord", "botId": "app", "method": "POST", "path": "/interactions", "headers": [],+ "bodyB64": base64.b64encode(json.dumps(_MODAL).encode()).decode()}}+++async def test_relayed_modal_admitted_only_with_reader_proof(tmp_path, monkeypatch):+ import gateway.run as gr+ from gateway.config import GatewayConfig, Platform, PlatformConfig+ from gateway.platforms.base import SendResult+ from gateway.relay.adapter import RelayAdapter+ from gateway.relay.ws_transport import WebSocketRelayTransport, _passthrough_from_wire+ from gateway.session_authority import initialize_session_authority+ from tests.gateway.relay.test_relay_passthrough import _desc+ from tests.waiting import async_wait_until++ monkeypatch.setattr(gr, "_resolve_runtime_agent_kwargs", lambda: {"api_key": "fake"})+ runner = gr.GatewayRunner(GatewayConfig(sessions_dir=tmp_path / "sessions"))+ transport = WebSocketRelayTransport("ws://127.0.0.1:1/relay", "discord", "app",+ gateway_id="t", upgrade_secret="test-only")+ desc = _desc()+ transport._ws = MagicMock()+ transport._descriptor = desc+ transport._descriptors_by_platform = {"discord": desc}+ adapter = RelayAdapter(PlatformConfig(enabled=True), desc, transport=transport)+ runner.adapters = {Platform.RELAY: adapter}+ adapter.gateway_runner = runner+ authority = await initialize_session_authority(runner, profile_id="default", instance_id="t")+ runner._wire_adapter_handlers(adapter)+ transport.set_passthrough_handler(adapter._on_passthrough)+ calls = []++ async def run(**kwargs):+ calls.append(kwargs["message"])+ return {"final_response": "ok", "messages": [], "tools": [], "history_offset": 0,+ "last_prompt_tokens": 0}++ runner._run_agent = run+ adapter.send = AsyncMock(return_value=SendResult(success=True, message_id="sent"))++ def settled():+ return not adapter._background_tasks and not adapter._session_tasks++ try:+ # Forged: the same forward handed to the adapter without the reader's proof is refused.+ await adapter._on_passthrough(_passthrough_from_wire(_FRAME["forward"]))+ await async_wait_until(settled, timeout=30)+ assert calls == []+ # Authentic: the real WS reader dispatches the frame; the modal text is admitted.+ await transport._handle_frame(json.dumps(_FRAME))+ await async_wait_until(lambda: calls and settled(), timeout=30)+ assert calls == ["feedback\ntitle: MODAL_MARKER"]+ finally:+ authority.db.close()