From 96f5c650bf91d6a6efcfcfee377df975ac95032f Mon Sep 17 00:00:00 2001 From: Spencer Gibb Date: Fri, 25 May 2018 11:57:54 +0200 Subject: [PATCH] Adds null check in quoteIfNeeded fixes gh-337 --- .../headers/ForwardedHeadersFilter.java | 2 +- .../headers/ForwardedHeadersFilterTests.java | 23 +++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/filter/headers/ForwardedHeadersFilter.java b/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/filter/headers/ForwardedHeadersFilter.java index b3ffcf46e..f56714127 100644 --- a/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/filter/headers/ForwardedHeadersFilter.java +++ b/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/filter/headers/ForwardedHeadersFilter.java @@ -147,7 +147,7 @@ public class ForwardedHeadersFilter implements HttpHeadersFilter, Ordered { private String quoteIfNeeded(String s) { - if (s.contains(":")) { //TODO: broaded quote + if (s != null && s.contains(":")) { //TODO: broaded quote return "\""+s+"\""; } return s; diff --git a/spring-cloud-gateway-core/src/test/java/org/springframework/cloud/gateway/filter/headers/ForwardedHeadersFilterTests.java b/spring-cloud-gateway-core/src/test/java/org/springframework/cloud/gateway/filter/headers/ForwardedHeadersFilterTests.java index 8a756bc19..21e343fac 100644 --- a/spring-cloud-gateway-core/src/test/java/org/springframework/cloud/gateway/filter/headers/ForwardedHeadersFilterTests.java +++ b/spring-cloud-gateway-core/src/test/java/org/springframework/cloud/gateway/filter/headers/ForwardedHeadersFilterTests.java @@ -67,6 +67,29 @@ public class ForwardedHeadersFilterTests { .containsEntry("for", "\"10.0.0.1:80\""); } + @Test + public void noHostHeader() throws UnknownHostException { + MockServerHttpRequest request = MockServerHttpRequest + .get("http://localhost/get") + .remoteAddress(new InetSocketAddress(InetAddress.getByName("10.0.0.1"), 80)) + .build(); + + ForwardedHeadersFilter filter = new ForwardedHeadersFilter(); + + HttpHeaders headers = filter.filter(request.getHeaders(), MockServerWebExchange.from(request)); + + assertThat(headers.get(FORWARDED_HEADER)).hasSize(1); + + List forwardeds = ForwardedHeadersFilter.parse(headers.get(FORWARDED_HEADER)); + + assertThat(forwardeds).hasSize(1); + Forwarded forwarded = forwardeds.get(0); + + assertThat(forwarded.getValues()) + .containsEntry("proto", "http") + .containsEntry("for", "\"10.0.0.1:80\""); + } + @Test public void forwardedParsedCorrectly() { String[] valid = new String[] {