mirror of
https://github.com/floci-io/floci.git
synced 2026-11-23 22:11:36 -05:00
fix(config): enforce safe default network binding (#3684)
* fix: enforce safe default network binding * fix(config): keep signed test authority stable Pin the integration-test listener to the existing all-interface test bind and opt into that bind only in the test profile. This preserves localhost-based SigV4 fixtures while production keeps the loopback default. * fix(config): keep compat image backward compatible * chore: retrigger CI * fix(config): mirror JVM image network binding
This commit is contained in:
1 parent
a1419b9e69
commit
1831f10105
24 files changed
+444
-34
No files matched your search
@@ -209,6 +209,15 @@ public interface EmulatorConfig {
|
||||
*/
|
||||
@WithDefault("false")
|
||||
boolean corsAllowPrivateNetwork();
|
||||
|
||||
/**
|
||||
* Whether Floci may listen outside loopback (127.0.0.0/8, ::1, localhost), through
|
||||
* {@code quarkus.http.host} or the TLS proxy that uses it. Anyone who can reach such an
|
||||
* address can call Floci's APIs, so startup fails unless this is set; see
|
||||
* {@link NetworkExposureGuard}.
|
||||
*/
|
||||
@WithDefault("false")
|
||||
boolean allowUnsafeNetworkExposure();
|
||||
}
|
||||
|
||||
interface StorageConfig {
|
||||
|
||||
@@ -0,0 +1,138 @@
|
||||
package io.github.hectorvent.floci.config;
|
||||
|
||||
import io.quarkus.runtime.StartupEvent;
|
||||
import jakarta.annotation.Priority;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.enterprise.event.Observes;
|
||||
import jakarta.inject.Inject;
|
||||
import jakarta.interceptor.Interceptor;
|
||||
import org.eclipse.microprofile.config.Config;
|
||||
import org.eclipse.microprofile.config.ConfigValue;
|
||||
import org.eclipse.microprofile.config.spi.ConfigSource;
|
||||
|
||||
import java.net.InetAddress;
|
||||
import java.net.UnknownHostException;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
* Refuses to start Floci on an address outside loopback unless
|
||||
* {@code floci.security.allow-unsafe-network-exposure} is set: anyone who can reach Floci's port
|
||||
* can call its APIs.
|
||||
*
|
||||
* <p>The check runs before every other startup observer and before Quarkus opens its HTTP socket.
|
||||
* In TLS mode {@link TlsProxyServer} owns the public port and applies the same check before it binds.
|
||||
*/
|
||||
@ApplicationScoped
|
||||
public class NetworkExposureGuard {
|
||||
|
||||
private static final String HTTP_HOST = "quarkus.http.host";
|
||||
/** Quarkus's own production default, for a configuration that sets no host at all. */
|
||||
private static final String QUARKUS_DEFAULT_HOST = "0.0.0.0";
|
||||
private static final Pattern IPV4 = Pattern.compile("\\d{1,3}(\\.\\d{1,3}){3}");
|
||||
// Starts with a hex digit or a colon, which InetAddress parses as a literal and never resolves.
|
||||
private static final Pattern IPV6 = Pattern.compile("[0-9a-fA-F]*:[0-9a-fA-F:.]*");
|
||||
private static final Pattern HOSTNAME = Pattern.compile(
|
||||
"(?=.{1,253}$)[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?"
|
||||
+ "(?:\\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*");
|
||||
|
||||
private final EmulatorConfig config;
|
||||
private final Config mpConfig;
|
||||
|
||||
@Inject
|
||||
public NetworkExposureGuard(EmulatorConfig config, Config mpConfig) {
|
||||
this.config = config;
|
||||
this.mpConfig = mpConfig;
|
||||
}
|
||||
|
||||
void onStart(@Observes @Priority(Interceptor.Priority.PLATFORM_BEFORE) StartupEvent ignored) {
|
||||
requireConsent(publicHost(mpConfig), config.security());
|
||||
}
|
||||
|
||||
/**
|
||||
* The address Floci's public listener binds: Quarkus itself, or {@link TlsProxyServer} in TLS
|
||||
* mode. {@link TlsConfigSource} pins {@code quarkus.http.host} to loopback for the internal
|
||||
* backends, so when the effective value is that pin the configured host is the next source's.
|
||||
*/
|
||||
static String publicHost(Config config) {
|
||||
ConfigValue effective = config.getConfigValue(HTTP_HOST);
|
||||
if (effective.getValue() != null && !TlsConfigSource.NAME.equals(effective.getSourceName())) {
|
||||
return effective.getValue();
|
||||
}
|
||||
for (ConfigSource source : config.getConfigSources()) {
|
||||
String host = source.getValue(HTTP_HOST);
|
||||
if (host != null && !TlsConfigSource.NAME.equals(source.getName())) {
|
||||
return host;
|
||||
}
|
||||
}
|
||||
return QUARKUS_DEFAULT_HOST;
|
||||
}
|
||||
|
||||
static void requireConsent(String host, EmulatorConfig.SecurityConfig security) {
|
||||
if (!isValidHost(host)) {
|
||||
throw new IllegalStateException("Refusing to listen on malformed host: " + host);
|
||||
}
|
||||
if (isLoopback(host) || security.allowUnsafeNetworkExposure()) {
|
||||
return;
|
||||
}
|
||||
throw new IllegalStateException("Refusing to listen on " + host
|
||||
+ ": it is not a loopback address, so anyone who can reach it can call Floci's APIs."
|
||||
+ " Set quarkus.http.host (QUARKUS_HTTP_HOST) to 127.0.0.1, or set"
|
||||
+ " floci.security.allow-unsafe-network-exposure (FLOCI_SECURITY_ALLOW_UNSAFE_NETWORK_EXPOSURE)"
|
||||
+ " to true to allow it.");
|
||||
}
|
||||
|
||||
/** True for 127.0.0.0/8, ::1 and localhost. Only IP literals are parsed; no name is resolved. */
|
||||
static boolean isLoopback(String host) {
|
||||
String literal = host.strip();
|
||||
if (literal.startsWith("[") && literal.endsWith("]")) {
|
||||
literal = literal.substring(1, literal.length() - 1);
|
||||
}
|
||||
if (literal.equalsIgnoreCase("localhost")) {
|
||||
return true;
|
||||
}
|
||||
if (IPV4.matcher(literal).matches()) {
|
||||
return literal.startsWith("127.");
|
||||
}
|
||||
if (!IPV6.matcher(literal).matches()) {
|
||||
return false;
|
||||
}
|
||||
try {
|
||||
return InetAddress.getByName(literal).isLoopbackAddress();
|
||||
} catch (UnknownHostException e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean isValidHost(String host) {
|
||||
if (host == null || host.isBlank()) {
|
||||
return false;
|
||||
}
|
||||
String literal = host.strip();
|
||||
boolean bracketed = literal.startsWith("[") || literal.endsWith("]");
|
||||
if (bracketed) {
|
||||
if (!(literal.startsWith("[") && literal.endsWith("]"))) {
|
||||
return false;
|
||||
}
|
||||
literal = literal.substring(1, literal.length() - 1);
|
||||
}
|
||||
if (IPV4.matcher(literal).matches()) {
|
||||
for (String part : literal.split("\\.")) {
|
||||
if (Integer.parseInt(part) > 255) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return !bracketed;
|
||||
}
|
||||
if (literal.indexOf(':') >= 0) {
|
||||
if (!IPV6.matcher(literal).matches()) {
|
||||
return false;
|
||||
}
|
||||
try {
|
||||
return InetAddress.getByName(literal).getHostAddress() != null;
|
||||
} catch (UnknownHostException e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return !bracketed && HOSTNAME.matcher(literal).matches();
|
||||
}
|
||||
}
|
||||
@@ -45,6 +45,8 @@ public class TlsConfigSource implements ConfigSource {
|
||||
|
||||
private static final Logger LOG = Logger.getLogger(TlsConfigSource.class);
|
||||
|
||||
static final String NAME = "FlociTlsConfigSource";
|
||||
|
||||
/**
|
||||
* Internal ports Quarkus binds when TLS is enabled. {@link TlsProxyServer} listens on the
|
||||
* public Floci port and routes to these by protocol, so the two classes must agree; they are
|
||||
@@ -169,7 +171,7 @@ public class TlsConfigSource implements ConfigSource {
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "FlociTlsConfigSource";
|
||||
return NAME;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -10,6 +10,7 @@ import io.vertx.core.net.NetSocket;
|
||||
import jakarta.annotation.PreDestroy;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.inject.Inject;
|
||||
import org.eclipse.microprofile.config.Config;
|
||||
import org.jboss.logging.Logger;
|
||||
|
||||
import java.util.ArrayList;
|
||||
@@ -40,6 +41,10 @@ import java.util.concurrent.CompletionException;
|
||||
* so they PUT to 443 regardless of Floci's configured port; binding 443 lets those callbacks
|
||||
* reach Floci. The extra binding is skipped when the port is {@code 0} or equals the public port.
|
||||
*
|
||||
* <p>Both listen on the configured {@code quarkus.http.host}, not the loopback pin
|
||||
* {@link TlsConfigSource} applies to Quarkus itself, and a non-loopback host needs the same consent
|
||||
* as without TLS; see {@link NetworkExposureGuard}.
|
||||
*
|
||||
* <p>This bean is only active when {@code floci.tls.enabled=true}. When TLS is disabled,
|
||||
* Quarkus serves HTTP directly on port 4566 and this proxy is not started.
|
||||
*/
|
||||
@@ -57,6 +62,7 @@ public class TlsProxyServer {
|
||||
|
||||
private final Vertx vertx;
|
||||
private final EmulatorConfig config;
|
||||
private final String host;
|
||||
private final int httpBackendPort;
|
||||
private final int httpsBackendPort;
|
||||
private final List<NetServer> proxyServers = new ArrayList<>();
|
||||
@@ -82,14 +88,15 @@ public class TlsProxyServer {
|
||||
private NetClient client;
|
||||
|
||||
@Inject
|
||||
public TlsProxyServer(Vertx vertx, EmulatorConfig config) {
|
||||
this(vertx, config, HTTP_BACKEND_PORT, HTTPS_BACKEND_PORT);
|
||||
public TlsProxyServer(Vertx vertx, EmulatorConfig config, Config mpConfig) {
|
||||
this(vertx, config, NetworkExposureGuard.publicHost(mpConfig), HTTP_BACKEND_PORT, HTTPS_BACKEND_PORT);
|
||||
}
|
||||
|
||||
/** Visible for testing — lets tests point the proxy at backends on non-default ports. */
|
||||
TlsProxyServer(Vertx vertx, EmulatorConfig config, int httpBackendPort, int httpsBackendPort) {
|
||||
TlsProxyServer(Vertx vertx, EmulatorConfig config, String host, int httpBackendPort, int httpsBackendPort) {
|
||||
this.vertx = vertx;
|
||||
this.config = config;
|
||||
this.host = host;
|
||||
this.httpBackendPort = httpBackendPort;
|
||||
this.httpsBackendPort = httpsBackendPort;
|
||||
startIfTlsEnabled();
|
||||
@@ -100,12 +107,14 @@ public class TlsProxyServer {
|
||||
return;
|
||||
}
|
||||
|
||||
NetworkExposureGuard.requireConsent(host, config.security());
|
||||
|
||||
client = vertx.createNetClient();
|
||||
Handler<NetSocket> connectHandler = buildConnectHandler();
|
||||
|
||||
for (int port : listenPorts()) {
|
||||
NetServerOptions options = new NetServerOptions()
|
||||
.setHost("0.0.0.0")
|
||||
.setHost(host)
|
||||
.setPort(port);
|
||||
NetServer server = vertx.createNetServer(options);
|
||||
server.connectHandler(connectHandler);
|
||||
@@ -114,8 +123,8 @@ public class TlsProxyServer {
|
||||
bind.onComplete(ar -> {
|
||||
if (ar.succeeded()) {
|
||||
failedPorts.remove(port);
|
||||
LOG.infov("TLS proxy: listening on port {0} (HTTP→{1}, HTTPS→{2})",
|
||||
String.valueOf(port), String.valueOf(httpBackendPort), String.valueOf(httpsBackendPort));
|
||||
LOG.infov("TLS proxy: listening on {0}:{1} (HTTP→{2}, HTTPS→{3})",
|
||||
host, String.valueOf(port), String.valueOf(httpBackendPort), String.valueOf(httpsBackendPort));
|
||||
} else if (port == config.port()) {
|
||||
LOG.errorv("TLS proxy: failed to start on public port {0}: {1}",
|
||||
String.valueOf(port), ar.cause().getMessage());
|
||||
|
||||
Reference in new issue
Block a user