The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(files): refuse an oversized image paste before reading its body
emozillaa807042c1070
diff --git a/hermes_cli/web_routers/files.py b/hermes_cli/web_routers/files.pyindex a83fbd5690d..52a6fa19130 100644--- a/hermes_cli/web_routers/files.py+++ b/hermes_cli/web_routers/files.py@@ -30,6 +30,7 @@ from hermes_cli._subprocess_compat import windows_hide_flagsfrom hermes_cli.sqlite_safe_read import LiveConnectionError, offline_file_accessfrom hermes_cli.staged_uploads import ensure_profile_subdir, removed_on_failure, resolve_upload_homefrom hermes_cli.web_deps import late+from hermes_cli.web_upload_limit import CHAT_IMAGE_MAX_BYTESfrom hermes_cli.web_server_files import (SANDBOX_NOT_FOUND, _agent_fs_path, _fs_path, _hosted_fs_read_guard, _is_unmapped_container_path,_managed_file_entry, _managed_response_meta, _resolve_managed_path,@@ -495,7 +496,7 @@ def _decode_data_url(data_url: str) -> tuple[bytes, str]:return data, mime_type-_CHAT_IMAGE_UPLOAD_MAX_BYTES = 25 * 1024 * 1024+_CHAT_IMAGE_UPLOAD_MAX_BYTES = CHAT_IMAGE_MAX_BYTES_CHAT_IMAGE_ALLOWED_EXTENSIONS = frozenset({".png", ".jpg", ".jpeg", ".gif", ".webp", ".bmp"})_CHAT_IMAGE_MAGIC: tuple[tuple[bytes, str], ...] = ((b"\x89PNG\r\n\x1a\n", ".png"), (b"\xff\xd8\xff", ".jpg"),diff --git a/hermes_cli/web_upload_limit.py b/hermes_cli/web_upload_limit.pyindex 3ed3f6db314..cb5afcfb7cd 100644--- a/hermes_cli/web_upload_limit.py+++ b/hermes_cli/web_upload_limit.py@@ -1,4 +1,4 @@-"""Bound Webapp attachment ingestion before FastAPI parses/spools multipart."""+"""Bound chat upload request bodies before FastAPI parses, spools or buffers them."""from __future__ import annotations@@ -13,8 +13,22 @@ from hermes_constants import WEBAPP_ATTACHMENT_MAX_BYTES# and boundaries without letting arbitrary extra parts consume unbounded disk.CHAT_FILE_BODY_MAX_BYTES = WEBAPP_ATTACHMENT_MAX_BYTES + 64 * 1024+# Largest decoded image /api/chat/image-upload stores. The image arrives base64-encoded in a JSON+# body, which FastAPI reads whole before the handler checks the image: bound the body to that+# image's base64 length plus room for the JSON around it, so an oversized paste is never buffered.+CHAT_IMAGE_MAX_BYTES = 25 * 1024 * 1024+CHAT_IMAGE_BODY_MAX_BYTES = 4 * -(-CHAT_IMAGE_MAX_BYTES // 3) + 64 * 1024++_BODY_MAX_BYTES = {+ "/api/chat/file-upload": CHAT_FILE_BODY_MAX_BYTES,+ "/api/chat/image-upload": CHAT_IMAGE_BODY_MAX_BYTES,+}+class ChatFileUploadLimitMiddleware:+ """Refuse a chat file or image upload with 413 once its body passes the route's cap: at once+ when Content-Length declares more, else at the first chunk past it."""+def __init__(self, app: ASGIApp):self.app = app@@ -23,7 +37,8 @@ class ChatFileUploadLimitMiddleware:root = scope.get("root_path", "")if root and path.startswith(root + "/"):path = path[len(root) :]- if scope["type"] != "http" or scope["method"] != "POST" or path != "/api/chat/file-upload":+ body_max = _BODY_MAX_BYTES.get(path)+ if scope["type"] != "http" or scope["method"] != "POST" or body_max is None:await self.app(scope, receive, send)return@@ -32,7 +47,7 @@ class ChatFileUploadLimitMiddleware:if (length.isascii()and length.isdecimal()- and (len(length) > len(str(CHAT_FILE_BODY_MAX_BYTES)) or int(length) > CHAT_FILE_BODY_MAX_BYTES)+ and (len(length) > len(str(body_max)) or int(length) > body_max)):await rejection(scope, receive, send)return@@ -45,12 +60,13 @@ class ChatFileUploadLimitMiddleware:message = await receive()if message["type"] == "http.request":received += len(message.get("body", b""))- if received > CHAT_FILE_BODY_MAX_BYTES:+ if received > body_max:exceeded = True# This exception is deliberate: Starlette's multipart parser# closes *all* open spools on MultiPartException, unlike an# HTTPException or cancellation. The rejected chunk never- # reaches the parser. Request._get_form converts it to 400.+ # reaches the parser. Request._get_form converts it to 400,+ # as FastAPI does for a JSON body whose read fails.raise MultiPartException("Upload request is too large")return messagediff --git a/tests/hermes_cli/test_webapp_upload_limit.py b/tests/hermes_cli/test_webapp_upload_limit.pyindex 999b3ab63e3..dcae11f6792 100644--- a/tests/hermes_cli/test_webapp_upload_limit.py+++ b/tests/hermes_cli/test_webapp_upload_limit.py@@ -129,6 +129,53 @@ def test_upload_bound_stops_parser_and_closes_spools(upload_app, monkeypatch, deasyncio.run(run())[email protected]("declared_length", [None, "1", "oversize"])+def test_an_oversized_image_paste_is_refused_before_its_body_is_read(upload_app, monkeypatch, declared_length):+ """``/api/chat/image-upload`` carries the image as a base64 data URL in a JSON body. A body+ longer than the largest image it may carry is refused with 413 at once when Content-Length+ declares it, and otherwise near the cap while it streams in: never read whole into memory,+ and the handler never runs."""+ from hermes_cli.web_routers import files++ home, _spools = upload_app+ # The base64 length of the largest image the route accepts, plus room for the JSON around it.+ allowed = 4 * -(-files._CHAT_IMAGE_UPLOAD_MAX_BYTES // 3) + 1024 * 1024+ prefix = b'{"filename": "huge.png", "data_url": "data:image/png;base64,'+ received = 0++ async def body():+ nonlocal received+ received += len(prefix)+ yield prefix+ while received <= allowed:+ chunk = b"A" * (256 * 1024)+ received += len(chunk)+ yield chunk+ pytest.fail("image upload body was read past its cap")+ yield b'"}'++ def handler_must_not_run(*args, **kwargs):+ pytest.fail("image upload handler ran on an oversized body")++ monkeypatch.setattr(files, "resolve_upload_home", handler_must_not_run)++ async def run():+ async with httpx.AsyncClient(+ transport=httpx.ASGITransport(app=web_server.app), base_url="https://dashboard.example.com"+ ) as client:+ token = await _login(client)+ headers = {"Authorization": f"Bearer {token}", "Content-Type": "application/json"}+ if declared_length is not None:+ headers["Content-Length"] = str(2 * allowed) if declared_length == "oversize" else declared_length+ response = await client.post("/api/chat/image-upload", content=body(), headers=headers)+ assert response.status_code == 413, response.text+ assert not (home / "cache").exists()+ if declared_length == "oversize":+ assert received == 0++ asyncio.run(run())++@pytest.mark.parametrize("chunked", [False, True])def test_exact_cap_file_survives_multipart_framing(upload_app, chunked):_, spools = upload_app