Skip to content

Commit

Permalink
Add CORSRegexWildcardTest
Browse files Browse the repository at this point in the history
  • Loading branch information
sberyozkin committed Nov 12, 2024
1 parent 29e9cc6 commit cd28056
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package io.quarkus.vertx.http.cors;

import static io.restassured.RestAssured.given;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.quarkus.test.QuarkusUnitTest;

public class CORSRegexWildcardTestCase {

@RegisterExtension
static QuarkusUnitTest runner = new QuarkusUnitTest()
.withApplicationRoot((jar) -> jar
.addClasses(BeanRegisteringRoute.class)
.addAsResource("conf/cors-regex-wildcard.properties", "application.properties"));

@Test
public void corsRegexValidOriginTest() {
given().header("Origin", "https://asdf.domain.com")
.when()
.get("/test").then()
.statusCode(200)
.header("Access-Control-Allow-Origin", "https://asdf.domain.com")
.header("Access-Control-Allow-Credentials", "false");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ public void corsPreflightTest() {
.statusCode(200)
.header("Access-Control-Allow-Origin", origin)
.header("Access-Control-Allow-Methods", methods)
.header("Access-Control-Allow-Headers", headers);
.header("Access-Control-Allow-Headers", headers)
.header("Access-Control-Allow-Credentials", "false");

given().header("Origin", origin)
.header("Access-Control-Request-Method", methods)
Expand All @@ -68,7 +69,8 @@ public void corsPreflightTest() {
.statusCode(200)
.header("Access-Control-Allow-Origin", origin)
.header("Access-Control-Allow-Methods", methods)
.header("Access-Control-Allow-Headers", headers);
.header("Access-Control-Allow-Headers", headers)
.header("Access-Control-Allow-Credentials", "false");

given().header("Origin", origin)
.header("Access-Control-Request-Method", methods)
Expand All @@ -79,7 +81,8 @@ public void corsPreflightTest() {
.statusCode(200)
.header("Access-Control-Allow-Origin", origin)
.header("Access-Control-Allow-Methods", methods)
.header("Access-Control-Allow-Headers", headers);
.header("Access-Control-Allow-Headers", headers)
.header("Access-Control-Allow-Credentials", "false");

given().header("Origin", origin)
.header("Access-Control-Request-Method", methods)
Expand All @@ -90,41 +93,44 @@ public void corsPreflightTest() {
.statusCode(200)
.header("Access-Control-Allow-Origin", origin)
.header("Access-Control-Allow-Methods", methods)
.header("Access-Control-Allow-Headers", headers);
.header("Access-Control-Allow-Headers", headers)
.header("Access-Control-Allow-Credentials", "false");
}

@Test
@DisplayName("Handles a direct CORS request correctly")
public void corsNoPreflightTest() {
String origin = "http://custom.origin.quarkus";
String methods = "GET, POST";
String headers = "X-Custom";
given().header("Origin", origin)
.when()
.get("/test").then()
.statusCode(401)
.header("Access-Control-Allow-Origin", origin);
.header("Access-Control-Allow-Origin", origin)
.header("Access-Control-Allow-Credentials", "false");

given().header("Origin", origin)
.when()
.auth().basic("test", "test")
.get("/test").then()
.statusCode(200)
.header("Access-Control-Allow-Origin", origin)
.body(Matchers.equalTo("test:/test"));
.body(Matchers.equalTo("test:/test"))
.header("Access-Control-Allow-Credentials", "false");

given().header("Origin", origin)
.when()
.auth().basic("test", "wrongpassword")
.get("/test").then()
.statusCode(401)
.header("Access-Control-Allow-Origin", origin);
.header("Access-Control-Allow-Origin", origin)
.header("Access-Control-Allow-Credentials", "false");

given().header("Origin", origin)
.when()
.auth().basic("user", "user")
.get("/test").then()
.statusCode(403)
.header("Access-Control-Allow-Origin", origin);
.header("Access-Control-Allow-Origin", origin)
.header("Access-Control-Allow-Credentials", "false");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
quarkus.http.cors=true
quarkus.http.cors.origins=/.*/
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ public void handle(RoutingContext event) {

//for both normal and preflight requests we need to check the origin
boolean allowsOrigin = wildcardOrigin;
boolean originMatches = corsConfig.origins.isPresent() &&
boolean originMatches = !wildcardOrigin && corsConfig.origins.isPresent() &&
(corsConfig.origins.get().contains(origin) || isOriginAllowedByRegex(allowedOriginsRegex, origin));
if (!allowsOrigin) {
if (corsConfig.origins.isPresent()) {
Expand Down

0 comments on commit cd28056

Please sign in to comment.