diff --git a/docs/configuration/environment-variables.md b/docs/configuration/environment-variables.md index d54b70d49d..ff8806fe82 100644 --- a/docs/configuration/environment-variables.md +++ b/docs/configuration/environment-variables.md @@ -33,6 +33,7 @@ Floci is configured exclusively through environment variables. Every option belo | `FLOCI_SECURITY_EXTRA_CORS_ALLOWED_HEADERS` | _(none)_ | Additional header names to include in `Access-Control-Allow-Headers`. Alias: `EXTRA_CORS_ALLOWED_HEADERS` | | `FLOCI_SECURITY_EXTRA_CORS_EXPOSE_HEADERS` | _(none)_ | Additional header names to include in `Access-Control-Expose-Headers`. Alias: `EXTRA_CORS_EXPOSE_HEADERS` | | `FLOCI_SECURITY_DISABLE_CORS_HEADERS` | `false` | Disable Floci's global CORS response headers. Alias: `DISABLE_CORS_HEADERS` | +| `FLOCI_SECURITY_CORS_ALLOW_PRIVATE_NETWORK` | `false` | Answer Private Network Access preflights with `Access-Control-Allow-Private-Network: true`, letting a page on a public/secure origin reach this loopback backend. Only applies after the origin passes the allow-list above. | --- diff --git a/src/main/java/io/github/hectorvent/floci/config/EmulatorConfig.java b/src/main/java/io/github/hectorvent/floci/config/EmulatorConfig.java index 125d65ed9c..655fe41fa6 100644 --- a/src/main/java/io/github/hectorvent/floci/config/EmulatorConfig.java +++ b/src/main/java/io/github/hectorvent/floci/config/EmulatorConfig.java @@ -126,6 +126,18 @@ public interface EmulatorConfig { @WithDefault("false") boolean disableCorsHeaders(); + + /** + * Whether to grant Private Network Access preflights (respond with + * {@code Access-Control-Allow-Private-Network: true}) when the browser asks. + * Only takes effect after the origin already passes the CORS allow-list, so a + * page served from a public/secure origin can reach this loopback backend. + * + *

Off by default: it lets a public origin reach the private network, so it + * must be opted into explicitly.

+ */ + @WithDefault("false") + boolean corsAllowPrivateNetwork(); } interface StorageConfig { diff --git a/src/main/java/io/github/hectorvent/floci/core/common/GlobalCorsFilter.java b/src/main/java/io/github/hectorvent/floci/core/common/GlobalCorsFilter.java index fa180ebfcd..ad0b61f7b7 100644 --- a/src/main/java/io/github/hectorvent/floci/core/common/GlobalCorsFilter.java +++ b/src/main/java/io/github/hectorvent/floci/core/common/GlobalCorsFilter.java @@ -28,6 +28,11 @@ import java.util.stream.Collectors; * {@code FLOCI_SECURITY_EXTRA_CORS_ALLOWED_ORIGINS}). Service-level CORS * responses, such as S3 bucket CORS rules or API Gateway integrations, keep * precedence because this filter only fills missing CORS headers.

+ * + *

