feat(core): add opt-in Private Network Access for CORS preflights (#1530)

This commit is contained in:
Jongsic Choi authored and GitHub committed 2026-06-25 00:56:42 -05:00
1 parent 05d050190e
commit 398535ed55
5 files changed
+95 -3

No files matched your search

@@ -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. |
---
@@ -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.
*
* <p>Off by default: it lets a public origin reach the private network, so it
* must be opted into explicitly.</p>
*/
@WithDefault("false")
boolean corsAllowPrivateNetwork();
}
interface StorageConfig {
@@ -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.</p>
*
* <p>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.</p>
*/
@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<String> allowedOrigins,
Set<String> extraAllowedHeaders,
Set<String> extraExposeHeaders) {
Set<String> extraExposeHeaders,
boolean allowPrivateNetwork) {
static CorsSettings from(EmulatorConfig.SecurityConfig security) {
Set<String> origins = security.extraCorsAllowedOrigins()
@@ -146,7 +162,8 @@ public class GlobalCorsFilter implements ContainerRequestFilter, ContainerRespon
Set<String> exposeHeaders = security.extraCorsExposeHeaders()
.map(list -> (Set<String>) 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() {
+3
View File
@@ -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)
@@ -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<String, String> getConfigOverrides() {
return Map.of("floci.security.extra-cors-allowed-origins", "http://localhost:3000");
}
}
}
}