Skip to content

AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.

fix(backend): render file previews within a bounded size and concurrency cap (#15672)

Reinier van der Leerb662fbe5fd4d
diff --git a/autogpt_platform/backend/backend/api/features/workspace/preview.py b/autogpt_platform/backend/backend/api/features/workspace/preview.py
index bfcf1982c0..79675d81c0 100644
--- a/autogpt_platform/backend/backend/api/features/workspace/preview.py
+++ b/autogpt_platform/backend/backend/api/features/workspace/preview.py
@@ -19,8 +19,9 @@ import asyncio
import base64
import io
import logging
+import math
import zipfile
-from typing import Awaitable, Callable, Optional
+from typing import Awaitable, Callable, Optional, cast
import fastapi
import pypdfium2
@@ -35,7 +36,19 @@ logger = logging.getLogger(__name__)
PREVIEW_CACHE_TTL = 86_400 # 1 day
WEBP_QUALITY = 80
-MAX_IMAGE_PIXELS = 50_000_000 # decompression-bomb guard (~50 MP)
+# A preview is a small thumbnail, so rasterising a source larger than this spends
+# memory and CPU for no visible gain. Every renderer stays within this budget:
+# images decline a source above it, PDFs pick a render scale from the page size.
+MAX_RENDER_PIXELS = 50_000_000
+# Thumbnails rasterise in worker threads; cap how many run at once so a burst of
+# large previews can't hold many full-resolution bitmaps in memory together and
+# slow every other request on the process.
+MAX_CONCURRENT_RENDERS = 4
+# Upper bound on the render scale. A PDF page with a sub-pixel side has that
+# side rounded up to a whole pixel, so without a scale ceiling the other side
+# could still rasterise well past the pixel budget.
+MAX_RENDER_SCALE = 3
+_MIN_RENDER_SCALE = 0.01
CACHE_HEADERS = {"Cache-Control": "private, max-age=86400"}
# Per-kind size ceilings. The frontend mirrors these exactly so it never
@@ -140,35 +153,51 @@ def _webp_response(content: bytes) -> Response:
# Renderers (cached)
# ---------------------------------------------------------------------------
+_render_semaphores: dict[asyncio.AbstractEventLoop, asyncio.Semaphore] = {}
+
+
+def _render_semaphore() -> asyncio.Semaphore:
+ loop = asyncio.get_running_loop()
+ semaphore = _render_semaphores.get(loop)
+ if semaphore is None:
+ semaphore = asyncio.Semaphore(MAX_CONCURRENT_RENDERS)
+ _render_semaphores[loop] = semaphore
+ return semaphore
+
async def _image_thumbnail(file: WorkspaceFile, width: int) -> bytes:
async def render() -> bytes:
- content = await _retrieve(file)
- return await _to_thread_webp(_resize_webp, content, width)
+ async with _render_semaphore():
+ content = await _retrieve(file)
+ return await _to_thread_webp(_resize_webp, content, width)
return await _cached(file, "img", width, render)
async def _pdf_thumbnail(file: WorkspaceFile, width: int) -> bytes:
async def render() -> bytes:
- content = await _retrieve(file)
- return await _to_thread_webp(_pdfium_first_page, content, width)
+ async with _render_semaphore():
+ content = await _retrieve(file)
+ return await _to_thread_webp(_pdfium_first_page, content, width)
return await _cached(file, "pdf", width, render)
async def _office_thumbnail(file: WorkspaceFile, width: int) -> bytes:
async def render() -> bytes:
- content = await _retrieve(file)
- try:
- raw = await asyncio.to_thread(_extract_zip_thumbnail, content)
- except Exception as e:
- raise fastapi.HTTPException(
- status_code=415, detail="Cannot render preview"
- ) from e
- if raw is None:
- raise fastapi.HTTPException(status_code=415, detail="No embedded thumbnail")
- return await _to_thread_webp(_resize_webp, raw, width)
+ async with _render_semaphore():
+ content = await _retrieve(file)
+ try:
+ raw = await asyncio.to_thread(_extract_zip_thumbnail, content)
+ except Exception as e:
+ raise fastapi.HTTPException(
+ status_code=415, detail="Cannot render preview"
+ ) from e
+ if raw is None:
+ raise fastapi.HTTPException(
+ status_code=415, detail="No embedded thumbnail"
+ )
+ return await _to_thread_webp(_resize_webp, raw, width)
return await _cached(file, "office", width, render)
@@ -229,27 +258,49 @@ async def _cached(
def _resize_webp(content: bytes, width: int) -> bytes:
- Image.MAX_IMAGE_PIXELS = MAX_IMAGE_PIXELS
- with Image.open(io.BytesIO(content)) as img:
- # Image.open positions at the first frame, so animated GIFs/WebP already
- # render their frame 0 without an explicit seek.
- if img.mode not in ("RGB", "RGBA", "L"):
- img = img.convert("RGB")
- img.thumbnail((width, width * 4), Image.Resampling.LANCZOS)
- out = io.BytesIO()
- img.save(out, format="WEBP", quality=WEBP_QUALITY, method=4)
- return out.getvalue()
+ Image.MAX_IMAGE_PIXELS = MAX_RENDER_PIXELS
+ try:
+ with Image.open(io.BytesIO(content)) as img:
+ _ensure_within_render_budget(img.size)
+ # Image.open positions at the first frame, so animated GIFs/WebP
+ # already render their frame 0 without an explicit seek.
+ if img.mode not in ("RGB", "RGBA", "L"):
+ img = img.convert("RGB")
+ img.thumbnail((width, width * 4), Image.Resampling.LANCZOS)
+ out = io.BytesIO()
+ img.save(out, format="WEBP", quality=WEBP_QUALITY, method=4)
+ return out.getvalue()
+ except Image.DecompressionBombError as e:
+ # Pillow raises this above 2x the pixel limit, before the size check
+ # above runs; treat it as the same over-budget case, not a 415.
+ raise fastapi.HTTPException(
+ status_code=413, detail="File too large to preview"
+ ) from e
+
+
+def _ensure_within_render_budget(size: tuple[int, int]) -> None:
+ source_width, source_height = size
+ if source_width * source_height > MAX_RENDER_PIXELS:
+ raise fastapi.HTTPException(status_code=413, detail="File too large to preview")
def _pdfium_first_page(content: bytes, width: int) -> bytes:
pdf = pypdfium2.PdfDocument(content)
try:
page = pdf[0]
- page_width = page.get_width() or width
- # Integer render scale (pypdfium2's scale defaults to int); PIL downscales
- # to the exact target width afterwards.
- scale = max(1, min(3, round(width / page_width)))
- bitmap = page.render(scale=scale)
+ page_width, page_height = page.get_width(), page.get_height()
+ scale = _render_scale(page_width, page_height, width)
+ # A page can declare dimensions so large that even the minimum scale
+ # exceeds the budget; pypdfium2 rounds each side up, so check the bitmap
+ # it would allocate rather than the scaled area, and decline if over.
+ _ensure_within_render_budget(
+ (math.ceil(page_width * scale), math.ceil(page_height * scale))
+ )
+ # pypdfium2's ``scale`` is documented as a float but defaults to ``1``,
+ # so the inferred type is ``int``; a sub-unit scale is what shrinks a
+ # large page to thumbnail size.
+ render_page = cast("Callable[..., pypdfium2.PdfBitmap]", page.render)
+ bitmap = render_page(scale=scale)
image = bitmap.to_pil()
out = io.BytesIO()
if image.mode not in ("RGB", "L"):
@@ -261,6 +312,22 @@ def _pdfium_first_page(content: bytes, width: int) -> bytes:
pdf.close()
+def _render_scale(page_width: float, page_height: float, target_width: int) -> float:
+ """Render scale that reaches ~target_width while staying within the budget.
+
+ Rendering a large page at a fixed scale produces a proportionally large
+ bitmap; scaling from the page's own size keeps the output near thumbnail
+ size whatever the page dimensions. A fixed upper bound also caps the scale,
+ so a page with a sub-pixel side (which the renderer rounds up to one pixel)
+ cannot rasterise past the budget on its other side.
+ """
+ if page_width <= 0 or page_height <= 0:
+ return 1.0
+ to_target_width = target_width / page_width
+ within_budget = (MAX_RENDER_PIXELS / (page_width * page_height)) ** 0.5
+ return max(min(to_target_width, within_budget, MAX_RENDER_SCALE), _MIN_RENDER_SCALE)
+
+
def _extract_zip_thumbnail(content: bytes) -> Optional[bytes]:
"""Return the embedded ``docProps/thumbnail.*`` image from an openxml file."""
with zipfile.ZipFile(io.BytesIO(content)) as archive:
diff --git a/autogpt_platform/backend/backend/api/features/workspace/preview_test.py b/autogpt_platform/backend/backend/api/features/workspace/preview_test.py
index fdb0ec5fa6..825f4aee94 100644
--- a/autogpt_platform/backend/backend/api/features/workspace/preview_test.py
+++ b/autogpt_platform/backend/backend/api/features/workspace/preview_test.py
@@ -1,5 +1,9 @@
+import asyncio
import base64
import io
+import math
+import threading
+import time
import zipfile
from datetime import datetime, timezone
from unittest.mock import AsyncMock
@@ -10,6 +14,7 @@ import pytest
from fpdf import FPDF
from PIL import Image
+from backend.api.features.workspace import preview as preview_module
from backend.api.features.workspace.routes import router
from backend.data.workspace import Workspace, WorkspaceFile
@@ -72,15 +77,51 @@ def _pdf_bytes() -> bytes:
return bytes(pdf.output())
-def _zip_bytes(*, with_thumbnail: bool) -> bytes:
+def _zip_bytes(*, with_thumbnail: bool, thumbnail: bytes | None = None) -> bytes:
buf = io.BytesIO()
with zipfile.ZipFile(buf, "w") as archive:
if with_thumbnail:
- archive.writestr("docProps/thumbnail.png", _png_bytes(120, 90))
+ archive.writestr("docProps/thumbnail.png", thumbnail or _png_bytes(120, 90))
archive.writestr("ppt/presentation.xml", "<p/>")
return buf.getvalue()
+def _oversized_png() -> bytes:
+ # A source well above the render budget (~50 MP), kept cheap on disk and in
+ # memory by using a single-channel solid image.
+ side = int((preview_module.MAX_RENDER_PIXELS * 1.1) ** 0.5)
+ out = io.BytesIO()
+ Image.new("L", (side, side)).save(out, format="PNG")
+ return out.getvalue()
+
+
+def _decompression_bomb_png() -> bytes:
+ # Above 2x the budget, where Pillow raises its own error at open, before the
+ # explicit size check runs.
+ side = int((2 * preview_module.MAX_RENDER_PIXELS) ** 0.5) + 100
+ out = io.BytesIO()
+ Image.new("L", (side, side)).save(out, format="PNG")
+ return out.getvalue()
+
+
+def _large_page_pdf() -> bytes:
+ # A page far larger than a thumbnail; a fixed render scale would rasterise it
+ # to a bitmap many times the budget.
+ pdf = FPDF(unit="mm", format=(3000, 3000))
+ pdf.add_page()
+ pdf.set_font("helvetica", size=24)
+ pdf.cell(40, 10, "Hello preview")
+ return bytes(pdf.output())
+
+
+def _oversized_page_pdf() -> bytes:
+ # A compact PDF declaring a page so large that even the minimum render scale
+ # would exceed the budget; the preview is declined before rendering.
+ pdf = FPDF(unit="pt", format=(1_000_000, 1_000_000))
+ pdf.add_page()
+ return bytes(pdf.output())
+
+
def _mock_lookups(mocker, file: WorkspaceFile) -> None:
mocker.patch(
"backend.api.features.workspace.routes.get_workspace",
@@ -288,3 +329,137 @@ def test_image_preview_cache_hit_skips_retrieve(mocker):
assert response.status_code == 200
assert response.content == cached
storage.retrieve.assert_not_called()
+
+
+# ---------------------------------------------------------------------------
+# Render budget: a thumbnail never rasterises more than MAX_RENDER_PIXELS, so
+# each renderer stays within the budget whatever the source's dimensions.
+# ---------------------------------------------------------------------------
+
+
+def test_render_scale_keeps_a_large_page_within_budget():
+ # A ~3000 mm page is ~8503 pt on a side.
+ page_pt = 3000 / 25.4 * 72
+ scale = preview_module._render_scale(page_pt, page_pt, 400)
+
+ rendered_pixels = (page_pt * scale) ** 2
+ assert rendered_pixels <= preview_module.MAX_RENDER_PIXELS
+
+ # Without the budget, a fixed render scale rasterises the whole page and
+ # blows past it — this is what the budget exists to prevent.
+ fixed_scale = max(1, min(3, round(400 / page_pt)))
+ assert (page_pt * fixed_scale) ** 2 > preview_module.MAX_RENDER_PIXELS
+
+
+def test_render_scale_caps_a_sliver_page():
+ # A page with a sub-pixel side: the renderer rounds each rendered side up, so
+ # the continuous-area budget alone underestimates the bitmap. The scale
+ # ceiling keeps the rounded bitmap within the budget.
+ page_width, page_height = 0.000025, 10_000.0
+ scale = preview_module._render_scale(page_width, page_height, 400)
+
+ assert scale <= preview_module.MAX_RENDER_SCALE
+ rounded_pixels = math.ceil(page_width * scale) * math.ceil(page_height * scale)
+ assert rounded_pixels <= preview_module.MAX_RENDER_PIXELS
+
+
+def test_pdf_preview_renders_a_large_page_as_a_thumbnail(mocker):
+ _mock_lookups(mocker, _make_file(mime_type="application/pdf"))
+ _mock_storage(mocker, _large_page_pdf())
+ _mock_redis(mocker)
+
+ response = client.get("/files/file-001/preview?w=200")
+
+ assert response.status_code == 200
+ assert response.headers["content-type"] == "image/webp"
+ with Image.open(io.BytesIO(response.content)) as rendered:
+ assert max(rendered.size) <= 200 * 4
+
+
+def test_pdf_preview_declines_a_page_too_large_for_the_budget(mocker):
+ _mock_lookups(mocker, _make_file(mime_type="application/pdf"))
+ _mock_storage(mocker, _oversized_page_pdf())
+ _mock_redis(mocker)
+
+ response = client.get("/files/file-001/preview?w=400")
+
+ assert response.status_code == 413
+
+
+def test_image_preview_declines_an_oversized_source(mocker):
+ _mock_lookups(mocker, _make_file(mime_type="image/png"))
+ _mock_storage(mocker, _oversized_png())
+ _mock_redis(mocker)
+
+ response = client.get("/files/file-001/preview?w=100")
+
+ assert response.status_code == 413
+
+
+def test_image_preview_declines_a_decompression_bomb_source(mocker):
+ # Above 2x the budget Pillow raises before the size check; it must still be a
+ # 413 (too large), not a generic 415 render failure.
+ _mock_lookups(mocker, _make_file(mime_type="image/png"))
+ _mock_storage(mocker, _decompression_bomb_png())
+ _mock_redis(mocker)
+
+ response = client.get("/files/file-001/preview?w=100")
+
+ assert response.status_code == 413
+
+
+def test_office_preview_declines_an_oversized_thumbnail(mocker):
+ _mock_lookups(mocker, _make_file(mime_type=PPTX_MIME))
+ _mock_storage(mocker, _zip_bytes(with_thumbnail=True, thumbnail=_oversized_png()))
+ _mock_redis(mocker)
+
+ response = client.get("/files/file-001/preview")
+
+ assert response.status_code == 413
+
+
+async def test_preview_render_concurrency_is_bounded(mocker):
+ # Instrument the rasteriser (it runs in a worker thread) to record the peak
+ # number of simultaneous renders, then fire more previews than the cap.
+ peak = 0
+ in_flight = 0
+ lock = threading.Lock()
+
+ def instrumented(_content: bytes, _width: int) -> bytes:
+ nonlocal peak, in_flight
+ with lock:
+ in_flight += 1
+ peak = max(peak, in_flight)
+ time.sleep(0.1)
+ with lock:
+ in_flight -= 1
+ return b"RIFFfake-webp"
+
+ mocker.patch.object(preview_module, "_resize_webp", instrumented)
+
+ storage = mocker.MagicMock()
+ storage.retrieve = AsyncMock(return_value=b"image-bytes")
+ mocker.patch.object(
+ preview_module, "get_workspace_storage", AsyncMock(return_value=storage)
+ )
+ redis = mocker.MagicMock()
+ redis.get = AsyncMock(return_value=None)
+ redis.set = AsyncMock(return_value=None)
+ mocker.patch.object(
+ preview_module, "get_redis_async", AsyncMock(return_value=redis)
+ )
+
+ files = [
+ _make_file(id=f"file-{i}", mime_type="image/png", checksum=f"sum-{i}")
+ for i in range(10)
+ ]
+
+ await asyncio.gather(
+ *(
+ preview_module.build_preview_response(f, width=100, max_bytes=4096)
+ for f in files
+ )
+ )
+
+ assert peak > 1 # the renders really did overlap
+ assert peak <= preview_module.MAX_CONCURRENT_RENDERS