Browse Source
Forwarded headers are now processed before ServerWebExchange is created through ForwardedHeaderTransformer which has the same logic as the ForwardedHeaderFilter but works on the request only. ForwardedHeaderFilter is deprecated as of 5.1 but if registered it is removed from the list of filters and ForwardedHeaderTransformer is used instead. Issue: SPR-17072pull/1913/merge
10 changed files with 475 additions and 300 deletions
@ -0,0 +1,131 @@
@@ -0,0 +1,131 @@
|
||||
/* |
||||
* Copyright 2002-2018 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
package org.springframework.web.server.adapter; |
||||
|
||||
import java.net.URI; |
||||
import java.util.LinkedHashSet; |
||||
import java.util.Set; |
||||
import java.util.function.Function; |
||||
|
||||
import org.springframework.http.HttpHeaders; |
||||
import org.springframework.http.server.reactive.ServerHttpRequest; |
||||
import org.springframework.lang.Nullable; |
||||
import org.springframework.web.util.UriComponentsBuilder; |
||||
|
||||
/** |
||||
* Extract values from "Forwarded" and "X-Forwarded-*" headers to override the |
||||
* request URI (i.e. {@link ServerHttpRequest#getURI()}) so it reflects the |
||||
* client-originated protocol and address. |
||||
* |
||||
* <p>Alternatively if {@link #setRemoveOnly removeOnly} is set to "true", then |
||||
* "Forwarded" and "X-Forwarded-*" headers are only removed, and not used. |
||||
* |
||||
* @author Rossen Stoyanchev |
||||
* @since 5.1 |
||||
* @see <a href="https://tools.ietf.org/html/rfc7239">https://tools.ietf.org/html/rfc7239</a>
|
||||
*/ |
||||
public class ForwardedHeaderTransformer implements Function<ServerHttpRequest, ServerHttpRequest> { |
||||
|
||||
static final Set<String> FORWARDED_HEADER_NAMES = new LinkedHashSet<>(5); |
||||
|
||||
static { |
||||
FORWARDED_HEADER_NAMES.add("Forwarded"); |
||||
FORWARDED_HEADER_NAMES.add("X-Forwarded-Host"); |
||||
FORWARDED_HEADER_NAMES.add("X-Forwarded-Port"); |
||||
FORWARDED_HEADER_NAMES.add("X-Forwarded-Proto"); |
||||
FORWARDED_HEADER_NAMES.add("X-Forwarded-Prefix"); |
||||
FORWARDED_HEADER_NAMES.add("X-Forwarded-Ssl"); |
||||
} |
||||
|
||||
|
||||
private boolean removeOnly; |
||||
|
||||
|
||||
/** |
||||
* Enables mode in which any "Forwarded" or "X-Forwarded-*" headers are |
||||
* removed only and the information in them ignored. |
||||
* @param removeOnly whether to discard and ignore forwarded headers |
||||
*/ |
||||
public void setRemoveOnly(boolean removeOnly) { |
||||
this.removeOnly = removeOnly; |
||||
} |
||||
|
||||
/** |
||||
* Whether the "remove only" mode is on. |
||||
*/ |
||||
public boolean isRemoveOnly() { |
||||
return this.removeOnly; |
||||
} |
||||
|
||||
|
||||
/** |
||||
* Apply and remove, or remove Forwarded type headers. |
||||
* @param request the request |
||||
*/ |
||||
@Override |
||||
public ServerHttpRequest apply(ServerHttpRequest request) { |
||||
|
||||
if (hasForwardedHeaders(request)) { |
||||
ServerHttpRequest.Builder builder = request.mutate(); |
||||
if (!this.removeOnly) { |
||||
URI uri = UriComponentsBuilder.fromHttpRequest(request).build().toUri(); |
||||
builder.uri(uri); |
||||
String prefix = getForwardedPrefix(request); |
||||
if (prefix != null) { |
||||
builder.path(prefix + uri.getPath()); |
||||
builder.contextPath(prefix); |
||||
} |
||||
} |
||||
removeForwardedHeaders(builder); |
||||
request = builder.build(); |
||||
} |
||||
|
||||
return request; |
||||
} |
||||
|
||||
/** |
||||
* Whether the request has any Forwarded headers. |
||||
* @param request the request |
||||
*/ |
||||
protected boolean hasForwardedHeaders(ServerHttpRequest request) { |
||||
HttpHeaders headers = request.getHeaders(); |
||||
for (String headerName : FORWARDED_HEADER_NAMES) { |
||||
if (headers.containsKey(headerName)) { |
||||
return true; |
||||
} |
||||
} |
||||
return false; |
||||
} |
||||
|
||||
private void removeForwardedHeaders(ServerHttpRequest.Builder builder) { |
||||
builder.headers(map -> FORWARDED_HEADER_NAMES.forEach(map::remove)); |
||||
} |
||||
|
||||
@Nullable |
||||
private static String getForwardedPrefix(ServerHttpRequest request) { |
||||
HttpHeaders headers = request.getHeaders(); |
||||
String prefix = headers.getFirst("X-Forwarded-Prefix"); |
||||
if (prefix != null) { |
||||
int endIndex = prefix.length(); |
||||
while (endIndex > 1 && prefix.charAt(endIndex - 1) == '/') { |
||||
endIndex--; |
||||
} |
||||
prefix = (endIndex != prefix.length() ? prefix.substring(0, endIndex) : prefix); |
||||
} |
||||
return prefix; |
||||
} |
||||
|
||||
} |
||||
@ -1,161 +0,0 @@
@@ -1,161 +0,0 @@
|
||||
/* |
||||
* Copyright 2002-2018 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.web.filter.reactive; |
||||
|
||||
import java.net.URI; |
||||
import java.time.Duration; |
||||
|
||||
import org.junit.Test; |
||||
import reactor.core.publisher.Mono; |
||||
|
||||
import org.springframework.http.HttpHeaders; |
||||
import org.springframework.http.server.reactive.ServerHttpRequest; |
||||
import org.springframework.lang.Nullable; |
||||
import org.springframework.mock.http.server.reactive.test.MockServerHttpRequest; |
||||
import org.springframework.mock.web.test.server.MockServerWebExchange; |
||||
import org.springframework.web.server.ServerWebExchange; |
||||
import org.springframework.web.server.WebFilterChain; |
||||
|
||||
import static org.junit.Assert.*; |
||||
|
||||
/** |
||||
* Unit tests for {@link ForwardedHeaderFilter}. |
||||
* @author Arjen Poutsma |
||||
* @author Rossen Stoyanchev |
||||
*/ |
||||
public class ForwardedHeaderFilterTests { |
||||
|
||||
private static final String BASE_URL = "http://example.com/path"; |
||||
|
||||
|
||||
private final ForwardedHeaderFilter filter = new ForwardedHeaderFilter(); |
||||
|
||||
private final TestWebFilterChain filterChain = new TestWebFilterChain(); |
||||
|
||||
|
||||
@Test |
||||
public void removeOnly() { |
||||
|
||||
this.filter.setRemoveOnly(true); |
||||
|
||||
HttpHeaders headers = new HttpHeaders(); |
||||
headers.add("Forwarded", "for=192.0.2.60;proto=http;by=203.0.113.43"); |
||||
headers.add("X-Forwarded-Host", "example.com"); |
||||
headers.add("X-Forwarded-Port", "8080"); |
||||
headers.add("X-Forwarded-Proto", "http"); |
||||
headers.add("X-Forwarded-Prefix", "prefix"); |
||||
headers.add("X-Forwarded-Ssl", "on"); |
||||
this.filter.filter(getExchange(headers), this.filterChain).block(Duration.ZERO); |
||||
|
||||
this.filterChain.assertForwardedHeadersRemoved(); |
||||
} |
||||
|
||||
@Test |
||||
public void xForwardedHeaders() throws Exception { |
||||
HttpHeaders headers = new HttpHeaders(); |
||||
headers.add("X-Forwarded-Host", "84.198.58.199"); |
||||
headers.add("X-Forwarded-Port", "443"); |
||||
headers.add("X-Forwarded-Proto", "https"); |
||||
headers.add("foo", "bar"); |
||||
this.filter.filter(getExchange(headers), this.filterChain).block(Duration.ZERO); |
||||
|
||||
assertEquals(new URI("https://84.198.58.199/path"), this.filterChain.uri); |
||||
this.filterChain.assertForwardedHeadersRemoved(); |
||||
} |
||||
|
||||
@Test |
||||
public void forwardedHeader() throws Exception { |
||||
HttpHeaders headers = new HttpHeaders(); |
||||
headers.add("Forwarded", "host=84.198.58.199;proto=https"); |
||||
this.filter.filter(getExchange(headers), this.filterChain).block(Duration.ZERO); |
||||
|
||||
assertEquals(new URI("https://84.198.58.199/path"), this.filterChain.uri); |
||||
this.filterChain.assertForwardedHeadersRemoved(); |
||||
} |
||||
|
||||
@Test |
||||
public void xForwardedPrefix() throws Exception { |
||||
HttpHeaders headers = new HttpHeaders(); |
||||
headers.add("X-Forwarded-Prefix", "/prefix"); |
||||
this.filter.filter(getExchange(headers), this.filterChain).block(Duration.ZERO); |
||||
|
||||
assertEquals(new URI("http://example.com/prefix/path"), this.filterChain.uri); |
||||
assertEquals("/prefix/path", this.filterChain.requestPathValue); |
||||
this.filterChain.assertForwardedHeadersRemoved(); |
||||
} |
||||
|
||||
@Test |
||||
public void xForwardedPrefixTrailingSlash() throws Exception { |
||||
HttpHeaders headers = new HttpHeaders(); |
||||
headers.add("X-Forwarded-Prefix", "/prefix////"); |
||||
this.filter.filter(getExchange(headers), this.filterChain).block(Duration.ZERO); |
||||
|
||||
assertEquals(new URI("http://example.com/prefix/path"), this.filterChain.uri); |
||||
assertEquals("/prefix/path", this.filterChain.requestPathValue); |
||||
this.filterChain.assertForwardedHeadersRemoved(); |
||||
} |
||||
|
||||
private MockServerWebExchange getExchange(HttpHeaders headers) { |
||||
MockServerHttpRequest request = MockServerHttpRequest.get(BASE_URL).headers(headers).build(); |
||||
return MockServerWebExchange.from(request); |
||||
} |
||||
|
||||
|
||||
private static class TestWebFilterChain implements WebFilterChain { |
||||
|
||||
@Nullable |
||||
private HttpHeaders headers; |
||||
|
||||
@Nullable |
||||
private URI uri; |
||||
|
||||
@Nullable String requestPathValue; |
||||
|
||||
|
||||
@Nullable |
||||
public HttpHeaders getHeaders() { |
||||
return this.headers; |
||||
} |
||||
|
||||
@Nullable |
||||
public String getHeader(String name) { |
||||
assertNotNull(this.headers); |
||||
return this.headers.getFirst(name); |
||||
} |
||||
|
||||
public void assertForwardedHeadersRemoved() { |
||||
assertNotNull(this.headers); |
||||
ForwardedHeaderFilter.FORWARDED_HEADER_NAMES |
||||
.forEach(name -> assertFalse(this.headers.containsKey(name))); |
||||
} |
||||
|
||||
@Nullable |
||||
public URI getUri() { |
||||
return this.uri; |
||||
} |
||||
|
||||
@Override |
||||
public Mono<Void> filter(ServerWebExchange exchange) { |
||||
ServerHttpRequest request = exchange.getRequest(); |
||||
this.headers = request.getHeaders(); |
||||
this.uri = request.getURI(); |
||||
this.requestPathValue = request.getPath().value(); |
||||
return Mono.empty(); |
||||
} |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,112 @@
@@ -0,0 +1,112 @@
|
||||
/* |
||||
* Copyright 2002-2018 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.web.server.adapter; |
||||
|
||||
import java.net.URI; |
||||
|
||||
import org.junit.Test; |
||||
|
||||
import org.springframework.http.HttpHeaders; |
||||
import org.springframework.http.server.reactive.ServerHttpRequest; |
||||
import org.springframework.mock.http.server.reactive.test.MockServerHttpRequest; |
||||
|
||||
import static org.junit.Assert.*; |
||||
|
||||
/** |
||||
* Unit tests for {@link ForwardedHeaderTransformer}. |
||||
* @author Rossen Stoyanchev |
||||
*/ |
||||
public class ForwardedHeaderTransformerTests { |
||||
|
||||
private static final String BASE_URL = "http://example.com/path"; |
||||
|
||||
|
||||
private final ForwardedHeaderTransformer requestMutator = new ForwardedHeaderTransformer(); |
||||
|
||||
|
||||
@Test |
||||
public void removeOnly() { |
||||
|
||||
this.requestMutator.setRemoveOnly(true); |
||||
|
||||
HttpHeaders headers = new HttpHeaders(); |
||||
headers.add("Forwarded", "for=192.0.2.60;proto=http;by=203.0.113.43"); |
||||
headers.add("X-Forwarded-Host", "example.com"); |
||||
headers.add("X-Forwarded-Port", "8080"); |
||||
headers.add("X-Forwarded-Proto", "http"); |
||||
headers.add("X-Forwarded-Prefix", "prefix"); |
||||
headers.add("X-Forwarded-Ssl", "on"); |
||||
ServerHttpRequest request = this.requestMutator.apply(getRequest(headers)); |
||||
|
||||
assertForwardedHeadersRemoved(request); |
||||
} |
||||
|
||||
@Test |
||||
public void xForwardedHeaders() throws Exception { |
||||
HttpHeaders headers = new HttpHeaders(); |
||||
headers.add("X-Forwarded-Host", "84.198.58.199"); |
||||
headers.add("X-Forwarded-Port", "443"); |
||||
headers.add("X-Forwarded-Proto", "https"); |
||||
headers.add("foo", "bar"); |
||||
ServerHttpRequest request = this.requestMutator.apply(getRequest(headers)); |
||||
|
||||
assertEquals(new URI("https://84.198.58.199/path"), request.getURI()); |
||||
assertForwardedHeadersRemoved(request); |
||||
} |
||||
|
||||
@Test |
||||
public void forwardedHeader() throws Exception { |
||||
HttpHeaders headers = new HttpHeaders(); |
||||
headers.add("Forwarded", "host=84.198.58.199;proto=https"); |
||||
ServerHttpRequest request = this.requestMutator.apply(getRequest(headers)); |
||||
|
||||
assertEquals(new URI("https://84.198.58.199/path"), request.getURI()); |
||||
assertForwardedHeadersRemoved(request); |
||||
} |
||||
|
||||
@Test |
||||
public void xForwardedPrefix() throws Exception { |
||||
HttpHeaders headers = new HttpHeaders(); |
||||
headers.add("X-Forwarded-Prefix", "/prefix"); |
||||
ServerHttpRequest request = this.requestMutator.apply(getRequest(headers)); |
||||
|
||||
assertEquals(new URI("http://example.com/prefix/path"), request.getURI()); |
||||
assertEquals("/prefix/path", request.getPath().value()); |
||||
assertForwardedHeadersRemoved(request); |
||||
} |
||||
|
||||
@Test |
||||
public void xForwardedPrefixTrailingSlash() throws Exception { |
||||
HttpHeaders headers = new HttpHeaders(); |
||||
headers.add("X-Forwarded-Prefix", "/prefix////"); |
||||
ServerHttpRequest request = this.requestMutator.apply(getRequest(headers)); |
||||
|
||||
assertEquals(new URI("http://example.com/prefix/path"), request.getURI()); |
||||
assertEquals("/prefix/path", request.getPath().value()); |
||||
assertForwardedHeadersRemoved(request); |
||||
} |
||||
|
||||
private MockServerHttpRequest getRequest(HttpHeaders headers) { |
||||
return MockServerHttpRequest.get(BASE_URL).headers(headers).build(); |
||||
} |
||||
|
||||
private void assertForwardedHeadersRemoved(ServerHttpRequest request) { |
||||
ForwardedHeaderTransformer.FORWARDED_HEADER_NAMES |
||||
.forEach(name -> assertFalse(request.getHeaders().containsKey(name))); |
||||
} |
||||
|
||||
} |
||||
Loading…
Reference in new issue