From 12c2c95cbb6814a09265ed65e9f99da78e36eeb4 Mon Sep 17 00:00:00 2001 From: FanouZeng-TT <18280587072@163.com> Date: Mon, 14 Sep 2026 20:15:54 +0800 Subject: [PATCH] fix(api): require boolean stream values Validate the OpenAI-compatible stream flag before choosing the response mode so string values such as "false" are rejected instead of enabling SSE. Co-Authored-By: Claude Code --- nanobot/api/server.py | 5 ++++- tests/test_api_stream.py | 23 +++++++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/nanobot/api/server.py b/nanobot/api/server.py index 9743fbd1e..c22233107 100644 --- a/nanobot/api/server.py +++ b/nanobot/api/server.py @@ -315,7 +315,10 @@ async def handle_chat_completions(request: web.Request) -> web.Response | web.St if not isinstance(body, dict): return _error_json(400, "Invalid JSON body") body = cast(dict[str, Any], body) - stream = body.get("stream", False) + stream_value = body.get("stream", False) + if not isinstance(stream_value, bool): + return _error_json(400, "stream must be a boolean") + stream = stream_value requested_model = body.get("model") text, media_paths = _parse_json_content(body) session_id = body.get("session_id") diff --git a/tests/test_api_stream.py b/tests/test_api_stream.py index a555e5dde..9646722ea 100644 --- a/tests/test_api_stream.py +++ b/tests/test_api_stream.py @@ -171,6 +171,29 @@ async def test_stream_default_is_false(aiohttp_client) -> None: assert body["object"] == "chat.completion" +@pytest.mark.skipif(not HAS_AIOHTTP, reason="aiohttp not installed") +@pytest.mark.asyncio +async def test_stream_rejects_non_boolean_values(aiohttp_client) -> None: + """The OpenAI-compatible stream flag must be a JSON boolean.""" + agent = MagicMock() + agent.process_direct = AsyncMock(return_value="normal reply") + agent.aclose = AsyncMock() + + app = create_app(agent, model_name="m", api_key=API_KEY) + client = await aiohttp_client(app) + + resp = await client.post( + "/v1/chat/completions", + headers=AUTH_HEADERS, + json={"messages": [{"role": "user", "content": "hi"}], "stream": "false"}, + ) + + assert resp.status == 400 + body = await resp.json() + assert body["error"]["message"] == "stream must be a boolean" + agent.process_direct.assert_not_called() + + @pytest.mark.skipif(not HAS_AIOHTTP, reason="aiohttp not installed") @pytest.mark.asyncio async def test_stream_sse_chunk_ids_are_consistent(aiohttp_client) -> None: