Repository navigation
Description
MultPartRequests do not process correctly when using Spring Security (without plugin)
Replication instructions
just include the spring-boot-starter on any app that process uploads
implementation "org.springframework.boot:spring-boot-starter-security"and notice that params.file does not bind.
Description
During a normal workflow the handler mapping is retrieved after the multipartRequest is parsed.
DispatcherServlet.doDispatch
DispatcherServlet.doDispatch(..) {
// ...
processedRequest = checkMultipart(request); // if true, wraps request with MultipartHttpServletRequest
multipartRequestParsed = (processedRequest != request);
// Determine handler for the current request.
mappedHandler = getHandler(processedRequest);
// Creates GrailsParameterMap for first time with GrailsParameterMap because originalParams is null
}But with SpringSecurity getHandler is called first in HandlerMappingIntrospector which results in originalParams being not null and MultiFileMap not being set.
HandlerMappingIntrospector.getMatchableHandlerMapping
public MatchableHandlerMapping getMatchableHandlerMapping(HttpServletRequest request) throws Exception {
CachedResult result = CachedResult.getResultFor(request);
if (result != null) {
return result.getHandlerMapping();
}
this.cacheLogHelper.logHandlerMappingCacheMiss(request);
HttpServletRequest requestToUse = new AttributesPreservingRequest(request);
return doWithHandlerMapping(requestToUse, false,
(mapping, executionChain) -> createMatchableHandlerMapping(mapping, requestToUse));
}
private <T> T doWithHandlerMapping(
HttpServletRequest request, boolean ignoreException,
BiFunction<HandlerMapping, HandlerExecutionChain, T> extractor) throws Exception {
Assert.state(this.handlerMappings != null, "HandlerMapping's not initialized");
boolean parsePath = !this.pathPatternMappings.isEmpty();
RequestPath previousPath = null;
if (parsePath) {
previousPath = (RequestPath) request.getAttribute(ServletRequestPathUtils.PATH_ATTRIBUTE);
ServletRequestPathUtils.parseAndCache(request);
}
try {
for (HandlerMapping handlerMapping : this.handlerMappings) {
HandlerExecutionChain chain = null;
try {
chain = handlerMapping.getHandler(request);
// ...This is because standard Spring Security uses the RequestMatcherDelegatingAuthorizationManager which uses the HandlerMappingIntrospector.
org.grails.web.servlet.mvc.GrailsWebRequest.getOriginalParams(GrailsWebRequest.java:255)
org.grails.web.servlet.mvc.GrailsWebRequest.resetParams(GrailsWebRequest.java:264)
org.grails.web.servlet.mvc.GrailsWebRequest.getParams(GrailsWebRequest.java:245)
org.grails.web.mime.HttpServletResponseExtension.getMimeTypeForRequest(HttpServletResponseExtension.groovy:134)
org.grails.web.mime.HttpServletResponseExtension.getMimeType(HttpServletResponseExtension.groovy:127)
org.grails.web.mime.DefaultMimeTypeResolver.resolveResponseMimeType(DefaultMimeTypeResolver.groovy:41)
org.grails.web.mapping.mvc.UrlMappingsHandlerMapping.findRequestedVersion(UrlMappingsHandlerMapping.groovy:184)
org.grails.web.mapping.mvc.UrlMappingsHandlerMapping.getHandlerInternal(UrlMappingsHandlerMapping.groovy:132)
org.springframework.web.servlet.handler.AbstractHandlerMapping.getHandler(AbstractHandlerMapping.java:509)
org.springframework.web.servlet.handler.HandlerMappingIntrospector.doWithHandlerMapping(HandlerMappingIntrospector.java:390)
org.springframework.web.servlet.handler.HandlerMappingIntrospector.setCache(HandlerMappingIntrospector.java:268)
org.springframework.web.servlet.handler.HandlerMappingIntrospector.lambda$createCacheFilter$3(HandlerMappingIntrospector.java:241)
org.springframework.web.filter.CompositeFilter$VirtualFilterChain.doFilter(CompositeFilter.java:113)
org.springframework.web.filter.CompositeFilter.doFilter(CompositeFilter.java:74)
It does not happen with the Spring Security Core plugin because it does not use HandlerMappingIntrospector
--
Note: this same problem could occur in any Filter or code that calls GrailsWebRequest.getParams() prior to the DispatcherServlet invoking checkMultipart(request).
It seems like others have attempted workarounds by calling WebUtils.clearGrailsWebRequest() which IMO is a little hacky.
Debugging this I also noticed inefficiencies such as how GrailsControllerUrlMappings.matchAll works. It appears StandardServletMultipartResolver.resolveMultipart is invoked multiple times during the same request.
Activity
MultipartHttpServletRequest - Handler mapping introspection fix. Fixes …
Discover the multipart request instead of substituting it