mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -05:00
fix(security): map EXTRA_CORS_* env variables to enable global CORS preflight (#1890)
* fix(security): map EXTRA_CORS_* env variables to enable global CORS preflight * test(security): add negative preflight and default-disabled CORS tests
This commit is contained in:
1 parent
b4d80a5596
commit
18044d966f
4 files changed
+140
-3
No files matched your search
@@ -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.
|
||||
|
||||
+55
@@ -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<String, String> getConfigOverrides() {
|
||||
return Map.of("EXTRA_CORS_ALLOWED_ORIGINS", "http://localhost:4000");
|
||||
}
|
||||
}
|
||||
}
|
||||
+79
@@ -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<String, String> 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
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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:
|
||||
|
||||
Reference in new issue
Block a user