Skip to content

Commit 2b42687

Browse files
dsbeckerjzheaux
authored andcommitted
Use InetSocketAddress#getHostString
Sometimes InetSocketAddress#getAddress#getHostAddress retuns null. In that case, call InetSocketAddress#getHostString instead. There is no performance loss since IpAddressMatcher#matches attemptsi to re-parse and resolve the address anyway. Closes gh-11888
1 parent d5354db commit 2b42687

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

web/src/main/java/org/springframework/security/web/server/util/matcher/IpAddressServerWebExchangeMatcher.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public IpAddressServerWebExchangeMatcher(String ipAddress) {
4848
public Mono<MatchResult> matches(ServerWebExchange exchange) {
4949
// @formatter:off
5050
return Mono.justOrEmpty(exchange.getRequest().getRemoteAddress())
51-
.map((remoteAddress) -> remoteAddress.getAddress().getHostAddress())
51+
.map((remoteAddress) -> remoteAddress.isUnresolved() ? remoteAddress.getHostString() : remoteAddress.getAddress().getHostAddress())
5252
.map(this.ipAddressMatcher::matches)
5353
.flatMap((matches) -> matches ? MatchResult.match() : MatchResult.notMatch())
5454
.switchIfEmpty(MatchResult.notMatch());

web/src/test/java/org/springframework/security/web/server/util/matcher/IpAddressServerWebExchangeMatcherTests.java

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,22 @@ public void matchesWhenZeroMaskAndAnythingThenTrue() throws UnknownHostException
101101
assertThat(matcher.matches(exchange("192.168.0.159")).block().isMatch()).isTrue();
102102
}
103103

104+
@Test
105+
public void matchesWhenIpv4UnresolvedThenTrue() throws UnknownHostException {
106+
ServerWebExchange ipv4Exchange = exchange("192.168.1.104", true);
107+
ServerWebExchangeMatcher.MatchResult matches = new IpAddressServerWebExchangeMatcher("192.168.1.104")
108+
.matches(ipv4Exchange).block();
109+
assertThat(matches.isMatch()).isTrue();
110+
}
111+
112+
@Test
113+
public void matchesWhenIpv6UnresolvedThenTrue() throws UnknownHostException {
114+
ServerWebExchange ipv6Exchange = exchange("fe80::21f:5bff:fe33:bd68", true);
115+
ServerWebExchangeMatcher.MatchResult matches = new IpAddressServerWebExchangeMatcher("fe80::21f:5bff:fe33:bd68")
116+
.matches(ipv6Exchange).block();
117+
assertThat(matches.isMatch()).isTrue();
118+
}
119+
104120
@Test
105121
public void constructorWhenIpv4AddressMaskTooLongThenIllegalArgumentException() {
106122
String ipv4AddressWithTooLongMask = "192.168.1.104/33";
@@ -119,8 +135,14 @@ public void constructorWhenIpv6AddressMaskTooLongThenIllegalArgumentException()
119135
}
120136

121137
private static ServerWebExchange exchange(String ipAddress) throws UnknownHostException {
138+
return exchange(ipAddress, false);
139+
}
140+
141+
private static ServerWebExchange exchange(String ipAddress, boolean unresolved) throws UnknownHostException {
122142
return MockServerWebExchange.builder(MockServerHttpRequest.get("/")
123-
.remoteAddress(new InetSocketAddress(InetAddress.getByName(ipAddress), 8080))).build();
143+
.remoteAddress(unresolved ? InetSocketAddress.createUnresolved(ipAddress, 8080)
144+
: new InetSocketAddress(InetAddress.getByName(ipAddress), 8080)))
145+
.build();
124146
}
125147

126148
}

0 commit comments

Comments
 (0)