Skip to content

Commit c99b54c

Browse files
committed
fix(ws-next): validate base URI scheme
1 parent 78af426 commit c99b54c

File tree

3 files changed

+53
-0
lines changed

3 files changed

+53
-0
lines changed

extensions/websockets-next/deployment/src/test/java/io/quarkus/websockets/next/test/client/BasicConnectorTest.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import static org.assertj.core.api.Assertions.assertThat;
44
import static org.assertj.core.api.Assertions.assertThatThrownBy;
5+
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
56
import static org.junit.jupiter.api.Assertions.assertEquals;
67
import static org.junit.jupiter.api.Assertions.assertNotNull;
78
import static org.junit.jupiter.api.Assertions.assertNull;
@@ -144,6 +145,27 @@ void testClientOptionsCustomization() {
144145
})).rootCause().isInstanceOf(UnknownHostException.class).hasMessageContaining("robert");
145146
}
146147

148+
@Test
149+
void testBaseUriValidationFailure() {
150+
assertThrows(IllegalArgumentException.class, () -> connector.baseUri("localhost:8080"));
151+
assertThrows(IllegalArgumentException.class, () -> connector.baseUri("127.0.0.1:8080/"));
152+
assertThrows(IllegalArgumentException.class, () -> connector.baseUri("localhost:8080/hello"));
153+
assertThrows(IllegalArgumentException.class, () -> connector.baseUri("jdbc:localhost:8080/hello"));
154+
assertThrows(IllegalArgumentException.class, () -> connector.baseUri("jdbc://localhost:8080/hello"));
155+
assertDoesNotThrow(() -> connector.baseUri("http://localhost:8080/hello"));
156+
assertDoesNotThrow(() -> connector.baseUri("http://localhost:8080/"));
157+
assertDoesNotThrow(() -> connector.baseUri("http://localhost:8080"));
158+
assertDoesNotThrow(() -> connector.baseUri("ws://localhost:8080/hello"));
159+
assertDoesNotThrow(() -> connector.baseUri("ws://localhost:8080/"));
160+
assertDoesNotThrow(() -> connector.baseUri("ws://localhost:8080"));
161+
assertDoesNotThrow(() -> connector.baseUri("https://localhost:8080/hello"));
162+
assertDoesNotThrow(() -> connector.baseUri("https://localhost:8080/"));
163+
assertDoesNotThrow(() -> connector.baseUri("https://localhost:8080"));
164+
assertDoesNotThrow(() -> connector.baseUri("wss://localhost:8080/hello"));
165+
assertDoesNotThrow(() -> connector.baseUri("wss://localhost:8080/"));
166+
assertDoesNotThrow(() -> connector.baseUri("wss://localhost:8080"));
167+
}
168+
147169
private WebSocketClientConnection createConnection2(CountDownLatch conn2Latch,
148170
BiConsumer<WebSocketConnectOptions, WebSocketClientOptions> customizer) {
149171
return BasicWebSocketConnector

extensions/websockets-next/deployment/src/test/java/io/quarkus/websockets/next/test/client/ClientEndpointTest.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package io.quarkus.websockets.next.test.client;
22

33
import static org.assertj.core.api.Assertions.assertThatThrownBy;
4+
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
45
import static org.junit.jupiter.api.Assertions.assertEquals;
56
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
7+
import static org.junit.jupiter.api.Assertions.assertThrows;
68
import static org.junit.jupiter.api.Assertions.assertTrue;
79

810
import java.net.URI;
@@ -87,6 +89,27 @@ void testClient() throws InterruptedException {
8789
.rootCause().isInstanceOf(UnknownHostException.class).hasMessageContaining("robert");
8890
}
8991

92+
@Test
93+
void testBaseUriValidationFailure() {
94+
assertThrows(IllegalArgumentException.class, () -> connector.baseUri("localhost:8080"));
95+
assertThrows(IllegalArgumentException.class, () -> connector.baseUri("127.0.0.1:8080/"));
96+
assertThrows(IllegalArgumentException.class, () -> connector.baseUri("localhost:8080/hello"));
97+
assertThrows(IllegalArgumentException.class, () -> connector.baseUri("jdbc:localhost:8080/hello"));
98+
assertThrows(IllegalArgumentException.class, () -> connector.baseUri("jdbc://localhost:8080/hello"));
99+
assertDoesNotThrow(() -> connector.baseUri("http://localhost:8080/hello"));
100+
assertDoesNotThrow(() -> connector.baseUri("http://localhost:8080/"));
101+
assertDoesNotThrow(() -> connector.baseUri("http://localhost:8080"));
102+
assertDoesNotThrow(() -> connector.baseUri("ws://localhost:8080/hello"));
103+
assertDoesNotThrow(() -> connector.baseUri("ws://localhost:8080/"));
104+
assertDoesNotThrow(() -> connector.baseUri("ws://localhost:8080"));
105+
assertDoesNotThrow(() -> connector.baseUri("https://localhost:8080/hello"));
106+
assertDoesNotThrow(() -> connector.baseUri("https://localhost:8080/"));
107+
assertDoesNotThrow(() -> connector.baseUri("https://localhost:8080"));
108+
assertDoesNotThrow(() -> connector.baseUri("wss://localhost:8080/hello"));
109+
assertDoesNotThrow(() -> connector.baseUri("wss://localhost:8080/"));
110+
assertDoesNotThrow(() -> connector.baseUri("wss://localhost:8080"));
111+
}
112+
90113
@WebSocket(path = "/endpoint/{name}")
91114
public static class ServerEndpoint {
92115

extensions/websockets-next/runtime/src/main/java/io/quarkus/websockets/next/runtime/WebSocketConnectorBase.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,10 @@ abstract class WebSocketConnectorBase<THIS extends WebSocketConnectorBase<THIS>>
9393
}
9494

9595
public THIS baseUri(URI baseUri) {
96+
if (!isSecure(baseUri) && !isNotSecure(baseUri)) {
97+
throw new IllegalArgumentException(
98+
String.format("[%s] is not a valid scheme in the base URI %s", baseUri.getScheme(), baseUri));
99+
}
96100
this.baseUri = Objects.requireNonNull(baseUri);
97101
return self();
98102
}
@@ -240,6 +244,10 @@ protected WebSocketConnectOptions newConnectOptions(URI serverEndpointUri) {
240244
return connectOptions;
241245
}
242246

247+
protected boolean isNotSecure(URI uri) {
248+
return "http".equals(uri.getScheme()) || "ws".equals(uri.getScheme());
249+
}
250+
243251
protected boolean isSecure(URI uri) {
244252
return "https".equals(uri.getScheme()) || "wss".equals(uri.getScheme());
245253
}

0 commit comments

Comments
 (0)