-
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.
Support '*' in CORS filter configuration
- Loading branch information
Showing
7 changed files
with
354 additions
and
16 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
149 changes: 149 additions & 0 deletions
149
...ttp/deployment/src/test/java/io/quarkus/vertx/http/cors/CORSWildcardSecurityTestCase.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,149 @@ | ||
package io.quarkus.vertx.http.cors; | ||
|
||
import static io.restassured.RestAssured.given; | ||
|
||
import java.util.function.Supplier; | ||
|
||
import org.hamcrest.Matchers; | ||
import org.jboss.shrinkwrap.api.ShrinkWrap; | ||
import org.jboss.shrinkwrap.api.asset.StringAsset; | ||
import org.jboss.shrinkwrap.api.spec.JavaArchive; | ||
import org.junit.jupiter.api.BeforeAll; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.quarkus.security.test.utils.TestIdentityController; | ||
import io.quarkus.security.test.utils.TestIdentityProvider; | ||
import io.quarkus.test.QuarkusUnitTest; | ||
import io.quarkus.vertx.http.security.PathHandler; | ||
|
||
public class CORSWildcardSecurityTestCase { | ||
|
||
private static final String APP_PROPS = "" + | ||
"quarkus.http.cors=true\n" + | ||
"quarkus.http.auth.basic=true\n" + | ||
"quarkus.http.auth.policy.r1.roles-allowed=test\n" + | ||
"quarkus.http.auth.permission.roles1.paths=/test\n" + | ||
"quarkus.http.auth.permission.roles1.policy=r1\n"; | ||
|
||
@RegisterExtension | ||
static QuarkusUnitTest test = new QuarkusUnitTest().setArchiveProducer(new Supplier<JavaArchive>() { | ||
@Override | ||
public JavaArchive get() { | ||
return ShrinkWrap.create(JavaArchive.class) | ||
.addClasses(TestIdentityProvider.class, TestIdentityController.class, PathHandler.class) | ||
.addAsResource(new StringAsset(APP_PROPS), "application.properties"); | ||
} | ||
}); | ||
|
||
@BeforeAll | ||
public static void setup() { | ||
TestIdentityController.resetRoles().add("test", "test", "test").add("user", "user", "user"); | ||
} | ||
|
||
@Test | ||
@DisplayName("Handles a preflight CORS request correctly") | ||
public void corsPreflightTest() { | ||
String origin = "http://custom.origin.quarkus"; | ||
String methods = "GET,POST"; | ||
String headers = "X-Custom"; | ||
given().header("Origin", origin) | ||
.header("Access-Control-Request-Method", methods) | ||
.header("Access-Control-Request-Headers", headers) | ||
.when() | ||
.options("/test").then() | ||
.statusCode(200) | ||
.header("Access-Control-Allow-Origin", origin) | ||
.header("Access-Control-Allow-Methods", methods) | ||
.header("Access-Control-Allow-Headers", headers); | ||
|
||
given().header("Origin", origin) | ||
.header("Access-Control-Request-Method", methods) | ||
.header("Access-Control-Request-Headers", headers) | ||
.when() | ||
.auth().basic("test", "test") | ||
.options("/test").then() | ||
.statusCode(200) | ||
.header("Access-Control-Allow-Origin", origin) | ||
.header("Access-Control-Allow-Methods", methods) | ||
.header("Access-Control-Allow-Headers", headers); | ||
|
||
given().header("Origin", origin) | ||
.header("Access-Control-Request-Method", methods) | ||
.header("Access-Control-Request-Headers", headers) | ||
.when() | ||
.auth().basic("test", "wrongpassword") | ||
.options("/test").then() | ||
.statusCode(200) | ||
.header("Access-Control-Allow-Origin", origin) | ||
.header("Access-Control-Allow-Methods", methods) | ||
.header("Access-Control-Allow-Headers", headers); | ||
|
||
given().header("Origin", origin) | ||
.header("Access-Control-Request-Method", methods) | ||
.header("Access-Control-Request-Headers", headers) | ||
.when() | ||
.auth().basic("user", "user") | ||
.options("/test").then() | ||
.statusCode(200) | ||
.header("Access-Control-Allow-Origin", origin) | ||
.header("Access-Control-Allow-Methods", methods) | ||
.header("Access-Control-Allow-Headers", headers); | ||
} | ||
|
||
@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) | ||
.header("Access-Control-Request-Method", methods) | ||
.header("Access-Control-Request-Headers", headers) | ||
.when() | ||
.log().headers() | ||
.get("/test").then() | ||
.statusCode(401) | ||
.header("Access-Control-Allow-Origin", origin) | ||
.header("Access-Control-Allow-Methods", methods) | ||
.header("Access-Control-Allow-Headers", headers); | ||
|
||
given().header("Origin", origin) | ||
.header("Access-Control-Request-Method", methods) | ||
.header("Access-Control-Request-Headers", headers) | ||
.when() | ||
.auth().basic("test", "test") | ||
.log().headers() | ||
.get("/test").then() | ||
.statusCode(200) | ||
.header("Access-Control-Allow-Origin", origin) | ||
.header("Access-Control-Allow-Methods", methods) | ||
.header("Access-Control-Allow-Headers", headers) | ||
.body(Matchers.equalTo("test:/test")); | ||
|
||
given().header("Origin", origin) | ||
.header("Access-Control-Request-Method", methods) | ||
.header("Access-Control-Request-Headers", headers) | ||
.when() | ||
.auth().basic("test", "wrongpassword") | ||
.log().headers() | ||
.get("/test").then() | ||
.statusCode(401) | ||
.header("Access-Control-Allow-Origin", origin) | ||
.header("Access-Control-Allow-Methods", methods) | ||
.header("Access-Control-Allow-Headers", headers); | ||
|
||
given().header("Origin", origin) | ||
.header("Access-Control-Request-Method", methods) | ||
.header("Access-Control-Request-Headers", headers) | ||
.when() | ||
.auth().basic("user", "user") | ||
.log().headers() | ||
.get("/test").then() | ||
.statusCode(403) | ||
.header("Access-Control-Allow-Origin", origin) | ||
.header("Access-Control-Allow-Methods", methods) | ||
.header("Access-Control-Allow-Headers", headers); | ||
} | ||
} |
153 changes: 153 additions & 0 deletions
153
...deployment/src/test/java/io/quarkus/vertx/http/cors/CORSWildcardStarSecurityTestCase.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,153 @@ | ||
package io.quarkus.vertx.http.cors; | ||
|
||
import static io.restassured.RestAssured.given; | ||
|
||
import java.util.function.Supplier; | ||
|
||
import org.hamcrest.Matchers; | ||
import org.jboss.shrinkwrap.api.ShrinkWrap; | ||
import org.jboss.shrinkwrap.api.asset.StringAsset; | ||
import org.jboss.shrinkwrap.api.spec.JavaArchive; | ||
import org.junit.jupiter.api.BeforeAll; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.quarkus.security.test.utils.TestIdentityController; | ||
import io.quarkus.security.test.utils.TestIdentityProvider; | ||
import io.quarkus.test.QuarkusUnitTest; | ||
import io.quarkus.vertx.http.security.PathHandler; | ||
|
||
public class CORSWildcardStarSecurityTestCase { | ||
|
||
private static final String APP_PROPS = "" + | ||
"quarkus.http.cors=true\n" + | ||
"quarkus.http.cors.methods=*\n" + | ||
"quarkus.http.cors.origins=*\n" + | ||
"quarkus.http.cors.headers=*\n" + | ||
"quarkus.http.auth.basic=true\n" + | ||
"quarkus.http.auth.policy.r1.roles-allowed=test\n" + | ||
"quarkus.http.auth.permission.roles1.paths=/test\n" + | ||
"quarkus.http.auth.permission.roles1.policy=r1\n"; | ||
|
||
@RegisterExtension | ||
static QuarkusUnitTest test = new QuarkusUnitTest().setArchiveProducer(new Supplier<JavaArchive>() { | ||
@Override | ||
public JavaArchive get() { | ||
return ShrinkWrap.create(JavaArchive.class) | ||
.addClasses(TestIdentityProvider.class, TestIdentityController.class, PathHandler.class) | ||
.addAsResource(new StringAsset(APP_PROPS), "application.properties"); | ||
} | ||
}); | ||
|
||
@BeforeAll | ||
public static void setup() { | ||
TestIdentityController.resetRoles().add("test", "test", "test").add("user", "user", "user"); | ||
} | ||
|
||
@Test | ||
@DisplayName("Handles a preflight CORS request correctly") | ||
public void corsPreflightTest() { | ||
String origin = "http://custom.origin.quarkus"; | ||
String methods = "GET,POST,OPTIONS,DELETE"; | ||
String headers = "X-Custom,B-Custom,Test-Headers"; | ||
given().header("Origin", origin) | ||
.header("Access-Control-Request-Method", methods) | ||
.header("Access-Control-Request-Headers", headers) | ||
.when() | ||
.options("/test").then() | ||
.statusCode(200) | ||
.header("Access-Control-Allow-Origin", origin) | ||
.header("Access-Control-Allow-Methods", methods) | ||
.header("Access-Control-Allow-Headers", headers); | ||
|
||
given().header("Origin", origin) | ||
.header("Access-Control-Request-Method", methods) | ||
.header("Access-Control-Request-Headers", headers) | ||
.when() | ||
.auth().basic("test", "test") | ||
.options("/test").then() | ||
.statusCode(200) | ||
.header("Access-Control-Allow-Origin", origin) | ||
.header("Access-Control-Allow-Methods", methods) | ||
.header("Access-Control-Allow-Headers", headers); | ||
|
||
given().header("Origin", origin) | ||
.header("Access-Control-Request-Method", methods) | ||
.header("Access-Control-Request-Headers", headers) | ||
.when() | ||
.auth().basic("test", "wrongpassword") | ||
.options("/test").then() | ||
.statusCode(200) | ||
.header("Access-Control-Allow-Origin", origin) | ||
.header("Access-Control-Allow-Methods", methods) | ||
.header("Access-Control-Allow-Headers", headers); | ||
|
||
given().header("Origin", origin) | ||
.header("Access-Control-Request-Method", methods) | ||
.header("Access-Control-Request-Headers", headers) | ||
.when() | ||
.auth().basic("user", "user") | ||
.options("/test").then() | ||
.statusCode(200) | ||
.header("Access-Control-Allow-Origin", origin) | ||
.header("Access-Control-Allow-Methods", methods) | ||
.header("Access-Control-Allow-Headers", headers); | ||
} | ||
|
||
@Test | ||
@DisplayName("Handles a direct CORS request correctly") | ||
public void corsNoPreflightTest() { | ||
String origin = "http://custom.origin.quarkus"; | ||
String methods = "GET,POST,OPTIONS,DELETE"; | ||
String headers = "X-Custom,B-Custom,Test-Headers"; | ||
given().header("Origin", origin) | ||
.header("Access-Control-Request-Method", methods) | ||
.header("Access-Control-Request-Headers", headers) | ||
.when() | ||
.log().headers() | ||
.get("/test").then() | ||
.statusCode(401) | ||
.header("Access-Control-Allow-Origin", origin) | ||
.header("Access-Control-Allow-Methods", methods) | ||
.header("Access-Control-Allow-Headers", headers); | ||
|
||
given().header("Origin", origin) | ||
.header("Access-Control-Request-Method", methods) | ||
.header("Access-Control-Request-Headers", headers) | ||
.when() | ||
.auth().basic("test", "test") | ||
.log().headers() | ||
.get("/test").then() | ||
.statusCode(200) | ||
.header("Access-Control-Allow-Origin", origin) | ||
.header("Access-Control-Allow-Methods", methods) | ||
.header("Access-Control-Allow-Headers", headers) | ||
.body(Matchers.equalTo("test:/test")); | ||
|
||
given().header("Origin", origin) | ||
.header("Access-Control-Request-Method", methods) | ||
.header("Access-Control-Request-Headers", headers) | ||
.when() | ||
.auth().basic("test", "wrongpassword") | ||
.log().headers() | ||
.get("/test").then() | ||
.statusCode(401) | ||
.header("Access-Control-Allow-Origin", origin) | ||
.header("Access-Control-Allow-Methods", methods) | ||
.header("Access-Control-Allow-Headers", headers); | ||
|
||
given().header("Origin", origin) | ||
.header("Access-Control-Request-Method", methods) | ||
.header("Access-Control-Request-Headers", headers) | ||
.when() | ||
.auth().basic("user", "user") | ||
.log().headers() | ||
.get("/test").then() | ||
.statusCode(403) | ||
.header("Access-Control-Allow-Origin", origin) | ||
.header("Access-Control-Allow-Methods", methods) | ||
.header("Access-Control-Allow-Headers", headers); | ||
} | ||
|
||
} |
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
Oops, something went wrong.