Skip to content

Commit

Permalink
Added configuration to the allowed file extension. (#61)
Browse files Browse the repository at this point in the history
Co-authored-by: Ian <[email protected]>
Co-authored-by: Luis Oliveira <[email protected]>
  • Loading branch information
3 people authored Jan 30, 2024
1 parent f72ae43 commit 6a80d58
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,10 @@ public static enum ContentFamily {

public static final String GP_MAX_STORAGE_FILE_SIZE = MODULE_ARTIFACT_ID + ".maxStorageFileSize";

public static final String GP_ALLOWED_FILE_EXTENSIONS = MODULE_ARTIFACT_ID + ".allowedFileExtensions";

public static final String GP_DENIED_FILE_NAMES = MODULE_ARTIFACT_ID + ".deniedFileNames";

public static final String GP_WEBCAM_ALLOWED = MODULE_ARTIFACT_ID + ".allowWebcam";

public static final String GP_ENCOUNTER_SAVING_FLOW = MODULE_ARTIFACT_ID + ".encounterSavingFlow";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,14 @@ public Encounter getAttachmentEncounter(Patient patient, Visit visit, Provider p
return encounter;
}

/*
* @return An array of comma-separated values for the named global property
*/
protected String[] getCommaSeparatedGlobalPropertyValues(String globalPropertyName) {
String globalProperty = administrationService.getGlobalProperty(globalPropertyName);
return StringUtils.isEmpty(globalProperty) ? new String[0] : globalProperty.split(",");
}

/*
* See super#getIntegerByGlobalProperty(String globalPropertyName)
*/
Expand Down Expand Up @@ -330,6 +338,20 @@ public Double getMaxUploadFileSize() {
return getDoubleByGlobalProperty(AttachmentsConstants.GP_MAX_UPLOAD_FILE_SIZE);
}

/**
* @return The allowed file extensions.
*/
public String[] getAllowedFileExtensions() {
return getCommaSeparatedGlobalPropertyValues(AttachmentsConstants.GP_ALLOWED_FILE_EXTENSIONS);
}

/**
* @return The denied file names.
*/
public String[] getDeniedFileNames() {
return getCommaSeparatedGlobalPropertyValues(AttachmentsConstants.GP_DENIED_FILE_NAMES);
}

/**
* @return The max file size allowed to be stored (in Megabytes).
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.BooleanUtils;
import org.apache.commons.lang.StringUtils;
import org.openmrs.Encounter;
Expand Down Expand Up @@ -114,6 +116,21 @@ public Object upload(MultipartFile file, RequestContext context) throws Response
throw new IllegalRequestException("The file exceeds the maximum size");
}

// Verify file extension
String fileName = file.getOriginalFilename();
int idx = fileName.lastIndexOf(".");
String fileExtension = idx > 0 && idx < fileName.length() - 1 ? fileName.substring(idx + 1) : "";
if (!ArrayUtils.isEmpty(ctx.getAllowedFileExtensions()) && !Arrays.stream(ctx.getAllowedFileExtensions())
.filter(e -> e.equalsIgnoreCase(fileExtension)).findAny().isPresent()) {
throw new IllegalRequestException("The extension is not valid");
}

// Verify file name
if (!ArrayUtils.isEmpty(ctx.getDeniedFileNames())
&& Arrays.stream(ctx.getDeniedFileNames()).filter(e -> e.equalsIgnoreCase(fileName)).findAny().isPresent()) {
throw new IllegalRequestException("The file name is not valid");
}

// Verify Parameters
if (patient == null) {
throw new IllegalRequestException("A patient parameter must be provided when uploading an attachment.");
Expand Down
16 changes: 16 additions & 0 deletions omod/src/main/resources/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,22 @@
</description>
</globalProperty>

<globalProperty>
<property>${project.parent.artifactId}.allowedFileExtensions</property>
<defaultValue/>
<description>
Comma-separated list of case-insensitive file extensions that are allowed to be uploaded.
</description>
</globalProperty>

<globalProperty>
<property>${project.parent.artifactId}.deniedFileNames</property>
<defaultValue>eicar.txt</defaultValue>
<description>
Comma-separated list of case-insensitive file names that will be rejected if the attached file has this name.
</description>
</globalProperty>

<globalProperty>
<property>${project.parent.artifactId}.encounterSavingFlow</property>
<defaultValue></defaultValue>
Expand Down

0 comments on commit 6a80d58

Please sign in to comment.