Skip to content

Accept header with trailing comma produces HTTP "406 Not Acceptable" #23241

Closed

Description

@j3r0lin

Spring version: 5.1.8.RELEASE

Send a http request with header like this, containing , at the end.

Accept: application/json, 

HeaderContentNegotiationStrategy will produce a HttpMediaTypeNotAcceptableException.

org.springframework.web.HttpMediaTypeNotAcceptableException: Could not parse 'Accept' header [application/json,]: Invalid mime type "": 'mimeType' must not be empty
	at org.springframework.web.accept.HeaderContentNegotiationStrategy.resolveMediaTypes(HeaderContentNegotiationStrategy.java:59) ~[spring-web-5.1.8.RELEASE.jar!/:5.1.8.RELEASE]
	at org.springframework.web.accept.ContentNegotiationManager.resolveMediaTypes(ContentNegotiationManager.java:124) ~[spring-web-5.1.8.RELEASE.jar!/:5.1.8.RELEASE]
	at org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodProcessor.getAcceptableMediaTypes(AbstractMessageConverterMethodProcessor.java:391) ~[spring-webmvc-5.1.8.RELEASE.jar!/:5.1.8.RELEASE]
	at org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodProcessor.writeWithMessageConverters(AbstractMessageConverterMethodProcessor.java:229) ~[spring-webmvc-5.1.8.RELEASE.jar!/:5.1.8.RELEASE]
	at org.springframework.web.servlet.mvc.method.annotation.HttpEntityMethodProcessor.handleReturnValue(HttpEntityMethodProcessor.java:225) ~[spring-webmvc-5.1.8.RELEASE.jar!/:5.1.8.RELEASE]
	at org.springframework.web.method.support.HandlerMethodReturnValueHandlerComposite.handleReturnValue(HandlerMethodReturnValueHandlerComposite.java:82) ~[spring-web-5.1.8.RELEASE.jar!/:5.1.8.RELEASE]

That http request works before 5.1.2.RELEASE.

Maybe the cause of this is the parsing method refactoring in this commit f4b05dc.

StringUtils.tokenizeToStringArray(mimeTypes, ",") will trim tokens and ignore empty tokens by default, but now, the replacement method not support that anymore.

Activity

  1. added
    status: waiting-for-triage
    on Jul 5, 2019
  2. sbrannen commented on Jul 5, 2019

    @sbrannen
    Member

    Send a http request with header like this, containing , at the end.

    What is your use case for that?

    What client is sending the Accept request header with a trailing comma?

  3. added
    status: waiting-for-feedback
    in: web
    and removed
    status: waiting-for-feedback
    on Jul 5, 2019
  4. j3r0lin commented on Jul 5, 2019

    @j3r0lin
    Author

    I'm using an HTTP client named Paw. It sends that request from its built in oauth2 client.

    Although it seems to be an invalid header, it did work before.

  5. added
    status: feedback-provided
    and removed
    status: waiting-for-feedback
    on Jul 5, 2019
  6. changed the title [-]Accept header ends with "," produces HTTP "406 Not Acceptable"[/-] [+]Accept header with trailing comma produces HTTP "406 Not Acceptable"[/+] on Jul 6, 2019
  7. added
    type: regression
    on Jul 6, 2019
  8. added this to the 5.1.9 milestone on Jul 6, 2019
  9. removed
    status: waiting-for-triage
    on Jul 6, 2019
  10. sbrannen commented on Jul 6, 2019

    @sbrannen
    Member

    Thanks for the feedback. I've labeled this as a regression.

  11. sbrannen commented on Jul 6, 2019

    @sbrannen
    Member

    The following test passes against the v5.1.2.RELEASE tag and fails on the current 5.1.x branch (beginning with the v5.1.3.RELEASE tag).

    @Test
    public void parseMediaTypesWithTrailingComma() {
    	List<MediaType> mediaTypes = MediaType.parseMediaTypes("text/plain, text/html, ");
    	assertNotNull("No media types returned", mediaTypes);
    	assertEquals("Incorrect number of media types", 2, mediaTypes.size());
    }

    The following fails on master.

    @Test
    public void parseMimeTypesWithTrailingComma() {
    	List<MimeType> mimeTypes = MimeTypeUtils.parseMimeTypes("text/plain, text/html,");
    	assertThat(mimeTypes).as("No mime types returned").isNotNull();
    	assertThat(mimeTypes.size()).as("Invalid number of mime types").isEqualTo(2);
    }
  12. self-assigned this
    on Jul 6, 2019
  13. added a commit that references this issue on Jul 7, 2019
    efab6eb
  14. removed
    status: feedback-provided
    on Dec 20, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

in: webtype: regression

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions