web_upload_limit.py 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. """Bound chat upload request bodies before FastAPI parses, spools or buffers them."""
  2. from __future__ import annotations
  3. from starlette.datastructures import Headers
  4. from starlette.formparsers import MultiPartException
  5. from starlette.responses import JSONResponse
  6. from starlette.types import ASGIApp, Message, Receive, Scope, Send
  7. from hermes_cli.web_server_surface import policy
  8. from hermes_constants import WEBAPP_ATTACHMENT_MAX_BYTES
  9. # The file cap remains exact in the handler; allow ordinary multipart headers
  10. # and boundaries without letting arbitrary extra parts consume unbounded disk.
  11. CHAT_FILE_BODY_MAX_BYTES = WEBAPP_ATTACHMENT_MAX_BYTES + 64 * 1024
  12. # Largest decoded image /api/chat/image-upload stores. The image arrives base64-encoded in a JSON
  13. # body, which FastAPI reads whole before the handler checks the image: bound the body to that
  14. # image's base64 length plus room for the JSON around it, so an oversized paste is never buffered.
  15. CHAT_IMAGE_MAX_BYTES = 25 * 1024 * 1024
  16. CHAT_IMAGE_BODY_MAX_BYTES = 4 * -(-CHAT_IMAGE_MAX_BYTES // 3) + 64 * 1024
  17. _BODY_MAX_BYTES = {
  18. "/api/chat/file-upload": CHAT_FILE_BODY_MAX_BYTES,
  19. "/api/chat/image-upload": CHAT_IMAGE_BODY_MAX_BYTES,
  20. }
  21. class ChatFileUploadLimitMiddleware:
  22. """Refuse a chat file or image upload with 413 once its body passes the route's cap: at once
  23. when Content-Length declares more, else at the first chunk past it. Only on the surfaces whose
  24. policy serves the browser's file routes (the Webapp); elsewhere uploads reach their handlers
  25. as on main."""
  26. def __init__(self, app: ASGIApp):
  27. self.app = app
  28. async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None:
  29. path = scope.get("path", "")
  30. root = scope.get("root_path", "")
  31. if root and path.startswith(root + "/"):
  32. path = path[len(root) :]
  33. body_max = _BODY_MAX_BYTES.get(path)
  34. if (scope["type"] != "http" or scope["method"] != "POST" or body_max is None
  35. or not policy(getattr(scope.get("app"), "state", None)).browser_files):
  36. await self.app(scope, receive, send)
  37. return
  38. rejection = JSONResponse(status_code=413, content={"detail": "Upload request is too large"})
  39. length = Headers(scope=scope).get("content-length", "").lstrip("0") or "0"
  40. if (
  41. length.isascii()
  42. and length.isdecimal()
  43. and (len(length) > len(str(body_max)) or int(length) > body_max)
  44. ):
  45. await rejection(scope, receive, send)
  46. return
  47. received = 0
  48. exceeded = False
  49. async def limited_receive() -> Message:
  50. nonlocal received, exceeded
  51. message = await receive()
  52. if message["type"] == "http.request":
  53. received += len(message.get("body", b""))
  54. if received > body_max:
  55. exceeded = True
  56. # This exception is deliberate: Starlette's multipart parser
  57. # closes *all* open spools on MultiPartException, unlike an
  58. # HTTPException or cancellation. The rejected chunk never
  59. # reaches the parser. Request._get_form converts it to 400,
  60. # as FastAPI does for a JSON body whose read fails.
  61. raise MultiPartException("Upload request is too large")
  62. return message
  63. async def limited_send(message: Message) -> None:
  64. if not exceeded:
  65. await send(message)
  66. elif message["type"] == "http.response.start":
  67. # Replace the parser's 400 (including its Content-Length) with
  68. # 413, after its cleanup, and suppress the original body.
  69. await rejection(scope, receive, send)
  70. await self.app(scope, limited_receive, limited_send)