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

Added configuration to the allowed file extension. #61

Merged
merged 18 commits into from
Jan 30, 2024
Merged
Show file tree
Hide file tree
Changes from 17 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 @@ -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,20 @@ 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 (!Arrays.stream(ctx.getAllowedFileExtensions()).filter(e -> e.equalsIgnoreCase(fileExtension)).findAny()
.isPresent()) {
throw new IllegalRequestException("The extension is not valid");
}

// Verify file name
if (Arrays.stream(ctx.getAllowedFileExtensions()).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
Loading