Skip to content

Accept header with quoted parameter causes IllegalArgumentException in HttpHeaders.getContentType for the outputMessage [SPR-8917] #13557

@spring-projects-issues

Description

@spring-projects-issues

David Pacheco opened SPR-8917 and commented

A HTTP request with the following Accept header

Accept: application/xhtml+xml; profile="http://www.wapforum.org/xhtml", application/vnd.wap.xhtml+xml

to a String Controller, causes the following exception to be thrown:

SEVERE: Servlet.service() for servlet dispatcher threw exception
java.lang.IllegalArgumentException: Invalid token character ':' in token "http://www.wapforum.org/xhtml"
        at org.springframework.http.MediaType.checkToken(MediaType.java:282)
        at org.springframework.http.MediaType.checkParameters(MediaType.java:302)
        at org.springframework.http.MediaType.<init>(MediaType.java:263)
        at org.springframework.http.MediaType.parseMediaType(MediaType.java:584)
        at org.springframework.http.HttpHeaders.getContentType(HttpHeaders.java:286)
        at org.springframework.http.converter.StringHttpMessageConverter.writeInternal(StringHttpMessageConverter.java:97)
        at org.springframework.http.converter.StringHttpMessageConverter.writeInternal(StringHttpMessageConverter.java:1)
        at org.springframework.http.converter.AbstractHttpMessageConverter.write(AbstractHttpMessageConverter.java:181)
        at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter$ServletHandlerMethodInvoker.writeWithMessageConverters(AnnotationMethodHandlerAdapter.java:973)
        at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter$ServletHandlerMethodInvoker.handleResponseBody(AnnotationMethodHandlerAdapter.java:931)
        at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter$ServletHandlerMethodInvoker.getModelAndView(AnnotationMethodHandlerAdapter.java:880)
        at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:429)
        at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:415)
        at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:788)
        at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:717)
        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)

In brief:

This is due to the AbstractHttpMessageConverter write method calling the headers.setContentType() method of the outputMessage parameter with a MediaType with the following value "application/xhtml+xml;profile=http://www.wapforum.org/xhtml".

Note the lack of quotes around the profile value part.

The program flow is:

The MediaType is parsed from the HTTP Accept header with the value

application/xhtml+xml;profile="http://www.wapforum.org/xhtml"

Note - The inclusion of the quotes means that this value does not trigger the checkToken(String) method of MediaType.

In the StringHttpMessageConverter write method, a call is made to set the content type for the outputMessage.
This uses the toString() method of MediaType to set the Content-Type header, this results in the value being set to "application/xhtml+xml;profile=http://www.wapforum.org/xhtml".

Next the writeInternal method of StringHttpMessageConverter is called, this tries to get the MediaType for the outputMessage by calling outputMessage.getHeaders().getContentType(), which in turn calls MediaType.parseMediaType(value) with the value of "application/xhtml+xml;profile=http://www.wapforum.org/xhtml", due to the missing quotes in the profile parameter value, this will now trigger the checkToken(String) method of MediaType which ultimately results in the IllegalArgumentException being thrown.


Example request headers

GET /index.html HTTP/1.1
Accept: application/xhtml+xml; profile="http://www.wapforum.org/xhtml", application/vnd.wap.xhtml+xml
Accept-Language: en-us
Host: app.example.com
Connection: Keep-Alive
Pragma: no-cache


Affects: 3.0.4

Issue Links:

1 votes, 7 watchers

Metadata

Metadata

Assignees

Labels

in: webIssues in web modules (web, webmvc, webflux, websocket)type: bugA general bug

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions