-
Notifications
You must be signed in to change notification settings - Fork 6.1k
Support IP whitelist for Spring Security Webflux #10007
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
...ingframework/security/web/server/authorization/IpAddressReactiveAuthorizationManager.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/* | ||
* Copyright 2002-2021 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 | ||
* | ||
* https://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.security.web.server.authorization; | ||
|
||
import reactor.core.publisher.Mono; | ||
|
||
import org.springframework.security.authorization.AuthorizationDecision; | ||
import org.springframework.security.authorization.ReactiveAuthorizationManager; | ||
import org.springframework.security.core.Authentication; | ||
import org.springframework.security.web.server.util.matcher.IpAddressServerWebExchangeMatcher; | ||
import org.springframework.util.Assert; | ||
|
||
/** | ||
* A {@link ReactiveAuthorizationManager}, that determines if the current request contains | ||
* the specified address or range of addresses | ||
* | ||
* @author Guirong Hu | ||
* @since 5.7 | ||
*/ | ||
public final class IpAddressReactiveAuthorizationManager implements ReactiveAuthorizationManager<AuthorizationContext> { | ||
|
||
private final IpAddressServerWebExchangeMatcher ipAddressExchangeMatcher; | ||
|
||
IpAddressReactiveAuthorizationManager(String ipAddress) { | ||
this.ipAddressExchangeMatcher = new IpAddressServerWebExchangeMatcher(ipAddress); | ||
} | ||
|
||
@Override | ||
public Mono<AuthorizationDecision> check(Mono<Authentication> authentication, AuthorizationContext context) { | ||
return Mono.just(context.getExchange()).flatMap(this.ipAddressExchangeMatcher::matches) | ||
.map((matchResult) -> new AuthorizationDecision(matchResult.isMatch())); | ||
} | ||
|
||
/** | ||
* Creates an instance of {@link IpAddressReactiveAuthorizationManager} with the | ||
* provided IP address. | ||
* @param ipAddress the address or range of addresses from which the request must | ||
* @return the new instance | ||
*/ | ||
public static IpAddressReactiveAuthorizationManager hasIpAddress(String ipAddress) { | ||
Assert.notNull(ipAddress, "This IP address is required; it must not be null"); | ||
return new IpAddressReactiveAuthorizationManager(ipAddress); | ||
} | ||
|
||
} |
63 changes: 63 additions & 0 deletions
63
...g/springframework/security/web/server/util/matcher/IpAddressServerWebExchangeMatcher.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* | ||
* Copyright 2002-2021 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 | ||
* | ||
* https://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.security.web.server.util.matcher; | ||
|
||
import reactor.core.publisher.Mono; | ||
|
||
import org.springframework.security.web.util.matcher.IpAddressMatcher; | ||
import org.springframework.util.Assert; | ||
import org.springframework.web.server.ServerWebExchange; | ||
|
||
/** | ||
* Matches a request based on IP Address or subnet mask matching against the remote | ||
* address. | ||
* | ||
* @author Guirong Hu | ||
* @since 5.7 | ||
*/ | ||
public class IpAddressServerWebExchangeMatcher implements ServerWebExchangeMatcher { | ||
|
||
private final IpAddressMatcher ipAddressMatcher; | ||
|
||
/** | ||
* Takes a specific IP address or a range specified using the IP/Netmask (e.g. | ||
* 192.168.1.0/24 or 202.24.0.0/14). | ||
* @param ipAddress the address or range of addresses from which the request must | ||
* come. | ||
*/ | ||
public IpAddressServerWebExchangeMatcher(String ipAddress) { | ||
Assert.hasText(ipAddress, "IP address cannot be empty"); | ||
this.ipAddressMatcher = new IpAddressMatcher(ipAddress); | ||
} | ||
|
||
@Override | ||
public Mono<MatchResult> matches(ServerWebExchange exchange) { | ||
// @formatter:off | ||
return Mono.justOrEmpty(exchange.getRequest().getRemoteAddress()) | ||
.map((remoteAddress) -> remoteAddress.getAddress().getHostAddress()) | ||
.map(this.ipAddressMatcher::matches) | ||
.flatMap((matches) -> matches ? MatchResult.match() : MatchResult.notMatch()) | ||
.switchIfEmpty(MatchResult.notMatch()); | ||
terminux marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// @formatter:on | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "IpAddressServerWebExchangeMatcher{ipAddressMatcher=" + this.ipAddressMatcher + '}'; | ||
} | ||
|
||
} |
75 changes: 75 additions & 0 deletions
75
...amework/security/web/server/authorization/IpAddressReactiveAuthorizationManagerTests.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/* | ||
* Copyright 2002-2021 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 | ||
* | ||
* https://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.security.web.server.authorization; | ||
|
||
import java.net.InetAddress; | ||
import java.net.InetSocketAddress; | ||
import java.net.UnknownHostException; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import org.springframework.mock.http.server.reactive.MockServerHttpRequest; | ||
import org.springframework.mock.web.server.MockServerWebExchange; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
/** | ||
* Tests for {@link IpAddressReactiveAuthorizationManager} | ||
* | ||
* @author Guirong Hu | ||
*/ | ||
public class IpAddressReactiveAuthorizationManagerTests { | ||
terminux marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
@Test | ||
public void checkWhenHasIpv6AddressThenReturnTrue() throws UnknownHostException { | ||
IpAddressReactiveAuthorizationManager v6manager = IpAddressReactiveAuthorizationManager | ||
.hasIpAddress("fe80::21f:5bff:fe33:bd68"); | ||
boolean granted = v6manager.check(null, context("fe80::21f:5bff:fe33:bd68")).block().isGranted(); | ||
assertThat(granted).isTrue(); | ||
} | ||
|
||
@Test | ||
public void checkWhenHasIpv6AddressThenReturnFalse() throws UnknownHostException { | ||
IpAddressReactiveAuthorizationManager v6manager = IpAddressReactiveAuthorizationManager | ||
.hasIpAddress("fe80::21f:5bff:fe33:bd68"); | ||
boolean granted = v6manager.check(null, context("fe80::1c9a:7cfd:29a8:a91e")).block().isGranted(); | ||
assertThat(granted).isFalse(); | ||
} | ||
|
||
@Test | ||
public void checkWhenHasIpv4AddressThenReturnTrue() throws UnknownHostException { | ||
IpAddressReactiveAuthorizationManager v4manager = IpAddressReactiveAuthorizationManager | ||
.hasIpAddress("192.168.1.104"); | ||
boolean granted = v4manager.check(null, context("192.168.1.104")).block().isGranted(); | ||
assertThat(granted).isTrue(); | ||
} | ||
|
||
@Test | ||
public void checkWhenHasIpv4AddressThenReturnFalse() throws UnknownHostException { | ||
IpAddressReactiveAuthorizationManager v4manager = IpAddressReactiveAuthorizationManager | ||
.hasIpAddress("192.168.1.104"); | ||
boolean granted = v4manager.check(null, context("192.168.100.15")).block().isGranted(); | ||
assertThat(granted).isFalse(); | ||
} | ||
|
||
private static AuthorizationContext context(String ipAddress) throws UnknownHostException { | ||
MockServerWebExchange exchange = MockServerWebExchange.builder(MockServerHttpRequest.get("/") | ||
.remoteAddress(new InetSocketAddress(InetAddress.getByName(ipAddress), 8080))).build(); | ||
return new AuthorizationContext(exchange); | ||
} | ||
|
||
} |
126 changes: 126 additions & 0 deletions
126
...ingframework/security/web/server/util/matcher/IpAddressServerWebExchangeMatcherTests.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
/* | ||
* Copyright 2002-2021 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 | ||
* | ||
* https://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.security.web.server.util.matcher; | ||
|
||
import java.net.InetAddress; | ||
import java.net.InetSocketAddress; | ||
import java.net.UnknownHostException; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
|
||
import org.springframework.mock.http.server.reactive.MockServerHttpRequest; | ||
import org.springframework.mock.web.server.MockServerWebExchange; | ||
import org.springframework.web.server.ServerWebExchange; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; | ||
|
||
/** | ||
* Tests for {@link IpAddressServerWebExchangeMatcher} | ||
* | ||
* @author Guirong Hu | ||
*/ | ||
@ExtendWith(MockitoExtension.class) | ||
public class IpAddressServerWebExchangeMatcherTests { | ||
|
||
@Test | ||
public void matchesWhenIpv6RangeAndIpv6AddressThenTrue() throws UnknownHostException { | ||
ServerWebExchange ipv6Exchange = exchange("fe80::21f:5bff:fe33:bd68"); | ||
ServerWebExchangeMatcher.MatchResult matches = new IpAddressServerWebExchangeMatcher("fe80::21f:5bff:fe33:bd68") | ||
.matches(ipv6Exchange).block(); | ||
assertThat(matches.isMatch()).isTrue(); | ||
} | ||
|
||
@Test | ||
public void matchesWhenIpv6RangeAndIpv4AddressThenFalse() throws UnknownHostException { | ||
ServerWebExchange ipv4Exchange = exchange("192.168.1.104"); | ||
ServerWebExchangeMatcher.MatchResult matches = new IpAddressServerWebExchangeMatcher("fe80::21f:5bff:fe33:bd68") | ||
.matches(ipv4Exchange).block(); | ||
assertThat(matches.isMatch()).isFalse(); | ||
} | ||
|
||
@Test | ||
public void matchesWhenIpv4RangeAndIpv4AddressThenTrue() throws UnknownHostException { | ||
ServerWebExchange ipv4Exchange = exchange("192.168.1.104"); | ||
ServerWebExchangeMatcher.MatchResult matches = new IpAddressServerWebExchangeMatcher("192.168.1.104") | ||
.matches(ipv4Exchange).block(); | ||
assertThat(matches.isMatch()).isTrue(); | ||
} | ||
|
||
@Test | ||
public void matchesWhenIpv4SubnetAndIpv4AddressThenTrue() throws UnknownHostException { | ||
ServerWebExchange ipv4Exchange = exchange("192.168.1.104"); | ||
IpAddressServerWebExchangeMatcher matcher = new IpAddressServerWebExchangeMatcher("192.168.1.0/24"); | ||
assertThat(matcher.matches(ipv4Exchange).block().isMatch()).isTrue(); | ||
} | ||
|
||
@Test | ||
public void matchesWhenIpv4SubnetAndIpv4AddressThenFalse() throws UnknownHostException { | ||
ServerWebExchange ipv4Exchange = exchange("192.168.1.104"); | ||
IpAddressServerWebExchangeMatcher matcher = new IpAddressServerWebExchangeMatcher("192.168.1.128/25"); | ||
assertThat(matcher.matches(ipv4Exchange).block().isMatch()).isFalse(); | ||
} | ||
|
||
@Test | ||
public void matchesWhenIpv6SubnetAndIpv6AddressThenTrue() throws UnknownHostException { | ||
ServerWebExchange ipv6Exchange = exchange("2001:DB8:0:FFFF:FFFF:FFFF:FFFF:FFFF"); | ||
IpAddressServerWebExchangeMatcher matcher = new IpAddressServerWebExchangeMatcher("2001:DB8::/48"); | ||
assertThat(matcher.matches(ipv6Exchange).block().isMatch()).isTrue(); | ||
} | ||
|
||
@Test | ||
public void matchesWhenIpv6SubnetAndIpv6AddressThenFalse() throws UnknownHostException { | ||
ServerWebExchange ipv6Exchange = exchange("2001:DB8:1:0:0:0:0:0"); | ||
IpAddressServerWebExchangeMatcher matcher = new IpAddressServerWebExchangeMatcher("2001:DB8::/48"); | ||
assertThat(matcher.matches(ipv6Exchange).block().isMatch()).isFalse(); | ||
} | ||
|
||
@Test | ||
public void matchesWhenZeroMaskAndAnythingThenTrue() throws UnknownHostException { | ||
IpAddressServerWebExchangeMatcher matcher = new IpAddressServerWebExchangeMatcher("0.0.0.0/0"); | ||
assertThat(matcher.matches(exchange("123.4.5.6")).block().isMatch()).isTrue(); | ||
assertThat(matcher.matches(exchange("192.168.0.159")).block().isMatch()).isTrue(); | ||
matcher = new IpAddressServerWebExchangeMatcher("192.168.0.159/0"); | ||
assertThat(matcher.matches(exchange("123.4.5.6")).block().isMatch()).isTrue(); | ||
assertThat(matcher.matches(exchange("192.168.0.159")).block().isMatch()).isTrue(); | ||
} | ||
|
||
@Test | ||
public void constructorWhenIpv4AddressMaskTooLongThenIllegalArgumentException() { | ||
String ipv4AddressWithTooLongMask = "192.168.1.104/33"; | ||
assertThatIllegalArgumentException() | ||
.isThrownBy(() -> new IpAddressServerWebExchangeMatcher(ipv4AddressWithTooLongMask)) | ||
.withMessage(String.format("IP address %s is too short for bitmask of length %d", "192.168.1.104", 33)); | ||
} | ||
|
||
@Test | ||
public void constructorWhenIpv6AddressMaskTooLongThenIllegalArgumentException() { | ||
String ipv6AddressWithTooLongMask = "fe80::21f:5bff:fe33:bd68/129"; | ||
assertThatIllegalArgumentException() | ||
.isThrownBy(() -> new IpAddressServerWebExchangeMatcher(ipv6AddressWithTooLongMask)) | ||
.withMessage(String.format("IP address %s is too short for bitmask of length %d", | ||
"fe80::21f:5bff:fe33:bd68", 129)); | ||
} | ||
|
||
private static ServerWebExchange exchange(String ipAddress) throws UnknownHostException { | ||
return MockServerWebExchange.builder(MockServerHttpRequest.get("/") | ||
.remoteAddress(new InetSocketAddress(InetAddress.getByName(ipAddress), 8080))).build(); | ||
} | ||
|
||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.