https://arxiv.org/abs/2606.28344. The end of web parsing. The beginning of scalable pixel-native search. link: https://pixelrag.ai/
fix(serve): bound query image decode and batch size to prevent OOM/DoS (#154)
Shubham Srivastavac0cfe7f7140f
diff --git a/serve/src/pixelrag_serve/api.py b/serve/src/pixelrag_serve/api.pyindex 0271ff9..d606a9e 100644--- a/serve/src/pixelrag_serve/api.py+++ b/serve/src/pixelrag_serve/api.py@@ -171,7 +171,9 @@ class Query(BaseModel):class SearchRequest(BaseModel):- queries: list[Query]+ # Bounded: an unbounded batch multiplies every per-query cost (embedding,+ # image decode) on the public endpoint. Real callers send 1; 32 is generous.+ queries: list[Query] = Field(max_length=32)# Bounded: fetch_k = n_docs * 10 feeds FAISS search k, so an unbounded value# is a trivial OOM/DoS on the public endpoint. Largest real caller uses 10.n_docs: int = Field(default=10, ge=1, le=1000)@@ -271,6 +273,12 @@ class StatusResponse(BaseModel):DEFAULT_INSTRUCTION = "Retrieve images or text relevant to the user's query."+# Bounds for attacker-controlled query images on the public /search endpoint.+# ~10 MB of base64 caps the payload/decode; 25 MP caps the decoded RGB canvas+# (query images are downscaled far below this for the VL model anyway).+_MAX_IMAGE_B64_LEN = 10 * 1024 * 1024+_MAX_IMAGE_PIXELS = 25_000_000+def _parse_queries(queries: list[Query], instruction: str | None = None@@ -291,8 +299,27 @@ def _parse_queries(img_data = q.imageif img_data.startswith("data:"):img_data = img_data.split(",", 1)[-1]- img_bytes = base64.b64decode(img_data)- img = Image.open(io.BytesIO(img_bytes)).convert("RGB")+ # Public endpoint: q.image is attacker-controlled. Bound the payload+ # and the decoded canvas so an oversized blob or a tiny+ # "decompression bomb" can't OOM the service, and turn any decode+ # failure into a clean 400 instead of an uncaught 500.+ if len(img_data) > _MAX_IMAGE_B64_LEN:+ raise HTTPException(+ status_code=400,+ detail=f"image too large (max {_MAX_IMAGE_B64_LEN} base64 chars)",+ )+ try:+ img = Image.open(io.BytesIO(base64.b64decode(img_data)))+ if img.width * img.height > _MAX_IMAGE_PIXELS:+ raise HTTPException(+ status_code=400,+ detail=f"image too large (max {_MAX_IMAGE_PIXELS} pixels)",+ )+ img = img.convert("RGB")+ except HTTPException:+ raise+ except Exception:+ raise HTTPException(status_code=400, detail="invalid image data")user_content.append({"type": "image", "image": img})if q.text:user_content.append({"type": "text", "text": q.text})diff --git a/tests/test_serve_image_input.py b/tests/test_serve_image_input.pynew file mode 100644index 0000000..ca8f721--- /dev/null+++ b/tests/test_serve_image_input.py@@ -0,0 +1,58 @@+"""Input bounds on the public /search endpoint (OOM/DoS hardening).++`_parse_queries` decodes attacker-controlled base64 images and `SearchRequest`+caps the batch size. A crafted request must not exhaust memory or turn an+uncaught decode error into a 500 — the same public-endpoint hardening as the+n_docs bound.+"""++import base64+import io++import pytest++pytest.importorskip("fastapi", reason="serve extra not installed")+from fastapi import HTTPException+from PIL import Image+from pixelrag_serve.api import Query, SearchRequest, _parse_queries+from pydantic import ValidationError+++def _png_b64(w: int, h: int) -> str:+ buf = io.BytesIO()+ Image.new("RGB", (w, h)).save(buf, format="PNG")+ return base64.b64encode(buf.getvalue()).decode()+++def test_queries_batch_is_capped():+ SearchRequest(queries=[Query(text="ok")]) # small batch fine+ with pytest.raises(ValidationError):+ SearchRequest(queries=[Query(text="x")] * 33)+++def test_valid_image_decodes():+ _, images = _parse_queries([Query(image=_png_b64(4, 4))])+ assert images[0] is not None and images[0].mode == "RGB"+++def test_corrupt_image_is_400_not_500():+ not_an_image = base64.b64encode(b"hello world, definitely not a png").decode()+ with pytest.raises(HTTPException) as e:+ _parse_queries([Query(image=not_an_image)])+ assert e.value.status_code == 400+++def test_oversized_payload_is_400(monkeypatch):+ monkeypatch.setattr("pixelrag_serve.api._MAX_IMAGE_B64_LEN", 16)+ with pytest.raises(HTTPException) as e:+ _parse_queries([Query(image=_png_b64(4, 4))]) # base64 longer than 16+ assert e.value.status_code == 400+++def test_decompression_bomb_is_400(monkeypatch):+ # A small image over a lowered pixel cap stands in for a bomb whose header+ # claims a huge canvas — the guard must reject before .convert() allocates.+ monkeypatch.setattr("pixelrag_serve.api._MAX_IMAGE_PIXELS", 100)+ with pytest.raises(HTTPException) as e:+ _parse_queries([Query(image=_png_b64(50, 50))]) # 2500 px > 100+ assert e.value.status_code == 400