CVE-2022-24830 (published 2022-05-14T00:15Z)

OpenClinica is an open source software for Electronic Data Capture (EDC) and Clinical Data Management (CDM). OpenClinica prior to version 3.16 is vulnerable to path traversal in multiple endpoints, leading to arbitrary file read/write, and potential remote code execution. There are no known workarounds. This issue has been patched and users are recommended to upgrade.

Severity: HIGH
CVSS v2 base score: 7.5 (AV:N/AC:L/Au:N/C:P/I:P/A:P)
CVSS v3 base score: 9.8, CRITICAL (CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:U/C:H/I:H/A:H)

Weakness information:
cwe_id: CWE-22
cwe_name: Improper Limitation of a Pathname to a Restricted Directory ('Path Traversal')
description: The product uses external input to construct a pathname that is intended to identify a file or directory that is located underneath a restricted parent directory, but the product does not properly neutralize special elements within the pathname that can cause the pathname to resolve to a location that is outside of the restricted directory.
extended_description: 
            Many file operations are intended to take place within a restricted directory. By using special elements such as ".." and "/" separators, attackers can escape outside of the restricted location to access files or directories that are elsewhere on the system. One of the most common special elements is the "../" sequence, which in most modern operating systems is interpreted as the parent directory of the current location. This is referred to as relative path traversal. Path traversal also covers the use of absolute pathnames such as "/usr/local/bin" to access unexpected files. This is referred to as absolute path traversal.
         
         

Fix information:
repo_url: https://github.com/OpenClinica/OpenClinica
commit_details: repo_url: https://github.com/OpenClinica/OpenClinica
author: Tao Li
author_date: 2022-02-03 18:05:59-05:00
msg: OC-17139: code  changes after code review
repository: repo_url: https://github.com/OpenClinica/OpenClinica
repo_name: OpenClinica/OpenClinica
description: OpenClinica is the world's first commercial open source clinical trial software for Electronic Data Capture (EDC) Clinical Data Management (CDM).
repo_language: Java
owner: OpenClinica
file_changes: filename: DownloadVersionSpreadSheetServlet.java
change_type: ModificationType.MODIFY
diff: @@ -95,14 +95,22 @@ public class DownloadVersionSpreadSheetServlet extends SecureController {
             excelFile = new File(dir,excelFileName);
             // backwards compat
             File oldExcelFile = new File(dir, oldExcelFileName);            
-       
-        	if (oldExcelFile.exists() && oldExcelFile.length() > 0) {
-                 if (!excelFile.exists() || excelFile.length() <= 0) {
-                     // if the old name exists and the new name does not...
-                     excelFile = oldExcelFile;
-                     excelFileName = oldExcelFileName;
-                 }
-             }                  
+            
+            String canonicalPath1= excelFile.getCanonicalPath();
+            String canonicalPath2= oldExcelFile.getCanonicalPath();
+            if (canonicalPath1.startsWith(dir) && canonicalPath2.startsWith(dir)) {
+            	if (oldExcelFile.exists() && oldExcelFile.length() > 0) {
+                    if (!excelFile.exists() || excelFile.length() <= 0) {
+                        // if the old name exists and the new name does not...
+                        excelFile = oldExcelFile;
+                        excelFileName = oldExcelFileName;
+                    }
+                }     
+            }else {
+            	 addPageMessage(respage.getString("the_excel_is_not_available_on_server_contact"));
+                 forwardPage(Page.CRF_LIST_SERVLET);
+            }
+        	             
 
         }
         logger.info("looking for : " + excelFile.getName());

