Fixed: Secure the uploads (OFBIZ-12080)

2020/08/10 the OFBiz security team received a security report by Harshit Shukla
<harshit.shukz@gmail.com>, roughly it was (quoting part of it to simplify):

<<I have identified a Remote Code Execution (RCE) Vulnerability. The reason
behind this RCE is lack of file extension check at
catalog/control/UploadCategoryImage?productCategoryId=CATALOG1_BEST_SELL&pload_file_type=category>>

Using this post-auth RCE in OFBiz demos, Harshit was able to get some AWS
credentials by uploading a webshell (based on [0]).
By security, it was then decided by the Infra and OFBiz security teams to shut
down the demos.

After discussing the elements reported with Mark J Cox (VP of ASF security team)
we in common decided that no CVE was necessary.

# Conflicts handled by hand in R18:
#	applications/content/src/main/java/org/apache/ofbiz/content/data/DataResourceWorker.java
#	applications/content/src/main/java/org/apache/ofbiz/content/data/DataServices.java
#	applications/product/groovyScripts/catalog/category/EditCategory.groovy
#	applications/product/groovyScripts/catalog/config/EditProductConfigItemContent.groovy
#	applications/product/groovyScripts/catalog/imagemanagement/ImageUpload.groovy
#	applications/product/groovyScripts/catalog/imagemanagement/SetDefaultImage.groovy
#	applications/product/groovyScripts/catalog/product/EditProductContent.groovy
#	applications/product/src/main/java/org/apache/ofbiz/product/image/ScaleImage.java
#	applications/product/src/main/java/org/apache/ofbiz/product/imagemanagement/FrameImage.java
#	applications/product/src/main/java/org/apache/ofbiz/product/imagemanagement/ImageManagementServices.java
#	applications/product/src/main/java/org/apache/ofbiz/product/product/ProductServices.java
#	build.gradle
#	framework/base/src/main/java/org/apache/ofbiz/base/util/FileUtil.java
#	framework/base/src/main/java/org/apache/ofbiz/base/util/HttpRequestFileUpload.java
#	framework/base/src/main/java/org/apache/ofbiz/base/util/template/FreeMarkerWorker.java
#	framework/security/config/security.properties

