From 3efe00592f55fb6b8ae29cc09a81230800838e81 Mon Sep 17 00:00:00 2001 From: fei <204683769+feiiiiii5@users.noreply.github.com> Date: Thu, 10 Sep 2026 13:09:52 +0800 Subject: [PATCH 1/2] FIX: reject non-finite AddImageText rotation --- pyrit/converter/add_image_text_converter.py | 8 ++++++-- tests/unit/converter/test_add_image_text_converter.py | 7 +++++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/pyrit/converter/add_image_text_converter.py b/pyrit/converter/add_image_text_converter.py index 0906c91daa..913ad63b43 100644 --- a/pyrit/converter/add_image_text_converter.py +++ b/pyrit/converter/add_image_text_converter.py @@ -3,6 +3,7 @@ import base64 import logging +import math from io import BytesIO from typing import cast @@ -59,13 +60,14 @@ def __init__( bounding_box (tuple[int, int, int, int] | None): Optional (x1, y1, x2, y2) region to constrain text within. When not set, the full image is used with a default margin. Defaults to None. - rotation (float): Rotation angle in degrees for the text. Defaults to 0.0. + rotation (float): Rotation angle in degrees for the text. Must be finite. Defaults to 0.0. center_text (bool): Whether to center text horizontally and vertically within the bounding box. Defaults to False. Raises: ValueError: If img_to_add is empty, font_name doesn't end with ".ttf", - font_size is invalid, or bounding_box coordinates are invalid. + font_size is invalid, bounding_box coordinates are invalid, + or rotation is non-finite. """ if not img_to_add: raise ValueError("Please provide valid image path") @@ -76,6 +78,8 @@ def __init__( x1, y1, x2, y2 = bounding_box if x2 <= x1 or y2 <= y1: raise ValueError("bounding_box must have x2 > x1 and y2 > y1") + if not math.isfinite(rotation): + raise ValueError(f"rotation must be finite, got {rotation}") self._img_to_add = img_to_add self._font_name = font_name self._font_size = self._font_size_max diff --git a/tests/unit/converter/test_add_image_text_converter.py b/tests/unit/converter/test_add_image_text_converter.py index 504a980f35..c97227c1e4 100644 --- a/tests/unit/converter/test_add_image_text_converter.py +++ b/tests/unit/converter/test_add_image_text_converter.py @@ -256,3 +256,10 @@ def test_add_image_text_converter_auto_font_size_no_bounding_box(large_sample_im ) updated_image = converter._add_text_to_image("Auto-sized text on full image") assert updated_image is not None + + +@pytest.mark.parametrize("rotation", [float("nan"), float("inf"), float("-inf")]) +def test_add_image_text_converter_rejects_non_finite_rotation(image_text_converter_sample_image, rotation): + """Non-finite rotation angles should fail during converter construction.""" + with pytest.raises(ValueError, match="rotation must be finite"): + AddImageTextConverter(img_to_add=image_text_converter_sample_image, rotation=rotation) From 59a4de98b1960877ea527b3487210199681a948d Mon Sep 17 00:00:00 2001 From: Roman Lutz Date: Thu, 10 Sep 2026 15:06:14 -0700 Subject: [PATCH 2/2] MAINT: annotate AddImageText rotation test Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> --- tests/unit/converter/test_add_image_text_converter.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/unit/converter/test_add_image_text_converter.py b/tests/unit/converter/test_add_image_text_converter.py index c97227c1e4..e9f7baf5bf 100644 --- a/tests/unit/converter/test_add_image_text_converter.py +++ b/tests/unit/converter/test_add_image_text_converter.py @@ -259,7 +259,9 @@ def test_add_image_text_converter_auto_font_size_no_bounding_box(large_sample_im @pytest.mark.parametrize("rotation", [float("nan"), float("inf"), float("-inf")]) -def test_add_image_text_converter_rejects_non_finite_rotation(image_text_converter_sample_image, rotation): +def test_add_image_text_converter_rejects_non_finite_rotation( + image_text_converter_sample_image: str, rotation: float +) -> None: """Non-finite rotation angles should fail during converter construction.""" with pytest.raises(ValueError, match="rotation must be finite"): AddImageTextConverter(img_to_add=image_text_converter_sample_image, rotation=rotation)