Fix StringIndexOutOfBoundsException

Closes gh-29588
This commit is contained in:
rstoyanchev 2023-02-07 15:44:34 +00:00
parent 08e7f5a292
commit 9c0b28ffdc
3 changed files with 13 additions and 2 deletions

View File

@ -488,7 +488,8 @@ public class MockHttpServletRequest implements HttpServletRequest {
} }
catch (IllegalArgumentException ex) { catch (IllegalArgumentException ex) {
// Try to get charset value anyway // Try to get charset value anyway
int charsetIndex = contentType.toLowerCase().indexOf(CHARSET_PREFIX); contentType = contentType.toLowerCase();
int charsetIndex = contentType.indexOf(CHARSET_PREFIX);
if (charsetIndex != -1) { if (charsetIndex != -1) {
this.characterEncoding = contentType.substring(charsetIndex + CHARSET_PREFIX.length()); this.characterEncoding = contentType.substring(charsetIndex + CHARSET_PREFIX.length());
} }

View File

@ -185,6 +185,15 @@ class MockHttpServletRequestTests {
assertThat(request.getCharacterEncoding()).isEqualTo("UTF-8"); assertThat(request.getCharacterEncoding()).isEqualTo("UTF-8");
} }
@Test // gh-29255
void setContentTypeInvalidWithNonAsciiCharacterAndCharset() {
String contentType = "İcharset=";
request.addHeader(HttpHeaders.CONTENT_TYPE, contentType);
assertThat(request.getContentType()).isEqualTo(contentType);
assertThat(request.getHeader(HttpHeaders.CONTENT_TYPE)).isEqualTo(contentType);
assertThat(request.getCharacterEncoding()).isEqualTo("");
}
@Test @Test
void contentTypeHeader() { void contentTypeHeader() {
String contentType = "test/plain"; String contentType = "test/plain";

View File

@ -488,7 +488,8 @@ public class MockHttpServletRequest implements HttpServletRequest {
} }
catch (IllegalArgumentException ex) { catch (IllegalArgumentException ex) {
// Try to get charset value anyway // Try to get charset value anyway
int charsetIndex = contentType.toLowerCase().indexOf(CHARSET_PREFIX); contentType = contentType.toLowerCase();
int charsetIndex = contentType.indexOf(CHARSET_PREFIX);
if (charsetIndex != -1) { if (charsetIndex != -1) {
this.characterEncoding = contentType.substring(charsetIndex + CHARSET_PREFIX.length()); this.characterEncoding = contentType.substring(charsetIndex + CHARSET_PREFIX.length());
} }