Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

fix(websocket): cap inbound WebSocket message size at the transport (#8036)

LearningCircuitea32c8ff25a2
diff --git a/changelog.d/+websocket-message-size-limit.security.md b/changelog.d/+websocket-message-size-limit.security.md
new file mode 100644
index 0000000000..2c166768ee
--- /dev/null
+++ b/changelog.d/+websocket-message-size-limit.security.md
@@ -0,0 +1 @@
+Inbound WebSocket / Socket.IO messages are now capped at 64 KiB: uvicorn is launched with an explicit `ws_max_size` (previously its 16 MiB default) so oversized frames are rejected at the transport, and engine.io's buffer limit is lowered to match. Real client messages are well under 1 KiB.
diff --git a/scripts/ci/zap_api_target.py b/scripts/ci/zap_api_target.py
index 2656c0a6a3..005438dad1 100644
--- a/scripts/ci/zap_api_target.py
+++ b/scripts/ci/zap_api_target.py
@@ -58,7 +58,8 @@ def main():
Path("zap-openapi.json").write_text(
json.dumps(build_spec(app.openapi()), indent=2) + "\n", encoding="utf-8"
)
- uvicorn.run(app, host="127.0.0.1", port=5000)
+ # Mirror the production launcher's WebSocket message cap (web/app.py).
+ uvicorn.run(app, host="127.0.0.1", port=5000, ws_max_size=65536)
if __name__ == "__main__":
diff --git a/src/local_deep_research/web/app.py b/src/local_deep_research/web/app.py
index e87bbf5778..7ce9fbf9c0 100644
--- a/src/local_deep_research/web/app.py
+++ b/src/local_deep_research/web/app.py
@@ -200,6 +200,11 @@ def _run_with_uvicorn(host: str, port: int, debug: bool) -> None:
timeout_graceful_shutdown=10,
proxy_headers=trust_proxy,
forwarded_allow_ips=forwarded_allow_ips if trust_proxy else None,
+ # Cap inbound WebSocket messages at the transport (uvicorn's default
+ # is 16 MiB). Must equal socketio_asgi.MAX_INBOUND_MESSAGE_BYTES;
+ # kept as a literal so this launcher does not import the app module
+ # before uvicorn starts.
+ ws_max_size=65536,
)
diff --git a/src/local_deep_research/web/services/socketio_asgi.py b/src/local_deep_research/web/services/socketio_asgi.py
index 95151f92d9..19d5ba2912 100644
--- a/src/local_deep_research/web/services/socketio_asgi.py
+++ b/src/local_deep_research/web/services/socketio_asgi.py
@@ -231,6 +231,21 @@ def _install_origin_rejection_logging(sio: "socketio.AsyncServer") -> bool:
return True
+# Largest inbound Socket.IO message the server accepts, in bytes.
+#
+# Clients only ever send ``subscribe_to_research`` /
+# ``unsubscribe_from_research`` (``{"research_id": <id>}``) plus engine.io
+# ping/pong frames -- well under 1 KiB each. 64 KiB leaves ample headroom
+# while bounding what a single connection can make the server buffer.
+#
+# Enforced twice: uvicorn's ``ws_max_size`` (``web/app.py``) rejects an
+# oversized WebSocket frame at the transport before it is fully read, and
+# engine.io's ``max_http_buffer_size`` below bounds long-polling bodies and
+# remains as the app-level check. ``web/app.py`` repeats the literal rather
+# than importing it (it must not import this module before uvicorn starts);
+# a test keeps the two in sync.
+MAX_INBOUND_MESSAGE_BYTES = 64 * 1024
+
# Create the async Socket.IO server
sio = socketio.AsyncServer(
async_mode="asgi",
@@ -239,6 +254,7 @@ sio = socketio.AsyncServer(
engineio_logger=False,
ping_timeout=20,
ping_interval=5,
+ max_http_buffer_size=MAX_INBOUND_MESSAGE_BYTES,
)
# Make a rejected WebSocket origin diagnosable (otherwise it is a silent
diff --git a/tests/ui_tests/research_completion_server.py b/tests/ui_tests/research_completion_server.py
index a31305c364..a0dc0ce79f 100644
--- a/tests/ui_tests/research_completion_server.py
+++ b/tests/ui_tests/research_completion_server.py
@@ -98,6 +98,8 @@ def main() -> None:
workers=1,
server_header=False,
access_log=False,
+ # Mirrors MAX_INBOUND_MESSAGE_BYTES in web/services/socketio_asgi.py.
+ ws_max_size=65536,
)
diff --git a/tests/web/services/test_socketio_message_size_limit.py b/tests/web/services/test_socketio_message_size_limit.py
new file mode 100644
index 0000000000..142e760c7e
--- /dev/null
+++ b/tests/web/services/test_socketio_message_size_limit.py
@@ -0,0 +1,96 @@
+"""Inbound Socket.IO / WebSocket message size is bounded explicitly.
+
+Clients send only tiny ``subscribe_to_research`` /
+``unsubscribe_from_research`` payloads and engine.io ping/pong frames, so
+the server caps inbound messages well below uvicorn's 16 MiB WebSocket
+default and engine.io's 1 MB buffer default:
+
+* engine.io's ``max_http_buffer_size`` (app level, also bounds polling);
+* uvicorn's ``ws_max_size`` on every launch path (transport level, rejects
+ an oversized frame before it is buffered).
+
+``web/app.py`` repeats the limit as a literal (it must not import the app
+before uvicorn starts), so these tests keep the copies in agreement.
+"""
+
+import ast
+import json
+from pathlib import Path
+
+from local_deep_research.web import app as web_app_module
+from local_deep_research.web.services import socketio_asgi as sio_mod
+
+REPO_ROOT = Path(__file__).resolve().parents[3]
+ZAP_TARGET = REPO_ROOT / "scripts" / "ci" / "zap_api_target.py"
+UI_TEST_SERVER = (
+ REPO_ROOT / "tests" / "ui_tests" / "research_completion_server.py"
+)
+
+
+def _uvicorn_run_kwargs(path: Path) -> dict[str, ast.expr]:
+ tree = ast.parse(path.read_text(encoding="utf-8"))
+ calls = [
+ node
+ for node in ast.walk(tree)
+ if isinstance(node, ast.Call)
+ and ast.unparse(node.func) == "uvicorn.run"
+ ]
+ assert len(calls) == 1, (
+ f"expected exactly one uvicorn.run() call in {path}, found {len(calls)}"
+ )
+ return {kw.arg: kw.value for kw in calls[0].keywords if kw.arg}
+
+
+def _literal_ws_max_size(path: Path) -> int:
+ kwargs = _uvicorn_run_kwargs(path)
+ assert "ws_max_size" in kwargs, (
+ f"uvicorn.run() in {path} no longer passes ws_max_size; uvicorn "
+ "would fall back to its 16 MiB default WebSocket message size"
+ )
+ value = kwargs["ws_max_size"]
+ assert isinstance(value, ast.Constant) and isinstance(value.value, int), (
+ f"ws_max_size in {path} is not an int literal: {ast.unparse(value)}"
+ )
+ return value.value
+
+
+def test_limit_is_small_and_explicit():
+ limit = sio_mod.MAX_INBOUND_MESSAGE_BYTES
+ assert isinstance(limit, int)
+ # Far below uvicorn's 16 MiB default and engine.io's 1 MB default.
+ assert 0 < limit <= 64 * 1024
+
+
+def test_limit_fits_the_real_client_messages():
+ """The largest message a client sends must fit with wide headroom."""
+ # Socket.IO EVENT packet framing ("42" + JSON array) around the
+ # subscribe payload with a generously long research id.
+ payload = json.dumps(["subscribe_to_research", {"research_id": "x" * 256}])
+ packet = "42" + payload
+ assert len(packet.encode("utf-8")) * 16 < sio_mod.MAX_INBOUND_MESSAGE_BYTES
+
+
+def test_engineio_buffer_uses_the_limit():
+ """App-level check (also covers the long-polling transport)."""
+ assert (
+ sio_mod.sio.eio.max_http_buffer_size
+ == sio_mod.MAX_INBOUND_MESSAGE_BYTES
+ )
+
+
+def test_production_launcher_sets_transport_limit():
+ path = Path(web_app_module.__file__).resolve()
+ assert _literal_ws_max_size(path) == sio_mod.MAX_INBOUND_MESSAGE_BYTES
+
+
+def test_zap_ci_target_mirrors_transport_limit():
+ assert ZAP_TARGET.is_file(), f"missing {ZAP_TARGET}"
+ assert _literal_ws_max_size(ZAP_TARGET) == sio_mod.MAX_INBOUND_MESSAGE_BYTES
+
+
+def test_ui_test_server_mirrors_transport_limit():
+ assert UI_TEST_SERVER.is_file(), f"missing {UI_TEST_SERVER}"
+ assert (
+ _literal_ws_max_size(UI_TEST_SERVER)
+ == sio_mod.MAX_INBOUND_MESSAGE_BYTES
+ )