Skip to content

MockHttpServletRequest#setContentType fails with StringIndexOutOfBoundsException #29255

Closed
@rwinch

Description

@rwinch

For the default Locale of en_US, the following code:

MockHttpServletRequest request = new MockHttpServletRequest();
request.setContentType("İcharset=");

fails with:

java.lang.StringIndexOutOfBoundsException: begin 10, end 9, length 9
	at java.base/java.lang.String.checkBoundsBeginEnd(String.java:4604)
	at java.base/java.lang.String.substring(String.java:2707)
	at java.base/java.lang.String.substring(String.java:2680)
	at org.springframework.mock.web.MockHttpServletRequest.setContentType(MockHttpServletRequest.java:495)

The problem is that contentType.toLowerCase() changes the length of the String, so when using the index on the original String causes a StringIndexOutOfBoundsException.

This is causing failures to be reported to Spring Security by the oss-fuzz project which generated random inputs for their test cases which resulted in this error being reported to Spring Security.

While the header is invalid it would be nice to be able to test invalid headers using the mock object.

Metadata

Metadata

Assignees

Labels

in: testIssues in the test modulein: 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