Add MIME Type Validation via @Core.AcceptableMediaTypes Annotation#748
Add MIME Type Validation via @Core.AcceptableMediaTypes Annotation#748Schmarvinius wants to merge 9 commits intomainfrom
Conversation
…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
There was a problem hiding this comment.
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
Add MIME Type Validation via
@Core.AcceptableMediaTypesAnnotationNew Feature
✨ Introduced support for restricting allowed MIME types on attachment uploads using the
@Core.AcceptableMediaTypesannotation. When this annotation is present on thecontentelement 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, a415 Unsupported Media Typeerror is returned.Changes
README.md: Added documentation section "Restrict allowed MIME types" explaining how to use the@Core.AcceptableMediaTypesannotation, including examples with exact types, wildcards, and the default behavior.Registration.java: UpdatedCreateAttachmentsHandlerinstantiation to passCdsRuntimeas an additional dependency; bumped copyright year to 2026.CreateAttachmentsHandler.java: Added a new@Beforehandler methodprocessBeforeForMetadatathat invokesMediaTypeValidator.validateMediaAttachmentson create, update, and draft events. AcceptsCdsRuntimeas a constructor parameter.MediaTypeValidator.java(new): Core validation utility that resolves MIME types from filenames, reads@Core.AcceptableMediaTypesannotations, and throws aServiceExceptionwith 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 usingCdsDataProcessorto extract and validate file names from attachment fields, enforcing non-null, non-empty, and properly formatted filenames.AssociationCascader.java: AddedhasAttachmentPathandfindMediaEntityNamesmethods to support discovering media entity names from the composition tree.CreateAttachmentsHandlerTest.java: Updated constructor call to includeCdsRuntime; added tests for the newprocessBeforeForMetadatamethod and its annotations.AttachmentDataExtractorTest.java(new): Unit tests forAttachmentDataExtractorcovering valid inputs, blank/null/non-string filenames, missing filenames, multiple files, and skip conditions.MediaTypeValidatorTest.java(new): Comprehensive unit tests forMediaTypeValidator, covering MIME resolution, allowed type matching, wildcard handling, dotfile behavior, and annotation resolution.AssociationCascaderTest.java: Added tests forhasAttachmentPathandfindMediaEntityNamesmethods.data-model.cds(integration tests): AddedmediaValidatedAttachmentsandmimeValidatedAttachmentscomposition fields to theRootsentity.test-service.cds: Added@Core.AcceptableMediaTypesannotations 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 — addedsetFileName("test.txt")to avoid conflicts with new MIME validation.MockHttpRequestHelper.java: AddedexecutePatchWithMatchermethods to support PATCH requests in integration tests.RootEntityBuilder.java: InitializedsizeLimitedAttachmentslist in the constructor to avoid null checks.samples/bookshop/srv/attachments.cds: AddedmediaValidatedAttachmentsfield with@Core.AcceptableMediaTypesannotation forimage/jpegandimage/pngto the bookshop sample.pom.xml: Bumped version to1.3.2-SNAPSHOT.📬 Subscribe to the Hyperspace PR Bot DL to get the latest announcements and pilot features!
PR Bot Information
Version:
1.18| 📖 Documentation | 🚨 Create Incident | 💬 Feedback6d79a1c0-1ca7-11f1-982c-971eb7c87056pull_request.openedanthropic--claude-4.6-sonnet