feat: add global CORS filter for browser-based local development (#849)

* feat: add global CORS filter for browser-based local development

Introduces a JAX-RS filter that adds CORS headers for configured origins,
enabling browser clients to call Floci directly during local development.
Service-level CORS configurations take precedence as the filter only fills
missing headers.

Configurable via EXTRA_CORS_ALLOWED_ORIGINS (or floci.security.extra-cors-allowed-origins)
with optional extra allowed/exposed headers and a disable flag.

* feat: fix hit for cors by each request

---------

Co-authored-by: Hector Ventura <hectorvent@gmail.com>
This commit is contained in:
Fred PeñaandHector Ventura authored and GitHub committed 2026-05-29 00:35:50 -05:00
1 parent f8a2f2d5c4
commit 76edd1f78b
7 files changed
+304 -2

No files matched your search

@@ -0,0 +1,83 @@
package io.github.hectorvent.floci.core.common;
import io.quarkus.test.junit.QuarkusTest;
import io.quarkus.test.junit.QuarkusTestProfile;
import io.quarkus.test.junit.TestProfile;
import io.github.hectorvent.floci.testing.RestAssuredJsonUtils;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import java.util.Map;
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.nullValue;
@QuarkusTest
@TestProfile(GlobalCorsFilterIntegrationTest.CorsProfile.class)
class GlobalCorsFilterIntegrationTest {
@BeforeAll
static void configureRestAssured() {
RestAssuredJsonUtils.configureAwsContentTypes();
}
@Test
void preflightFromExtraAllowedOriginReturnsCorsHeaders() {
given()
.header("Origin", "http://localhost:3000")
.header("Access-Control-Request-Method", "POST")
.header("Access-Control-Request-Headers", "x-custom-header, x-amz-target")
.when()
.options("/")
.then()
.statusCode(204)
.header("Access-Control-Allow-Origin", equalTo("http://localhost:3000"))
.header("Access-Control-Allow-Methods", containsString("POST"))
.header("Access-Control-Allow-Headers", containsString("x-custom-header"))
.header("Access-Control-Allow-Headers", containsString("x-added-header"))
.header("Access-Control-Expose-Headers", containsString("x-visible-header"))
.header("Vary", containsString("Origin"));
}
@Test
void actualRequestFromExtraAllowedOriginGetsCorsHeaders() {
given()
.header("Origin", "https://ui.example.test")
.header("X-Amz-Target", "DynamoDB_20120810.ListTables")
.contentType("application/x-amz-json-1.0")
.body("{}")
.when()
.post("/")
.then()
.statusCode(200)
.header("Access-Control-Allow-Origin", equalTo("https://ui.example.test"))
.header("Access-Control-Expose-Headers", containsString("x-visible-header"))
.header("Vary", containsString("Origin"));
}
@Test
void requestFromUnlistedOriginGetsNoGlobalCorsHeaders() {
given()
.header("Origin", "https://not-allowed.example.test")
.header("X-Amz-Target", "DynamoDB_20120810.ListTables")
.contentType("application/x-amz-json-1.0")
.body("{}")
.when()
.post("/")
.then()
.statusCode(200)
.header("Access-Control-Allow-Origin", nullValue());
}
public static final class CorsProfile implements QuarkusTestProfile {
@Override
public Map<String, String> getConfigOverrides() {
return Map.of(
"floci.security.extra-cors-allowed-origins", "http://localhost:3000,https://ui.example.test",
"floci.security.extra-cors-allowed-headers", "x-added-header",
"floci.security.extra-cors-expose-headers", "x-visible-header");
}
}
}
@@ -303,6 +303,8 @@ class EventBridgeSchedulerIntegrationTest {
@Override
public AuthConfig auth() { return null; }
@Override
public SecurityConfig security() { return null; }
@Override
public ServicesConfig services() { return null; }
@Override
public DockerConfig docker() { return null; }