-
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
Open
samyuktaprabhu
wants to merge
2
commits into
main
Choose a base branch
from
sam-409-restrict-media-types
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
152 changes: 152 additions & 0 deletions
152
...cds/feature/attachments/handler/applicationservice/helper/AttachmentValidationHelper.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,152 @@ | ||
| /* | ||
| * © 2026 SAP SE or an SAP affiliate company and cds-feature-attachments contributors. | ||
| */ | ||
| package com.sap.cds.feature.attachments.handler.applicationservice.helper; | ||
|
|
||
| import java.net.URLConnection; | ||
| import java.util.Collection; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
|
|
||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| import com.sap.cds.services.ServiceException; | ||
| import com.sap.cds.services.ErrorStatuses; | ||
|
|
||
| public class AttachmentValidationHelper { | ||
|
|
||
| public static final String DEFAULT_MEDIA_TYPE = "application/octet-stream"; | ||
| public static final Map<String, String> EXT_TO_MEDIA_TYPE = Map.ofEntries( | ||
| Map.entry("aac", "audio/aac"), | ||
| Map.entry("abw", "application/x-abiword"), | ||
| Map.entry("arc", "application/octet-stream"), | ||
| Map.entry("avi", "video/x-msvideo"), | ||
| Map.entry("azw", "application/vnd.amazon.ebook"), | ||
| Map.entry("bin", "application/octet-stream"), | ||
| Map.entry("png", "image/png"), | ||
| Map.entry("gif", "image/gif"), | ||
| Map.entry("bmp", "image/bmp"), | ||
| Map.entry("bz", "application/x-bzip"), | ||
| Map.entry("bz2", "application/x-bzip2"), | ||
| Map.entry("csh", "application/x-csh"), | ||
| Map.entry("css", "text/css"), | ||
| Map.entry("csv", "text/csv"), | ||
| Map.entry("doc", "application/msword"), | ||
| Map.entry("docx", "application/vnd.openxmlformats-officedocument.wordprocessingml.document"), | ||
| Map.entry("odp", "application/vnd.oasis.opendocument.presentation"), | ||
| Map.entry("ods", "application/vnd.oasis.opendocument.spreadsheet"), | ||
| Map.entry("odt", "application/vnd.oasis.opendocument.text"), | ||
| Map.entry("epub", "application/epub+zip"), | ||
| Map.entry("gz", "application/gzip"), | ||
| Map.entry("htm", "text/html"), | ||
| Map.entry("html", "text/html"), | ||
| Map.entry("ico", "image/x-icon"), | ||
| Map.entry("ics", "text/calendar"), | ||
| Map.entry("jar", "application/java-archive"), | ||
| Map.entry("jpg", "image/jpeg"), | ||
| Map.entry("jpeg", "image/jpeg"), | ||
| Map.entry("js", "text/javascript"), | ||
| Map.entry("json", "application/json"), | ||
| Map.entry("mid", "audio/midi"), | ||
| Map.entry("midi", "audio/midi"), | ||
| Map.entry("mjs", "text/javascript"), | ||
| Map.entry("mov", "video/quicktime"), | ||
| Map.entry("mp3", "audio/mpeg"), | ||
| Map.entry("mp4", "video/mp4"), | ||
| Map.entry("mpeg", "video/mpeg"), | ||
| Map.entry("mpkg", "application/vnd.apple.installer+xml"), | ||
| Map.entry("otf", "font/otf"), | ||
| Map.entry("pdf", "application/pdf"), | ||
| Map.entry("ppt", "application/vnd.ms-powerpoint"), | ||
| Map.entry("pptx", "application/vnd.openxmlformats-officedocument.presentationml.presentation"), | ||
| Map.entry("rar", "application/x-rar-compressed"), | ||
| Map.entry("rtf", "application/rtf"), | ||
| Map.entry("svg", "image/svg+xml"), | ||
| Map.entry("tar", "application/x-tar"), | ||
| Map.entry("tif", "image/tiff"), | ||
| Map.entry("tiff", "image/tiff"), | ||
| Map.entry("ttf", "font/ttf"), | ||
| Map.entry("vsd", "application/vnd.visio"), | ||
| Map.entry("wav", "audio/wav"), | ||
| Map.entry("woff", "font/woff"), | ||
| Map.entry("woff2", "font/woff2"), | ||
| Map.entry("xhtml", "application/xhtml+xml"), | ||
| Map.entry("xls", "application/vnd.ms-excel"), | ||
| Map.entry("xlsx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"), | ||
| Map.entry("xml", "application/xml"), | ||
| Map.entry("zip", "application/zip"), | ||
| Map.entry("txt", "application/txt"), | ||
| Map.entry("lst", "application/txt"), | ||
| Map.entry("webp", "image/webp")); | ||
|
|
||
| private static final Logger logger = LoggerFactory.getLogger(AttachmentValidationHelper.class); | ||
|
|
||
| /** | ||
| * Validates the file name and resolves its media type. Ensures that the | ||
| * detected media type is part of the list of acceptable media types. | ||
| * | ||
| * @param fileName the name of the attachment file | ||
| * @param acceptableMediaTypes list of allowed media types (e.g. "image/*", | ||
| * "application/pdf") | ||
| * @return the detected media type | ||
| * @throws ServiceException if the file name is invalid or the media type is not | ||
| * allowed | ||
| */ | ||
| public static String validateMediaTypeForAttachment(String fileName, List<String> acceptableMediaTypes) { | ||
| validateFileName(fileName); | ||
| String detectedMediaType = resolveMimeType(fileName); | ||
| validateAcceptableMediaType(acceptableMediaTypes, detectedMediaType); | ||
| return detectedMediaType; | ||
| } | ||
|
|
||
| private static void validateFileName(String fileName) { | ||
| String clean = fileName.trim(); | ||
| int lastDotIndex = clean.lastIndexOf('.'); | ||
| if (lastDotIndex <= 0 || lastDotIndex == clean.length() - 1) { | ||
| throw new ServiceException( | ||
| ErrorStatuses.UNSUPPORTED_MEDIA_TYPE, | ||
| "Invalid filename format: " + fileName); | ||
| } | ||
| } | ||
|
|
||
| private static void validateAcceptableMediaType(List<String> acceptableMediaTypes, String actualMimeType) { | ||
| if (!checkMimeTypeMatch(acceptableMediaTypes, actualMimeType)) { | ||
| throw new ServiceException( | ||
| ErrorStatuses.UNSUPPORTED_MEDIA_TYPE, | ||
| "The attachment file type '{}' is not allowed. Allowed types are: {}", actualMimeType, | ||
| String.join(", ", acceptableMediaTypes)); | ||
| } | ||
| } | ||
|
|
||
| private static String resolveMimeType(String fileName) { | ||
| String actualMimeType = URLConnection.guessContentTypeFromName(fileName); | ||
|
|
||
| if (actualMimeType == null) { | ||
| String fileExtension = fileName.substring(fileName.lastIndexOf('.') + 1).toLowerCase(); | ||
| actualMimeType = EXT_TO_MEDIA_TYPE.get(fileExtension); | ||
|
|
||
| if (actualMimeType == null) { | ||
| logger.warn("Could not determine mime type for file: {}. Setting mime type to default: {}", | ||
| fileName, DEFAULT_MEDIA_TYPE); | ||
| actualMimeType = DEFAULT_MEDIA_TYPE; | ||
| } | ||
| } | ||
| return actualMimeType; | ||
| } | ||
|
|
||
| private static boolean checkMimeTypeMatch(Collection<String> acceptableMediaTypes, String mimeType) { | ||
| if (acceptableMediaTypes == null || acceptableMediaTypes.isEmpty() || acceptableMediaTypes.contains("*/*")) | ||
| return true; | ||
|
|
||
| String baseMimeType = mimeType.trim().toLowerCase(); | ||
|
|
||
| return acceptableMediaTypes.stream().anyMatch(type -> { | ||
| String normalizedType = type.trim().toLowerCase(); | ||
| return normalizedType.endsWith("/*") | ||
| ? baseMimeType.startsWith(normalizedType.substring(0, normalizedType.length() - 2) + "/") | ||
samyuktaprabhu marked this conversation as resolved.
Show resolved
Hide resolved
samyuktaprabhu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| : baseMimeType.equals(normalizedType); | ||
| }); | ||
| } | ||
|
|
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.