Skip to content

Commit 64a74c3

Browse files
nosansnicoll
authored andcommitted
Fix NullPointerException with empty X-Forwarded-For header
See spring-projectsgh-16046
1 parent 0c1efe3 commit 64a74c3

File tree

2 files changed

+111
-3
lines changed

2 files changed

+111
-3
lines changed

spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/web/trace/reactive/ServerWebExchangeTraceableRequest.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2018 the original author or authors.
2+
* Copyright 2012-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,6 +16,8 @@
1616

1717
package org.springframework.boot.actuate.web.trace.reactive;
1818

19+
import java.net.InetAddress;
20+
import java.net.InetSocketAddress;
1921
import java.net.URI;
2022
import java.util.LinkedHashMap;
2123
import java.util.List;
@@ -45,8 +47,7 @@ class ServerWebExchangeTraceableRequest implements TraceableRequest {
4547
this.method = request.getMethodValue();
4648
this.headers = request.getHeaders();
4749
this.uri = request.getURI();
48-
this.remoteAddress = (request.getRemoteAddress() != null)
49-
? request.getRemoteAddress().getAddress().toString() : null;
50+
this.remoteAddress = getRemoteAddress(request);
5051
}
5152

5253
@Override
@@ -69,4 +70,10 @@ public String getRemoteAddress() {
6970
return this.remoteAddress;
7071
}
7172

73+
private static String getRemoteAddress(ServerHttpRequest request) {
74+
InetSocketAddress remoteAddress = request.getRemoteAddress();
75+
InetAddress address = (remoteAddress != null) ? remoteAddress.getAddress() : null;
76+
return (address != null) ? address.toString() : null;
77+
}
78+
7279
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
/*
2+
* Copyright 2012-2019 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.boot.actuate.web.trace.reactive;
17+
18+
import java.net.InetSocketAddress;
19+
import java.net.URI;
20+
import java.util.Collections;
21+
22+
import org.junit.Before;
23+
import org.junit.Test;
24+
25+
import org.springframework.http.HttpHeaders;
26+
import org.springframework.http.server.reactive.ServerHttpRequest;
27+
import org.springframework.web.server.ServerWebExchange;
28+
29+
import static org.assertj.core.api.Assertions.assertThat;
30+
import static org.assertj.core.api.Assertions.entry;
31+
import static org.mockito.Mockito.doReturn;
32+
import static org.mockito.Mockito.mock;
33+
34+
/**
35+
* Tests for {@link ServerWebExchangeTraceableRequest}.
36+
*
37+
* @author Dmytro Nosan
38+
*/
39+
public class ServerWebExchangeTraceableRequestTests {
40+
41+
private ServerWebExchange exchange;
42+
43+
private ServerHttpRequest request;
44+
45+
@Before
46+
public void setUp() {
47+
this.exchange = mock(ServerWebExchange.class);
48+
this.request = mock(ServerHttpRequest.class);
49+
doReturn(this.request).when(this.exchange).getRequest();
50+
}
51+
52+
@Test
53+
public void getMethod() {
54+
String method = "POST";
55+
doReturn(method).when(this.request).getMethodValue();
56+
ServerWebExchangeTraceableRequest traceableRequest = new ServerWebExchangeTraceableRequest(
57+
this.exchange);
58+
assertThat(traceableRequest.getMethod()).isSameAs(method);
59+
}
60+
61+
@Test
62+
public void getUri() {
63+
URI uri = URI.create("http://localhost:8080/");
64+
doReturn(uri).when(this.request).getURI();
65+
ServerWebExchangeTraceableRequest traceableRequest = new ServerWebExchangeTraceableRequest(
66+
this.exchange);
67+
assertThat(traceableRequest.getUri()).isSameAs(uri);
68+
}
69+
70+
@Test
71+
public void getHeaders() {
72+
HttpHeaders httpHeaders = new HttpHeaders();
73+
httpHeaders.add("name", "value");
74+
doReturn(httpHeaders).when(this.request).getHeaders();
75+
ServerWebExchangeTraceableRequest traceableRequest = new ServerWebExchangeTraceableRequest(
76+
this.exchange);
77+
assertThat(traceableRequest.getHeaders())
78+
.containsOnly(entry("name", Collections.singletonList("value")));
79+
}
80+
81+
@Test
82+
public void getUnresolvedRemoteAddress() {
83+
InetSocketAddress socketAddress = InetSocketAddress.createUnresolved("", 0);
84+
doReturn(socketAddress).when(this.request).getRemoteAddress();
85+
ServerWebExchangeTraceableRequest traceableRequest = new ServerWebExchangeTraceableRequest(
86+
this.exchange);
87+
assertThat(traceableRequest.getRemoteAddress()).isNull();
88+
89+
}
90+
91+
@Test
92+
public void getRemoteAddress() {
93+
InetSocketAddress socketAddress = new InetSocketAddress(0);
94+
doReturn(socketAddress).when(this.request).getRemoteAddress();
95+
ServerWebExchangeTraceableRequest traceableRequest = new ServerWebExchangeTraceableRequest(
96+
this.exchange);
97+
assertThat(traceableRequest.getRemoteAddress())
98+
.isEqualTo(socketAddress.getAddress().toString());
99+
}
100+
101+
}

0 commit comments

Comments
 (0)