Skip to content

Commit

Permalink
WW-5528 Ensure multipart upload illegal characters reported as error
Browse files Browse the repository at this point in the history
  • Loading branch information
kusalk committed Feb 6, 2025
1 parent 7a77c7a commit e16d24b
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import com.opensymphony.xwork2.inject.Inject;
import com.opensymphony.xwork2.security.DefaultExcludedPatternsChecker;
import com.opensymphony.xwork2.security.ExcludedPatternsChecker;
import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.io.FilenameUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.struts2.StrutsConstants;
Expand All @@ -33,12 +33,17 @@
import java.util.List;
import java.util.Locale;

import static org.apache.commons.lang3.StringUtils.normalizeSpace;

/**
* Abstract class with some helper methods, it should be used
* when starting development of another implementation of {@link MultiPartRequest}
*/
public abstract class AbstractMultiPartRequest implements MultiPartRequest {

protected static final String STRUTS_MESSAGES_UPLOAD_ERROR_ILLEGAL_CHARACTERS_FIELD = "struts.messages.upload.error.illegal.characters.field";
protected static final String STRUTS_MESSAGES_UPLOAD_ERROR_ILLEGAL_CHARACTERS_NAME = "struts.messages.upload.error.illegal.characters.name";

private static final Logger LOG = LogManager.getLogger(AbstractMultiPartRequest.class);

private static final String EXCLUDED_FILE_PATTERN = "^(.*[<>&\"'|;\\\\/?*:]+.*|.*\\.\\..*)$";
Expand Down Expand Up @@ -88,13 +93,14 @@ public abstract class AbstractMultiPartRequest implements MultiPartRequest {

private final ExcludedPatternsChecker patternsChecker;

protected AbstractMultiPartRequest(String dmiValue) {
patternsChecker = new DefaultExcludedPatternsChecker();
if (BooleanUtils.toBoolean(dmiValue)) {
((DefaultExcludedPatternsChecker) patternsChecker).setAdditionalExcludePatterns(EXCLUDED_FILE_PATTERN_WITH_DMI_SUPPORT);
} else {
((DefaultExcludedPatternsChecker) patternsChecker).setAdditionalExcludePatterns(EXCLUDED_FILE_PATTERN);
}
protected AbstractMultiPartRequest() {
this(false);
}

protected AbstractMultiPartRequest(boolean dmiValue) {
DefaultExcludedPatternsChecker patternsChecker = new DefaultExcludedPatternsChecker();
patternsChecker.setAdditionalExcludePatterns(dmiValue ? EXCLUDED_FILE_PATTERN_WITH_DMI_SUPPORT : EXCLUDED_FILE_PATTERN);
this.patternsChecker = patternsChecker;
}

/**
Expand Down Expand Up @@ -174,16 +180,7 @@ public List<LocalizedMessage> getErrors() {
* @return the canonical name based on the supplied filename
*/
protected String getCanonicalName(final String originalFileName) {
String fileName = originalFileName;

int forwardSlash = fileName.lastIndexOf('/');
int backwardSlash = fileName.lastIndexOf('\\');
if (forwardSlash != -1 && forwardSlash > backwardSlash) {
fileName = fileName.substring(forwardSlash + 1);
} else {
fileName = fileName.substring(backwardSlash + 1);
}
return fileName;
return FilenameUtils.getName(originalFileName);
}

/**
Expand All @@ -194,4 +191,32 @@ protected boolean isExcluded(String fileName) {
return patternsChecker.isExcluded(fileName).isExcluded();
}

protected boolean isInvalidInput(String fieldName, String fileName) {
// Skip file uploads that don't have a file name - meaning that no file was selected.
if (fileName == null || fileName.trim().isEmpty()) {
LOG.debug(() -> "No file has been uploaded for the field: " + normalizeSpace(fieldName));
return true;
}

if (isExcluded(fileName)) {
String normalizedFileName = normalizeSpace(fileName);
LOG.debug("File name [{}] is not accepted", normalizedFileName);
errors.add(new LocalizedMessage(getClass(), STRUTS_MESSAGES_UPLOAD_ERROR_ILLEGAL_CHARACTERS_NAME, null,
new String[]{normalizedFileName}));
return true;
}

return isInvalidInput(fieldName);
}

protected boolean isInvalidInput(String fieldName) {
if (isExcluded(fieldName)) {
String normalizedFieldName = normalizeSpace(fieldName);
LOG.debug("Form field [{}}] is rejected!", normalizedFieldName);
errors.add(new LocalizedMessage(getClass(), STRUTS_MESSAGES_UPLOAD_ERROR_ILLEGAL_CHARACTERS_FIELD, null,
new String[]{normalizedFieldName}));
return true;
}
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.apache.commons.fileupload.disk.DiskFileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
Expand Down Expand Up @@ -60,13 +61,14 @@ public class JakartaMultiPartRequest extends AbstractMultiPartRequest {

// maps parameter name -> List of param values
protected Map<String, List<String>> params = new HashMap<>();

public JakartaMultiPartRequest() {
super(Boolean.FALSE.toString());
super();
}

@Inject(value = StrutsConstants.STRUTS_ENABLE_DYNAMIC_METHOD_INVOCATION, required = false)
public JakartaMultiPartRequest(String dmiValue) {
super(dmiValue);
super(BooleanUtils.toBoolean(dmiValue));
}

/**
Expand Down Expand Up @@ -123,21 +125,7 @@ protected void processUpload(HttpServletRequest request, String saveDir) throws
}

protected void processFileField(FileItem item) {
LOG.debug("Item is a file upload");

if (isExcluded(item.getName())) {
LOG.warn("File name [{}] is not accepted", normalizeSpace(item.getName()));
return;
}

if (isExcluded(item.getFieldName())) {
LOG.warn("Field name [{}] is not accepted", normalizeSpace(item.getFieldName()));
return;
}

// Skip file uploads that don't have a file name - meaning that no file was selected.
if (item.getName() == null || item.getName().trim().isEmpty()) {
LOG.debug("No file has been uploaded for the field: {}", normalizeSpace(item.getFieldName()));
if (isInvalidInput(item.getFieldName(), item.getName())) {
return;
}

Expand All @@ -154,10 +142,10 @@ protected void processFileField(FileItem item) {

protected void processNormalFormField(FileItem item, String charset) throws UnsupportedEncodingException {
try {
LOG.debug("Item is a normal form field");
String fieldName = item.getFieldName();
LOG.debug("Item: {} is a normal form field", fieldName);

if (isExcluded(item.getFieldName())) {
LOG.warn("Form field name [{}] is not accepted", normalizeSpace(item.getFieldName()));
if (isInvalidInput(fieldName)) {
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.fileupload.util.Streams;
import org.apache.commons.lang3.BooleanUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.struts2.StrutsConstants;
Expand Down Expand Up @@ -209,12 +210,12 @@ public void parse(HttpServletRequest request, String saveDir) throws IOException
}

public JakartaStreamMultiPartRequest() {
super(Boolean.FALSE.toString());
super();
}

@Inject(value = StrutsConstants.STRUTS_ENABLE_DYNAMIC_METHOD_INVOCATION, required = false)
public JakartaStreamMultiPartRequest(String dmiValue) {
super(dmiValue);
super(BooleanUtils.toBoolean(dmiValue));
}

/**
Expand Down Expand Up @@ -325,8 +326,7 @@ protected void addFileSkippedError(String fileName, HttpServletRequest request)
*/
protected void processFileItemStreamAsFormField(FileItemStream itemStream) {
String fieldName = itemStream.getFieldName();
if (isExcluded(fieldName)) {
LOG.warn("Form field [{}] rejected!", normalizeSpace(fieldName));
if (isInvalidInput(fieldName)) {
return;
}
try {
Expand All @@ -351,14 +351,7 @@ protected void processFileItemStreamAsFormField(FileItemStream itemStream) {
* @param location location
*/
protected void processFileItemStreamAsFileField(FileItemStream itemStream, String location) {
// Skip file uploads that don't have a file name - meaning that no file was selected.
if (itemStream.getName() == null || itemStream.getName().trim().isEmpty()) {
LOG.debug("No file has been uploaded for the field: {}", normalizeSpace(itemStream.getFieldName()));
return;
}

if (isExcluded(itemStream.getName())) {
LOG.warn("File field [{}] rejected", normalizeSpace(itemStream.getName()));
if (isInvalidInput(itemStream.getFieldName(), itemStream.getName())) {
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ struts.messages.removing.file=Removing file {0} {1}
struts.messages.error.uploading=Error uploading: {0}
struts.messages.error.file.too.large=File {0} is too large to be uploaded. Maximum allowed size is {4} bytes!
struts.messages.upload.error.parameter.too.long=The request parameter "{0}" was too long. Max length allowed is {1}, but found {2}!
struts.messages.upload.error.illegal.characters.field=The multipart upload field name "{0}" contains illegal characters!
struts.messages.upload.error.illegal.characters.name=The multipart upload filename "{0}" contains illegal characters!
struts.messages.error.content.type.not.allowed=Content-Type not allowed: {0} "{1}" "{2}" {3}
struts.messages.error.file.extension.not.allowed=File extension not allowed: {0} "{1}" "{2}" {3}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,6 @@
import com.opensymphony.xwork2.ValidationAwareSupport;
import com.opensymphony.xwork2.mock.MockActionInvocation;
import com.opensymphony.xwork2.mock.MockActionProxy;
import com.opensymphony.xwork2.security.DefaultAcceptedPatternsChecker;
import com.opensymphony.xwork2.security.DefaultExcludedPatternsChecker;
import com.opensymphony.xwork2.security.DefaultNotExcludedAcceptedPatternsChecker;
import com.opensymphony.xwork2.util.ClassLoaderUtil;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.struts2.ServletActionContext;
Expand Down Expand Up @@ -693,7 +690,8 @@ public void testUnacceptedFieldName() throws Exception {

interceptor.intercept(mai);

assertFalse(action.hasActionErrors());
assertThat(action.getActionErrors())
.containsExactly("The multipart upload field name \"top.file\" contains illegal characters!");
assertNull(action.getUploadFiles());
}

Expand Down Expand Up @@ -724,7 +722,8 @@ public void testUnacceptedFileName() throws Exception {

interceptor.intercept(mai);

assertFalse(action.hasActionErrors());
assertThat(action.getActionErrors())
.containsExactly("The multipart upload filename \"../deleteme.txt\" contains illegal characters!");
assertNull(action.getUploadFiles());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import com.opensymphony.xwork2.DefaultLocaleProvider;
import com.opensymphony.xwork2.ValidationAwareSupport;
import com.opensymphony.xwork2.mock.MockActionInvocation;
import com.opensymphony.xwork2.security.DefaultNotExcludedAcceptedPatternsChecker;
import com.opensymphony.xwork2.util.ClassLoaderUtil;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.struts2.ServletActionContext;
Expand Down Expand Up @@ -755,7 +754,8 @@ public void testUnacceptedFieldName() throws Exception {

interceptor.intercept(mai);

assertFalse(action.hasActionErrors());
assertThat(action.getActionErrors())
.containsExactly("The multipart upload field name \"top.file\" contains illegal characters!");
assertNull(action.getUploadFiles());
}

Expand Down Expand Up @@ -786,7 +786,8 @@ public void testUnacceptedFileName() throws Exception {

interceptor.intercept(mai);

assertFalse(action.hasActionErrors());
assertThat(action.getActionErrors())
.containsExactly("The multipart upload filename \"../deleteme.txt\" contains illegal characters!");
assertNull(action.getUploadFiles());
}

Expand Down

0 comments on commit e16d24b

Please sign in to comment.