# Conflicts handled by hand in R17:
#	applications/content/src/main/java/org/apache/ofbiz/content/data/DataResourceWorker.java
#	applications/content/src/main/java/org/apache/ofbiz/content/data/DataServices.java
#	applications/datamodel/data/seed/ContentSeedData.xml
#	build.gradle
#	framework/base/src/main/java/org/apache/ofbiz/base/util/HttpRequestFileUpload.java
This commit is contained in:
Jacques Le Roux
2020-12-01 10:14:48 +01:00
parent f6176dba99
commit f741bb2277
21 changed files with 833 additions and 71 deletions
@@ -41,6 +41,9 @@ import java.util.Set;
import org.apache.commons.io.FileUtils;
import org.apache.ofbiz.base.location.ComponentLocationResolver;
import net.lingala.zip4j.ZipFile;
import net.lingala.zip4j.exception.ZipException;
/**
* File Utilities
*
@@ -414,4 +417,26 @@ public final class FileUtil {
return new File(filePath).toPath().normalize().toFile();
}
/**
* Unzip file structure of the given zipFile to specified outputFolder The Zip slip vulnerabilty is handled since version 1.3.3 of
* net.lingala.zip4j.ZipFile; unzipFileToFolder is not as reliable and does not handle passwords
* @param source
* @param destination
* @param password optional
* @return true if OK
*/
public static boolean unZip(String source, String destination, String password) {
try {
if (password.isEmpty()) {
new ZipFile(source).extractAll(destination);
} else {
new ZipFile(source, password.toCharArray()).extractAll(destination);
}
} catch (ZipException e) {
Debug.logError("Error extracting [" + source + "] file to dir destination: " + destination, e.toString(), module);
return false;
}
return true;
}
}
@@ -30,6 +30,8 @@ import java.util.Map;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.imaging.ImageReadException;
import org.apache.ofbiz.entity.Delegator;
/**
* HttpRequestFileUpload - Receive a file upload through an HttpServletRequest
@@ -112,19 +114,19 @@ public class HttpRequestFileUpload {
}
}
public void doUpload(HttpServletRequest request) throws IOException {
public boolean doUpload(HttpServletRequest request, String fileType) throws IOException {
Delegator delegator = (Delegator) request.getAttribute("delegator");
ServletInputStream in = request.getInputStream();
String reqLengthString = request.getHeader("content-length");
Debug.logInfo("expect " + reqLengthString + " bytes.", module);
int requestLength = 0;
try {
requestLength = Integer.parseInt(reqLengthString);
} catch (Exception e2) {
e2.printStackTrace();
return;
} catch (NumberFormatException e) {
Debug.logError(e, e.getMessage(), module);
return false;
}
byte[] line = new byte[BUFFER_SIZE];
@@ -133,7 +135,8 @@ public class HttpRequestFileUpload {
i = waitingReadLine(in, line, 0, BUFFER_SIZE, requestLength);
requestLength -= i;
if (i < 3) {
return;
Debug.logError("Possibly a waitingReadLine error", module);
return false;
}
int boundaryLength = i - 2;
@@ -152,7 +155,7 @@ public class HttpRequestFileUpload {
if (newLine.indexOf("filename=\"") != -1) {
setFilename(new String(line, 0, i - 2, UtilIO.getUtf8()));
if (filename == null) {
return;
return false;
}
// this is the file content
i = waitingReadLine(in, line, 0, BUFFER_SIZE, requestLength);
@@ -190,8 +193,8 @@ public class HttpRequestFileUpload {
}
}
try (
FileOutputStream fos = new FileOutputStream(savePath + filenameToUse);) {
String fileTocheck = savePath + filenameToUse;
try (FileOutputStream fos = new FileOutputStream(fileTocheck);) {
boolean bail = (new String(line, 0, i, UtilIO.getUtf8()).startsWith(boundary));
boolean oneByteLine = (i == 1); // handle one-byte lines
@@ -231,11 +234,13 @@ public class HttpRequestFileUpload {
}
}
fos.flush();
fos.close();
} catch (RuntimeException e) {
throw e;
} catch (Exception e) {
if (!org.apache.ofbiz.security.SecuredUpload.isValidFile(fileTocheck, fileType, delegator)) {
return false;
}
} catch (ImageReadException e) {
Debug.logError(e, module);
return false;
}
} else {
// this is a field
@@ -259,7 +264,7 @@ public class HttpRequestFileUpload {
i = waitingReadLine(in, line, 0, BUFFER_SIZE, requestLength);
requestLength -= i;
if ((i == boundaryLength + 2 || i == boundaryLength + 4) // + 4 is eof
&& (new String(line, 0, i).startsWith(boundary))) {
&& (new String(line, 0, i).startsWith(boundary))) {
fieldValue.append(newLine.substring(0, newLine.length() - 2));
} else {
fieldValue.append(newLine);
@@ -275,6 +280,7 @@ public class HttpRequestFileUpload {
}
} // end while
return true;
}
// reads a line, waiting if there is nothing available and reqLen > 0
@@ -295,7 +301,7 @@ public class HttpRequestFileUpload {
while (endMS > (new Date().getTime())) {
try {
wait(WAIT_INTERVAL);
} catch (Exception e3) {
} catch (InterruptedException e) {
Debug.logInfo(".", module);
}
}
@@ -303,4 +309,5 @@ public class HttpRequestFileUpload {
}
return i;
}
}
@@ -116,8 +116,7 @@ public final class FreeMarkerWorker {
} catch (TemplateException e) {
Debug.logError("Unable to set date/time and number formats in FreeMarker: " + e, module);
}
String templateClassResolver = UtilProperties.getPropertyValue("security", "templateClassResolver",
"SAFER_RESOLVER");
String templateClassResolver = UtilProperties.getPropertyValue("security", "templateClassResolver", "SAFER_RESOLVER");
switch (templateClassResolver) {
case "UNRESTRICTED_RESOLVER":
newConfig.setNewBuiltinClassResolver(TemplateClassResolver.UNRESTRICTED_RESOLVER);