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

Some character are not encoded in UriComponentsBuilder

Open
#3,733 0 comments 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

I'm currently experimenting some weird behaviour when migrate from Zuul (with spring 5) to spring gateway mvc (with spring 6). During this migration, I see that the parameter are not alway correctly encoded to the downstream services.

Issue are linked to issue1 and issue2.

I reduce the scope of the issue, and observe that some parameters are not encoded in the parameter, and I really don't understand why it's the current behaviour.

Char Encoded in URI Encoded in PARAM
@ 🔴 🔴
[ 🟢 🔴
% 🟢 🟢
'& 🟢 🔴
'+ 🔴 🔴
'{ 🟢 🟢
'? 🟢 🔴

Can someone explain me why this is a wanted behaviour, if not a bug?

        String uri = UriComponentsBuilder.fromPath("/user@[%&+{?")
                .scheme("http")
                .host("localhost")
                .port("9099")
                .replaceQueryParams(new LinkedMultiValueMap<>(
                        Map.of(
                                "at", List.of("@"),
                                "squareBracket", List.of("["),
                                "percent", List.of("%"),
                                "and", List.of("&"),
                                "plus", List.of("+"),
                                "bracket", List.of("{"),
                                "point", List.of("?")
                        )
                ))
                .build(false)
                .toUri()
                .toString();

// uri = http://localhost:9099/user@%5B%25&+%7B%3F?at=@&and=&&squareBracket=[&point=?&percent=%25&bracket=%7B&plus=+

Note: I voluntary keep the code close to org.springframework.cloud.gateway.server.mvc.handler.ProxyExchangeHandlerFunction#handle in spring gateway 4.1.6:

	@Override
	public ServerResponse handle(ServerRequest serverRequest) {
		URI uri = uriResolver.apply(serverRequest);
		boolean encoded = containsEncodedQuery(serverRequest.uri(), serverRequest.params());
		// @formatter:off
		URI url = UriComponentsBuilder.fromUri(serverRequest.uri())
				.scheme(uri.getScheme())
				.host(uri.getHost())
				.port(uri.getPort())
				.replaceQueryParams(serverRequest.params())
				.build(encoded)
				.toUri();
		// @formatter:on

		HttpHeaders filteredRequestHeaders = filterHeaders(this.requestHttpHeadersFilters,
				serverRequest.headers().asHttpHeaders(), serverRequest);

		boolean preserveHost = (boolean) serverRequest.attributes()
			.getOrDefault(MvcUtils.PRESERVE_HOST_HEADER_ATTRIBUTE, false);
		if (preserveHost) {
			filteredRequestHeaders.set(HttpHeaders.HOST, serverRequest.headers().firstHeader(HttpHeaders.HOST));
		}
		else {
			filteredRequestHeaders.remove(HttpHeaders.HOST);
		}

		// @formatter:off
		ProxyExchange.Request proxyRequest = proxyExchange.request(serverRequest).uri(url)
				.headers(filteredRequestHeaders)
				// TODO: allow injection of ResponseConsumer
				.responseConsumer((response, serverResponse) -> {
					HttpHeaders httpHeaders = filterHeaders(this.responseHttpHeadersFilters, response.getHeaders(), serverResponse);
					serverResponse.headers().putAll(httpHeaders);
				})
				.build();
		// @formatter:on
		return proxyExchange.exchange(proxyRequest);
	}
```

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 reproducing the URI and query-parameter output in the issue, then read UriComponentsBuilder and ProxyExchangeHandlerFunction#handle in Spring Cloud Gateway 4.1.6. Compare the encoding behavior for each listed character and determine the expected result. Done should include an agreed behavior and regression coverage for the affected parameters.

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
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.