Files
romm/backend/utils/uploads.py
Georges-Antoine Assi 96833892ce security: enforce asset upload limits before spooling, cover screenshots
Addresses the Greptile review on #3952.

- Reject oversized asset uploads from Content-Length in middleware, before
  FastAPI parses and spools the multipart body to temporary storage. The
  handler-level check stays as a backstop for chunked requests.
- Apply the limit to screenshot fields on the save, state and standalone
  screenshot endpoints, not just the primary file.
- Clamp the autocleanup limit to at least 1 so a non-positive value cannot
  wipe every save in a slot.
- Document the new settings in env.template.

Rename MAX_SAVE_UPLOAD_SIZE_BYTES to MAX_ASSET_UPLOAD_SIZE_BYTES now that it
covers states and screenshots too.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-07-26 09:24:42 -04:00

24 lines
869 B
Python

from fastapi import HTTPException, UploadFile, status
from config import MAX_ASSET_UPLOAD_SIZE_BYTES
def check_asset_upload_size(file: UploadFile | None, label: str) -> None:
"""Reject an asset upload whose parsed size exceeds the configured ceiling.
Backstop for `UploadSizeLimitMiddleware`, which rejects on Content-Length
before the body is spooled. This catches requests that arrive without a
declared length (chunked transfer encoding).
"""
if file is None or not MAX_ASSET_UPLOAD_SIZE_BYTES:
return
if file.size is not None and file.size > MAX_ASSET_UPLOAD_SIZE_BYTES:
raise HTTPException(
status_code=status.HTTP_413_CONTENT_TOO_LARGE,
detail=(
f"{label} exceeds the maximum allowed size of "
f"{MAX_ASSET_UPLOAD_SIZE_BYTES} bytes"
),
)