Browse Source

Allows duplicate X-Forwarded-For headers.

Fixes gh-826
Fixes gh-1760
pull/1807/head
Mete Alpaslan Katırcıoğlu 5 years ago committed by spencergibb
parent
commit
1f00f23e0d
No known key found for this signature in database
GPG Key ID: 7788A47380690861
  1. 7
      spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/filter/headers/XForwardedHeadersFilter.java
  2. 19
      spring-cloud-gateway-core/src/test/java/org/springframework/cloud/gateway/filter/headers/XForwardedHeadersFilterTests.java

7
spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/filter/headers/XForwardedHeadersFilter.java

@ -207,12 +207,7 @@ public class XForwardedHeadersFilter implements HttpHeadersFilter, Ordered { @@ -207,12 +207,7 @@ public class XForwardedHeadersFilter implements HttpHeadersFilter, Ordered {
if (isForEnabled() && request.getRemoteAddress() != null
&& request.getRemoteAddress().getAddress() != null) {
String remoteAddr = request.getRemoteAddress().getAddress().getHostAddress();
List<String> xforwarded = original.get(X_FORWARDED_FOR_HEADER);
// prevent duplicates
if (remoteAddr != null
&& (xforwarded == null || !xforwarded.contains(remoteAddr))) {
write(updated, X_FORWARDED_FOR_HEADER, remoteAddr, isForAppend());
}
write(updated, X_FORWARDED_FOR_HEADER, remoteAddr, isForAppend());
}
String proto = request.getURI().getScheme();

19
spring-cloud-gateway-core/src/test/java/org/springframework/cloud/gateway/filter/headers/XForwardedHeadersFilterTests.java

@ -331,4 +331,23 @@ public class XForwardedHeadersFilterTests { @@ -331,4 +331,23 @@ public class XForwardedHeadersFilterTests {
assertThat(headers).isEmpty();
}
@Test
public void allowDuplicateEntriesInXForwardedForHeader() throws Exception {
MockServerHttpRequest request = MockServerHttpRequest
.get("http://localhost:8080/get")
.remoteAddress(
new InetSocketAddress(InetAddress.getByName("10.0.0.1"), 80))
.header(X_FORWARDED_FOR_HEADER, "10.0.0.1")
.build();
XForwardedHeadersFilter filter = new XForwardedHeadersFilter();
HttpHeaders headers = filter.filter(request.getHeaders(),
MockServerWebExchange.from(request));
assertThat(headers).containsKeys(X_FORWARDED_FOR_HEADER);
assertThat(headers.getFirst(X_FORWARDED_FOR_HEADER))
.isEqualTo("10.0.0.1,10.0.0.1");
}
}

Loading…
Cancel
Save