-
Notifications
You must be signed in to change notification settings - Fork 6
Add support to restrict @Core.AcceptableMediaTypes #716
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Summary
I've reviewed the pull request implementing @Core.AcceptableMediaTypes restriction support for attachments. The implementation includes validation logic, annotation processing, and comprehensive test coverage.
Key Issues Identified:
- Logic Error: The filename validation incorrectly rejects common patterns like
archive.tar.gzormy.document.v2.pdfby requiring exactly one dot - Bug: Case-insensitive comparison missing for acceptable media types—uppercase types in configuration won't match
- Performance: ObjectMapper instantiated repeatedly instead of being reused
- Typo: Development comment left in production code
The architectural approach is sound, with proper separation between validation logic, annotation processing, and handler integration. Test coverage appears thorough across both draft and non-draft scenarios.
PR Bot Information
Version: 1.17.29 | 📖 Documentation | 🚨 Create Incident | 💬 Feedback
- Correlation ID:
72739fa0-f6e1-11f0-8f4f-1fcf26d4251d - Event Trigger:
pull_request.opened - LLM:
anthropic--claude-4.5-sonnet
...ap/cds/feature/attachments/handler/applicationservice/helper/AttachmentValidationHelper.java
Outdated
Show resolved
Hide resolved
...ap/cds/feature/attachments/handler/applicationservice/helper/AttachmentValidationHelper.java
Outdated
Show resolved
Hide resolved
...ap/cds/feature/attachments/handler/applicationservice/helper/AttachmentValidationHelper.java
Outdated
Show resolved
Hide resolved
...ds/feature/attachments/handler/applicationservice/helper/ModifyApplicationHandlerHelper.java
Outdated
Show resolved
Hide resolved
...ds/feature/attachments/handler/applicationservice/helper/ModifyApplicationHandlerHelper.java
Outdated
Show resolved
Hide resolved
1bb0ad2 to
73a260b
Compare
4b745b2 to
a040926
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Summary
The pull request adds MIME type validation for attachments using @Core.AcceptableMediaTypes annotation. I identified 4 substantive issues:
- Typo: Incorrect MIME type
application/txtshould betext/plainfor .txt/.lst files - Logic Error: Wildcard matching uses
startsWith()which allows unintended matches like "application-custom/xml" matching "application/*" - Typo: Missing space in CDS import statement (
fromcom.sap...should befromcom.sap...) - Bug: Potential ArrayIndexOutOfBoundsException if MIME type doesn't contain "/" when splitting for wildcard comparison
The most critical is the wildcard matching logic error, which could allow unauthorized file types to bypass validation. The incorrect text/plain MIME type could also cause legitimate uploads to fail validation.
PR Bot Information
Version: 1.17.30 | 📖 Documentation | 🚨 Create Incident | 💬 Feedback
- Event Trigger:
issue_comment.created - LLM:
anthropic--claude-4.5-sonnet - Correlation ID:
69c8f670-f7b3-11f0-9744-1ffc39f47963
...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
...ap/cds/feature/attachments/handler/applicationservice/helper/AttachmentValidationHelper.java
Show resolved
Hide resolved
...ds/feature/attachments/handler/applicationservice/helper/AttachmentValidationHelperTest.java
Show resolved
Hide resolved
Update cds-feature-attachments/src/main/java/com/sap/cds/feature/attachments/handler/applicationservice/helper/ModifyApplicationHandlerHelper.java Co-authored-by: hyperspace-insights[bot] <209611008+hyperspace-insights[bot]@users.noreply.github.com> Update cds-feature-attachments/src/main/java/com/sap/cds/feature/attachments/handler/applicationservice/helper/AttachmentValidationHelper.java Co-authored-by: hyperspace-insights[bot] <209611008+hyperspace-insights[bot]@users.noreply.github.com> fix review comments by hyperspace update readme, revert the types update to var remove redundant empty lines
c84337d to
3caedb0
Compare
Add Support to Restrict @Core.AcceptableMediaTypes
New Features
✨ Introduced validation for acceptable media types in attachment uploads using the
@Core.AcceptableMediaTypesannotation, enabling applications to restrict file types for enhanced security and consistency.Changes
AttachmentValidationHelper.java: New validation helper class that determines file MIME types and validates them against allowed media types. Includes fallback to default MIME typeapplication/octet-streamwhen type cannot be detected. Maps common file extensions to their respective MIME types and supports wildcard patterns for flexible type matching.ModifyApplicationHandlerHelper.java: Enhanced attachment processing to validate media types before upload. Added methods to extract acceptable media types from entity annotations (getEntityAcceptableMediaTypes) and retrieve filenames from attachments or path values (getFileName). Integrated validation into the attachment handling workflow to ensure MIME type compliance.AttachmentValidationHelperTest.java: Comprehensive test suite for media type validation including edge cases for unknown extensions, uppercase extensions, and wildcard type support. Tests cover scenarios with various MIME types, default fallback behavior, and error handling for invalid or restricted file types.ModifyApplicationHandlerHelperTest.java: Tests for filename retrieval and acceptable media types extraction from entity annotations. Validates behavior when filenames are missing or retrieved from different sources, and ensures proper handling of annotation-based media type restrictions.Test Files: Updated test infrastructure across multiple files to support new validation scenarios:
CreateAttachmentsHandlerTest.java&UpdateAttachmentsHandlerTest.java: Added filename validation to ensure all attachment entities specify a filenameDraftPatchAttachmentsHandlerTest.java: Updated to include filename in test attachmentsAcceptedMediaTypesAttachmentsValidationDraftTest.java&AcceptedMediaTypesAttachmentsValidationNonDraftTest.java: New integration tests verifying media type validation in both draft and non-draft scenariosCDS Models:
data-model.cds: AddedmediaValidatedAttachmentscomposition to theRootsentity for testing media type validationtest-service.cds&attachments.cds: Configured@Core.AcceptableMediaTypesannotation to restrict uploads toimage/jpegandimage/pngonlyDocumentation:
README.md: Added new section "Restrict allowed MIME types" with examples showing how to use@Core.AcceptableMediaTypesannotation, including wildcard patterns and default behaviorHelper Classes:
RootEntityBuilder.java: Added helper method to build entities with media-validated attachmentsOdataRequestValidationBase.java&DraftOdataRequestValidationBase.java: Code formatting improvements