From 998ecc025f0de7380e25225a9ca7f0240d3feae9 Mon Sep 17 00:00:00 2001 From: iFixKart Dev Date: Mon, 9 Nov 2026 17:29:50 +0530 Subject: [PATCH] fix(middleware): allow up to 4GB payloads for migration datasets and media uploads --- app/core/middleware/security_middleware.py | 18 +++++++++++++++--- app/main.py | 7 ++++++- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/app/core/middleware/security_middleware.py b/app/core/middleware/security_middleware.py index c3e65db..7faa7c2 100644 --- a/app/core/middleware/security_middleware.py +++ b/app/core/middleware/security_middleware.py @@ -31,9 +31,17 @@ class SecurityHeadersMiddleware(BaseHTTPMiddleware): return response class RequestSizeLimitMiddleware(BaseHTTPMiddleware): - def __init__(self, app, max_bytes: int = 10 * 1024 * 1024): # Default 10MB + def __init__( + self, + app, + max_bytes: int = 50 * 1024 * 1024, + large_paths_max_bytes: int = 4 * 1024 * 1024 * 1024, # 4GB for migration / archives + large_paths: tuple = ("/api/v1/migration", "/api/v1/files/upload") + ): super().__init__(app) self.max_bytes = max_bytes + self.large_paths_max_bytes = large_paths_max_bytes + self.large_paths = large_paths async def dispatch(self, request: Request, call_next): # Read content length header @@ -41,8 +49,12 @@ class RequestSizeLimitMiddleware(BaseHTTPMiddleware): if content_length: try: length = int(content_length) - if length > self.max_bytes: - max_mb = self.max_bytes // (1024 * 1024) + # Determine limit based on endpoint path + is_large_path = any(request.url.path.startswith(prefix) for prefix in self.large_paths) + limit = self.large_paths_max_bytes if is_large_path else self.max_bytes + + if length > limit: + max_mb = limit // (1024 * 1024) return JSONResponse( status_code=status.HTTP_413_REQUEST_ENTITY_TOO_LARGE, content={"detail": f"Payload too large. Maximum size allowed is {max_mb}MB."} diff --git a/app/main.py b/app/main.py index dc1dab1..b386550 100644 --- a/app/main.py +++ b/app/main.py @@ -192,7 +192,12 @@ app.add_exception_handler(RateLimitExceeded, _rate_limit_exceeded_handler) app.add_middleware(AuditMiddleware) app.add_middleware(KillSwitchMiddleware) -app.add_middleware(RequestSizeLimitMiddleware, max_bytes=50 * 1024 * 1024) # 50MB standard upload limit +app.add_middleware( + RequestSizeLimitMiddleware, + max_bytes=50 * 1024 * 1024, # 50MB standard upload limit + large_paths_max_bytes=4 * 1024 * 1024 * 1024, # 4GB for migration datasets / media archives + large_paths=("/api/v1/migration", "/api/v1/files/upload") +) app.add_middleware(SecurityHeadersMiddleware) app.add_middleware(RequestTraceMiddleware)