fix(middleware): allow up to 4GB payloads for migration datasets and media uploads
This commit is contained in:
parent
fb1ec060fc
commit
998ecc025f
2 changed files with 21 additions and 4 deletions
|
|
@ -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."}
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue