-
Notifications
You must be signed in to change notification settings - Fork 3.4k
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
Fix: prevent duplicated encoding request parameters filter #3598
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,6 +29,7 @@ | |
import org.springframework.util.MultiValueMap; | ||
import org.springframework.web.server.ServerWebExchange; | ||
import org.springframework.web.util.UriComponentsBuilder; | ||
import org.springframework.web.util.UriUtils; | ||
|
||
import static org.springframework.cloud.gateway.support.GatewayToStringStyler.filterToStringCreator; | ||
import static org.springframework.util.CollectionUtils.unmodifiableMultiValueMap; | ||
|
@@ -57,14 +58,19 @@ public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) { | |
MultiValueMap<String, String> queryParams = new LinkedMultiValueMap<>(request.getQueryParams()); | ||
queryParams.remove(config.getName()); | ||
|
||
URI newUri = UriComponentsBuilder.fromUri(request.getURI()) | ||
.replaceQueryParams(unmodifiableMultiValueMap(queryParams)) | ||
.build() | ||
.toUri(); | ||
try { | ||
MultiValueMap<String, String> encodedQueryParams = UriUtils.encodeQueryParams(queryParams); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Encode only |
||
URI newUri = UriComponentsBuilder.fromUri(request.getURI()) | ||
.replaceQueryParams(unmodifiableMultiValueMap(encodedQueryParams)) | ||
.build(true) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Indicates that it is encoded. |
||
.toUri(); | ||
|
||
ServerHttpRequest updatedRequest = exchange.getRequest().mutate().uri(newUri).build(); | ||
|
||
return chain.filter(exchange.mutate().request(updatedRequest).build()); | ||
ServerHttpRequest updatedRequest = exchange.getRequest().mutate().uri(newUri).build(); | ||
return chain.filter(exchange.mutate().request(updatedRequest).build()); | ||
} | ||
catch (IllegalArgumentException ex) { | ||
throw new IllegalStateException("Invalid URI query: \"" + queryParams + "\""); | ||
} | ||
} | ||
|
||
@Override | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,10 +26,14 @@ | |
import org.springframework.cloud.gateway.filter.GatewayFilterChain; | ||
import org.springframework.http.server.reactive.ServerHttpRequest; | ||
import org.springframework.util.Assert; | ||
import org.springframework.util.LinkedMultiValueMap; | ||
import org.springframework.util.MultiValueMap; | ||
import org.springframework.web.server.ServerWebExchange; | ||
import org.springframework.web.util.UriComponentsBuilder; | ||
import org.springframework.web.util.UriUtils; | ||
|
||
import static org.springframework.cloud.gateway.support.GatewayToStringStyler.filterToStringCreator; | ||
import static org.springframework.util.CollectionUtils.unmodifiableMultiValueMap; | ||
|
||
/** | ||
* @author Fredrich Ombico | ||
|
@@ -59,14 +63,25 @@ public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) { | |
ServerHttpRequest req = exchange.getRequest(); | ||
|
||
UriComponentsBuilder uriComponentsBuilder = UriComponentsBuilder.fromUri(req.getURI()); | ||
if (req.getQueryParams().containsKey(config.getName())) { | ||
uriComponentsBuilder.replaceQueryParam(config.getName(), config.getReplacement()); | ||
|
||
MultiValueMap<String, String> queryParams = new LinkedMultiValueMap<>(req.getQueryParams()); | ||
if (queryParams.containsKey(config.getName())) { | ||
queryParams.remove(config.getName()); | ||
queryParams.add(config.getName(), config.getReplacement()); | ||
} | ||
Comment on lines
63
to
71
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
|
||
URI uri = uriComponentsBuilder.build().toUri(); | ||
ServerHttpRequest request = req.mutate().uri(uri).build(); | ||
try { | ||
MultiValueMap<String, String> encodedQueryParams = UriUtils.encodeQueryParams(queryParams); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Encode only |
||
URI uri = uriComponentsBuilder.replaceQueryParams(unmodifiableMultiValueMap(encodedQueryParams)) | ||
.build(true) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Indicates that it is encoded. |
||
.toUri(); | ||
|
||
return chain.filter(exchange.mutate().request(request).build()); | ||
ServerHttpRequest request = req.mutate().uri(uri).build(); | ||
return chain.filter(exchange.mutate().request(request).build()); | ||
} | ||
catch (IllegalArgumentException ex) { | ||
throw new IllegalStateException("Invalid URI query: \"" + queryParams + "\""); | ||
} | ||
} | ||
|
||
@Override | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ServerHttpRequest.getRequest()
returnsdecoded query parameters
as a response.So, it finds the
parameter
in thedecoded query parameters
and removes them.