Skip to content

FIX Clamp ImageResizingConverter output dimensions#2169

Merged
romanlutz merged 3 commits into
microsoft:mainfrom
ThryLox:feat/image-resizing-dimensions-validation
Jul 19, 2026
Merged

FIX Clamp ImageResizingConverter output dimensions#2169
romanlutz merged 3 commits into
microsoft:mainfrom
ThryLox:feat/image-resizing-dimensions-validation

Conversation

@ThryLox

@ThryLox ThryLox commented Jul 12, 2026

Copy link
Copy Markdown
Contributor

Description

ImageResizingConverter can compute a zero width or height when a positive fractional scale factor is applied to a tiny image, causing Pillow to fail. It also accepted non-finite scale factors such as nan and inf.

This change rejects non-finite scale factors and clamps each computed output dimension to at least one pixel. Clamping allows tiny images to resize successfully rather than replacing Pillow's exception with another error, while preserving the existing int() truncation behavior whenever dimensions are already positive.

Fixes: #2162

Tests and Documentation

  • Added validation coverage for nan, positive infinity, and negative infinity.
  • Added resize coverage for 1x1 images and images with a one-pixel width or height.
  • Ran the focused converter unit tests and file-scoped pre-commit checks.
  • Documentation and JupyText updates are not applicable because the public API is unchanged.

…low-1-pixel dimensions in ImageResizingConverter
@ThryLox

ThryLox commented Jul 12, 2026

Copy link
Copy Markdown
Contributor Author

@microsoft-github-policy-service agree

@rlundeen2 rlundeen2 self-assigned this Jul 13, 2026
@rlundeen2

Copy link
Copy Markdown
Contributor

Thx for taking a look! I was on the fence, but I think ultimately I'm going to close it. I know this is a judgement call, but I think this makes the code less readable; and ultimately it just changes the exceptions raised.

@rlundeen2 rlundeen2 closed this Jul 13, 2026
@romanlutz romanlutz reopened this Jul 19, 2026
@romanlutz

romanlutz commented Jul 19, 2026

Copy link
Copy Markdown
Contributor

Thanks again for putting this together. I revisited the approach and would like to preserve this PR and your contribution. The non-finite scale-factor validation here is useful; for the zero-dimension case, the revised behavior should clamp each calculated dimension with max(1, int(...)) so tiny images resize successfully instead of replacing Pillow's exception with a custom one.

@romanlutz romanlutz changed the title fix(converter): validate scale factor finiteness and guard against below-1-pixel dimensions in ImageResizingConverter FIX validate scale factor finiteness and guard against below-1-pixel dimensions in ImageResizingConverter Jul 19, 2026
Keep fractional resizing successful for tiny images while preserving the original non-finite scale-factor validation.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

Copilot-Session: 0b9b23c1-ac03-4d04-962f-d4e01df4f2a7
@romanlutz romanlutz changed the title FIX validate scale factor finiteness and guard against below-1-pixel dimensions in ImageResizingConverter FIX Clamp ImageResizingConverter output dimensions Jul 19, 2026

@romanlutz romanlutz left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated this in a minor way to actually address the issue #2162

@romanlutz romanlutz assigned romanlutz and unassigned rlundeen2 Jul 19, 2026
@romanlutz
romanlutz enabled auto-merge July 19, 2026 13:33
@romanlutz
romanlutz added this pull request to the merge queue Jul 19, 2026
Merged via the queue into microsoft:main with commit 0b122fa Jul 19, 2026
53 checks passed
romanlutz pushed a commit to romanlutz/PyRIT that referenced this pull request Jul 20, 2026
Integrates microsoft#2169 (ImageResizingConverter clamp) and microsoft#2186 (Scenarios/Technique Registry alignment).

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 52d2518d-ebe5-4888-8189-6146b6503448
@akshan-main

Copy link
Copy Markdown

@romanlutz I thought it was good practice to open an issue before creating a PR, otherwise I would've created the PR directly myself. I feel like @ThryLox is taking credit for my finding. I'd have preferred to handle this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

BUG ImageResizingConverter can produce zero-sized output dimensions

5 participants