Skip to content

Add MIME Type Validation via @Core.AcceptableMediaTypes Annotation#748

Closed
Schmarvinius wants to merge 9 commits intomainfrom
sam-restrict-media-types-409-marvin
Closed

Add MIME Type Validation via @Core.AcceptableMediaTypes Annotation#748
Schmarvinius wants to merge 9 commits intomainfrom
sam-restrict-media-types-409-marvin

Conversation

@Schmarvinius
Copy link
Collaborator

@Schmarvinius Schmarvinius commented Mar 10, 2026

Add MIME Type Validation via @Core.AcceptableMediaTypes Annotation

New Feature

✨ Introduced support for restricting allowed MIME types on attachment uploads using the @Core.AcceptableMediaTypes annotation. When this annotation is present on the content element of an attachment entity, the plugin validates the uploaded file's MIME type (derived from its filename extension) against the allowed types before processing. Wildcard patterns (e.g., image/*) and the global wildcard (*/*) are supported. If a disallowed file type is uploaded, a 415 Unsupported Media Type error is returned.

Changes

  • README.md: Added documentation section "Restrict allowed MIME types" explaining how to use the @Core.AcceptableMediaTypes annotation, including examples with exact types, wildcards, and the default behavior.
  • Registration.java: Updated CreateAttachmentsHandler instantiation to pass CdsRuntime as an additional dependency; bumped copyright year to 2026.
  • CreateAttachmentsHandler.java: Added a new @Before handler method processBeforeForMetadata that invokes MediaTypeValidator.validateMediaAttachments on create, update, and draft events. Accepts CdsRuntime as a constructor parameter.
  • MediaTypeValidator.java (new): Core validation utility that resolves MIME types from filenames, reads @Core.AcceptableMediaTypes annotations, and throws a ServiceException with HTTP 415 if the type is not allowed. Supports exact matching, wildcard subtypes, and full wildcards.
  • AttachmentDataExtractor.java (new): Helper class that traverses entity data using CdsDataProcessor to extract and validate file names from attachment fields, enforcing non-null, non-empty, and properly formatted filenames.
  • AssociationCascader.java: Added hasAttachmentPath and findMediaEntityNames methods to support discovering media entity names from the composition tree.
  • CreateAttachmentsHandlerTest.java: Updated constructor call to include CdsRuntime; added tests for the new processBeforeForMetadata method and its annotations.
  • AttachmentDataExtractorTest.java (new): Unit tests for AttachmentDataExtractor covering valid inputs, blank/null/non-string filenames, missing filenames, multiple files, and skip conditions.
  • MediaTypeValidatorTest.java (new): Comprehensive unit tests for MediaTypeValidator, covering MIME resolution, allowed type matching, wildcard handling, dotfile behavior, and annotation resolution.
  • AssociationCascaderTest.java: Added tests for hasAttachmentPath and findMediaEntityNames methods.
  • data-model.cds (integration tests): Added mediaValidatedAttachments and mimeValidatedAttachments composition fields to the Roots entity.
  • test-service.cds: Added @Core.AcceptableMediaTypes annotations for the new test attachment fields.
  • MediaValidatedAttachmentsNonDraftTest.java (new): Integration tests for MIME type validation in the non-draft service, covering valid/invalid types, case-insensitive extensions, empty filenames, deep creates, and updates.
  • MediaValidatedAttachmentsDraftTest.java (new): Integration tests for MIME type validation in the draft service, including create, patch, and edge cases.
  • SizeLimitedAttachmentValidationNonDraftTest.java / SizeLimitedAttachmentsSizeValidationDraftTest.java: Minor fixes — added setFileName("test.txt") to avoid conflicts with new MIME validation.
  • MockHttpRequestHelper.java: Added executePatchWithMatcher methods to support PATCH requests in integration tests.
  • RootEntityBuilder.java: Initialized sizeLimitedAttachments list in the constructor to avoid null checks.
  • samples/bookshop/srv/attachments.cds: Added mediaValidatedAttachments field with @Core.AcceptableMediaTypes annotation for image/jpeg and image/png to the bookshop sample.
  • pom.xml: Bumped version to 1.3.2-SNAPSHOT.
  • 🔄 Regenerate and Update Summary

📬 Subscribe to the Hyperspace PR Bot DL to get the latest announcements and pilot features!

PR Bot Information

Version: 1.18 | 📖 Documentation | 🚨 Create Incident | 💬 Feedback

  • Correlation ID: 6d79a1c0-1ca7-11f1-982c-971eb7c87056
  • Output Template: Default Template
  • Event Trigger: pull_request.opened
  • LLM: anthropic--claude-4.6-sonnet
  • Summary Prompt: Default Prompt

samyuktaprabhu and others added 8 commits February 25, 2026 09:40
…chments/configuration/Registration.java

Co-authored-by: Marvin <marvin.lindner@sap.com>

Update cds-feature-attachments/src/main/java/com/sap/cds/feature/attachments/configuration/Registration.java

Co-authored-by: Marvin <marvin.lindner@sap.com>

Update cds-feature-attachments/src/main/java/com/sap/cds/feature/attachments/configuration/Registration.java

Co-authored-by: Marvin <marvin.lindner@sap.com>

Update cds-feature-attachments/src/main/java/com/sap/cds/feature/attachments/configuration/Registration.java

Co-authored-by: Marvin <marvin.lindner@sap.com>

apply mvnspotless:apply

Rename AttachmentDataExtractor to validation package

Refactor MediaTypeService to use URLConnection for MIME type resolution and update AttachmentValidationHelper method visibility

Refactor AttachmentValidationHelper to streamline MIME type validation logic

Add unit tests for AssociationCascader and refactor AttachmentValidationHelper and MediaTypeResolver to utilize AssociationCascader for media entity resolution

Refactor MediaTypeResolver to remove dependency on ApplicationHandlerHelper and update related tests

Refactor file name validation logic and migrate to mimeTypeValidation package

Refactor AttachmentDataExtractor to integrate filename validation logic and remove FileNameValidator class

Refactor AttachmentDataExtractor and update tests to improve structure and maintainability

Refactor AttachmentValidationHelper and MediaTypeResolver to improve structure and add unit tests for media type resolution

revert a change
Copy link
Contributor

@hyperspace-insights hyperspace-insights bot left a comment

Choose a reason for hiding this comment

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

The PR introduces MIME type validation for attachments via @Core.AcceptableMediaTypes. The core logic has several correctness issues that need addressing: inconsistent error semantics between validateAndNormalize and resolveMimeType (causing wrong HTTP status codes for extension-less filenames), a pre-normalization wildcard check in isMimeTypeAllowed that can fail with whitespace-padded values, a static mutable field for dependency injection that is not thread-safe, and extension-only MIME detection that should be clearly documented as a security limitation. The integration tests also risk flakiness due to shared database state without per-test cleanup.

PR Bot Information

Version: 1.18 | 📖 Documentation | 🚨 Create Incident | 💬 Feedback

  • Event Trigger: pull_request.opened
  • LLM: anthropic--claude-4.6-sonnet
  • Correlation ID: 6d79a1c0-1ca7-11f1-982c-971eb7c87056

@Schmarvinius Schmarvinius changed the title Sam restrict media types 409 marvin Add MIME Type Validation via @Core.AcceptableMediaTypes Annotation Mar 10, 2026
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.

2 participants