Skip to content

Enforce acceptable media types on update and draft patch#864

Open
Schmarvinius wants to merge 2 commits into
mainfrom
fix/validate-media-on-update-and-draft-patch
Open

Enforce acceptable media types on update and draft patch#864
Schmarvinius wants to merge 2 commits into
mainfrom
fix/validate-media-on-update-and-draft-patch

Conversation

@Schmarvinius

Copy link
Copy Markdown
Contributor

Issue

@Core.AcceptableMediaTypes validation was only applied on the create / draft-new path. Two write paths skipped it entirely:

  • Non-draft update (UpdateAttachmentsHandler) — a user able to update an attachment could replace its content, or even do a metadata-only change (e.g. set mimeType to text/html), bypassing the allowlist.
  • Draft patch (DraftPatchAttachmentsHandler) — draft content/metadata was stored without validation before activation.

Both paths only enforced the size limit, making the media type policy asymmetric with create.

Fix

Both handlers now call AttachmentValidationHelper.validateMediaAttachments before dispatching the modification:

  • UpdateAttachmentsHandler validates in a dedicated @Before(BEFORE) method so the check also runs for metadata-only changes (not just when content is present).
  • DraftPatchAttachmentsHandler validates at the start of the draft-patch handling.

CdsRuntime is injected into both handlers to resolve the CDS model, mirroring the existing create handler.

Tests

  • MediaValidatedAttachmentsNonDraftTest: updating an attachment to a disallowed media type → 415.
  • MediaValidatedAttachmentsDraftTest: patching a draft attachment to a disallowed media type → 415.

@Schmarvinius
Schmarvinius requested a review from a team as a code owner July 21, 2026 10:21
@hyperspace-pr-bot

Copy link
Copy Markdown
Contributor

Summary

The following content is AI-generated and provides a summary of the pull request:


Enforce @Core.AcceptableMediaTypes Validation on Update and Draft Patch Paths

Bug Fix

🐛 Previously, @Core.AcceptableMediaTypes validation was only enforced during create / draft-new operations. Both the non-draft update (UpdateAttachmentsHandler) and draft patch (DraftPatchAttachmentsHandler) paths skipped media type validation entirely, only enforcing file size limits. This asymmetry allowed users to bypass the media type allowlist by updating or patching attachments with disallowed content types or metadata.

Both handlers now call AttachmentValidationHelper.validateMediaAttachments before dispatching modifications, ensuring consistent enforcement across all write paths.

Changes

  • Registration.java: Injects CdsRuntime into UpdateAttachmentsHandler and DraftPatchAttachmentsHandler constructors to enable CDS model resolution for media type validation.

  • UpdateAttachmentsHandler.java: Added CdsRuntime field and a new @Before handler method (processBeforeForMetadata) that calls AttachmentValidationHelper.validateMediaAttachments, ensuring media type validation runs for all updates including metadata-only changes (e.g., changing mimeType or fileName).

  • DraftPatchAttachmentsHandler.java: Added CdsRuntime field and a call to AttachmentValidationHelper.validateMediaAttachments at the start of processBeforeDraftPatch, blocking disallowed media types before draft content or metadata is stored.

  • AttachmentValidationHelper.java: Widened the data parameter type from List<CdsData> to List<? extends CdsData> to accommodate calls from both handlers.

  • UpdateAttachmentsHandlerTest.java: Updated test setup to pass runtime to the UpdateAttachmentsHandler constructor.

  • DraftPatchAttachmentsHandlerTest.java: Updated test setup to pass runtime to the DraftPatchAttachmentsHandler constructor.

  • MediaValidatedAttachmentsNonDraftTest.java: Added integration test verifying that patching a non-draft attachment to a disallowed media type returns HTTP 415.

  • MediaValidatedAttachmentsDraftTest.java: Added integration test verifying that patching a draft attachment to a disallowed media type returns HTTP 415.


  • 🔄 Regenerate and Update Summary
  • ✏️ Insert as PR Description (deletes this comment)
  • 🗑️ Delete comment
PR Bot Information

Version: 1.28.2

  • LLM: anthropic--claude-4.6-sonnet
  • Output Template: Default Template
  • Correlation ID: 01d00950-84ee-11f1-8656-3beb8ee12b6a
  • File Content Strategy: Full file content
  • Summary Prompt: Default Prompt
  • Event Trigger: pull_request.opened

@hyperspace-pr-bot hyperspace-pr-bot Bot 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.

The PR correctly closes the media-type validation gap on the update and draft-patch paths, and the implementation follows the existing handler structure well. The main issues found are: (1) both new CdsRuntime constructor parameters are missing the project-mandated requireNonNull guard; (2) the validation logic has a blind spot for requests that update only the mimeType field without providing a fileName, since the entire check is filename-driven; and (3) a minor test maintainability concern around the hand-crafted OData key URL in the non-draft integration test.

PR Bot Information

Version: 1.28.2

  • LLM: anthropic--claude-4.6-sonnet
  • Correlation ID: 01d00950-84ee-11f1-8656-3beb8ee12b6a
  • File Content Strategy: Full file content
  • Agent Instructions:
  • Event Trigger: pull_request.opened

this.persistence = persistence;
this.eventFactory = eventFactory;
this.defaultMaxSize = defaultMaxSize;
this.cdsRuntime = cdsRuntime;

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.

Best Practices: Missing requireNonNull guard for the new cdsRuntime constructor parameter.

Same as UpdateAttachmentsHandler: the project mandates requireNonNull for every constructor parameter, but the new cdsRuntime field is stored without validation. A null value would cause an opaque NullPointerException at call time instead of a clear failure at wiring time.

Suggested change
this.cdsRuntime = cdsRuntime;
this.cdsRuntime = Objects.requireNonNull(cdsRuntime, "cdsRuntime must not be null");

Double-check suggestion before committing. Edit this comment for amendments.


Please provide feedback on the review comment by checking the appropriate box:

  • 🌟 Awesome comment, a human might have missed that.
  • ✅ Helpful comment
  • 🤷 Neutral
  • ❌ This comment is not helpful

Acceptable-media-types validation was only applied on create/draft-new.
Non-draft updates (UpdateAttachmentsHandler) and draft patches
(DraftPatchAttachmentsHandler) stored replacement content and metadata
without validation, so a user able to update an attachment could replace
it (including a metadata-only mimeType/fileName change) with a disallowed
media type that bypassed @Core.AcceptableMediaTypes.

Both handlers now validate via AttachmentValidationHelper before
dispatching the modification, matching the create path. On update the
check runs in a dedicated @before(BEFORE) method so it also covers
metadata-only changes. CdsRuntime is injected into both handlers to
resolve the CDS model.
Adds integration tests asserting that replacing an attachment with a
disallowed media type is rejected (415) on the non-draft update path and
on the draft patch path.
@Schmarvinius
Schmarvinius force-pushed the fix/validate-media-on-update-and-draft-patch branch from e03017f to 1e4cd58 Compare July 21, 2026 11:42
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.

1 participant