Skip to content

The method org.springframework.http.MediaType.parseMediaType does not always throw IllegalArgumentException as the documentantion claims [SPR-6786] #11452

@spring-projects-issues

Description

@spring-projects-issues

Tomas Johansson opened SPR-6786 and commented

For example, the following invocation throws a StringIndexOutOfBoundsException:

MediaType.parseMediaType("audio");

(because the implementation assumes existance of a slash and does not check the value of the index before trying to get the assumed substrings separated by slashes)

The following input string passes without getting IllegalArgumentException:

MediaType mediaType = MediaType.parseMediaType("audio/*;q=");

but when you then invoke 'mediaType.getQualityValue()' you get a NumberFormatException.
This validation should be taken care of by the parser method as the documentation claims.

Except from these kind of obvious and easy fixes, I wonder if there also should not be some kind of sanity check, or if it should be considered as correct behaviour that the following kind of test currently passes green:

String mediaTypeUsedAsTypeAndSubtype = "´`'*^~:.,@!$%&{}[]()&";
MediaType mediaType = MediaType.parseMediaType(mediaTypeUsedAsTypeAndSubtype + "/" + mediaTypeUsedAsTypeAndSubtype);
assertEquals(mediaTypeUsedAsTypeAndSubtype, mediaType.getType());
assertEquals(mediaTypeUsedAsTypeAndSubtype, mediaType.getSubtype());

/ Tomas Johansson


Referenced from: commits 4462605, 0135a94

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