programming_language: Java
filename: DownloadAttachedFileServlet.java
change_type: ModificationType.MODIFY
diff: @@ -70,17 +70,23 @@ public class DownloadAttachedFileServlet extends SecureController {
         String filePathName = "";
         String fileName = fp.getString("fileName");
         File f = new File(fileName);
-        
-        if(fileName != null && fileName.indexOf("..") > -1) {
-        	throw new RuntimeException("Traversal attempt - absolute path not allowed " + fileName);
-        }
-        
+              
         if (fileName != null && fileName.length() > 0) {
-            int parentStudyId = currentStudy.getParentStudyId();
+            int parentStudyId = currentStudy.getParentStudyId();           
             String testPath = Utils.getAttachedFileRootPath();
             String tail = File.separator + f.getName();
             String testName = testPath + currentStudy.getOid() + tail;
-            File temp = new File(testName);
+            
+            String filePath = testPath + currentStudy.getOid() +File.separator;            
+            File temp = new File(filePath,f.getName());            
+            String canonicalPath= temp.getCanonicalPath();
+            
+            if (canonicalPath.startsWith(filePath)) {
+            	;
+            }else {
+            	throw new RuntimeException("Traversal attempt - file path not allowed " + fileName);
+            }
+            
             if (temp.exists()) {
                 filePathName = testName;
                 logger.info(currentStudy.getName() + " existing filePathName=" + filePathName);

programming_language: Java
filename: BatchCRFMigrationController.java
change_type: ModificationType.MODIFY
diff: @@ -127,13 +127,20 @@ public class BatchCRFMigrationController implements Runnable {
         InputStream inputStream = null;
         try {
         	//Validate/Sanitize user input filename using a standard library, prevent from path traversal 
-            String logFileName = getFilePath() + File.separator + FilenameUtils.getName(fileName);
-            File fileToDownload = new File(logFileName);
-            inputStream = new FileInputStream(fileToDownload);
-            response.setContentType("application/force-download");
-            response.setHeader("Content-Disposition", "attachment; filename=" + fileName);
-            IOUtils.copy(inputStream, response.getOutputStream());
-            response.flushBuffer();
+            String logFilePath = getFilePath() + File.separator;
+            File fileToDownload = new File(logFilePath, fileName);                    
+            String canonicalPath= fileToDownload.getCanonicalPath();
+            
+            if (canonicalPath.startsWith(logFilePath)) {
+            	inputStream = new FileInputStream(fileToDownload);
+                response.setContentType("application/force-download");
+                response.setHeader("Content-Disposition", "attachment; filename=" + fileName);
+                IOUtils.copy(inputStream, response.getOutputStream());
+                response.flushBuffer();
+            }else {
+            	throw new RuntimeException("Traversal attempt - file path not allowed " + fileName);
+            }
+            
         } catch (Exception e) {
             logger.debug("Request could not be completed at this moment. Please try again.");
             logger.debug(e.getStackTrace().toString());

programming_language: Java
filename: OpenRosaSubmissionController.java
change_type: ModificationType.MODIFY
diff: @@ -27,7 +27,6 @@ import org.akaza.openclinica.web.pform.PFormCache;
 import org.apache.commons.fileupload.FileItem;
 import org.apache.commons.fileupload.disk.DiskFileItemFactory;
 import org.apache.commons.fileupload.servlet.ServletFileUpload;
-import org.apache.commons.io.FilenameUtils;
 import org.apache.commons.io.IOUtils;
 import org.apache.commons.lang.exception.ExceptionUtils;
 import org.slf4j.Logger;
@@ -194,16 +193,25 @@ public class OpenRosaSubmissionController {
         return accessPermission;
     }
 
-    public static String getAttachedFilePath(String inputStudyOid) {
-    	// Using a standard library to validate/Sanitize user inputs which will be used in path expression to prevent from path traversal
-    	String studyOid =  FilenameUtils.getName(inputStudyOid);
+    public static String getAttachedFilePath(String studyOid) throws Exception {    	
         String attachedFilePath = CoreResources.getField("attached_file_location");
         if (attachedFilePath == null || attachedFilePath.length() <= 0) {
-            attachedFilePath = CoreResources.getField("filePath") + "attached_files" + File.separator + studyOid + File.separator;
-        } else {
-            attachedFilePath += studyOid + File.separator;
+            attachedFilePath = CoreResources.getField("filePath") + "attached_files" + File.separator;
+        }
+        File tempFile =  new File(attachedFilePath,studyOid);
+        String canonicalPath= tempFile.getCanonicalPath();
+        
+        if (canonicalPath.startsWith(attachedFilePath)) {
+        	if (attachedFilePath == null || attachedFilePath.length() <= 0) {
+                attachedFilePath = CoreResources.getField("filePath") + "attached_files" + File.separator + studyOid + File.separator;
+            } else {
+                attachedFilePath += studyOid + File.separator;
+            }
+            return attachedFilePath;
+        }else {
+        	throw new RuntimeException("Traversal attempt - file path not allowed " + studyOid);
         }
-        return attachedFilePath;
+        
     }
 
     private File processUploadedFile(FileItem item, String dirToSaveUploadedFileIn) {

programming_language: Java