Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,17 @@ public void handle(RoutingContext context) {
.end();

} else {
// Origin correct and not a pre-flight request

// in this case, we to verify the method, if it is not allowed we return a 403
if (!allowedMethods.isEmpty() && !allowedMethods.contains(request.method().name())) {
response
.setStatusMessage("CORS Rejected - Method not allowed");
context
.fail(403, new VertxException("CORS Rejected - Method not allowed", true));
return;
}

// when it is possible to determine if only one origin is allowed, we can skip this extra caching header
// If allow credentials is set, the response cannot be '*' thus we need to vary on origin
if (!(starOrigin() && !allowCredentials) && !uniqueOrigin()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,18 @@ public void testPreflightSimple() throws Exception {
}, resp -> checkHeaders(resp, "http://vertx.io", "PUT,DELETE", null, null), 204, "No Content", null);
}

@Test
public void testPreflightMismatchMethod() throws Exception {
Consumer<RoutingContext> handler = mock(Consumer.class);

Set<HttpMethod> allowedMethods = new LinkedHashSet<>(Arrays.asList(HttpMethod.PUT, HttpMethod.DELETE));

router.route().handler(CorsHandler.create().addOrigin("http://vertx.io").allowedMethods(allowedMethods));
router.route().handler(context -> context.response().end());
router.errorHandler(403, handler::accept);
testRequest(HttpMethod.GET, "/", req -> req.headers().add("origin", "http://vertx.io"), resp -> verify(handler).accept(any()), 403, "CORS Rejected - Method not allowed", null);
}

@Test
public void testPreflightAllowedHeaders() throws Exception {
Set<HttpMethod> allowedMethods = new LinkedHashSet<>(Arrays.asList(HttpMethod.PUT, HttpMethod.DELETE));
Expand Down Expand Up @@ -207,23 +219,23 @@ public void testPreflightMaxAge() throws Exception {

@Test
public void testRealRequestAllowCredentials() throws Exception {
Set<HttpMethod> allowedMethods = new LinkedHashSet<>(Arrays.asList(HttpMethod.PUT, HttpMethod.DELETE));
Set<HttpMethod> allowedMethods = new LinkedHashSet<>(Arrays.asList(HttpMethod.GET, HttpMethod.PUT, HttpMethod.DELETE));
router.route().handler(CorsHandler.create().addOriginWithRegex("http://vertx\\.io").allowedMethods(allowedMethods).allowCredentials(true));
router.route().handler(context -> context.response().end());
testRequest(HttpMethod.GET, "/", req -> req.headers().add("origin", "http://vertx.io"), resp -> checkHeaders(resp, "http://vertx.io", null, null, null, "true", null), 200, "OK", null);
}

@Test
public void testRealRequestCredentialsNoWildcardOrigin() throws Exception {
Set<HttpMethod> allowedMethods = new LinkedHashSet<>(Arrays.asList(HttpMethod.PUT, HttpMethod.DELETE));
Set<HttpMethod> allowedMethods = new LinkedHashSet<>(Arrays.asList(HttpMethod.GET, HttpMethod.PUT, HttpMethod.DELETE));
router.route().handler(CorsHandler.create().addOriginWithRegex("http://vertx.*").allowedMethods(allowedMethods).allowCredentials(true));
router.route().handler(context -> context.response().end());
testRequest(HttpMethod.GET, "/", req -> req.headers().add("origin", "http://vertx.io"), resp -> checkHeaders(resp, "http://vertx.io", null, null, null, "true", null), 200, "OK", null);
}

@Test
public void testRealRequestCredentialsWildcard() throws Exception {
Set<HttpMethod> allowedMethods = new LinkedHashSet<>(Arrays.asList(HttpMethod.PUT, HttpMethod.DELETE));
Set<HttpMethod> allowedMethods = new LinkedHashSet<>(Arrays.asList(HttpMethod.GET, HttpMethod.PUT, HttpMethod.DELETE));
router.route().handler(CorsHandler.create().allowedMethods(allowedMethods).allowCredentials(true));
router.route().handler(context -> context.response().end());
testRequest(HttpMethod.GET, "/", req -> req.headers().add("origin", "http://vertx.io"), resp -> checkHeaders(resp, "http://vertx.io", null, null, null, "true", null), 200, "OK", null);
Expand Down Expand Up @@ -502,7 +514,7 @@ public void testPreflightMaxAgeMultiOrigin() throws Exception {

@Test
public void testRealRequestAllowCredentialsMultiOrigin() throws Exception {
Set<HttpMethod> allowedMethods = new LinkedHashSet<>(Arrays.asList(HttpMethod.PUT, HttpMethod.DELETE));
Set<HttpMethod> allowedMethods = new LinkedHashSet<>(Arrays.asList(HttpMethod.GET, HttpMethod.PUT, HttpMethod.DELETE));
router.route().handler(CorsHandler.create().addOrigins(Arrays.asList("http://www.example.com", "https://www.vertx.io")).allowedMethods(allowedMethods).allowCredentials(true));
router.route().handler(context -> context.response().end());
testRequest(HttpMethod.GET, "/", req -> req.headers().add("origin", "https://www.vertx.io"), resp -> checkHeaders(resp, "https://www.vertx.io", null, null, null, "true", null), 200, "OK", null);
Expand Down
Loading