diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 4494f5da78..4c19af51c0 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -203,10 +203,10 @@ floci: security: # Comma-separated browser origins allowed by the global CORS filter. # Env var: EXTRA_CORS_ALLOWED_ORIGINS=http://localhost:3000 - # extra-cors-allowed-origins: + extra-cors-allowed-origins: ${EXTRA_CORS_ALLOWED_ORIGINS:} # Extra request/response CORS header names for browser clients. - # extra-cors-allowed-headers: - # extra-cors-expose-headers: + extra-cors-allowed-headers: ${EXTRA_CORS_ALLOWED_HEADERS:} + extra-cors-expose-headers: ${EXTRA_CORS_EXPOSE_HEADERS:} disable-cors-headers: false # Grant Private Network Access preflights (Access-Control-Allow-Private-Network) # so a page on a public/secure origin can reach this loopback backend. diff --git a/src/test/java/io/github/hectorvent/floci/core/common/GlobalCorsFilterAliasIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/core/common/GlobalCorsFilterAliasIntegrationTest.java new file mode 100644 index 0000000000..abce9b323e --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/core/common/GlobalCorsFilterAliasIntegrationTest.java @@ -0,0 +1,55 @@ +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.equalTo; +import static org.hamcrest.Matchers.nullValue; + +@QuarkusTest +@TestProfile(GlobalCorsFilterAliasIntegrationTest.AliasProfile.class) +public class GlobalCorsFilterAliasIntegrationTest { + + @BeforeAll + static void configure() { + RestAssuredJsonUtils.configureAwsContentTypes(); + } + + @Test + void preflightWithAliasEnvVarReturnsOk() { + given() + .header("Origin", "http://localhost:4000") + .header("Access-Control-Request-Method", "PUT") + .when() + .options("/my-bucket/some-key") + .then() + .statusCode(204) + .header("Access-Control-Allow-Origin", equalTo("http://localhost:4000")); + } + + @Test + void preflightFromUnlistedOriginGetsNoGlobalCorsHeaders() { + given() + .header("Origin", "http://localhost:5000") + .header("Access-Control-Request-Method", "PUT") + .when() + .options("/my-bucket/some-key") + .then() + .statusCode(403) + .header("Access-Control-Allow-Origin", nullValue()); + } + + public static final class AliasProfile implements QuarkusTestProfile { + @Override + public Map getConfigOverrides() { + return Map.of("EXTRA_CORS_ALLOWED_ORIGINS", "http://localhost:4000"); + } + } +} diff --git a/src/test/java/io/github/hectorvent/floci/core/common/GlobalCorsFilterIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/core/common/GlobalCorsFilterIntegrationTest.java index da370a51ac..39c09b3f91 100644 --- a/src/test/java/io/github/hectorvent/floci/core/common/GlobalCorsFilterIntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/core/common/GlobalCorsFilterIntegrationTest.java @@ -41,6 +41,33 @@ class GlobalCorsFilterIntegrationTest { .header("Vary", containsString("Origin")); } + @Test + void preflightS3ObjectReturnsCorsHeaders() { + given() + .header("Origin", "http://localhost:3000") + .header("Access-Control-Request-Method", "PUT") + .when() + .options("/my-bucket/some-key") + .then() + .statusCode(204) + .header("Access-Control-Allow-Origin", equalTo("http://localhost:3000")); + } + + @Test + void preflightS3ObjectVirtualHostReturnsCorsHeaders() { + given() + .header("Host", "my-bucket.s3.localhost.floci.io:4566") + .header("Origin", "http://localhost:3000") + .header("Access-Control-Request-Method", "PUT") + .when() + .options("/some-key") + .then() + .statusCode(204) + .header("Access-Control-Allow-Origin", equalTo("http://localhost:3000")); + } + + + @Test void preflightRequestingPrivateNetworkAccessIsGranted() { given() @@ -97,6 +124,18 @@ class GlobalCorsFilterIntegrationTest { .header("Access-Control-Allow-Origin", nullValue()); } + @Test + void preflightFromUnlistedOriginGetsNoGlobalCorsHeaders() { + given() + .header("Origin", "https://not-allowed.example.test") + .header("Access-Control-Request-Method", "PUT") + .when() + .options("/my-bucket/some-key") + .then() + .statusCode(403) + .header("Access-Control-Allow-Origin", nullValue()); + } + public static final class CorsProfile implements QuarkusTestProfile { @Override public Map getConfigOverrides() { @@ -139,4 +178,44 @@ class GlobalCorsFilterIntegrationTest { } } } + + @QuarkusTest + @TestProfile(DisabledGlobalCorsFilterTest.DisabledCorsProfile.class) + static class DisabledGlobalCorsFilterTest { + + @BeforeAll + static void configureRestAssured() { + RestAssuredJsonUtils.configureAwsContentTypes(); + } + + @Test + void preflightReturnsForbiddenWithoutCorsHeaders() { + given() + .header("Origin", "http://localhost:3000") + .header("Access-Control-Request-Method", "PUT") + .when() + .options("/my-bucket/some-key") + .then() + .statusCode(403) + .header("Access-Control-Allow-Origin", nullValue()); + } + + @Test + void actualRequestGetsNoCorsHeaders() { + given() + .header("Origin", "http://localhost:3000") + .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 DisabledCorsProfile implements QuarkusTestProfile { + // No config overrides, testing default application.yml values + } + } } diff --git a/src/test/resources/application.yml b/src/test/resources/application.yml index f3daf8d7a4..21ba4848e3 100644 --- a/src/test/resources/application.yml +++ b/src/test/resources/application.yml @@ -88,6 +88,9 @@ floci: presign-secret: test-secret security: + extra-cors-allowed-origins: ${EXTRA_CORS_ALLOWED_ORIGINS:} + extra-cors-allowed-headers: ${EXTRA_CORS_ALLOWED_HEADERS:} + extra-cors-expose-headers: ${EXTRA_CORS_EXPOSE_HEADERS:} disable-cors-headers: false init-hooks: