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

AddRequestParameter double encodes parameter value #3466

Open
onacit opened this issue Jul 20, 2024 · 1 comment
Open

AddRequestParameter double encodes parameter value #3466

onacit opened this issue Jul 20, 2024 · 1 comment

Comments

@onacit
Copy link

onacit commented Jul 20, 2024

Describe the bug
AddRequestParameter encodes the already-encoded value when other parameter is encoded.

There is an remote endpoint which has two request parameters(Say, a and b) which each may need to be url-encoded.

Within my route, I'm intending putting the b parameter's value.

Per, #2717, I configured the b parameter's value as url-encoded.

When sending a with an ascii value, it works.

When sending a with an url-encoded value, the b parameter's value is double-encoded.

# application.yaml

spring:
  cloud:
    gateway:
      routes:
        filters:
          - AddRequestParameter=b, %2F
CLIENT                  GATEWAY
--------------------------------------------------
a=%2F                   a=%2F&b=%2F   OK
a=a                     a=a&b=%252F   NOT OK
@onacit onacit changed the title AddRequestParameter double encodes when other parameter is already encoded AddRequestParameter double encodes parameter value Jul 20, 2024
@onacit
Copy link
Author

onacit commented Jul 24, 2024

Here comes what I've used to solve my case.

            @Override
            public Mono<Void> filter(final ServerWebExchange exchange, final GatewayFilterChain chain) {
                final var request = exchange.getRequest();
                final var uri = request.getURI();
                final var builder = UriComponentsBuilder.fromUri(uri);
                request.getQueryParams().forEach((k, l) -> builder.replaceQueryParam(
                        URLEncoder.encode(k, StandardCharsets.UTF_8),
                        l.stream().map(v -> URLEncoder.encode(v, StandardCharsets.UTF_8)).toList()
                ));
                builder.queryParam(
                        URLEncoder.encode(config.getName(), StandardCharsets.UTF_8),
                        URLEncoder.encode(config.getValue(), StandardCharsets.UTF_8)
                );
                final var newUri = builder.build(true).toUri();
                final var newRequest = exchange.getRequest().mutate().uri(newUri).build();
                return chain.filter(exchange.mutate().request(newRequest).build());
            }

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