-
Notifications
You must be signed in to change notification settings - Fork 42
Add uploaded file validation #1626
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
Merged
Merged
Changes from all commits
Commits
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
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
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
71 changes: 71 additions & 0 deletions
71
...g/cloudfoundry/multiapps/controller/core/validators/parameters/FileMimeTypeValidator.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,71 @@ | ||
| package org.cloudfoundry.multiapps.controller.core.validators.parameters; | ||
|
|
||
| import java.io.IOException; | ||
| import java.io.InputStream; | ||
| import java.text.MessageFormat; | ||
|
|
||
| import jakarta.inject.Named; | ||
| import org.apache.commons.io.FilenameUtils; | ||
| import org.apache.tika.Tika; | ||
| import org.cloudfoundry.multiapps.common.SLException; | ||
| import org.cloudfoundry.multiapps.controller.core.Messages; | ||
| import org.springframework.web.multipart.MultipartFile; | ||
| import org.yaml.snakeyaml.LoaderOptions; | ||
| import org.yaml.snakeyaml.Yaml; | ||
| import org.yaml.snakeyaml.constructor.SafeConstructor; | ||
| import org.yaml.snakeyaml.error.YAMLException; | ||
|
|
||
| @Named | ||
| public class FileMimeTypeValidator { | ||
|
|
||
| private static final String APPLICATION_ZIP_MIME_TYPE = "application/zip"; | ||
| private static final String APPLICATION_OCTET_STREAM_MIME_TYPE = "application/octet-stream"; | ||
| private static final String TEXT_PLAIN_MIME_TYPE = "text/plain"; | ||
| private static final String YAML_FILE_EXTENSION = "yaml"; | ||
| private static final String EXTENSION_DESCRIPTOR_FILE_EXTENSION = "mtaext"; | ||
| private static final Tika tika = new Tika(); | ||
|
|
||
| public void validateMultipartFileMimeType(MultipartFile multipartFile) { | ||
| if (multipartFile == null || multipartFile.isEmpty()) { | ||
| throw new IllegalArgumentException(Messages.THE_PROVIDED_MULTIPART_FILE_CANNOT_BE_EMPTY); | ||
| } | ||
|
|
||
| try { | ||
| validateInputStreamMimeType(multipartFile.getInputStream(), multipartFile.getOriginalFilename()); | ||
| } catch (IOException e) { | ||
| throw new SLException(e); | ||
| } | ||
| } | ||
|
|
||
| public void validateInputStreamMimeType(InputStream uploadedFileInputStream, String filename) throws IOException { | ||
| String detectedType = getFileMimeType(uploadedFileInputStream); | ||
| switch (detectedType) { | ||
| case TEXT_PLAIN_MIME_TYPE -> validateYamlFile(uploadedFileInputStream, filename); | ||
| case APPLICATION_ZIP_MIME_TYPE, APPLICATION_OCTET_STREAM_MIME_TYPE -> { | ||
| } | ||
| default -> throw new IllegalArgumentException(MessageFormat.format(Messages.UNSUPPORTED_FILE_FORMAT, detectedType)); | ||
| } | ||
| } | ||
|
|
||
| private String getFileMimeType(InputStream uploadedFileInputStream) throws IOException { | ||
| return tika.detect(uploadedFileInputStream); | ||
IvanBorislavovDimitrov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| private void validateYamlFile(InputStream uploadedFileInputStream, String filename) { | ||
| validateTextFileExtension(filename); | ||
| Yaml yaml = new Yaml(new SafeConstructor(new LoaderOptions())); | ||
| try { | ||
| yaml.load(uploadedFileInputStream); | ||
github-advanced-security[bot] marked this conversation as resolved.
Fixed
Show fixed
Hide fixed
|
||
| } catch (YAMLException e) { | ||
| throw new IllegalArgumentException(MessageFormat.format(Messages.THE_PROVIDED_0_FILE_IS_INVALID, filename), e); | ||
| } | ||
| } | ||
|
|
||
| private void validateTextFileExtension(String filename) { | ||
| String fileExtension = FilenameUtils.getExtension(filename); | ||
|
|
||
| if (!(YAML_FILE_EXTENSION.equals(fileExtension) || EXTENSION_DESCRIPTOR_FILE_EXTENSION.equals(fileExtension))) { | ||
| throw new IllegalArgumentException(MessageFormat.format(Messages.THE_PROVIDED_0_FILE_IS_INVALID, filename)); | ||
| } | ||
| } | ||
| } | ||
67 changes: 67 additions & 0 deletions
67
...oudfoundry/multiapps/controller/core/validators/parameters/FileMimeTypeValidatorTest.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,67 @@ | ||
| package org.cloudfoundry.multiapps.controller.core.validators.parameters; | ||
|
|
||
| import java.io.ByteArrayInputStream; | ||
| import java.io.InputStream; | ||
|
|
||
| import org.cloudfoundry.multiapps.controller.core.Messages; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.mockito.Mockito; | ||
| import org.springframework.web.multipart.MultipartFile; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertThrows; | ||
| import static org.mockito.Mockito.when; | ||
|
|
||
| class FileMimeTypeValidatorTest { | ||
|
|
||
| private MultipartFile multipartFile; | ||
| private InputStream inputStream; | ||
| private final FileMimeTypeValidator fileMimeTypeValidator = new FileMimeTypeValidator(); | ||
|
|
||
| @BeforeEach | ||
| void setUp() { | ||
| multipartFile = Mockito.mock(MultipartFile.class); | ||
| } | ||
|
|
||
| @Test | ||
| void testValidateMultipartFileMimeTypeWithNullFile() { | ||
| assertThrows(IllegalArgumentException.class, () -> { | ||
| fileMimeTypeValidator.validateMultipartFileMimeType(null); | ||
| }, Messages.THE_PROVIDED_MULTIPART_FILE_CANNOT_BE_EMPTY); | ||
| } | ||
|
|
||
| @Test | ||
| void testValidateMultipartFileMimeTypeWithEmptyFile() { | ||
| when(multipartFile.isEmpty()).thenReturn(true); | ||
|
|
||
| assertThrows(IllegalArgumentException.class, () -> { | ||
| fileMimeTypeValidator.validateMultipartFileMimeType(multipartFile); | ||
| }, Messages.THE_PROVIDED_MULTIPART_FILE_CANNOT_BE_EMPTY); | ||
| } | ||
|
|
||
| @Test | ||
| void testValidateMultipartFileMimeType_ValidYamlFile() throws Exception { | ||
| inputStream = new ByteArrayInputStream("test: test".getBytes()); | ||
|
|
||
| when(multipartFile.getInputStream()).thenReturn(inputStream); | ||
| when(multipartFile.getOriginalFilename()).thenReturn("valid.yaml"); | ||
|
|
||
| fileMimeTypeValidator.validateMultipartFileMimeType(multipartFile); | ||
| } | ||
|
|
||
| @Test | ||
| void testValidateMultipartFileOctetStreamMimeType() throws Exception { | ||
| when(multipartFile.getInputStream()).thenReturn(inputStream); | ||
| when(multipartFile.getOriginalFilename()).thenReturn("valid.zip"); | ||
|
|
||
| fileMimeTypeValidator.validateMultipartFileMimeType(multipartFile); | ||
| } | ||
|
|
||
| @Test | ||
| void testValidateMultipartFileOApplicationZipMimeType() throws Exception { | ||
| when(multipartFile.getInputStream()).thenReturn(inputStream); | ||
| when(multipartFile.getOriginalFilename()).thenReturn("valid.zip"); | ||
|
|
||
| fileMimeTypeValidator.validateMultipartFileMimeType(multipartFile); | ||
| } | ||
| } |
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.