Preflights that request Private Network Access (Chrome's + * {@code Access-Control-Request-Private-Network}) are granted only when opted in + * via {@code FLOCI_SECURITY_CORS_ALLOW_PRIVATE_NETWORK}, letting a UI served from a + * public/secure origin reach a loopback backend.

*/ @Provider @PreMatching @@ -37,6 +42,8 @@ public class GlobalCorsFilter implements ContainerRequestFilter, ContainerRespon private static final String ORIGIN = "Origin"; private static final String REQUEST_METHOD = "Access-Control-Request-Method"; private static final String REQUEST_HEADERS = "Access-Control-Request-Headers"; + private static final String REQUEST_PRIVATE_NETWORK = "Access-Control-Request-Private-Network"; + private static final String ALLOW_PRIVATE_NETWORK = "Access-Control-Allow-Private-Network"; private static final String ALLOW_ORIGIN = "Access-Control-Allow-Origin"; private static final String ALLOW_METHODS = "Access-Control-Allow-Methods"; private static final String ALLOW_HEADERS = "Access-Control-Allow-Headers"; @@ -96,6 +103,14 @@ public class GlobalCorsFilter implements ContainerRequestFilter, ContainerRespon .header(MAX_AGE, "86400") .header(VARY, ORIGIN); + // Private Network Access: a public/secure page (e.g. an HTTPS-hosted UI) + // probing a loopback/private-network backend sends this on the preflight; + // grant it (unless disabled) so the browser doesn't block the follow-up request. + if (s.allowPrivateNetwork() + && "true".equalsIgnoreCase(requestContext.getHeaderString(REQUEST_PRIVATE_NETWORK))) { + builder.header(ALLOW_PRIVATE_NETWORK, "true"); + } + s.exposeHeaders().ifPresent(value -> builder.header(EXPOSE_HEADERS, value)); requestContext.abortWith(builder.build()); } @@ -134,7 +149,8 @@ public class GlobalCorsFilter implements ContainerRequestFilter, ContainerRespon private record CorsSettings(boolean disabled, Set allowedOrigins, Set extraAllowedHeaders, - Set extraExposeHeaders) { + Set extraExposeHeaders, + boolean allowPrivateNetwork) { static CorsSettings from(EmulatorConfig.SecurityConfig security) { Set origins = security.extraCorsAllowedOrigins() @@ -146,7 +162,8 @@ public class GlobalCorsFilter implements ContainerRequestFilter, ContainerRespon Set exposeHeaders = security.extraCorsExposeHeaders() .map(list -> (Set) new LinkedHashSet<>(list)) .orElse(Set.of()); - return new CorsSettings(security.disableCorsHeaders(), origins, allowedHeaders, exposeHeaders); + return new CorsSettings(security.disableCorsHeaders(), origins, allowedHeaders, exposeHeaders, + security.corsAllowPrivateNetwork()); } boolean enabled() { diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index e572a3e48d..69e4c9bb91 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -141,6 +141,9 @@ floci: # extra-cors-allowed-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. + cors-allow-private-network: false tls: enabled: false # FLOCI_TLS_ENABLED (default: false) 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 7f1d75c2cb..da370a51ac 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,32 @@ class GlobalCorsFilterIntegrationTest { .header("Vary", containsString("Origin")); } + @Test + void preflightRequestingPrivateNetworkAccessIsGranted() { + given() + .header("Origin", "http://localhost:3000") + .header("Access-Control-Request-Method", "POST") + .header("Access-Control-Request-Private-Network", "true") + .when() + .options("/") + .then() + .statusCode(204) + .header("Access-Control-Allow-Origin", equalTo("http://localhost:3000")) + .header("Access-Control-Allow-Private-Network", equalTo("true")); + } + + @Test + void preflightWithoutPrivateNetworkRequestOmitsAllowHeader() { + given() + .header("Origin", "http://localhost:3000") + .header("Access-Control-Request-Method", "POST") + .when() + .options("/") + .then() + .statusCode(204) + .header("Access-Control-Allow-Private-Network", nullValue()); + } + @Test void actualRequestFromExtraAllowedOriginGetsCorsHeaders() { given() @@ -77,7 +103,40 @@ class GlobalCorsFilterIntegrationTest { 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"); + "floci.security.extra-cors-expose-headers", "x-visible-header", + "floci.security.cors-allow-private-network", "true"); + } + } + + @QuarkusTest + @TestProfile(PrivateNetworkDefaultOffTest.CorsPnaDefaultProfile.class) + static class PrivateNetworkDefaultOffTest { + + @BeforeAll + static void configureRestAssured() { + RestAssuredJsonUtils.configureAwsContentTypes(); + } + + @Test + void privateNetworkAccessNotGrantedByDefault() { + given() + .header("Origin", "http://localhost:3000") + .header("Access-Control-Request-Method", "POST") + .header("Access-Control-Request-Private-Network", "true") + .when() + .options("/") + .then() + .statusCode(204) + .header("Access-Control-Allow-Origin", equalTo("http://localhost:3000")) + .header("Access-Control-Allow-Private-Network", nullValue()); + } + + // Only origins set — cors-allow-private-network left at its (off) default. + public static final class CorsPnaDefaultProfile implements QuarkusTestProfile { + @Override + public Map getConfigOverrides() { + return Map.of("floci.security.extra-cors-allowed-origins", "http://localhost:3000"); + } } } }