Add support to restrict @Core.AcceptableMediaTypes#732
Add support to restrict @Core.AcceptableMediaTypes#732samyuktaprabhu wants to merge 11 commits intomainfrom
Conversation
There was a problem hiding this comment.
The PR adds media type restriction support for attachments. I've identified a few issues: a typo in MIME type definitions (application/txt should be text/plain), potential security concerns with filename validation (missing path traversal checks), and a minor note about null handling. Overall the implementation is reasonable but needs these corrections before merging.
PR Bot Information
Version: 1.17.53 | 📖 Documentation | 🚨 Create Incident | 💬 Feedback
- Event Trigger:
pull_request.opened - LLM:
anthropic--claude-4.5-sonnet - Correlation ID:
78c854a0-0757-11f1-833c-299ee35f3dc8
...ap/cds/feature/attachments/handler/applicationservice/helper/AttachmentValidationHelper.java
Outdated
Show resolved
Hide resolved
...s/src/main/java/com/sap/cds/feature/attachments/handler/common/ApplicationHandlerHelper.java
Show resolved
Hide resolved
...ap/cds/feature/attachments/handler/applicationservice/helper/AttachmentValidationHelper.java
Outdated
Show resolved
Hide resolved
8b6c095 to
a12bc05
Compare
There was a problem hiding this comment.
Summary
The pull request introduces MIME type validation for attachments with good test coverage. However, several null safety issues were identified in the validation logic that could lead to NullPointerException errors. Additionally, a minor CDS syntax inconsistency and a misleading test variable name should be addressed.
PR Bot Information
Version: 1.17.61 | 📖 Documentation | 🚨 Create Incident | 💬 Feedback
- Event Trigger:
issue_comment.created - Correlation ID:
b7b79080-0bdb-11f1-9b97-78cdc3653ab1 - LLM:
anthropic--claude-4.5-sonnet
...ap/cds/feature/attachments/handler/applicationservice/helper/AttachmentValidationHelper.java
Show resolved
Hide resolved
...ap/cds/feature/attachments/handler/applicationservice/helper/AttachmentValidationHelper.java
Show resolved
Hide resolved
...s/src/main/java/com/sap/cds/feature/attachments/handler/common/ApplicationHandlerHelper.java
Show resolved
Hide resolved
...ture/attachments/integrationtests/nondraftservice/MediaValidatedAttachmentsNonDraftTest.java
Outdated
Show resolved
Hide resolved
e2b000e to
26b499c
Compare
Add Support to Restrict Media Types
New Features
✨ Introduced support for restricting allowed MIME types for attachments using the
@Core.AcceptableMediaTypesannotation. This feature enables validation of file types during upload, ensuring only specified media types are accepted.Changes
README.md:
@Core.AcceptableMediaTypesannotation with examples showing exact matches and wildcard patternsCreateAttachmentsHandler.java:
@Beforehandler methodprocessBeforeForMetadatathat validates acceptable media types early in the request lifecycle atHandlerOrder.BEFOREApplicationHandlerHelper.validateAcceptableMediaTypes()CdsRuntimedependency to support metadata validationAttachmentValidationHelper.java (new file):
validateMediaTypeForAttachment()to validate file names and resolve MIME typesimage/*) and default fallback behavior usingURLConnection.guessContentTypeFromName()ApplicationHandlerHelper.java:
FILE_NAME_FILTERconstant for extracting file names during validationvalidateAcceptableMediaTypes()method coordinating the validation flowgetEntityAcceptableMediaTypes()to read@Core.AcceptableMediaTypesannotation from entityextractFileName()to safely extract file names from attachment data*/*(allow all) when no annotation is specifiedRegistration.java:
CdsRuntimeinstance toCreateAttachmentsHandlerTest files:
CreateAttachmentsHandlerTest.java: Added tests for new metadata validation handlerAttachmentValidationHelperTest.java(new): Comprehensive test suite with 16 test cases covering MIME type detection, validation, wildcards, error scenarios, and edge casesApplicationHandlerHelperTest.java: Added tests forextractFileName(),getEntityAcceptableMediaTypes(), andvalidateAcceptableMediaTypes()methodsMediaValidatedAttachmentsDraftTest.java(new): Integration tests for draft service media validationMediaValidatedAttachmentsNonDraftTest.java(new): Integration tests for non-draft service media validationSizeLimitedAttachmentsSizeValidationDraftTest.java: Updated with file name assignmentsSizeLimitedAttachmentValidationNonDraftTest.java: Updated with file name assignmentsIntegration test configuration:
data-model.cds: AddedmediaValidatedAttachmentscomposition to Roots entitytest-service.cds: Configured annotation to only accept JPEG and PNG imagesRootEntityBuilder.java: Enhanced builder to support media-validated attachmentsSample configuration (
samples/bookshop/srv/attachments.cds):mediaValidatedAttachmentscomposition to Books entity@Core.AcceptableMediaTypes: ['image/jpeg', 'image/png']📬 Subscribe to the Hyperspace PR Bot DL to get the latest announcements and pilot features!
PR Bot Information
Version:
1.17.61| 📖 Documentation | 🚨 Create Incident | 💬 Feedbackpull_request.edited1cdf0b90-0be2-11f1-9988-99fe6992d9d8anthropic--claude-4.5-sonnet