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