From 64a74c3bbb148b8726c5f8e8da8887eff3917df7 Mon Sep 17 00:00:00 2001 From: Dmytro Nosan Date: Wed, 27 Feb 2019 15:37:01 +0200 Subject: [PATCH 1/2] Fix NullPointerException with empty X-Forwarded-For header See gh-16046 --- .../ServerWebExchangeTraceableRequest.java | 13 ++- ...erverWebExchangeTraceableRequestTests.java | 101 ++++++++++++++++++ 2 files changed, 111 insertions(+), 3 deletions(-) create mode 100644 spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/web/trace/reactive/ServerWebExchangeTraceableRequestTests.java diff --git a/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/web/trace/reactive/ServerWebExchangeTraceableRequest.java b/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/web/trace/reactive/ServerWebExchangeTraceableRequest.java index a70a9ce2f1..ea1da8a422 100644 --- a/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/web/trace/reactive/ServerWebExchangeTraceableRequest.java +++ b/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/web/trace/reactive/ServerWebExchangeTraceableRequest.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2018 the original author or authors. + * Copyright 2012-2019 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. @@ -16,6 +16,8 @@ package org.springframework.boot.actuate.web.trace.reactive; +import java.net.InetAddress; +import java.net.InetSocketAddress; import java.net.URI; import java.util.LinkedHashMap; import java.util.List; @@ -45,8 +47,7 @@ class ServerWebExchangeTraceableRequest implements TraceableRequest { this.method = request.getMethodValue(); this.headers = request.getHeaders(); this.uri = request.getURI(); - this.remoteAddress = (request.getRemoteAddress() != null) - ? request.getRemoteAddress().getAddress().toString() : null; + this.remoteAddress = getRemoteAddress(request); } @Override @@ -69,4 +70,10 @@ class ServerWebExchangeTraceableRequest implements TraceableRequest { return this.remoteAddress; } + private static String getRemoteAddress(ServerHttpRequest request) { + InetSocketAddress remoteAddress = request.getRemoteAddress(); + InetAddress address = (remoteAddress != null) ? remoteAddress.getAddress() : null; + return (address != null) ? address.toString() : null; + } + } diff --git a/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/web/trace/reactive/ServerWebExchangeTraceableRequestTests.java b/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/web/trace/reactive/ServerWebExchangeTraceableRequestTests.java new file mode 100644 index 0000000000..433675cad8 --- /dev/null +++ b/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/web/trace/reactive/ServerWebExchangeTraceableRequestTests.java @@ -0,0 +1,101 @@ +/* + * Copyright 2012-2019 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.boot.actuate.web.trace.reactive; + +import java.net.InetSocketAddress; +import java.net.URI; +import java.util.Collections; + +import org.junit.Before; +import org.junit.Test; + +import org.springframework.http.HttpHeaders; +import org.springframework.http.server.reactive.ServerHttpRequest; +import org.springframework.web.server.ServerWebExchange; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.entry; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.mock; + +/** + * Tests for {@link ServerWebExchangeTraceableRequest}. + * + * @author Dmytro Nosan + */ +public class ServerWebExchangeTraceableRequestTests { + + private ServerWebExchange exchange; + + private ServerHttpRequest request; + + @Before + public void setUp() { + this.exchange = mock(ServerWebExchange.class); + this.request = mock(ServerHttpRequest.class); + doReturn(this.request).when(this.exchange).getRequest(); + } + + @Test + public void getMethod() { + String method = "POST"; + doReturn(method).when(this.request).getMethodValue(); + ServerWebExchangeTraceableRequest traceableRequest = new ServerWebExchangeTraceableRequest( + this.exchange); + assertThat(traceableRequest.getMethod()).isSameAs(method); + } + + @Test + public void getUri() { + URI uri = URI.create("http://localhost:8080/"); + doReturn(uri).when(this.request).getURI(); + ServerWebExchangeTraceableRequest traceableRequest = new ServerWebExchangeTraceableRequest( + this.exchange); + assertThat(traceableRequest.getUri()).isSameAs(uri); + } + + @Test + public void getHeaders() { + HttpHeaders httpHeaders = new HttpHeaders(); + httpHeaders.add("name", "value"); + doReturn(httpHeaders).when(this.request).getHeaders(); + ServerWebExchangeTraceableRequest traceableRequest = new ServerWebExchangeTraceableRequest( + this.exchange); + assertThat(traceableRequest.getHeaders()) + .containsOnly(entry("name", Collections.singletonList("value"))); + } + + @Test + public void getUnresolvedRemoteAddress() { + InetSocketAddress socketAddress = InetSocketAddress.createUnresolved("", 0); + doReturn(socketAddress).when(this.request).getRemoteAddress(); + ServerWebExchangeTraceableRequest traceableRequest = new ServerWebExchangeTraceableRequest( + this.exchange); + assertThat(traceableRequest.getRemoteAddress()).isNull(); + + } + + @Test + public void getRemoteAddress() { + InetSocketAddress socketAddress = new InetSocketAddress(0); + doReturn(socketAddress).when(this.request).getRemoteAddress(); + ServerWebExchangeTraceableRequest traceableRequest = new ServerWebExchangeTraceableRequest( + this.exchange); + assertThat(traceableRequest.getRemoteAddress()) + .isEqualTo(socketAddress.getAddress().toString()); + } + +} From c224eebef3064e7b2e88e92fc85a4e9f41e7080c Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Thu, 28 Feb 2019 14:56:42 +0100 Subject: [PATCH 2/2] Polish "Fix NullPointerException with empty X-Forwarded-For header" Closes gh-16046 --- .../ServerWebExchangeTraceableRequest.java | 12 ++++++------ ...ServerWebExchangeTraceableRequestTests.java | 18 +++++++++--------- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/web/trace/reactive/ServerWebExchangeTraceableRequest.java b/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/web/trace/reactive/ServerWebExchangeTraceableRequest.java index ea1da8a422..a2187e3c86 100644 --- a/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/web/trace/reactive/ServerWebExchangeTraceableRequest.java +++ b/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/web/trace/reactive/ServerWebExchangeTraceableRequest.java @@ -50,6 +50,12 @@ class ServerWebExchangeTraceableRequest implements TraceableRequest { this.remoteAddress = getRemoteAddress(request); } + private static String getRemoteAddress(ServerHttpRequest request) { + InetSocketAddress remoteAddress = request.getRemoteAddress(); + InetAddress address = (remoteAddress != null) ? remoteAddress.getAddress() : null; + return (address != null) ? address.toString() : null; + } + @Override public String getMethod() { return this.method; @@ -70,10 +76,4 @@ class ServerWebExchangeTraceableRequest implements TraceableRequest { return this.remoteAddress; } - private static String getRemoteAddress(ServerHttpRequest request) { - InetSocketAddress remoteAddress = request.getRemoteAddress(); - InetAddress address = (remoteAddress != null) ? remoteAddress.getAddress() : null; - return (address != null) ? address.toString() : null; - } - } diff --git a/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/web/trace/reactive/ServerWebExchangeTraceableRequestTests.java b/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/web/trace/reactive/ServerWebExchangeTraceableRequestTests.java index 433675cad8..2a50106c60 100644 --- a/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/web/trace/reactive/ServerWebExchangeTraceableRequestTests.java +++ b/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/web/trace/reactive/ServerWebExchangeTraceableRequestTests.java @@ -28,7 +28,7 @@ import org.springframework.web.server.ServerWebExchange; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.entry; -import static org.mockito.Mockito.doReturn; +import static org.mockito.BDDMockito.given; import static org.mockito.Mockito.mock; /** @@ -46,13 +46,13 @@ public class ServerWebExchangeTraceableRequestTests { public void setUp() { this.exchange = mock(ServerWebExchange.class); this.request = mock(ServerHttpRequest.class); - doReturn(this.request).when(this.exchange).getRequest(); + given(this.exchange.getRequest()).willReturn(this.request); } @Test public void getMethod() { String method = "POST"; - doReturn(method).when(this.request).getMethodValue(); + given(this.request.getMethodValue()).willReturn(method); ServerWebExchangeTraceableRequest traceableRequest = new ServerWebExchangeTraceableRequest( this.exchange); assertThat(traceableRequest.getMethod()).isSameAs(method); @@ -61,7 +61,7 @@ public class ServerWebExchangeTraceableRequestTests { @Test public void getUri() { URI uri = URI.create("http://localhost:8080/"); - doReturn(uri).when(this.request).getURI(); + given(this.request.getURI()).willReturn(uri); ServerWebExchangeTraceableRequest traceableRequest = new ServerWebExchangeTraceableRequest( this.exchange); assertThat(traceableRequest.getUri()).isSameAs(uri); @@ -71,7 +71,7 @@ public class ServerWebExchangeTraceableRequestTests { public void getHeaders() { HttpHeaders httpHeaders = new HttpHeaders(); httpHeaders.add("name", "value"); - doReturn(httpHeaders).when(this.request).getHeaders(); + given(this.request.getHeaders()).willReturn(httpHeaders); ServerWebExchangeTraceableRequest traceableRequest = new ServerWebExchangeTraceableRequest( this.exchange); assertThat(traceableRequest.getHeaders()) @@ -80,18 +80,18 @@ public class ServerWebExchangeTraceableRequestTests { @Test public void getUnresolvedRemoteAddress() { - InetSocketAddress socketAddress = InetSocketAddress.createUnresolved("", 0); - doReturn(socketAddress).when(this.request).getRemoteAddress(); + InetSocketAddress socketAddress = InetSocketAddress + .createUnresolved("unresolved.example.com", 8080); + given(this.request.getRemoteAddress()).willReturn(socketAddress); ServerWebExchangeTraceableRequest traceableRequest = new ServerWebExchangeTraceableRequest( this.exchange); assertThat(traceableRequest.getRemoteAddress()).isNull(); - } @Test public void getRemoteAddress() { InetSocketAddress socketAddress = new InetSocketAddress(0); - doReturn(socketAddress).when(this.request).getRemoteAddress(); + given(this.request.getRemoteAddress()).willReturn(socketAddress); ServerWebExchangeTraceableRequest traceableRequest = new ServerWebExchangeTraceableRequest( this.exchange); assertThat(traceableRequest.getRemoteAddress())