spring-cloud / spring-cloud/spring-cloud-gateway

Form request parameters become garbled texts if non-UTF8 encoding

Open
#3,572 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

waiting-for-triage
Dominant language
Java
Stars
4.9k
Forks
3.5k
Avg merge
20h 57m
Merged PRs (30d)
8

Description

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>

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by tracing ProxyExchange's form POST handling and how the ServletInputStream is read after request.setCharacterEncoding("Shift-JIS"). Reproduce the supplied Shift-JIS form request and verify that the body remains decodable with that encoding rather than becoming replacement characters.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, spring, spring-boot
Domain
api, backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.