Skip to content
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

feat: add more validation to file upload #19104

Merged
merged 2 commits into from
Nov 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,17 @@ void testSaveBadAvatarImageData() {
assertEquals("Failed to resize image: src cannot be null", errorMessage.string());
}

@Test
void testSaveBadOrgUnitImageData() {
MockMultipartFile image =
new MockMultipartFile(
"file", "OU_profile_image.png", "image/png", "<<png data>>".getBytes());
HttpResponse response = POST_MULTIPART("/fileResources?domain=ORG_UNIT", image);
JsonString errorMessage =
response.content(HttpStatus.INTERNAL_SERVER_ERROR).getString("message");
assertEquals("Failed to resize image: src cannot be null", errorMessage.string());
}

@Test
void testSaveBadAvatarContentType() {
MockMultipartFile image =
Expand Down Expand Up @@ -122,6 +133,13 @@ void testSaveOrgUnitImage() throws IOException {
JsonObject savedObject =
response.content(HttpStatus.ACCEPTED).getObject("response").getObject("fileResource");
assertEquals("OU_profile_image.png", savedObject.getString("name").string());

String uid = savedObject.getString("id").string();
JsonObject fr = GET("/fileResources/{uid}", uid).content();
assertEquals("ORG_UNIT", fr.getString("domain").string());

JsonObject large = GET("/fileResources/{uid}?dimension=LARGE", uid).content();
assertEquals("ORG_UNIT", large.getString("domain").string());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,12 @@ public WebMessage saveFileResource(
fileResource =
fileResourceUtils.saveFileResource(uid, resizeAvatarToDefaultSize(file), domain);

} else if (domain.equals(FileResourceDomain.ORG_UNIT)) {
fileResourceUtils.validateOrgUnitImage(file);
fileResource =
fileResourceUtils.saveFileResource(
uid, fileResourceUtils.resizeOrgToDefaultSize(file), domain);

} else {
fileResource = fileResourceUtils.saveFileResource(uid, file, domain);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,11 @@ public class FileResourceUtils {
private static final int AVATAR_TARGET_HEIGHT = 200;
private static final int AVATAR_TARGET_WIDTH = 200;

private static final int ORGUNIT_TARGET_HEIGHT = 800;
private static final int ORGUNIT_TARGET_WIDTH = 800;

private static final long MAX_AVATAR_IMAGE_SIZE_IN_BYTES = 2_000_000;
private static final long MAX_ORGUNIT_IMAGE_SIZE_IN_BYTES = 8_000_000;

private static final List<String> ALLOWED_IMAGE_FILE_EXTENSIONS =
List.of("jpg", "jpeg", "png", "gif");
Expand Down Expand Up @@ -240,6 +244,12 @@ public void validateUserAvatar(MultipartFile file) {
validateFileSize(file, MAX_AVATAR_IMAGE_SIZE_IN_BYTES);
}

public void validateOrgUnitImage(MultipartFile file) {
validateContentType(file.getContentType(), ALLOWED_IMAGE_MIME_TYPES);
validateFileExtension(file.getOriginalFilename(), ALLOWED_IMAGE_FILE_EXTENSIONS);
validateFileSize(file, MAX_ORGUNIT_IMAGE_SIZE_IN_BYTES);
}

private void validateContentType(String contentType, List<String> validExtensions) {
if (contentType == null) {
throw new IllegalQueryException("Invalid content type, content type is NULL");
Expand Down Expand Up @@ -312,4 +322,9 @@ public static MultipartFile resizeAvatarToDefaultSize(MultipartFile multipartFil
return resizeImageToCustomSize(
multipartFile, AVATAR_TARGET_WIDTH, AVATAR_TARGET_HEIGHT, Mode.AUTOMATIC);
}

public MultipartFile resizeOrgToDefaultSize(MultipartFile multipartFile) throws IOException {
return resizeImageToCustomSize(
multipartFile, ORGUNIT_TARGET_WIDTH, ORGUNIT_TARGET_HEIGHT, Mode.AUTOMATIC);
}
}
Loading