Skip to content

Commit

Permalink
Disallow invalid host/port values on the vertx-websocket server consumer
Browse files Browse the repository at this point in the history
Fixes #5118
  • Loading branch information
jamesnetherton committed Jul 27, 2023
1 parent 70c3a08 commit f2e6f05
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,9 @@ from("vertx-websocket:/my-websocket-path")
.setBody().constant("Hello World");
----

Or alternatively, you can refer to the full host & port configuration for the Quarkus HTTP server.

[source,java]
----
from("vertx-websocket:{{quarkus.http.host}}:{{quarkus.http.port}}/my-websocket-path")
.setBody().constant("Hello World");
----
NOTE: While you do not need to explicitly configure the host/port on the vertx-websocket consumer. If you choose to,
the host & port must exactly match the value of the Quarkus HTTP server configuration values for `quarkus.http.host` and `quarkus.http.port`.
Otherwise an exception will be thrown at runtime.

[id="extensions-vertx-websocket-usage-vert-x-websocket-producers"]
=== Vert.x WebSocket producers
Expand All @@ -93,7 +89,7 @@ from("direct:sendToWebSocket")
.log("vertx-websocket:{{quarkus.http.host}}:{{quarkus.http.port}}/my-websocket-path");
----

When producing messages to an external WebSocket server, then you must provide the host name and port (if required).
When producing messages to an external WebSocket server, then you must always provide the host name and port (if required).


[id="extensions-vertx-websocket-additional-camel-quarkus-configuration"]
Expand Down
12 changes: 4 additions & 8 deletions extensions/vertx-websocket/runtime/src/main/doc/usage.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,9 @@ from("vertx-websocket:/my-websocket-path")
.setBody().constant("Hello World");
----

Or alternatively, you can refer to the full host & port configuration for the Quarkus HTTP server.

[source,java]
----
from("vertx-websocket:{{quarkus.http.host}}:{{quarkus.http.port}}/my-websocket-path")
.setBody().constant("Hello World");
----
NOTE: While you do not need to explicitly configure the host/port on the vertx-websocket consumer. If you choose to,
the host & port must exactly match the value of the Quarkus HTTP server configuration values for `quarkus.http.host` and `quarkus.http.port`.
Otherwise an exception will be thrown at runtime.

=== Vert.x WebSocket producers

Expand All @@ -40,4 +36,4 @@ from("direct:sendToWebSocket")
.log("vertx-websocket:{{quarkus.http.host}}:{{quarkus.http.port}}/my-websocket-path");
----

When producing messages to an external WebSocket server, then you must provide the host name and port (if required).
When producing messages to an external WebSocket server, then you must always provide the host name and port (if required).
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,13 @@ static final class QuarkusVertxWebsocketComponent extends VertxWebsocketComponen
@Override
protected VertxWebsocketHost createVertxWebsocketHost(VertxWebsocketHostConfiguration hostConfiguration,
VertxWebsocketHostKey hostKey) {
// If a host / port was specified on the consumer, it must be the same as what the Quarkus HTTP server is bound to
if (!hostKey.getHost().equals(HOST) || hostKey.getPort() != PORT) {
String message = String.format(
"Invalid host/port %s:%d. The host/port can only be configured as %s:%d",
hostKey.getHost(), hostKey.getPort(), HOST, PORT);
throw new IllegalArgumentException(message);
}
return new QuarkusVertxWebsocketHost(getCamelContext(), hostConfiguration, hostKey);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,20 @@
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.QueryParam;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;
import org.apache.camel.CamelContext;
import org.apache.camel.ConsumerTemplate;
import org.apache.camel.ProducerTemplate;
import org.apache.camel.quarkus.component.vertx.websocket.VertxWebsocketRecorder;
import org.apache.camel.support.jsse.KeyStoreParameters;
import org.apache.camel.support.jsse.SSLContextParameters;
import org.apache.camel.support.jsse.TrustManagersParameters;
import org.jboss.logging.Logger;

@Path("/vertx-websocket")
@ApplicationScoped
public class VertxWebsocketResource {
private static final Logger LOG = Logger.getLogger(VertxWebsocketResource.class);

@Inject
CamelContext context;
Expand Down Expand Up @@ -121,6 +124,20 @@ public int getDefaultPort(String scheme) {
return connectOptions.getPort();
}

@Path("/invalid/consumer/uri")
@GET
@Produces(MediaType.TEXT_PLAIN)
public Response invalidConsumerHostPort(@QueryParam("hostPort") String hostPort) {
try {
consumerTemplate.receive("vertx-websocket:" + hostPort + "/test");
return Response.ok().build();
} catch (Exception e) {
LOG.error("Error creating vertx-websocket consumer", e);
String message = e.getCause() != null ? e.getCause().getMessage() : e.getMessage();
return Response.serverError().entity(message).build();
}
}

@Named
public SSLContextParameters clientSSLContextParameters() {
KeyStoreParameters truststoreParameters = new KeyStoreParameters();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,6 @@ public void configure() throws Exception {
.end()
.to("vertx-websocket:/");

from("vertx-websocket:redundant.host:9999/test/default/host/port/applied")
.setBody(simple("Hello ${body}"))
.to("vertx-websocket:/test/default/host/port/applied");

from("direct:sendMessage")
.to("vertx-websocket:/test");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import org.junit.jupiter.params.provider.ValueSource;

import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.startsWith;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

Expand All @@ -54,9 +55,6 @@ class VertxWebsocketTest {
@TestHTTPResource("/echo")
URI echo;

@TestHTTPResource("/test/default/host/port/applied")
URI defaultHostPortApplied;

@TestHTTPResource("/client/consumer")
URI clientConsumer;

Expand All @@ -82,17 +80,15 @@ public void testEchoWithShortFormUri() throws Exception {
}
}

@Test
public void testEchoWithIgnoredHostPortConfig() throws Exception {
String message = "From Ignored Host Port Config";

try (WebSocketConnection connection = new WebSocketConnection(defaultHostPortApplied, message)) {
connection.connect();

List<String> messages = connection.getMessages();
assertEquals(1, messages.size());
assertEquals("Hello " + message, messages.get(0));
}
@ParameterizedTest
@ValueSource(strings = { "invalid.host", "localhost:9999", "invalid.host:9999" })
public void testInvalidHostPortConfig(String hostPort) throws Exception {
RestAssured.given()
.queryParam("hostPort", hostPort)
.get("/vertx-websocket/invalid/consumer/uri")
.then()
.statusCode(500)
.body(startsWith("Invalid host/port"));
}

@Test
Expand Down

0 comments on commit f2e6f05

Please sign in to comment.