@ -41,12 +41,15 @@ import io.vertx.core.Vertx;
import io.vertx.core.http.HttpClient ;
import io.vertx.core.http.HttpClient ;
import io.vertx.core.http.HttpClientOptions ;
import io.vertx.core.http.HttpClientOptions ;
import io.vertx.core.http.HttpMethod ;
import io.vertx.core.http.HttpMethod ;
import io.vertx.core.net.HostAndPort ;
import io.vertx.junit5.VertxExtension ;
import io.vertx.junit5.VertxExtension ;
import io.vertx.junit5.VertxTestContext ;
import io.vertx.junit5.VertxTestContext ;
import org.junit.jupiter.api.AfterEach ;
import org.junit.jupiter.api.AfterEach ;
import org.junit.jupiter.api.BeforeEach ;
import org.junit.jupiter.api.BeforeEach ;
import org.junit.jupiter.api.Test ;
import org.junit.jupiter.api.Test ;
import org.junit.jupiter.api.extension.ExtendWith ;
import org.junit.jupiter.api.extension.ExtendWith ;
import org.junit.jupiter.params.ParameterizedTest ;
import org.junit.jupiter.params.provider.ValueSource ;
@ExtendWith ( VertxExtension . class )
@ExtendWith ( VertxExtension . class )
public class WebSocketHostAllowlistTest {
public class WebSocketHostAllowlistTest {
@ -104,49 +107,60 @@ public class WebSocketHostAllowlistTest {
@Test
@Test
public void websocketRequestWithDefaultHeaderAndDefaultConfigIsAccepted ( ) {
public void websocketRequestWithDefaultHeaderAndDefaultConfigIsAccepted ( ) {
boolean result = websocketService . hasAllowedHostnameHeader ( Optional . of ( "localhost:50012" ) ) ;
final Optional < String > host =
Optional . of ( HostAndPort . parseAuthority ( "localhost" , 50012 ) ) . map ( HostAndPort : : host ) ;
boolean result = websocketService . checkHostInAllowlist ( host ) ;
assertThat ( result ) . isTrue ( ) ;
assertThat ( result ) . isTrue ( ) ;
}
}
@Test
@Test
public void httpRequestWithDefaultHeaderAndDefaultConfigIsAccepted ( ) throws InterruptedException {
public void httpRequestWithDefaultHeaderAndDefaultConfigIsAccepted ( ) throws Throwable {
doHttpRequestAndVerify ( testContext , "localhost:50012" , 400 ) ;
doHttpRequestAndVerify ( testContext , "localhost:50012" , 400 ) ;
}
}
@Test
@Test
public void websocketRequestWithEmptyHeaderAndDefaultConfigIsRejected ( ) {
public void websocketRequestWithEmptyHeaderAndDefaultConfigIsRejected ( ) {
assertThat ( websocketService . hasAllowedHostnameHeader ( Optional . of ( "" ) ) ) . isFalse ( ) ;
final Optional < String > host =
Optional . ofNullable ( HostAndPort . parseAuthority ( "" , 50012 ) ) . map ( HostAndPort : : host ) ;
boolean result = websocketService . checkHostInAllowlist ( host ) ;
assertThat ( result ) . isFalse ( ) ;
}
}
@Test
@Test
public void httpRequestWithEmptyHeaderAndDefaultConfigIsRejected ( ) throws InterruptedException {
public void httpRequestWithEmptyHeaderAndDefaultConfigIsRejected ( ) throws Throwable {
doHttpRequestAndVerify ( testContext , "" , 403 ) ;
doHttpRequestAndVerify ( testContext , "" , 403 ) ;
}
}
@Test
@ParameterizedTest
public void websocketRequestWithAnyHostnameAndWildcardConfigIsAccepted ( ) {
@ValueSource ( strings = { "ally" , "foe" } )
public void websocketRequestWithAnyHostnameAndWildcardConfigIsAccepted ( final String hostname ) {
webSocketConfiguration . setHostsAllowlist ( Collections . singletonList ( "*" ) ) ;
webSocketConfiguration . setHostsAllowlist ( Collections . singletonList ( "*" ) ) ;
assertThat ( websocketService . hasAllowedHostnameHeader ( Optional . of ( "ally" ) ) ) . isTrue ( ) ;
assertThat ( websocketService . hasAllowedHostnameHeader ( Optional . of ( "foe" ) ) ) . isTrue ( ) ;
final Optional < String > host =
Optional . ofNullable ( HostAndPort . parseAuthority ( hostname , - 1 ) ) . map ( HostAndPort : : host ) ;
boolean result = websocketService . checkHostInAllowlist ( host ) ;
assertThat ( result ) . isTrue ( ) ;
}
}
@Test
@Test
public void httpRequestWithAnyHostnameAndWildcardConfigIsAccepted ( ) throws InterruptedException {
public void httpRequestWithAnyHostnameAndWildcardConfigIsAccepted ( ) throws Throwable {
webSocketConfiguration . setHostsAllowlist ( Collections . singletonList ( "*" ) ) ;
webSocketConfiguration . setHostsAllowlist ( Collections . singletonList ( "*" ) ) ;
doHttpRequestAndVerify ( testContext , "ally" , 400 ) ;
doHttpRequestAndVerify ( testContext , "ally" , 400 ) ;
doHttpRequestAndVerify ( testContext , "foe" , 400 ) ;
doHttpRequestAndVerify ( testContext , "foe" , 400 ) ;
}
}
@Test
@ParameterizedTest
public void websocketRequestWithAllowedHostIsAccepted ( ) {
@ValueSource ( strings = { "ally" , "ally:12345" , "friend" } )
public void websocketRequestWithAllowedHostIsAccepted ( final String hostname ) {
webSocketConfiguration . setHostsAllowlist ( hostsAllowlist ) ;
webSocketConfiguration . setHostsAllowlist ( hostsAllowlist ) ;
assertThat ( websocketService . hasAllowedHostnameHeader ( Optional . of ( "ally" ) ) ) . isTrue ( ) ;
final Optional < String > host =
assertThat ( websocketService . hasAllowedHostnameHeader ( Optional . of ( "ally:12345" ) ) ) . isTrue ( ) ;
Optional . ofNullable ( HostAndPort . parseAuthority ( hostname , - 1 ) ) . map ( HostAndPort : : host ) ;
assertThat ( websocketService . hasAllowedHostnameHeader ( Optional . of ( "friend" ) ) ) . isTrue ( ) ;
boolean result = websocketService . checkHostInAllowlist ( host ) ;
assertThat ( result ) . isTrue ( ) ;
}
}
@Test
@Test
public void httpRequestWithAllowedHostIsAccepted ( ) throws InterruptedException {
public void httpRequestWithAllowedHostIsAccepted ( ) throws Throwable {
webSocketConfiguration . setHostsAllowlist ( hostsAllowlist ) ;
webSocketConfiguration . setHostsAllowlist ( hostsAllowlist ) ;
doHttpRequestAndVerify ( testContext , "ally" , 400 ) ;
doHttpRequestAndVerify ( testContext , "ally" , 400 ) ;
doHttpRequestAndVerify ( testContext , "ally:12345" , 400 ) ;
doHttpRequestAndVerify ( testContext , "ally:12345" , 400 ) ;
@ -156,37 +170,40 @@ public class WebSocketHostAllowlistTest {
@Test
@Test
public void websocketRequestWithUnknownHostIsRejected ( ) {
public void websocketRequestWithUnknownHostIsRejected ( ) {
webSocketConfiguration . setHostsAllowlist ( hostsAllowlist ) ;
webSocketConfiguration . setHostsAllowlist ( hostsAllowlist ) ;
assertThat ( websocketService . hasAllowedHostnameHeader ( Optional . of ( "foe" ) ) ) . isFalse ( ) ;
final Optional < String > host =
Optional . ofNullable ( HostAndPort . parseAuthority ( "foe" , - 1 ) ) . map ( HostAndPort : : host ) ;
assertThat ( websocketService . checkHostInAllowlist ( host ) ) . isFalse ( ) ;
}
}
@Test
@Test
public void httpRequestWithUnknownHostIsRejected ( ) throws InterruptedException {
public void httpRequestWithUnknownHostIsRejected ( ) throws Throwable {
webSocketConfiguration . setHostsAllowlist ( hostsAllowlist ) ;
webSocketConfiguration . setHostsAllowlist ( hostsAllowlist ) ;
doHttpRequestAndVerify ( testContext , "foe" , 403 ) ;
doHttpRequestAndVerify ( testContext , "foe" , 403 ) ;
}
}
@Test
@ParameterizedTest
public void websocketRequestWithMalformedHostIsRejected ( ) {
@ValueSource ( strings = { "ally:friend" , "ally:123456" , "ally:friend:1234" } )
public void websocketRequestWithMalformedHostIsRejected ( final String hostname ) {
webSocketConfiguration . setAuthenticationEnabled ( false ) ;
webSocketConfiguration . setAuthenticationEnabled ( false ) ;
webSocketConfiguration . setHostsAllowlist ( hostsAllowlist ) ;
webSocketConfiguration . setHostsAllowlist ( hostsAllowlist ) ;
assertThat ( websocketService . hasAllowedHostnameHeader ( Optional . of ( "ally:friend" ) ) ) . isFalse ( ) ;
final Optional < String > host =
assertThat ( websocketService . hasAllowedHostnameHeader ( Optional . of ( "ally:123456" ) ) ) . isFalse ( ) ;
Optional . ofNullable ( HostAndPort . parseAuthority ( hostname , - 1 ) ) . map ( HostAndPort : : host ) ;
assertThat ( websocketService . hasAllowedHostnameHeader ( Optional . of ( "ally:friend:1234" ) ) )
final boolean result = websocketService . checkHostInAllowlist ( host ) ;
. isFalse ( ) ;
assertThat ( result ) . isFalse ( ) ;
}
}
@Test
@Test
public void httpRequestWithMalformedHostIsRejected ( ) throws InterruptedException {
public void httpRequestWithMalformedHostIsRejected ( ) throws Throwable {
webSocketConfiguration . setAuthenticationEnabled ( false ) ;
webSocketConfiguration . setAuthenticationEnabled ( false ) ;
webSocketConfiguration . setHostsAllowlist ( hostsAllowlist ) ;
webSocketConfiguration . setHostsAllowlist ( hostsAllowlist ) ;
doHttpRequestAndVerify ( testContext , "ally:friend" , 400 ) ;
doHttpRequestAndVerify ( testContext , "ally:friend" , 403 ) ;
doHttpRequestAndVerify ( testContext , "ally:123456" , 403 ) ;
doHttpRequestAndVerify ( testContext , "ally:123456" , 403 ) ;
doHttpRequestAndVerify ( testContext , "ally:friend:1234" , 403 ) ;
doHttpRequestAndVerify ( testContext , "ally:friend:1234" , 403 ) ;
}
}
private void doHttpRequestAndVerify (
private void doHttpRequestAndVerify (
final VertxTestContext testContext , final String hostname , final int expectedResponse )
final VertxTestContext testContext , final String hostname , final int expectedResponse )
throws InterruptedException {
throws Throwable {
httpClient . request (
httpClient . request (
HttpMethod . POST ,
HttpMethod . POST ,
@ -200,10 +217,18 @@ public class WebSocketHostAllowlistTest {
. result ( )
. result ( )
. send (
. send (
response - > {
response - > {
assertThat ( response . result ( ) . statusCode ( ) ) . isEqualTo ( expectedResponse ) ;
if ( response . succeeded ( ) ) {
testContext . completeNow ( ) ;
assertThat ( response . result ( ) . statusCode ( ) ) . isEqualTo ( expectedResponse ) ;
testContext . completeNow ( ) ;
} else {
testContext . failNow ( response . cause ( ) ) ;
}
} ) ;
} ) ;
} ) ;
} ) ;
testContext . awaitCompletion ( VERTX_AWAIT_TIMEOUT_MILLIS , TimeUnit . MILLISECONDS ) ;
assertThat ( testContext . awaitCompletion ( VERTX_AWAIT_TIMEOUT_MILLIS , TimeUnit . MILLISECONDS ) )
. isTrue ( ) ;
if ( testContext . failed ( ) ) {
throw testContext . causeOfFailure ( ) ;
}
}
}
}
}