mirror of
https://github.com/floci-io/floci.git
synced 2026-12-04 18:48:00 -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
@@ -56,6 +56,22 @@ class ApplicationDefaultsTest {
|
||||
"the EmulatorConfig fallback should match the AWS SQS maximum too");
|
||||
}
|
||||
|
||||
@Test
|
||||
void productionConfigListensOnLoopbackWithoutNetworkExposureConsent() throws IOException, NoSuchMethodException {
|
||||
JsonNode config = new YAMLMapper().readTree(Path.of("src/main/resources/application.yml").toFile());
|
||||
|
||||
assertEquals("127.0.0.1", config.path("quarkus").path("http").path("host").asText(),
|
||||
"production application.yml should listen on loopback by default");
|
||||
assertFalse(config.path("floci").path("security").path("allow-unsafe-network-exposure").asBoolean(true),
|
||||
"production application.yml should not allow a non-loopback listener by default");
|
||||
|
||||
WithDefault fallback = EmulatorConfig.SecurityConfig.class
|
||||
.getMethod("allowUnsafeNetworkExposure")
|
||||
.getAnnotation(WithDefault.class);
|
||||
assertNotNull(fallback, "allowUnsafeNetworkExposure should declare a fallback default");
|
||||
assertEquals("false", fallback.value(), "the EmulatorConfig fallback should refuse exposure too");
|
||||
}
|
||||
|
||||
@Test
|
||||
void productionConfigDoesNotSeedIamDeployerPrincipalByDefault() throws IOException {
|
||||
JsonNode config = new YAMLMapper().readTree(Path.of("src/main/resources/application.yml").toFile());
|
||||
|
||||
+1
-1
@@ -112,7 +112,7 @@ class IotMqttWebSocketProxyIntegrationTest {
|
||||
when(config.tls()).thenReturn(tls);
|
||||
when(tls.enabled()).thenReturn(true);
|
||||
when(tls.awsHttpsPort()).thenReturn(0);
|
||||
proxy = new TlsProxyServer(vertx, config, testHttpPort, testSslPort);
|
||||
proxy = new TlsProxyServer(vertx, config, "127.0.0.1", testHttpPort, testSslPort);
|
||||
long deadline = System.nanoTime() + TimeUnit.SECONDS.toNanos(10);
|
||||
while (true) {
|
||||
try (Socket probe = new Socket("127.0.0.1", publicPort)) {
|
||||
|
||||
@@ -0,0 +1,107 @@
|
||||
package io.github.hectorvent.floci.config;
|
||||
|
||||
import io.smallrye.config.PropertiesConfigSource;
|
||||
import io.smallrye.config.SmallRyeConfig;
|
||||
import io.smallrye.config.SmallRyeConfigBuilder;
|
||||
import org.eclipse.microprofile.config.Config;
|
||||
import org.eclipse.microprofile.config.ConfigValue;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
|
||||
import java.nio.file.Path;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
class NetworkExposureGuardTest {
|
||||
|
||||
@TempDir
|
||||
Path tempDir;
|
||||
|
||||
@AfterEach
|
||||
void cleanup() {
|
||||
System.clearProperty("floci.tls.enabled");
|
||||
System.clearProperty("floci.tls.self-signed");
|
||||
System.clearProperty("floci.storage.persistent-path");
|
||||
}
|
||||
|
||||
private static EmulatorConfig.SecurityConfig security(boolean allowExposure) {
|
||||
EmulatorConfig.SecurityConfig security = mock(EmulatorConfig.SecurityConfig.class);
|
||||
when(security.allowUnsafeNetworkExposure()).thenReturn(allowExposure);
|
||||
return security;
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"127.0.0.1", "127.10.0.3", "::1", "[::1]", "localhost", "LOCALHOST"})
|
||||
void loopbackHostNeedsNoConsent(String host) {
|
||||
assertDoesNotThrow(() -> NetworkExposureGuard.requireConsent(host, security(false)));
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"0.0.0.0", "::", "192.168.1.20", "172.17.0.1", "floci.internal"})
|
||||
void nonLoopbackHostWithoutConsentNamesTheSettingToChange(String host) {
|
||||
IllegalStateException e = assertThrows(IllegalStateException.class,
|
||||
() -> NetworkExposureGuard.requireConsent(host, security(false)));
|
||||
|
||||
assertTrue(e.getMessage().contains(host), e.getMessage());
|
||||
assertTrue(e.getMessage().contains("floci.security.allow-unsafe-network-exposure"), e.getMessage());
|
||||
assertTrue(e.getMessage().contains("FLOCI_SECURITY_ALLOW_UNSAFE_NETWORK_EXPOSURE"), e.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void nonLoopbackHostWithConsentIsAllowed() {
|
||||
assertDoesNotThrow(() -> NetworkExposureGuard.requireConsent("0.0.0.0", security(true)));
|
||||
}
|
||||
|
||||
@Test
|
||||
void startupRefusesMalformedHostEvenWithNetworkExposureConsent() {
|
||||
Config config = mock(Config.class);
|
||||
ConfigValue host = mock(ConfigValue.class);
|
||||
when(host.getValue()).thenReturn("999.999.999.999");
|
||||
when(config.getConfigValue("quarkus.http.host")).thenReturn(host);
|
||||
EmulatorConfig emulatorConfig = mock(EmulatorConfig.class);
|
||||
EmulatorConfig.SecurityConfig security = security(true);
|
||||
when(emulatorConfig.security()).thenReturn(security);
|
||||
|
||||
NetworkExposureGuard guard = new NetworkExposureGuard(emulatorConfig, config);
|
||||
|
||||
IllegalStateException exception = assertThrows(IllegalStateException.class,
|
||||
() -> guard.onStart(null));
|
||||
|
||||
assertTrue(exception.getMessage().contains("malformed"), exception.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void publicHostIsTheConfiguredHost() {
|
||||
SmallRyeConfig config = new SmallRyeConfigBuilder()
|
||||
.withSources(new PropertiesConfigSource(Map.of("quarkus.http.host", "127.0.0.1"), "yml", 255))
|
||||
.withSources(new PropertiesConfigSource(Map.of("quarkus.http.host", "0.0.0.0"), "sysprops", 400))
|
||||
.build();
|
||||
|
||||
assertEquals("0.0.0.0", NetworkExposureGuard.publicHost(config));
|
||||
}
|
||||
|
||||
@Test
|
||||
void publicHostIgnoresTheLoopbackPinTlsModeAppliesToQuarkus() {
|
||||
System.setProperty("floci.tls.enabled", "true");
|
||||
System.setProperty("floci.tls.self-signed", "true");
|
||||
System.setProperty("floci.storage.persistent-path", tempDir.toString());
|
||||
SmallRyeConfig config = new SmallRyeConfigBuilder()
|
||||
.withSources(new TlsConfigSource())
|
||||
.withSources(new PropertiesConfigSource(Map.of("quarkus.http.host", "0.0.0.0"), "yml", 255))
|
||||
.build();
|
||||
|
||||
assertEquals("127.0.0.1", config.getValue("quarkus.http.host", String.class),
|
||||
"precondition: TLS mode keeps Quarkus itself on loopback behind the proxy");
|
||||
assertEquals("0.0.0.0", NetworkExposureGuard.publicHost(config),
|
||||
"the proxy must listen where the user asked, not on the internal pin");
|
||||
}
|
||||
}
|
||||
@@ -43,7 +43,7 @@ class TlsProxyServerReservedPortTest {
|
||||
when(vertx.createNetClient()).thenReturn(mock(NetClient.class));
|
||||
when(vertx.createNetServer(any())).thenReturn(server);
|
||||
|
||||
return new TlsProxyServer(vertx, config, 4510, 4511);
|
||||
return new TlsProxyServer(vertx, config, "127.0.0.1", 4510, 4511);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -76,7 +76,7 @@ class TlsProxyServerReservedPortTest {
|
||||
when(vertx.createNetClient()).thenReturn(mock(NetClient.class));
|
||||
when(vertx.createNetServer(any())).thenReturn(server);
|
||||
|
||||
TlsProxyServer proxy = new TlsProxyServer(vertx, config, 4510, 4511);
|
||||
TlsProxyServer proxy = new TlsProxyServer(vertx, config, "127.0.0.1", 4510, 4511);
|
||||
beforeStart.accept(proxy);
|
||||
return proxy;
|
||||
}
|
||||
|
||||
@@ -8,17 +8,25 @@ import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.Timeout;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.Inet4Address;
|
||||
import java.net.InetAddress;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.NetworkInterface;
|
||||
import java.net.ServerSocket;
|
||||
import java.net.Socket;
|
||||
import java.net.SocketException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Collections;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.junit.jupiter.api.Assertions.fail;
|
||||
import static org.junit.jupiter.api.Assumptions.assumeTrue;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@@ -62,27 +70,66 @@ class TlsProxyServerTest {
|
||||
when(config.tls()).thenReturn(tls);
|
||||
when(tls.enabled()).thenReturn(tlsEnabled);
|
||||
when(tls.awsHttpsPort()).thenReturn(awsHttpsPort);
|
||||
when(config.security()).thenReturn(mock(EmulatorConfig.SecurityConfig.class));
|
||||
return config;
|
||||
}
|
||||
|
||||
@Test
|
||||
void listenPorts_includesPublicAndAwsHttpsPort() {
|
||||
proxy = new TlsProxyServer(vertx, configWith(false, 4566, 443), 4510, 4511);
|
||||
proxy = new TlsProxyServer(vertx, configWith(false, 4566, 443), "127.0.0.1", 4510, 4511);
|
||||
assertEquals(Set.of(4566, 443), proxy.listenPorts());
|
||||
}
|
||||
|
||||
@Test
|
||||
void listenPorts_dropsAwsHttpsPortWhenZero() {
|
||||
proxy = new TlsProxyServer(vertx, configWith(false, 4566, 0), 4510, 4511);
|
||||
proxy = new TlsProxyServer(vertx, configWith(false, 4566, 0), "127.0.0.1", 4510, 4511);
|
||||
assertEquals(Set.of(4566), proxy.listenPorts());
|
||||
}
|
||||
|
||||
@Test
|
||||
void listenPorts_dedupesWhenAwsHttpsPortEqualsPublic() {
|
||||
proxy = new TlsProxyServer(vertx, configWith(false, 4566, 4566), 4510, 4511);
|
||||
proxy = new TlsProxyServer(vertx, configWith(false, 4566, 4566), "127.0.0.1", 4510, 4511);
|
||||
assertEquals(Set.of(4566), proxy.listenPorts());
|
||||
}
|
||||
|
||||
@Test
|
||||
@Timeout(20)
|
||||
void bindsTheConfiguredHost_notEveryInterface() throws Exception {
|
||||
InetAddress external = nonLoopbackIpv4();
|
||||
assumeTrue(external != null, "needs a non-loopback IPv4 interface");
|
||||
int publicPort = freePort();
|
||||
|
||||
proxy = new TlsProxyServer(vertx, configWith(true, publicPort, 0), "127.0.0.1", 4510, 4511);
|
||||
|
||||
awaitListening(publicPort);
|
||||
assertFalse(accepts(external, publicPort),
|
||||
"a proxy configured for 127.0.0.1 must not be reachable on " + external.getHostAddress());
|
||||
}
|
||||
|
||||
@Test
|
||||
@Timeout(20)
|
||||
void nonLoopbackHostWithoutConsent_refusesToListen() throws Exception {
|
||||
int publicPort = freePort();
|
||||
|
||||
IllegalStateException e = assertThrows(IllegalStateException.class,
|
||||
() -> new TlsProxyServer(vertx, configWith(true, publicPort, 0), "0.0.0.0", 4510, 4511));
|
||||
|
||||
assertTrue(e.getMessage().contains("FLOCI_SECURITY_ALLOW_UNSAFE_NETWORK_EXPOSURE"), e.getMessage());
|
||||
assertFalse(portAccepts(publicPort), "a refused proxy must not listen");
|
||||
}
|
||||
|
||||
@Test
|
||||
@Timeout(20)
|
||||
void nonLoopbackHostWithConsent_listens() throws Exception {
|
||||
int publicPort = freePort();
|
||||
EmulatorConfig config = configWith(true, publicPort, 0);
|
||||
when(config.security().allowUnsafeNetworkExposure()).thenReturn(true);
|
||||
|
||||
proxy = new TlsProxyServer(vertx, config, "0.0.0.0", 4510, 4511);
|
||||
|
||||
awaitListening(publicPort);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Timeout(20)
|
||||
void bindsBothPorts_andRoutesByFirstByte() throws Exception {
|
||||
@@ -93,7 +140,7 @@ class TlsProxyServerTest {
|
||||
httpBackend = startMarkerBackend(httpBe, "PLAIN");
|
||||
httpsBackend = startMarkerBackend(httpsBe, "TLS");
|
||||
|
||||
proxy = new TlsProxyServer(vertx, configWith(true, publicPort, awsHttpsPort), httpBe, httpsBe);
|
||||
proxy = new TlsProxyServer(vertx, configWith(true, publicPort, awsHttpsPort), "127.0.0.1", httpBe, httpsBe);
|
||||
|
||||
// Public port: TLS ClientHello → HTTPS backend; anything else → HTTP backend.
|
||||
assertEquals("TLS", roundTrip(publicPort, TLS_HANDSHAKE));
|
||||
@@ -107,7 +154,7 @@ class TlsProxyServerTest {
|
||||
@Timeout(20)
|
||||
void tlsDisabled_bindsNothing() throws Exception {
|
||||
int publicPort = freePort();
|
||||
proxy = new TlsProxyServer(vertx, configWith(false, publicPort, 443), 4510, 4511);
|
||||
proxy = new TlsProxyServer(vertx, configWith(false, publicPort, 443), "127.0.0.1", 4510, 4511);
|
||||
assertFalse(portAccepts(publicPort), "no proxy should listen when TLS is disabled");
|
||||
}
|
||||
|
||||
@@ -162,6 +209,29 @@ class TlsProxyServerTest {
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean accepts(InetAddress address, int port) {
|
||||
try (Socket s = new Socket()) {
|
||||
s.connect(new InetSocketAddress(address, port), 1000);
|
||||
return true;
|
||||
} catch (IOException e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private static InetAddress nonLoopbackIpv4() throws SocketException {
|
||||
for (NetworkInterface nic : Collections.list(NetworkInterface.getNetworkInterfaces())) {
|
||||
if (!nic.isUp() || nic.isLoopback()) {
|
||||
continue;
|
||||
}
|
||||
for (InetAddress address : Collections.list(nic.getInetAddresses())) {
|
||||
if (address instanceof Inet4Address) {
|
||||
return address;
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private static void closeBackend(NetServer server) {
|
||||
if (server != null) {
|
||||
server.close();
|
||||
|
||||
+2
-1
@@ -159,7 +159,8 @@ class ContainerCaBundleDockerIntegrationTest {
|
||||
"quarkus.tls.key-store.pem.0.cert", CERT_FILE.toString(),
|
||||
"quarkus.tls.key-store.pem.0.key", KEY_FILE.toString(),
|
||||
"quarkus.http.insecure-requests", "enabled",
|
||||
"quarkus.http.host", "0.0.0.0");
|
||||
"quarkus.http.host", "0.0.0.0",
|
||||
"floci.security.allow-unsafe-network-exposure", "true");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user