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

WW-5473 Fixes examining multiple HttpServletWrappers to find MultiPartRequestWrapper #1078

Merged
merged 1 commit into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -22,6 +22,7 @@
import com.opensymphony.xwork2.ActionProxy;
import com.opensymphony.xwork2.interceptor.ValidationAware;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletRequestWrapper;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.struts2.action.UploadedFilesAware;
Expand Down Expand Up @@ -135,7 +136,11 @@ public class ActionFileUploadInterceptor extends AbstractFileUploadInterceptor {
@Override
public String intercept(ActionInvocation invocation) throws Exception {
HttpServletRequest request = invocation.getInvocationContext().getServletRequest();
if (!(request instanceof MultiPartRequestWrapper multiWrapper)) {
MultiPartRequestWrapper multiWrapper = request instanceof HttpServletRequestWrapper wrapper
? findMultipartRequestWrapper(wrapper)
: null;

if (multiWrapper == null) {
if (LOG.isDebugEnabled()) {
ActionProxy proxy = invocation.getProxy();
LOG.debug(getTextMessage(STRUTS_MESSAGES_BYPASS_REQUEST_KEY, new String[]{proxy.getNamespace(), proxy.getActionName()}));
Expand All @@ -145,8 +150,8 @@ public String intercept(ActionInvocation invocation) throws Exception {

if (!(invocation.getAction() instanceof UploadedFilesAware action)) {
LOG.debug("Action: {} doesn't implement: {}, ignoring file upload",
invocation.getProxy().getActionName(),
UploadedFilesAware.class.getSimpleName());
invocation.getProxy().getActionName(),
UploadedFilesAware.class.getSimpleName());
return invocation.invoke();
}

Expand Down Expand Up @@ -185,5 +190,26 @@ public String intercept(ActionInvocation invocation) throws Exception {
return invocation.invoke();
}

/**
* Tries to find {@link MultiPartRequestWrapper} as the request can be already wrapped
* with another {@link HttpServletRequestWrapper}.
* If the {@link MultiPartRequestWrapper} cannot be found, null is returned instead.
*
* @param request current {@link HttpServletRequestWrapper}
* @return {@link MultiPartRequestWrapper} or null
* @since 7.0.0
*/
protected MultiPartRequestWrapper findMultipartRequestWrapper(HttpServletRequestWrapper request) {
if (request instanceof MultiPartRequestWrapper multiPartRequestWrapper) {
LOG.debug("Found multipart request: {}", multiPartRequestWrapper.getClass().getSimpleName());
return multiPartRequestWrapper;
} else if (request.getRequest() instanceof HttpServletRequestWrapper wrappedRequest) {
LOG.debug("Could not find multipart request wrapper, checking ancestor: {}",
wrappedRequest.getClass().getSimpleName());
return findMultipartRequestWrapper(wrappedRequest);
}
return null;
}

}

Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.opensymphony.xwork2.mock.MockActionInvocation;
import com.opensymphony.xwork2.mock.MockActionProxy;
import com.opensymphony.xwork2.util.ClassLoaderUtil;
import jakarta.servlet.http.HttpServletRequestWrapper;
import org.apache.commons.fileupload2.jakarta.servlet6.JakartaServletDiskFileUpload;
import org.apache.commons.fileupload2.jakarta.servlet6.JakartaServletFileUpload;
import org.apache.struts2.StrutsInternalTestCase;
Expand Down Expand Up @@ -514,6 +515,67 @@ public void testMultipartRequestLocalizedError() throws Exception {
assertTrue(msg.startsWith("Der Request übertraf die maximal erlaubte Größe"));
}

public void testSingleHttpRequestWrapper() throws Exception {
request.setCharacterEncoding(StandardCharsets.UTF_8.name());
request.setMethod("post");
request.addHeader("Content-type", "multipart/form-data; boundary=---1234");

// inspired by the unit tests for jakarta commons fileupload
String content = ("""
-----1234\r
Content-Disposition: form-data; name="file"; filename="deleteme.txt"\r
Content-Type: text/html\r
\r
Unit test of ActionFileUploadInterceptor\r
-----1234--\r
""");
request.setContent(content.getBytes(StandardCharsets.UTF_8));
MyFileUploadAction action = container.inject(MyFileUploadAction.class);

MockActionInvocation mai = new MockActionInvocation();
mai.setAction(action);
mai.setResultCode("success");
mai.setInvocationContext(ActionContext.getContext());
ActionContext.getContext()
.withServletRequest(new HttpServletRequestWrapper(createMultipartRequestMaxSize(1000)));

String result = interceptor.intercept(mai);

assertFalse(action.hasActionErrors());
assertEquals("success", result);
}

public void testMultipleHttpRequestWrappers() throws Exception {
request.setCharacterEncoding(StandardCharsets.UTF_8.name());
request.setMethod("post");
request.addHeader("Content-type", "multipart/form-data; boundary=---1234");

// inspired by the unit tests for jakarta commons fileupload
String content = ("""
-----1234\r
Content-Disposition: form-data; name="file"; filename="deleteme.txt"\r
Content-Type: text/html\r
\r
Unit test of ActionFileUploadInterceptor\r
-----1234--\r
""");
request.setContent(content.getBytes(StandardCharsets.US_ASCII));

MyFileUploadAction action = container.inject(MyFileUploadAction.class);

MockActionInvocation mai = new MockActionInvocation();
mai.setAction(action);
mai.setResultCode("success");
mai.setInvocationContext(ActionContext.getContext());
ActionContext.getContext()
.withServletRequest(new HttpServletRequestWrapper(new HttpServletRequestWrapper(createMultipartRequestMaxSize(1000))));

String result = interceptor.intercept(mai);

assertFalse(action.hasActionErrors());
assertEquals("success", result);
}

private String encodeTextFile(String filename, String contentType, String content) {
return endline +
"--" + boundary +
Expand Down
1 change: 0 additions & 1 deletion core/src/test/resources/log4j2.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,5 @@
<Root level="info">
<AppenderRef ref="STDOUT"/>
</Root>
<Logger name="org.apache.struts2.dispatcher.multipart" level="debug"/>
</Loggers>
</Configuration>
Loading