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

Invalid MIME-type causes a 500 error [SPR-7712] #12368

Closed
spring-projects-issues opened this issue Nov 2, 2010 · 6 comments
Closed

Invalid MIME-type causes a 500 error [SPR-7712] #12368

spring-projects-issues opened this issue Nov 2, 2010 · 6 comments
Assignees
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) status: backported An issue that has been backported to maintenance branches type: enhancement A general enhancement
Milestone

Comments

@spring-projects-issues
Copy link
Collaborator

Brett opened SPR-7712 and commented

I have a REST service setup with the ContentNegotiatingViewResolver to transform responses into either xml or json based on the Accept header. I set the useNotAcceptableStatusCode property to true and it appears to work as expected. However, if the Accept header is a completely invalid media type like 'foo' then an exception is thrown and the consumer ends up getting back a 500 Internal Server Error instead of the expected 406. The top of the resulting stack trace is included below:

SEVERE: Servlet.service() for servlet Spring MVC Dispatcher Servlet threw exception
java.lang.IllegalArgumentException: "foo" does not contain '/'
at org.springframework.http.MediaType.parseMediaType(MediaType.java:562)
at org.springframework.http.MediaType.parseMediaTypes(MediaType.java:602)
at org.springframework.web.servlet.view.ContentNegotiatingViewResolver.getMediaTypes(ContentNegotiatingViewResolver.java:306)
at org.springframework.web.servlet.view.ContentNegotiatingViewResolver.resolveViewName(ContentNegotiatingViewResolver.java:366)
at org.springframework.web.servlet.DispatcherServlet.resolveViewName(DispatcherServlet.java:1078)
at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1027)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:817)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:549)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at com.cerner.net.oauth.j2ee.filter.OAuthValidatorFilter$1.run(OAuthValidatorFilter.java:215)
at java.security.AccessController.doPrivileged(Native Method)
at javax.security.auth.Subject.doAs(Subject.java:396)
at com.cerner.net.oauth.j2ee.filter.OAuthValidatorFilter.doFilter(OAuthValidatorFilter.java:213)
at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:237)
at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:167)
...

Considering that an invalid media type is a problem with the client making the request to the service, I think it should return a proper 406 response if the useNotAcceptableStatusCode flag is set.


Affects: 3.0.4, 3.0.5

Reference URL: http://forum.springsource.org/showthread.php?p=317618#post317618

Referenced from: commits cb0c0c5, 0a48936

Backported to: 3.0.6

4 votes, 6 watchers

@spring-projects-issues
Copy link
Collaborator Author

Tristan Burch commented

Is there a timeline for this getting fixed? I agree that a 406 should be retured in this case.

@spring-projects-issues
Copy link
Collaborator Author

Juergen Hoeller commented

Fixed for both 3.1 RC1 and 3.0.6, handling an invalid accept header like a general non-match: returning null for ViewResolver chaining, or sending 406 if ContentNegotiatingViewResolver's "useNotAcceptableStatusCode" property has been set to "true".

Juergen

@spring-projects-issues
Copy link
Collaborator Author

David commented

We're using Spring 3.1.0RELEASE and still see this. Did the fix in 3.1RC1 make it to RELEASE?
I get a different traceback, but still an IllegalArgumentException is thrown which results in a 500
not a 4xx

Note: I think 400 would be more appropriate than 406; I think 406 assumes a valid media type

Here is the stack trace I got

                                                    HTTP Status 500 -
______________________________________________________________________________________________________________________

type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.IllegalArgumentException: "applicationjson" does not contain '/'
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:894)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:778)
javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:77)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)

root cause
java.lang.IllegalArgumentException: "applicationjson" does not contain '/'
org.springframework.http.MediaType.parseMediaType(MediaType.java:640)
org.springframework.http.MediaType.parseMediaTypes(MediaType.java:683)
org.springframework.web.servlet.mvc.condition.ProducesRequestCondition.getAcceptedMediaTypes(ProducesRequestCondition.java:214)
org.springframework.web.servlet.mvc.condition.ProducesRequestCondition.access$4(ProducesRequestCondition.java:211)
org.springframework.web.servlet.mvc.condition.ProducesRequestCondition$ProduceMediaTypeExpression.matchMediaType(ProducesRequestCondition.java:281)
org.springframework.web.servlet.mvc.condition.AbstractMediaTypeExpression.match(AbstractMediaTypeExpression.java:63)
org.springframework.web.servlet.mvc.condition.ProducesRequestCondition.getMatchingCondition(ProducesRequestCondition.java:163)
org.springframework.web.servlet.mvc.method.RequestMappingInfo.getMatchingCondition(RequestMappingInfo.java:175)
org.springframework.web.servlet.mvc.method.RequestMappingInfoHandlerMapping.getMatchingMapping(RequestMappingInfoHandlerMapping.java:64)
org.springframework.web.servlet.mvc.method.RequestMappingInfoHandlerMapping.getMatchingMapping(RequestMappingInfoHandlerMapping.java:1)
org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.lookupHandlerMethod(AbstractHandlerMethodMapping.java:248)
org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.getHandlerInternal(AbstractHandlerMethodMapping.java:214)
org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.getHandlerInternal(AbstractHandlerMethodMapping.java:1)
org.springframework.web.servlet.handler.AbstractHandlerMapping.getHandler(AbstractHandlerMapping.java:288)
org.springframework.web.servlet.DispatcherServlet.getHandler(DispatcherServlet.java:1040)
org.springframework.web.servlet.DispatcherServlet.getHandler(DispatcherServlet.java:1025)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:863)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:827)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:778)
javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:77)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)

@spring-projects-issues
Copy link
Collaborator Author

Juergen Hoeller commented

Rossen, can you have a look at this please? I'm pretty sure there's no regression but it might be something we never properly fixed...

Juergen

@spring-projects-issues
Copy link
Collaborator Author

Rossen Stoyanchev commented

There were a number of places exposed to invalid media types. The stack trace from David above is different from the one in the original comment.

We applied a more comprehensive fix in the 3.2.x timeline and backported it to 3.1.x (see commit 83ac44 including the link to the backport commit). There are tests specifically for ProducesRequestCondition there.

@spring-projects-issues
Copy link
Collaborator Author

Juergen Hoeller commented

Alright, Rossen, let's consider this one as fixed as of 3.1.2 then - and open a follow-up issue if needed.

David, note that the 3.1.x line is retired already, so please upgrade to a recent 3.2.x release and check whether any issues remain there.

Juergen

@spring-projects-issues spring-projects-issues added status: backported An issue that has been backported to maintenance branches type: enhancement A general enhancement in: web Issues in web modules (web, webmvc, webflux, websocket) labels Jan 11, 2019
@spring-projects-issues spring-projects-issues added this to the 3.1.2 milestone Jan 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) status: backported An issue that has been backported to maintenance branches type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

2 participants