Skip to content

Validate against max resolution before image generation - #4645

Open
kbalka wants to merge 11 commits into
mainfrom
kbalka_fix_CWE_770_validate_requested_res_vs_max_resolution
Open

kbalka wants to merge 11 commits into
mainfrom
kbalka_fix_CWE_770_validate_requested_res_vs_max_resolution

Conversation

@kbalka

@kbalka kbalka commented Oct 9, 2026

Copy link
Copy Markdown
Collaborator

🛠 Summary

CWE-770
Fix for image generation api to validate requested size against configured maxResolution

🧪 Checklist

  • Unit tests added.
  • [not needed] The documentation updated.
  • Change follows security best practices.
    ``

Copilot AI balanced review requested due to automatic review settings October 9, 2026 12:03

Copilot AI 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.

🟡 Changes recommended

Single-dimension requests bypass the security limit, and several tests fail due to zero-initialized limits.

3 open findings
What changed in this PR

Adds configured maximum-resolution validation to image generation and editing requests.

Changes:

  • Extracts shared request validation.
  • Rejects dimensions exceeding maxResolution.
  • Adds maximum-resolution unit tests.
File Description
src/​image_gen/​imagegenutils.cpp Adds shared dimension-limit validation.
src/​test/​text2image_test.cpp Adds resolution boundary tests.

🧠 Review effort: Balanced


💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread src/image_gen/imagegenutils.cpp
Comment thread src/image_gen/imagegenutils.cpp
Comment thread src/test/text2image_test.cpp Outdated

Copilot AI 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.

🟡 Changes recommended

Automatic or omitted dimensions can bypass the configured maximum-resolution cap.

3 open findings
2 resolved since last review

🧠 Review effort: Balanced

Comment thread src/image_gen/imagegenutils.cpp
Comment thread src/image_gen/imagegenutils.cpp Outdated

Copilot AI 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.

🟢 Approval recommended

The validation is consistently applied across request modes and is adequately covered by focused tests.

2 open findings
1 resolved since last review

🧠 Review effort: Balanced

@kbalka
kbalka requested a balanced review from Copilot October 12, 2026 12:04
@kbalka kbalka changed the title Validate agains max resolution before image generation Validate against max resolution before image generation Oct 12, 2026

Copilot AI 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.

🔵 Needs a closer look

Static requests bypass existing limits, and image-edit requests can still evade native-resolution validation.

0 open findings

2 resolved since last review
Previously missed (1)

In code that hasn't changed since last review

Medium severity Static requests bypass general option validation and defaults

src/​image_gen/​imagegenutils.cpp:341

This early return skips ensureAcceptableForDynamic, which now contains checks and defaulting that previously ran for every request mode. Static requests can therefore bypass the num_inference_steps bounds (and the image-count bounds) and no longer receive defaultNumInferenceSteps. Run those general option checks after the static-shape validation instead of returning here.

🧠 Review effort: Balanced

Copilot AI 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.

🔵 Needs a closer look

Image-edit requests can still bypass the maximum-resolution limit when no explicit or configured default size is present.

0 open findings

Previously missed (1)

In code that hasn't changed since last review

Medium severity Image edits bypass max-resolution validation at native size

src/​image_gen/​imagegenutils.cpp:435

The native-resolution fallback is only applied to create requests. getImageEditRequestOptions() still leaves width/height absent when no defaultResolution is configured, so an /images/edits request with an omitted or auto size passes ensureCommonValidations() without any max-resolution check and GenAI can generate at its native resolution even when that exceeds maxResolution. Please capture and validate the effective native resolution for image-to-image/inpainting requests as well, with multipart regression coverage.

🧠 Review effort: Balanced

Copilot AI 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.

🟢 Approval recommended

The resolution limit is consistently enforced before generation with adequate boundary and regression coverage.

0 open findings

🧠 Review effort: Balanced

// the priority chain (request > composite > individual adapter) determines the effective value.
using CompositeLoraMap = std::unordered_map<std::string, std::vector<std::pair<std::string, std::optional<float>>>>;
CompositeLoraMap compositeLoraAdapters;
std::optional<resolution_t> nativeResolution;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Minor:
keep resolution fields together? And add default value for:DEFAULTIMAGE_GEN_ARGS

if (nativeConfig.width <= 0 || nativeConfig.height <= 0) {
throw std::runtime_error("Failed to determine model-native image generation resolution");
}
this->args.nativeResolution = resolution_t{nativeConfig.width, nativeConfig.height};

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

We should add validation (rejection or warning), where nativeResolution > maxResolution, and maxResolution is set.

Comment on lines +1547 to 1561
TEST(Text2ImageTest, validateForStaticReshapeSettings_ExceedsDefaultMaxResolution) {
ImageGenPipelineArgs args = DEFAULTIMAGE_GEN_ARGS;
args.modelsPath = "/ovms/src/test/dummy";
args.staticReshapeSettings = ovms::StaticReshapeSettingsArgs({{5000, 512}});

std::string value = R"({"prompt": "test prompt", "size": "5000x512", "n": 1, "model": "test model"})";
ovms::HttpPayload payload;
payload.parsedJson = std::make_shared<rapidjson::Document>();
payload.parsedJson->Parse(value.c_str());
ASSERT_FALSE(payload.parsedJson->HasParseError());

auto requestOptions = ovms::getImageGenerationRequestOptions(*payload.parsedJson, args);
ASSERT_TRUE(std::holds_alternative<absl::Status>(requestOptions));
EXPECT_THAT(std::get<absl::Status>(requestOptions).message(), ::testing::HasSubstr("exceeds maxResolution"));
}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

We shoudl also test for getImageGenerationRequestOptions with nativeResolution set above max and missing size in request?

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.

3 participants