fix(gateway): cap proxy SSE line buffer

This commit is contained in:
luyifan 2026-07-05 03:33:47 +08:00 committed by Teknium
parent a0a3c716fc
commit 5b8593266f
2 changed files with 31 additions and 0 deletions

View file

@ -67,6 +67,7 @@ _AGENT_CACHE_MAX_SIZE = 128
_AGENT_CACHE_IDLE_TTL_SECS = 3600.0 # evict agents idle for >1h
_PLATFORM_CONNECT_TIMEOUT_SECS_DEFAULT = 30.0
_ADAPTER_DISCONNECT_TIMEOUT_SECS_DEFAULT = 5.0
_GATEWAY_PROXY_SSE_BUFFER_MAX_CHARS = 16 * 1024 * 1024
_TELEGRAM_COMMAND_MENTION_RE = re.compile(r"(?<![\w:/])/([A-Za-z0-9][A-Za-z0-9_-]*)")
_TELEGRAM_NOISY_STATUS_RE = re.compile(
@ -16219,6 +16220,10 @@ class GatewayRunner(GatewayAuthorizationMixin, GatewayKanbanWatchersMixin, Gatew
_stream_consumer.on_delta(content)
except json.JSONDecodeError:
pass
if len(buffer) > _GATEWAY_PROXY_SSE_BUFFER_MAX_CHARS:
raise ValueError(
"Proxy SSE stream exceeded max buffer size without a line boundary"
)
except asyncio.CancelledError:
raise

View file

@ -334,6 +334,32 @@ class TestRunAgentViaProxy:
assert "Proxy connection error" in result["final_response"]
@pytest.mark.asyncio
async def test_rejects_proxy_sse_without_line_boundary_after_buffer_cap(self, monkeypatch):
monkeypatch.setenv("GATEWAY_PROXY_URL", "http://host:8642")
monkeypatch.delenv("GATEWAY_PROXY_KEY", raising=False)
monkeypatch.setattr("gateway.run._GATEWAY_PROXY_SSE_BUFFER_MAX_CHARS", 16)
runner = _make_runner()
source = _make_source()
resp = _FakeSSEResponse(status=200, sse_chunks=[b"data: ", b"x" * 20])
session = _FakeSession(resp)
with patch("gateway.run._load_gateway_config", return_value={}):
with _patch_aiohttp(session):
with patch("aiohttp.ClientTimeout"):
result = await runner._run_agent_via_proxy(
message="hi",
context_prompt="",
history=[],
source=source,
session_id="test",
)
assert "Proxy connection error" in result["final_response"]
assert "exceeded max buffer size" in result["final_response"]
assert result["api_calls"] == 0
@pytest.mark.asyncio
async def test_skips_tool_messages_in_history(self, monkeypatch):
monkeypatch.setenv("GATEWAY_PROXY_URL", "http://host:8642")