mirror of
https://github.com/rommapp/romm.git
synced 2026-11-29 10:01:00 +01:00
- Rewrite the RetroArch Cloud Sync architecture section: add the PSP, kiosk, size cap and missing-file behavior, drop the stale hash note - Document SYNC_RETROARCH_PSP_SERIAL_MAP in env.template and the env table - Trim two docstrings and reuse CLIENT_DEVICE_IDENTIFIER for the device type Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
76 lines
2.6 KiB
Python
76 lines
2.6 KiB
Python
from re import Pattern
|
|
|
|
from starlette.datastructures import Headers
|
|
from starlette.exceptions import HTTPException
|
|
from starlette.responses import JSONResponse
|
|
from starlette.types import ASGIApp, Message, Receive, Scope, Send
|
|
|
|
|
|
class UploadSizeLimitMiddleware:
|
|
"""Reject oversized uploads on the given paths before they are spooled.
|
|
|
|
FastAPI spools `UploadFile` bodies before the endpoint runs, so only a
|
|
check here, on Content-Length and on the streamed bytes, bounds them.
|
|
"""
|
|
|
|
UNSAFE_METHODS = frozenset({"POST", "PUT", "PATCH"})
|
|
|
|
def __init__(
|
|
self,
|
|
app: ASGIApp,
|
|
*,
|
|
max_size: int,
|
|
paths: list[Pattern],
|
|
) -> None:
|
|
self.app = app
|
|
self.max_size = max_size
|
|
self.paths = paths
|
|
|
|
def _too_large_detail(self) -> str:
|
|
return f"Request body exceeds the maximum allowed size of {self.max_size} bytes"
|
|
|
|
async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None:
|
|
if (
|
|
scope["type"] != "http"
|
|
or not self.max_size
|
|
or scope["method"] not in self.UNSAFE_METHODS
|
|
or not any(pattern.match(scope["path"]) for pattern in self.paths)
|
|
):
|
|
await self.app(scope, receive, send)
|
|
return
|
|
|
|
content_length = Headers(scope=scope).get("content-length")
|
|
if content_length is not None:
|
|
try:
|
|
declared_size = int(content_length)
|
|
except ValueError:
|
|
response = JSONResponse(
|
|
{"detail": "Invalid Content-Length header"}, status_code=400
|
|
)
|
|
await response(scope, receive, send)
|
|
return
|
|
|
|
if declared_size > self.max_size:
|
|
response = JSONResponse(
|
|
{"detail": self._too_large_detail()}, status_code=413
|
|
)
|
|
await response(scope, receive, send)
|
|
return
|
|
|
|
received = 0
|
|
|
|
async def limited_receive() -> Message:
|
|
nonlocal received
|
|
message = await receive()
|
|
if message["type"] == "http.request":
|
|
received += len(message.get("body", b""))
|
|
# FastAPI re-raises an HTTPException from body parsing, so the
|
|
# app's exception handling turns this into the 413.
|
|
if received > self.max_size:
|
|
raise HTTPException(
|
|
status_code=413, detail=self._too_large_detail()
|
|
)
|
|
return message
|
|
|
|
await self.app(scope, limited_receive, send)
|