Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add one more CORS same origin unit test #32442

Merged
merged 1 commit into from
Apr 6, 2023
Merged
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 @@ -10,6 +10,8 @@
import java.util.Optional;
import java.util.regex.Pattern;

import org.jboss.logging.Logger;

import io.vertx.core.Handler;
import io.vertx.core.http.HttpHeaders;
import io.vertx.core.http.HttpMethod;
Expand All @@ -19,6 +21,7 @@

public class CORSFilter implements Handler<RoutingContext> {

private static final Logger LOG = Logger.getLogger(CORSFilter.class);
private static final Pattern COMMA_SEPARATED_SPLIT_REGEX = Pattern.compile("\\s*,\\s*");

// This is set in the recorder at runtime.
Expand Down Expand Up @@ -214,10 +217,12 @@ public void handle(RoutingContext event) {
}

if (!allowsOrigin) {
LOG.debug("Origin is not allowed");
response.setStatusCode(403);
response.setStatusMessage("CORS Rejected - Invalid origin");
response.end();
} else if (request.method().equals(HttpMethod.OPTIONS) && (requestedHeaders != null || requestedMethods != null)) {
LOG.debug("Preflight request has completed");
if (corsConfig.accessControlMaxAge.isPresent()) {
response.putHeader(HttpHeaders.ACCESS_CONTROL_MAX_AGE,
String.valueOf(corsConfig.accessControlMaxAge.get().getSeconds()));
Expand All @@ -233,6 +238,9 @@ static boolean isSameOrigin(HttpServerRequest request, String origin) {
//fast path check, when everything is the same
if (origin.startsWith(request.scheme())) {
if (!substringMatch(origin, request.scheme().length(), "://", false)) {
LOG.debugf(
"Same origin check has failed, the origin is not a substring of the request URI. Request URI: %s, origin: %s",
request.absoluteURI(), origin);
return false;
}
if (substringMatch(origin, request.scheme().length() + 3, request.host(), true)) {
Expand All @@ -253,9 +261,14 @@ static boolean isSameOriginSlowPath(HttpServerRequest request, String origin) {
if (!originUri.getPath().isEmpty()) {
//origin should not contain a path component
//just reject it in this case
LOG.debugf("Same origin check has failed as the origin contains a path component. Request URI: %s, origin: %s",
request.absoluteURI(), origin);
return false;
}
if (!baseUri.getHost().equals(originUri.getHost())) {
LOG.debugf("Same origin check has failed, the host values do not match. Request URI: %s, origin: %s",
request.absoluteURI(),
origin);
return false;
}
if (baseUri.getPort() == originUri.getPort()) {
Expand All @@ -280,6 +293,7 @@ static boolean isSameOriginSlowPath(HttpServerRequest request, String origin) {
}
}
}
LOG.debugf("Same origin check has failed. Request URI: %s, origin: %s", request.absoluteURI(), origin);
return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,17 @@ public void sameOriginTest() {

}

@Test
public void sameOriginPublicWebAddressTest() {
var request = Mockito.mock(HttpServerRequest.class);
Mockito.when(request.scheme()).thenReturn("https");
Mockito.when(request.host()).thenReturn("stage.code.quarkus.io");
Mockito.when(request.absoluteURI()).thenReturn("https://stage.code.quarkus.io/api/project");
Assertions.assertFalse(isSameOrigin(request, "http://localhost"));
Assertions.assertFalse(isSameOrigin(request, "https://code.quarkus.io"));
Assertions.assertTrue(isSameOrigin(request, "https://stage.code.quarkus.io"));
}

@Test
public void testSubstringMatches() {
Assertions.assertTrue(substringMatch("localhost", 0, "local", false));
Expand Down