diff --git a/docs/services/lambda.md b/docs/services/lambda.md index 54a140f4db..49a4cbfc86 100644 --- a/docs/services/lambda.md +++ b/docs/services/lambda.md @@ -92,7 +92,7 @@ aws lambda invoke --function-name my-function out.json ## Hot-Reload via Bind Mount -For the tightest inner-loop development cycle, Floci supports a **bind-mount hot-reload** mode. Instead of packaging code into a ZIP and uploading it to S3, you point Floci directly at a directory on your host machine. The directory is bind-mounted into `/var/task` inside the container, so every invocation runs the files as they currently exist on disk, with no upload or redeploy. +For the tightest inner-loop development cycle, Floci supports a **bind-mount hot-reload** mode. Instead of packaging code into a ZIP and uploading it to S3, you point Floci directly at a directory on your host machine. The directory is bind-mounted read-only into `/var/task` inside the container, as `/var/task` is read-only in AWS Lambda, so every invocation runs the files as they currently exist on disk, with no upload or redeploy. This is enabled by using the magic bucket name `hot-reload` when creating a function: @@ -126,7 +126,7 @@ FLOCI_SERVICES_LAMBDA_HOT_RELOAD_ENABLED=true FLOCI_SERVICES_LAMBDA_HOT_RELOAD_ALLOWED_PATHS=/home/user/projects,/tmp ``` -An `S3Key` is accepted when it is one of the listed directories or inside one. `.` and `..` segments are resolved first, so `/home/user/projects/../secrets` and a sibling such as `/home/user/projects-old` are rejected. Symbolic links on the Docker host are not resolved by Floci. A path containing `:` is rejected. Without an allow-list any absolute host path is accepted, and Floci logs a warning at startup when hot-reload is enabled that way. +An `S3Key` is accepted when it is one of the listed directories or inside one. `.` and `..` segments are resolved first, so `/home/user/projects/../secrets` and a sibling such as `/home/user/projects-old` are rejected. Symbolic links on the Docker host are not resolved by Floci. A path containing `:` is rejected. Without an allow-list any absolute host path is accepted except the host root, `/var`, `/proc`, and anything under `/run` or `/var/run`, where the Docker socket lives or can be reached. Floci logs a warning at startup when hot-reload is enabled without an allow-list. **Docker Compose setup**: enable the feature and share the Docker socket: diff --git a/src/main/java/io/github/hectorvent/floci/config/HotReloadExposureWarning.java b/src/main/java/io/github/hectorvent/floci/config/HotReloadExposureWarning.java new file mode 100644 index 0000000000..b545a19460 --- /dev/null +++ b/src/main/java/io/github/hectorvent/floci/config/HotReloadExposureWarning.java @@ -0,0 +1,41 @@ +package io.github.hectorvent.floci.config; + +import io.quarkus.runtime.StartupEvent; +import jakarta.enterprise.context.ApplicationScoped; +import jakarta.enterprise.event.Observes; +import jakarta.inject.Inject; +import org.jboss.logging.Logger; + +/** + * Warns at startup when Lambda hot-reload is enabled with no allow-list, because any absolute host + * path is then a legal bind-mount target. Unlike {@link NetworkExposureGuard} this only warns: the + * combination is the documented default of the published compose file, so refusing to start would + * break it. + * + *
Startup rather than first use: the operator who most needs the message is the one who has not
+ * touched Lambda yet.
+ */
+@ApplicationScoped
+public class HotReloadExposureWarning {
+
+ private static final Logger LOG = Logger.getLogger(HotReloadExposureWarning.class);
+
+ private final EmulatorConfig config;
+
+ @Inject
+ public HotReloadExposureWarning(EmulatorConfig config) {
+ this.config = config;
+ }
+
+ void onStart(@Observes StartupEvent ignored) {
+ if (hotReloadAcceptsAnyPath(config)) {
+ LOG.warn("Lambda hot-reload is enabled without FLOCI_SERVICES_LAMBDA_HOT_RELOAD_ALLOWED_PATHS: "
+ + "any absolute path on the Docker host can be bind-mounted into a function container");
+ }
+ }
+
+ static boolean hotReloadAcceptsAnyPath(EmulatorConfig config) {
+ EmulatorConfig.LambdaServiceConfig.HotReload hotReload = config.services().lambda().hotReload();
+ return hotReload.enabled() && hotReload.allowedPaths().isEmpty();
+ }
+}
diff --git a/src/main/java/io/github/hectorvent/floci/services/lambda/LambdaService.java b/src/main/java/io/github/hectorvent/floci/services/lambda/LambdaService.java
index 67fb198a53..e0b837aa23 100644
--- a/src/main/java/io/github/hectorvent/floci/services/lambda/LambdaService.java
+++ b/src/main/java/io/github/hectorvent/floci/services/lambda/LambdaService.java
@@ -283,15 +283,6 @@ public class LambdaService implements ResourceProvider {
void init() {
initializeStorage();
rehydrateConcurrency();
- warnWhenHotReloadHasNoAllowList();
- }
-
- private void warnWhenHotReloadHasNoAllowList() {
- if (config != null && config.services().lambda().hotReload().enabled()
- && config.services().lambda().hotReload().allowedPaths().isEmpty()) {
- LOG.warn("Lambda hot-reload is enabled without FLOCI_SERVICES_LAMBDA_HOT_RELOAD_ALLOWED_PATHS: "
- + "any absolute path on the Docker host can be bind-mounted into a function container");
- }
}
/**
@@ -2992,6 +2983,11 @@ public class LambdaService implements ResourceProvider {
"Path '" + hostPath + "' is not under an allowed hot-reload mount prefix.", 400);
}
});
+ if (config.services().lambda().hotReload().allowedPaths().isEmpty() && reachesDockerSocketDirectory(normalized)) {
+ throw new AwsException("InvalidParameterValueException",
+ "Path '" + hostPath + "' can expose the Docker socket. Set "
+ + "FLOCI_SERVICES_LAMBDA_HOT_RELOAD_ALLOWED_PATHS to mount it.", 400);
+ }
String resolvedHostPath = toDockerHostPath(normalized);
fn.setHotReloadHostPath(resolvedHostPath);
fn.setCodeLocalPath(null);
@@ -3011,6 +3007,19 @@ public class LambdaService implements ResourceProvider {
return path.length() == 0 ? "/" : path.toString();
}
+ /**
+ * True for the host root, {@code /var}, and anything in {@code /run} or {@code /var/run}, where the
+ * Docker socket lives. {@code /proc} counts too: {@code /proc/1/root/var/run} is a different string
+ * for the same directory, and no code directory lives there either.
+ */
+ static boolean reachesDockerSocketDirectory(Path normalized) {
+ return normalized.getNameCount() == 0
+ || normalized.equals(Path.of("/var"))
+ || normalized.startsWith(Path.of("/run"))
+ || normalized.startsWith(Path.of("/var/run"))
+ || normalized.startsWith(Path.of("/proc"));
+ }
+
private static boolean isUnderHotReloadPrefix(Path normalizedPath, String prefix) {
if (prefix == null || prefix.isBlank()) {
return false;
diff --git a/src/main/java/io/github/hectorvent/floci/services/lambda/launcher/ContainerLauncher.java b/src/main/java/io/github/hectorvent/floci/services/lambda/launcher/ContainerLauncher.java
index 98829615d7..87d230edad 100644
--- a/src/main/java/io/github/hectorvent/floci/services/lambda/launcher/ContainerLauncher.java
+++ b/src/main/java/io/github/hectorvent/floci/services/lambda/launcher/ContainerLauncher.java
@@ -291,7 +291,7 @@ public class ContainerLauncher implements LambdaRuntimeLauncher {
// and the create->start copy block agree.
boolean useCodeVolume = false;
if (fn.isHotReload()) {
- specBuilder.withBind(fn.getHotReloadHostPath(), TASK_DIR);
+ specBuilder.withReadOnlyBind(fn.getHotReloadHostPath(), TASK_DIR);
} else if (fn.getCodeLocalPath() != null) {
useCodeVolume = shouldUseCodeVolume(Path.of(fn.getCodeLocalPath()));
if (useCodeVolume) {
diff --git a/src/test/java/io/github/hectorvent/floci/config/HotReloadExposureWarningTest.java b/src/test/java/io/github/hectorvent/floci/config/HotReloadExposureWarningTest.java
new file mode 100644
index 0000000000..e3c76585a8
--- /dev/null
+++ b/src/test/java/io/github/hectorvent/floci/config/HotReloadExposureWarningTest.java
@@ -0,0 +1,43 @@
+package io.github.hectorvent.floci.config;
+
+import org.junit.jupiter.api.Test;
+
+import java.util.List;
+import java.util.Optional;
+
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+class HotReloadExposureWarningTest {
+
+ private static EmulatorConfig configWith(boolean enabled, List