Skip to content

Commit 39033f9

Browse files
committed
EMPT-41: Prevent upload of executable and script files
1 parent bd8f77b commit 39033f9

File tree

4 files changed

+41
-3
lines changed

4 files changed

+41
-3
lines changed

api/src/main/java/org/openmrs/module/attachments/AttachmentsConstants.java

+10
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,13 @@
99
*/
1010
package org.openmrs.module.attachments;
1111

12+
import java.util.List;
13+
import java.util.Arrays;
14+
1215
public class AttachmentsConstants {
1316

1417
public static enum ContentFamily {
18+
EXECUTABLE,
1519
IMAGE,
1620
PDF,
1721
OTHER
@@ -69,6 +73,12 @@ public static enum ContentFamily {
6973

7074
public static final String UNKNOWN_MIME_TYPE = "application/octet-stream";
7175

76+
public static final List<String> EXECUTABLE_MIME_TYPES = Arrays.asList("application/vnd.microsoft.portable-executable",
77+
"type/javascript", "application/javascrip", "application/x-sh", "application/java-archive",
78+
"application/x-httpd-php", "application/xhtml+xml", "application/x-vbs", "text/vbscript", "text/x-python",
79+
"application/x-ms-installer", "application/x-elf", "application/x-applescript", "application/x-ruby",
80+
"application/x-perl", "application/wasm");
81+
7282
public static final String ATT_VIEW_ORIGINAL = "complexdata.view.original";
7383

7484
public static final String ATT_VIEW_THUMBNAIL = "complexdata.view.thumbnail";

api/src/main/java/org/openmrs/module/attachments/AttachmentsContext.java

+4
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,10 @@ public static double getCompressionRatio(double fileByteSize, double maxByteSize
345345
*/
346346
public static ContentFamily getContentFamily(String mimeType) {
347347
ContentFamily contentFamily = ContentFamily.OTHER;
348+
if (AttachmentsConstants.EXECUTABLE_MIME_TYPES.contains(mimeType)) {
349+
contentFamily = ContentFamily.EXECUTABLE;
350+
}
351+
348352
if (StringUtils.equals(mimeType, "application/pdf")) {
349353
contentFamily = ContentFamily.PDF;
350354
}

omod-1.10/src/main/java/org/openmrs/module/attachments/rest/AttachmentResource1_10.java

+6-3
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ public class AttachmentResource1_10 extends DataDelegatingCrudResource<Attachmen
5757
private ComplexObsSaver obsSaver = Context.getRegisteredComponent(AttachmentsConstants.COMPONENT_COMPLEXOBS_SAVER,
5858
ComplexObsSaver.class);
5959

60-
private AttachmentsContext ctx = Context
61-
.getRegisteredComponent(AttachmentsConstants.COMPONENT_ATT_CONTEXT, AttachmentsContext.class);
60+
private AttachmentsContext ctx = Context.getRegisteredComponent(AttachmentsConstants.COMPONENT_ATT_CONTEXT,
61+
AttachmentsContext.class);
6262

6363
@Override
6464
public Attachment newDelegate() {
@@ -112,7 +112,7 @@ public Object upload(MultipartFile file, RequestContext context) throws Response
112112
file = new Base64MultipartFile(base64Content);
113113
}
114114
// Verify File Size
115-
if (ctx.getMaxUploadFileSize() * 1024 * 1024 < (double)file.getSize()) {
115+
if (ctx.getMaxUploadFileSize() * 1024 * 1024 < (double) file.getSize()) {
116116
throw new IllegalRequestException("The file exceeds the maximum size");
117117
}
118118

@@ -147,6 +147,9 @@ public Object upload(MultipartFile file, RequestContext context) throws Response
147147
obs = obsSaver.saveImageAttachment(visit, patient, encounter, fileCaption, file, instructions);
148148
break;
149149

150+
case EXECUTABLE:
151+
throw new IllegalRequestException("File type is not allowed as attachment.");
152+
150153
case OTHER:
151154
default:
152155
obs = obsSaver.saveOtherAttachment(visit, patient, encounter, fileCaption, file, instructions);

omod-1.10/src/test/java/org/openmrs/module/attachments/rest/AttachmentRestController1_10Test.java

+21
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,27 @@ public void postAttachment_shouldNotUploadFileAboveSizeLimit() throws Exception
439439
SimpleObject response = deserialize(handle(request));
440440
}
441441

442+
@Test(expected = IllegalRequestException.class)
443+
public void postAttachment_shouldNotUplodExecutbleFile() throws Exception {
444+
// Setup
445+
String dotExeMimeType = "application/vnd.microsoft.portable-executable";
446+
String fileCaption = "Test file caption";
447+
String fileName = "testFile1.dat";
448+
Patient patient = Context.getPatientService().getPatient(2);
449+
Visit visit = Context.getVisitService().getVisit(1);
450+
451+
MockMultipartHttpServletRequest request = newUploadRequest(getURI());
452+
MockMultipartFile dotExeFile = new MockMultipartFile("file", fileName, dotExeMimeType, randomData);
453+
454+
request.addFile(dotExeFile);
455+
request.addParameter("patient", patient.getUuid());
456+
request.addParameter("visit", visit.getUuid());
457+
request.addParameter("fileCaption", fileCaption);
458+
459+
// Replay
460+
SimpleObject response = deserialize(handle(request));
461+
}
462+
442463
@Test
443464
public void getAttachmentBytes_shouldDownloadFile() throws Exception {
444465

0 commit comments

Comments
 (0)