Skip to content

Commit

Permalink
feat: add max upload file size validation (#18113)
Browse files Browse the repository at this point in the history
* feat: add max upload file size validation

(cherry picked from commit eae9847)
  • Loading branch information
netroms committed Oct 26, 2024
1 parent ed0217f commit 9eae535
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -659,7 +659,9 @@ public enum ConfigurationKey {
LINKED_ACCOUNTS_RELOGIN_URL("linked_accounts.relogin_url", "", false),
SWITCH_USER_FEATURE_ENABLED("switch_user_feature.enabled", Constants.OFF, false),
SWITCH_USER_ALLOW_LISTED_IPS(
"switch_user_allow_listed_ips", "localhost,127.0.0.1,[0:0:0:0:0:0:0:1]", false);
"switch_user_allow_listed_ips", "localhost,127.0.0.1,[0:0:0:0:0:0:0:1]", false),

MAX_FILE_UPLOAD_SIZE_BYTES("max.file_upload_size", Integer.toString(10_000_000), false);

private final String key;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,18 @@

class FileResourceControllerTest extends DhisControllerConvenienceTest {

@Test
void testSaveTooBigFileSize() {
byte[] bytes = new byte[10_000_001];
MockMultipartFile image =
new MockMultipartFile("file", "OU_profile_image.png", "image/png", bytes);
HttpResponse response = POST_MULTIPART("/fileResources?domain=USER_AVATAR", image);
JsonString errorMessage = response.content(HttpStatus.CONFLICT).getString("message");
assertEquals(
"File size can't be bigger than 10000000, current file size 10000001",
errorMessage.string());
}

@Test
void testSaveBadAvatarImageData() {
MockMultipartFile image =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,11 @@ public WebMessage saveFileResource(
@RequestParam(defaultValue = "DATA_VALUE") FileResourceDomain domain,
@RequestParam(required = false) String uid)
throws IOException, ConflictException {
FileResource fileResource;

FileResourceUtils.validateFileSize(
file, Long.parseLong(dhisConfig.getProperty(ConfigurationKey.MAX_FILE_UPLOAD_SIZE_BYTES)));

FileResource fileResource;
if (domain.equals(FileResourceDomain.ICON)) {
validateCustomIconFile(file);
fileResource = fileResourceUtils.saveFileResource(uid, resizeIconToDefaultSize(file), domain);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ private static void validateFileExtension(String fileName, List<String> validExt
}
}

private static void validateFileSize(@Nonnull MultipartFile file, long maxFileSizeInBytes) {
public static void validateFileSize(@Nonnull MultipartFile file, long maxFileSizeInBytes) {
if (file.getSize() > maxFileSizeInBytes) {
throw new IllegalQueryException(
String.format(
Expand Down

0 comments on commit 9eae535

Please sign in to comment.