fix(middleware): allow up to 4GB payloads for migration datasets and media uploads

This commit is contained in:
iFixKart Dev 2026-11-09 17:29:50 +05:30
parent fb1ec060fc
commit 998ecc025f
2 changed files with 21 additions and 4 deletions

View file

@ -31,9 +31,17 @@ class SecurityHeadersMiddleware(BaseHTTPMiddleware):
return response return response
class RequestSizeLimitMiddleware(BaseHTTPMiddleware): 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) super().__init__(app)
self.max_bytes = max_bytes 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): async def dispatch(self, request: Request, call_next):
# Read content length header # Read content length header
@ -41,8 +49,12 @@ class RequestSizeLimitMiddleware(BaseHTTPMiddleware):
if content_length: if content_length:
try: try:
length = int(content_length) length = int(content_length)
if length > self.max_bytes: # Determine limit based on endpoint path
max_mb = self.max_bytes // (1024 * 1024) 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( return JSONResponse(
status_code=status.HTTP_413_REQUEST_ENTITY_TOO_LARGE, status_code=status.HTTP_413_REQUEST_ENTITY_TOO_LARGE,
content={"detail": f"Payload too large. Maximum size allowed is {max_mb}MB."} content={"detail": f"Payload too large. Maximum size allowed is {max_mb}MB."}

View file

@ -192,7 +192,12 @@ app.add_exception_handler(RateLimitExceeded, _rate_limit_exceeded_handler)
app.add_middleware(AuditMiddleware) app.add_middleware(AuditMiddleware)
app.add_middleware(KillSwitchMiddleware) 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(SecurityHeadersMiddleware)
app.add_middleware(RequestTraceMiddleware) app.add_middleware(RequestTraceMiddleware)