Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Spring MVC re-creates form data from request params and re-encoding can change the content-length #32471

Closed
ramyav16 opened this issue Mar 18, 2024 · 7 comments
Assignees
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) type: bug A general bug
Milestone

Comments

@ramyav16
Copy link

On Spring 6.1.5 when content type is set as application/x-www-form-urlencoded the request gets encoded value. However it also alters the request String to remove the number of characters added as part of the encoding.

Header has: Content-Type - application/x-www-form-urlencoded

Request Body has String: name=Test&lastName=Test@er

In Spring controller the String appearing is: name=Test&lastName=Test%40

While it encodes, the last 2 characters have been removed by Spring. This issue exists in 6.1.x. Request any help on the same.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Mar 18, 2024
@snicoll
Copy link
Member

snicoll commented Mar 18, 2024

@ramyav16 please share a small sample we can run to reproduce the problem. You can attach a zip to this issue or push the code to a GitHub repository.

@snicoll snicoll added the status: waiting-for-feedback We need additional information before we can continue label Mar 18, 2024
@ramyav16
Copy link
Author

ramyav16 commented Mar 18, 2024

Thanks for the response. Below is one sample. Tried on Java 17.

@RequestMapping(value = "/testing.action", method = RequestMethod.POST)
	public @ResponseBody
	void sampleCode(
			@RequestBody String sampleInput, ModelMap model,
			HttpServletRequest request, HttpServletResponse response) {
			System.out.println("This is the req body: " + sampleInput);
		}

@spring-projects-issues spring-projects-issues added status: feedback-provided Feedback has been provided and removed status: waiting-for-feedback We need additional information before we can continue labels Mar 18, 2024
@snicoll
Copy link
Member

snicoll commented Mar 18, 2024

@ramyav16 thanks, but a code snippet is not a sample. Please attach a sample as requested and how to trigger the query that leads to the problem you've described.

@snicoll snicoll added status: waiting-for-feedback We need additional information before we can continue and removed status: feedback-provided Feedback has been provided labels Mar 18, 2024
@rstoyanchev rstoyanchev added the in: web Issues in web modules (web, webmvc, webflux, websocket) label Mar 19, 2024
@ramyav16
Copy link
Author

@snicoll Thanks. I have added a sample - https://github.com/ramyav16/spring-mvc-example
Works on Java 17, Tomcat 10.1

Same can be tested via any tool such as Postman.
Header
image

Body
image

@spring-projects-issues spring-projects-issues added status: feedback-provided Feedback has been provided and removed status: waiting-for-feedback We need additional information before we can continue labels Mar 19, 2024
@rstoyanchev
Copy link
Contributor

rstoyanchev commented Mar 22, 2024

A different case was reported in #31327 with the same root cause, which is related to the StringHttpMessageConverter optimization in #30942 and ServletServerHttpRequest#getBody reconstructing the body from request params to ensure consistent form data access.

In #31327, both a request body and a query were present, and that made the reconstructed body larger than the content-length. For that case, we undid the optimization in 6.0.x, and added a check to avoid reconstructing the body if there is a query string.

Here the reconstructed body is larger because the request body has a character that wasn't encoded, but is after the body is reconstructed and that leads to a mismatch between content and content-length.

@rstoyanchev rstoyanchev added type: bug A general bug and removed status: waiting-for-triage An issue we've not yet triaged or decided on status: feedback-provided Feedback has been provided labels Mar 22, 2024
@rstoyanchev rstoyanchev added this to the 6.1.6 milestone Mar 22, 2024
@ramyav16
Copy link
Author

@rstoyanchev thanks for the details. Would this be fixed in version 6.1.6?

@rstoyanchev
Copy link
Contributor

If the client was encoding the body, as it should, it wouldn't cause this issue. That said, the goal here is to find a fix although I'm not sure yet what it will be.

@rstoyanchev rstoyanchev self-assigned this Apr 9, 2024
@rstoyanchev rstoyanchev changed the title Content-Type application/x-www-form-urlencoded on Spring 6.1.5 encodes and alters the String request value Spring MVC re-creates form data from request params and re-encoding can change the content-length Apr 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) type: bug A general bug
Projects
None yet
Development

No branches or pull requests

4 participants