Skip to content

The agent that grows with you

fix(api-server): send CORS headers on run events SSE stream

nikkoxgonzalesc7b8e44ffe12
diff --git a/gateway/platforms/api_server_runs.py b/gateway/platforms/api_server_runs.py
index 9f0556cf145..49500ac37ec 100644
--- a/gateway/platforms/api_server_runs.py
+++ b/gateway/platforms/api_server_runs.py
@@ -1084,8 +1084,14 @@ async def _handle_run_events(self, request: "web.Request", *, _api_server) -> "w
except (TypeError, ValueError):
last_seq = -1
q, replay = stream.attach(last_seq)
- response = web.StreamResponse(status=200, headers={
- "Content-Type": "text/event-stream", "Cache-Control": "no-cache", "X-Accel-Buffering": "no"})
+ # The CORS middleware can't inject headers after prepare() flushes them, so resolve
+ # the origin's CORS headers up front (same pattern as the chat/responses SSE paths).
+ sse_headers = {
+ "Content-Type": "text/event-stream", "Cache-Control": "no-cache", "X-Accel-Buffering": "no"}
+ origin = request.headers.get("Origin", "")
+ if origin:
+ sse_headers.update(self._cors_headers_for_origin(origin) or {})
+ response = web.StreamResponse(status=200, headers=sse_headers)
async def _write(data: bytes) -> None:
try:
diff --git a/tests/gateway/test_api_server_runs.py b/tests/gateway/test_api_server_runs.py
index 18c6e243a94..c8dbc25b179 100644
--- a/tests/gateway/test_api_server_runs.py
+++ b/tests/gateway/test_api_server_runs.py
@@ -853,6 +853,81 @@ class TestRunEvents:
attacker_interrupted.set()
+# ---------------------------------------------------------------------------
+# GET /v1/runs/{run_id}/events — CORS on the SSE stream
+# ---------------------------------------------------------------------------
+
+
+class TestRunEventsCORS:
+ """StreamResponse flushes headers on prepare(), so the CORS middleware cannot
+ inject them afterwards — the handler must resolve them up front (#6358)."""
+
+ @staticmethod
+ def _primed_adapter(api_key="sk-secret"):
+ adapter = _make_adapter(api_key=api_key)
+ adapter._cors_origins = ("http://localhost:3000",)
+ return adapter
+
+ @staticmethod
+ def _prime_closed_stream(adapter, run_id):
+ _claim_run(adapter, run_id)
+ q: asyncio.Queue = asyncio.Queue()
+ q.put_nowait(None) # run finished: handler writes ": stream closed" and returns
+ adapter._run_streams[run_id] = q
+
+ @pytest.mark.asyncio
+ async def test_events_cors_headers_present_for_allowed_origin(self):
+ adapter = self._primed_adapter()
+ self._prime_closed_stream(adapter, "cors_run_1")
+ app = _create_runs_app(adapter)
+ async with TestClient(TestServer(app)) as cli:
+ resp = await cli.get(
+ "/v1/runs/cors_run_1/events",
+ headers={
+ "Authorization": "Bearer sk-secret",
+ "Origin": "http://localhost:3000",
+ "Accept": "text/event-stream",
+ },
+ )
+ assert resp.status == 200
+ assert resp.headers.get("Content-Type") == "text/event-stream"
+ assert resp.headers.get("Access-Control-Allow-Origin") == "http://localhost:3000"
+ assert "GET" in resp.headers.get("Access-Control-Allow-Methods", "")
+ assert "Authorization" in resp.headers.get("Access-Control-Allow-Headers", "")
+ await resp.text()
+
+ @pytest.mark.asyncio
+ async def test_events_cors_headers_absent_without_origin(self):
+ adapter = self._primed_adapter()
+ self._prime_closed_stream(adapter, "cors_run_2")
+ app = _create_runs_app(adapter)
+ async with TestClient(TestServer(app)) as cli:
+ resp = await cli.get(
+ "/v1/runs/cors_run_2/events",
+ headers={"Authorization": "Bearer sk-secret"},
+ )
+ assert resp.status == 200
+ assert resp.headers.get("Access-Control-Allow-Origin") is None
+ await resp.text()
+
+ @pytest.mark.asyncio
+ async def test_events_rejects_disallowed_origin(self):
+ adapter = self._primed_adapter()
+ self._prime_closed_stream(adapter, "cors_run_3")
+ app = _create_runs_app(adapter)
+ async with TestClient(TestServer(app)) as cli:
+ resp = await cli.get(
+ "/v1/runs/cors_run_3/events",
+ headers={
+ "Authorization": "Bearer sk-secret",
+ "Origin": "http://evil.example",
+ },
+ )
+ assert resp.status == 403
+ assert resp.headers.get("Access-Control-Allow-Origin") is None
+ await resp.text()
+
+
# ---------------------------------------------------------------------------
# POST /v1/runs/{run_id}/steer — steer a running agent
# ---------------------------------------------------------------------------