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

Form request parameters become garbled texts if non-UTF8 encoding #3572

Open
rshindo opened this issue Oct 27, 2024 · 1 comment
Open

Form request parameters become garbled texts if non-UTF8 encoding #3572

rshindo opened this issue Oct 27, 2024 · 1 comment

Comments

@rshindo
Copy link

rshindo commented Oct 27, 2024

Versions

  • spring-cloud-gateway-mvc:4.1.5
  • spring-cloud-gateway-server-mvc:4.1.5

Bugs

Cannot read ServletInputSream with form request encoding when using ProxyExchange.

For example, a form parameter "こんにちは世界" (hello, world) encoded with Shift-JIS will be %EF%BF%BD%EF%BF%BD%EF%BF%BD%EF%BF%BD%C9%82%EF%BF%BD%EF%BF%BD%CD%90%EF%BF%BD%EF%BF%BDE. This is not an encoded value with Shift-JIS nor UTF-8.

Sample

Spring code

package com.example.gatewaymvc;

import java.io.IOException;
import java.nio.charset.Charset;

import org.springframework.cloud.gateway.mvc.ProxyExchange;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;

import jakarta.servlet.http.HttpServletRequest;

@Controller
public class RouteController {

	@PostMapping("/form")
	public ResponseEntity<?> formRoute(ProxyExchange<byte[]> proxy, HttpServletRequest request)
			throws IOException {
		request.setCharacterEncoding("Shift-JIS"); // This encoding is ignored
		byte[] body = request.getInputStream().readAllBytes();
		System.out.println(new String(body, Charset.forName("Shift-JIS"))); // Garbled!
		return proxy.uri("https://httpbin.org/post").post();
	}
}

Post request from this HTML

<!DOCTYPE html>
<html lang="ja">
<head>
    <meta charset="Shift_JIS" />
    <title>Example</title>
</head>
<body>

<form method="POST" action="http://localhost:8080/form">
    <label for="item">Item:</label><br>
    <input type="text" id="item" name="item"><br>
    <input type="submit" value="Submit">
</form>

</body>
</html>
@rshindo
Copy link
Author

rshindo commented Nov 10, 2024

I confirmed that same issue occurred even if not using ServletInputStream.

@Bean
public RouterFunction<ServerResponse> formRoute() {
	return route("form")
			.POST("/form", http("https://httpbin.org"))
			.before(rewritePath("/form", "/post"))
			.build();
}

I think FormFilter read request body with UTF-8 regardless of request encoding, so Shift-JIS parameter will be garbled text. FormFilter use the encoding by getCharacterEncoding(), this problem will be ok.

static HttpServletRequest getRequestWithBodyFromRequestParameters(HttpServletRequest request) throws IOException {
ByteArrayOutputStream bos = new ByteArrayOutputStream(1024);
Writer writer = new OutputStreamWriter(bos, FORM_CHARSET);

@rshindo rshindo changed the title Cannot read ServletInputSream with form request encoding when using ProxyExchange Form request parameters become garbled texts if non-UTF8 encoding Nov 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant