The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(vision): reject truncated images before embedding
HaiyiMeiad0e83062fcc
diff --git a/tests/tools/test_vision_native_fast_path.py b/tests/tools/test_vision_native_fast_path.pyindex 715b3ee7816..f0d436df94d 100644--- a/tests/tools/test_vision_native_fast_path.py+++ b/tests/tools/test_vision_native_fast_path.py@@ -11,6 +11,7 @@ from __future__ import annotationsimport asyncioimport base64import json+from io import BytesIOfrom unittest.mock import patch@@ -28,6 +29,47 @@ _TINY_PNG = base64.b64decode()+def _animated_gif_bytes(colors, *, size=(4, 4)):+ from PIL import Image++ frames = [Image.new("RGB", size, color) for color in colors]+ encoded = BytesIO()+ frames[0].save(+ encoded,+ format="GIF",+ save_all=True,+ append_images=frames[1:],+ duration=100,+ loop=0,+ )+ return encoded.getvalue()+++def _track_validated_frame_loads(monkeypatch):+ from PIL import ImageSequence++ loaded_frames = []+ real_iterator = ImageSequence.Iterator++ class TrackedFrame:+ def __init__(self, frame, frame_number):+ self.frame = frame+ self.frame_number = frame_number+ self.width = frame.width+ self.height = frame.height++ def load(self):+ loaded_frames.append(self.frame_number)+ return self.frame.load()++ def tracking_iterator(image):+ for frame_number, frame in enumerate(real_iterator(image), start=1):+ yield TrackedFrame(frame, frame_number)++ monkeypatch.setattr(ImageSequence, "Iterator", tracking_iterator)+ return loaded_frames++# ─── _supports_media_in_tool_results ─────────────────────────────────────────@@ -94,6 +136,121 @@ class TestVisionAnalyzeNative:url = next(p["image_url"]["url"] for p in parts if p.get("type") == "image_url")assert url.startswith("data:image/")+ def test_truncated_supported_image_is_rejected_before_embedding(self, tmp_path):+ """A valid header must not let partially downloaded bytes poison history."""+ pytest = __import__("pytest")+ Image = pytest.importorskip(+ "PIL.Image", reason="Pillow is required for full raster decode validation"+ )++ truncated = tmp_path / "truncated.png"+ truncated.write_bytes(_TINY_PNG[:-22])++ # This is the production failure shape: header parsing succeeds, but a+ # complete decode fails after the partial download is read.+ with Image.open(truncated) as image:+ assert image.format == "PNG"+ with pytest.raises(OSError, match="truncated"):+ image.load()++ result = asyncio.get_event_loop().run_until_complete(+ _vision_analyze_native(str(truncated), "describe")+ )++ assert isinstance(result, str), "corrupt image must not return a multimodal envelope"+ payload = json.loads(result)+ assert payload["success"] is False+ assert "decode" in payload["error"].lower()++ def test_truncated_animated_gif_frame_is_rejected_before_embedding(+ self, tmp_path, monkeypatch+ ):+ """Every frame must decode before an animated raster enters history."""+ pytest = __import__("pytest")+ pytest.importorskip(+ "PIL.Image", reason="Pillow is required for full raster decode validation"+ )+ from PIL import Image, ImageSequence++ truncated = tmp_path / "truncated.gif"+ truncated.write_bytes(_animated_gif_bytes(["red", "blue"])[:-3])+ monkeypatch.setattr(+ "tools.vision_tools._VISION_MAX_VALIDATED_FRAME_COUNT", 2+ )+ monkeypatch.setattr(+ "tools.vision_tools._VISION_MAX_VALIDATED_AGGREGATE_PIXELS", 32+ )++ with Image.open(truncated) as image:+ assert image.format == "GIF"+ assert getattr(image, "n_frames", 1) == 2+ with pytest.raises(OSError, match="truncated"):+ for frame in ImageSequence.Iterator(image):+ frame.load()++ result = asyncio.get_event_loop().run_until_complete(+ _vision_analyze_native(str(truncated), "describe")+ )++ assert isinstance(result, str), "corrupt animation must not return a multimodal envelope"+ payload = json.loads(result)+ assert payload["success"] is False+ assert "decode" in payload["error"].lower()++ def test_animation_over_frame_validation_limit_is_rejected(+ self, tmp_path, monkeypatch+ ):+ """A small animation is rejected before an excess frame is decoded."""+ pytest = __import__("pytest")+ pytest.importorskip(+ "PIL.Image", reason="Pillow is required for full raster decode validation"+ )++ animation = tmp_path / "too-many-frames.gif"+ animation.write_bytes(_animated_gif_bytes(["red", "green", "blue"]))+ monkeypatch.setattr(+ "tools.vision_tools._VISION_MAX_VALIDATED_FRAME_COUNT", 2+ )+ loaded_frames = _track_validated_frame_loads(monkeypatch)++ result = asyncio.get_event_loop().run_until_complete(+ _vision_analyze_native(str(animation), "describe")+ )++ assert isinstance(result, str)+ payload = json.loads(result)+ assert payload["success"] is False+ assert "frame 3" in payload["error"].lower()+ assert "maximum 2" in payload["error"].lower()+ assert loaded_frames == [1, 2]++ def test_animation_over_aggregate_pixel_validation_limit_is_rejected(+ self, tmp_path, monkeypatch+ ):+ """Aggregate decoded pixels are bounded independently of file size."""+ pytest = __import__("pytest")+ pytest.importorskip(+ "PIL.Image", reason="Pillow is required for full raster decode validation"+ )++ animation = tmp_path / "too-many-pixels.gif"+ animation.write_bytes(_animated_gif_bytes(["red", "blue"]))+ monkeypatch.setattr(+ "tools.vision_tools._VISION_MAX_VALIDATED_AGGREGATE_PIXELS", 31+ )+ loaded_frames = _track_validated_frame_loads(monkeypatch)++ result = asyncio.get_event_loop().run_until_complete(+ _vision_analyze_native(str(animation), "describe")+ )++ assert isinstance(result, str)+ payload = json.loads(result)+ assert payload["success"] is False+ assert "aggregate decoded pixel" in payload["error"].lower()+ assert "32" in payload["error"]+ assert "maximum 31" in payload["error"].lower()+ assert loaded_frames == [1]def test_file_url_scheme_resolves(self, tmp_path):img = tmp_path / "t.png"diff --git a/tools/vision_tools.py b/tools/vision_tools.pyindex 8d05c153e6f..f77d4a5eaef 100644--- a/tools/vision_tools.py+++ b/tools/vision_tools.py@@ -379,6 +379,62 @@ def _normalize_to_supported_image()+# Full raster validation runs on untrusted images in a shared CPU executor.+# Bound animated-image work by both iteration count and total decoded area so a+# compact file cannot monopolize a worker with an effectively unbounded number+# of frames. Images at or below both limits still have every frame decoded.+_VISION_MAX_VALIDATED_FRAME_COUNT = 100+_VISION_MAX_VALIDATED_AGGREGATE_PIXELS = 100_000_000+++def _validate_raster_image_decodable(image_path: Path) -> Optional[str]:+ """Return an error when Pillow cannot completely decode every image frame.++ Magic-byte MIME sniffing and ``Image.open`` only inspect container headers.+ A timed-out download can therefore look like a supported PNG/JPEG/GIF/WebP+ while its pixel stream is truncated. Native vision results are retained in+ conversation history, so embedding those bytes poisons every later provider+ request. Verify structure, then reopen and force every frame within the+ validation resource limits to decode before the image can enter history.+ """+ try:+ from PIL import Image as _PILImage+ from PIL import ImageSequence as _PILImageSequence++ with _PILImage.open(image_path) as image:+ image.verify()+ with _PILImage.open(image_path) as image:+ validated_pixels = 0+ for frame_number, frame in enumerate(+ _PILImageSequence.Iterator(image), start=1+ ):+ if frame_number > _VISION_MAX_VALIDATED_FRAME_COUNT:+ return (+ "Image validation rejected animation: "+ f"frame {frame_number} exceeds the maximum "+ f"{_VISION_MAX_VALIDATED_FRAME_COUNT} validated frames."+ )++ frame_pixels = frame.width * frame.height+ next_validated_pixels = validated_pixels + frame_pixels+ if (+ next_validated_pixels+ > _VISION_MAX_VALIDATED_AGGREGATE_PIXELS+ ):+ return (+ "Image validation rejected animation: aggregate decoded "+ f"pixel count would reach {next_validated_pixels} at frame "+ f"{frame_number}, exceeding the maximum "+ f"{_VISION_MAX_VALIDATED_AGGREGATE_PIXELS}."+ )++ frame.load()+ validated_pixels = next_validated_pixels+ except Exception as exc:+ return f"Image could not be fully decoded: {exc}"+ return None++def _is_retryable_download_error(error: Exception) -> bool:"""Return True only for transient image-download failures worth retrying.@@ -1226,6 +1282,12 @@ async def _vision_analyze_native(should_cleanup = Trueimage_size_bytes = temp_image_path.stat().st_size+ decode_error = await _run_encode_on_cpu_executor(+ _validate_raster_image_decodable, temp_image_path,+ )+ if decode_error:+ return tool_error(decode_error, success=False)+# Optional region zoom: crop BEFORE the downscale/embed-cap pipeline# so the cropped area gets the full resolution budget._crop_offset: dict = {}