Browse Source

ForwardedHeaderTransformer handles encoding correctly

Issue: SPR-17525
pull/23430/head
Rossen Stoyanchev 6 years ago
parent
commit
567fcc41cc
  1. 2
      spring-web/src/main/java/org/springframework/web/filter/reactive/ForwardedHeaderFilter.java
  2. 13
      spring-web/src/test/java/org/springframework/web/filter/reactive/ForwardedHeaderFilterTests.java

2
spring-web/src/main/java/org/springframework/web/filter/reactive/ForwardedHeaderFilter.java

@ -84,7 +84,7 @@ public class ForwardedHeaderFilter implements WebFilter { @@ -84,7 +84,7 @@ public class ForwardedHeaderFilter implements WebFilter {
return chain.filter(withoutForwardHeaders);
}
else {
URI uri = UriComponentsBuilder.fromHttpRequest(exchange.getRequest()).build().toUri();
URI uri = UriComponentsBuilder.fromHttpRequest(exchange.getRequest()).build(true).toUri();
String prefix = getForwardedPrefix(exchange.getRequest().getHeaders());
ServerWebExchange withChangedUri = exchange.mutate()

13
spring-web/src/test/java/org/springframework/web/filter/reactive/ForwardedHeaderFilterTests.java

@ -23,6 +23,7 @@ import org.junit.Test; @@ -23,6 +23,7 @@ import org.junit.Test;
import reactor.core.publisher.Mono;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.lang.Nullable;
import org.springframework.mock.http.server.reactive.test.MockServerHttpRequest;
import org.springframework.mock.web.test.server.MockServerWebExchange;
@ -113,6 +114,18 @@ public class ForwardedHeaderFilterTests { @@ -113,6 +114,18 @@ public class ForwardedHeaderFilterTests {
assertEquals(new URI("http://example.com/prefix/path"), uri);
}
@Test // SPR-17525
public void shouldNotDoubleEncode() throws Exception {
MockServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest
.method(HttpMethod.GET, new URI ("http://example.com/a%20b?q=a%2Bb"))
.header("Forwarded", "host=84.198.58.199;proto=https"));
this.filter.filter(exchange, this.filterChain).block(Duration.ZERO);
URI uri = this.filterChain.uri;
assertEquals(new URI("https://84.198.58.199/a%20b?q=a%2Bb"), uri);
}
private static class TestWebFilterChain implements WebFilterChain {

Loading…
Cancel
Save