-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6419840
commit 7169469
Showing
7 changed files
with
218 additions
and
8 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
125 changes: 125 additions & 0 deletions
125
...tx-http/deployment/src/test/java/io/quarkus/vertx/http/devconsole/DevConsoleCorsTest.java
This file contains 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,125 @@ | ||
package io.quarkus.vertx.http.devconsole; | ||
|
||
import static org.hamcrest.Matchers.emptyOrNullString; | ||
import static org.hamcrest.Matchers.not; | ||
import static org.hamcrest.Matchers.nullValue; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.quarkus.test.QuarkusDevModeTest; | ||
import io.restassured.RestAssured; | ||
|
||
public class DevConsoleCorsTest { | ||
|
||
@RegisterExtension | ||
static final QuarkusDevModeTest config = new QuarkusDevModeTest() | ||
.withEmptyApplication(); | ||
|
||
@Test | ||
public void testPreflightHttpLocalhostOrigin() { | ||
String origin = "http://localhost:8080"; | ||
String methods = "GET,POST"; | ||
RestAssured.given() | ||
.header("Origin", origin) | ||
.header("Access-Control-Request-Method", methods) | ||
.when() | ||
.options("q/dev/io.quarkus.quarkus-vertx-http/config").then() | ||
.statusCode(200) | ||
.header("Access-Control-Allow-Origin", origin) | ||
.header("Access-Control-Allow-Methods", methods) | ||
.body(emptyOrNullString()); | ||
} | ||
|
||
@Test | ||
public void testPreflightHttpsLocalhostOrigin() { | ||
String origin = "https://localhost:8080"; | ||
String methods = "GET,POST"; | ||
RestAssured.given() | ||
.header("Origin", origin) | ||
.header("Access-Control-Request-Method", methods) | ||
.when() | ||
.options("q/dev/io.quarkus.quarkus-vertx-http/config").then() | ||
.statusCode(200) | ||
.header("Access-Control-Allow-Origin", origin) | ||
.header("Access-Control-Allow-Methods", methods) | ||
.body(emptyOrNullString()); | ||
} | ||
|
||
@Test | ||
public void testPreflightNonLocalhostOrigin() { | ||
String methods = "GET,POST"; | ||
RestAssured.given() | ||
.header("Origin", "https://quarkus.io/http://localhost") | ||
.header("Access-Control-Request-Method", methods) | ||
.when() | ||
.options("q/dev/io.quarkus.quarkus-vertx-http/config").then() | ||
.statusCode(403) | ||
.header("Access-Control-Allow-Origin", nullValue()) | ||
.header("Access-Control-Allow-Methods", nullValue()) | ||
.body(emptyOrNullString()); | ||
} | ||
|
||
@Test | ||
public void testPreflightBadLocalhostOrigin() { | ||
String methods = "GET,POST"; | ||
RestAssured.given() | ||
.header("Origin", "http://localhost/devui") | ||
.header("Access-Control-Request-Method", methods) | ||
.when() | ||
.options("q/dev/io.quarkus.quarkus-vertx-http/config").then() | ||
.statusCode(403) | ||
.header("Access-Control-Allow-Origin", nullValue()) | ||
.body(emptyOrNullString()); | ||
} | ||
|
||
@Test | ||
public void testPreflightLocalhostOriginWithoutPort() { | ||
String methods = "GET,POST"; | ||
RestAssured.given() | ||
.header("Origin", "http://localhost") | ||
.header("Access-Control-Request-Method", methods) | ||
.when() | ||
.options("q/dev/io.quarkus.quarkus-vertx-http/config").then() | ||
.statusCode(403) | ||
.header("Access-Control-Allow-Origin", nullValue()) | ||
.body(emptyOrNullString()); | ||
} | ||
|
||
@Test | ||
public void testSimpleRequestHttpLocalhostOrigin() { | ||
String origin = "http://localhost:8080"; | ||
RestAssured.given() | ||
.header("Origin", origin) | ||
.when() | ||
.get("q/dev/io.quarkus.quarkus-vertx-http/config").then() | ||
.statusCode(200) | ||
.header("Access-Control-Allow-Origin", origin) | ||
.header("Access-Control-Allow-Methods", nullValue()) | ||
.body(not(emptyOrNullString())); | ||
} | ||
|
||
@Test | ||
public void testSimpleRequestHttpsLocalhostOrigin() { | ||
String origin = "https://localhost:8080"; | ||
RestAssured.given() | ||
.header("Origin", origin) | ||
.when() | ||
.get("q/dev/io.quarkus.quarkus-vertx-http/config").then() | ||
.statusCode(200) | ||
.header("Access-Control-Allow-Origin", origin) | ||
.header("Access-Control-Allow-Methods", nullValue()) | ||
.body(not(emptyOrNullString())); | ||
} | ||
|
||
@Test | ||
public void testSimpleRequestNonLocalhostOrigin() { | ||
RestAssured.given() | ||
.header("Origin", "https://quarkus.io/http://localhost") | ||
.when() | ||
.get("q/dev/io.quarkus.quarkus-vertx-http/config").then() | ||
.statusCode(403) | ||
.header("Access-Control-Allow-Origin", nullValue()) | ||
.body(emptyOrNullString()); | ||
} | ||
} |
This file contains 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
This file contains 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
53 changes: 53 additions & 0 deletions
53
...ttp/runtime/src/main/java/io/quarkus/vertx/http/runtime/devmode/DevConsoleCORSFilter.java
This file contains 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,53 @@ | ||
package io.quarkus.vertx.http.runtime.devmode; | ||
|
||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
import org.eclipse.microprofile.config.ConfigProvider; | ||
import org.jboss.logging.Logger; | ||
|
||
import io.quarkus.vertx.http.runtime.cors.CORSConfig; | ||
import io.quarkus.vertx.http.runtime.cors.CORSFilter; | ||
import io.vertx.core.Handler; | ||
import io.vertx.core.http.HttpHeaders; | ||
import io.vertx.core.http.HttpServerRequest; | ||
import io.vertx.core.http.HttpServerResponse; | ||
import io.vertx.ext.web.RoutingContext; | ||
|
||
public class DevConsoleCORSFilter implements Handler<RoutingContext> { | ||
private static final Logger LOG = Logger.getLogger(DevConsoleCORSFilter.class); | ||
|
||
private static final String HTTP_PORT_CONFIG_PROP = "quarkus.http.port"; | ||
private static final String LOCAL_HOST = "localhost"; | ||
private static final String HTTP_LOCAL_HOST = "http://" + LOCAL_HOST; | ||
private static final String HTTPS_LOCAL_HOST = "https://" + LOCAL_HOST; | ||
|
||
public DevConsoleCORSFilter() { | ||
} | ||
|
||
private static CORSFilter corsFilter() { | ||
int httpPort = ConfigProvider.getConfig().getValue(HTTP_PORT_CONFIG_PROP, int.class); | ||
CORSConfig config = new CORSConfig(); | ||
config.origins = Optional.of(List.of(HTTP_LOCAL_HOST + ":" + httpPort, HTTPS_LOCAL_HOST + ":" + httpPort)); | ||
return new CORSFilter(config); | ||
} | ||
|
||
@Override | ||
public void handle(RoutingContext event) { | ||
HttpServerRequest request = event.request(); | ||
HttpServerResponse response = event.response(); | ||
String origin = request.getHeader(HttpHeaders.ORIGIN); | ||
if (origin == null) { | ||
corsFilter().handle(event); | ||
} else { | ||
if (origin.startsWith(HTTP_LOCAL_HOST) || origin.startsWith(HTTPS_LOCAL_HOST)) { | ||
corsFilter().handle(event); | ||
} else { | ||
LOG.errorf("Only localhost origin is allowed, but Origin header value is: %s", origin); | ||
response.setStatusCode(403); | ||
response.setStatusMessage("CORS Rejected - Invalid origin"); | ||
response.end(); | ||
} | ||
} | ||
} | ||
} |