fix(lambda): resolve hot-reload paths before checking the allow-list (#4053)

Normalize the hot-reload S3Key and compare it against the allow-list by directory instead of by string prefix, so dot-dot segments and sibling directories no longer pass. Store the normalized path, reject a colon in it, and warn at startup when hot-reload runs without an allow-list.
This commit is contained in:
Priamo Germosen authored and GitHub committed 2026-09-21 07:53:07 -05:00
1 parent bc9f41df6f
commit 847ad256f2
5 files changed
+186 -6

No files matched your search

+2
View File
@@ -126,6 +126,8 @@ 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.
**Docker Compose setup**: enable the feature and share the Docker socket:
```yaml
@@ -2567,9 +2567,10 @@ public interface EmulatorConfig {
boolean enabled();
/**
* Optional allow-list of absolute path prefixes. When non-empty, the S3Key supplied
* to a hot-reload CreateFunction/UpdateFunctionCode must start with one of these
* prefixes. Empty = all absolute paths are accepted.
* Optional allow-list of absolute directories. When set, the S3Key supplied to a
* hot-reload CreateFunction/UpdateFunctionCode must be one of these directories or
* inside one, compared after {@code .} and {@code ..} segments are resolved.
* Unset = all absolute paths are accepted.
*
* Env var: FLOCI_SERVICES_LAMBDA_HOT_RELOAD_ALLOWED_PATHS
*/
@@ -39,6 +39,7 @@ import org.jboss.logging.Logger;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.InvalidPathException;
import java.nio.file.Path;
import java.time.Instant;
import java.time.ZoneOffset;
@@ -282,6 +283,15 @@ 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");
}
}
/**
@@ -2965,19 +2975,43 @@ public class LambdaService implements ResourceProvider {
throw new AwsException("InvalidParameterValueException",
"Hot-reload S3Key must be an absolute path on the Docker host, got: " + hostPath, 400);
}
if (hostPath.contains(":")) {
throw new AwsException("InvalidParameterValueException",
"Hot-reload S3Key must not contain ':', got: " + hostPath, 400);
}
Path normalized;
try {
normalized = Path.of(hostPath).normalize();
} catch (InvalidPathException e) {
throw new AwsException("InvalidParameterValueException",
"Hot-reload S3Key is not a valid path: " + hostPath, 400);
}
config.services().lambda().hotReload().allowedPaths().ifPresent(allowed -> {
if (allowed.stream().noneMatch(hostPath::startsWith)) {
if (allowed.stream().noneMatch(prefix -> isUnderHotReloadPrefix(normalized, prefix))) {
throw new AwsException("InvalidParameterValueException",
"Path '" + hostPath + "' is not under an allowed hot-reload mount prefix.", 400);
}
});
fn.setHotReloadHostPath(hostPath);
String resolvedHostPath = normalized.toString();
fn.setHotReloadHostPath(resolvedHostPath);
fn.setCodeLocalPath(null);
fn.setS3Bucket(null);
fn.setS3Key(null);
fn.setCodeSizeBytes(0);
fn.setCodeSha256("");
LOG.infov("Hot-reload configured for function {0}: bind-mounting {1}", fn.getFunctionName(), hostPath);
LOG.infov("Hot-reload configured for function {0}: bind-mounting {1}", fn.getFunctionName(), resolvedHostPath);
}
private static boolean isUnderHotReloadPrefix(Path normalizedPath, String prefix) {
if (prefix == null || prefix.isBlank()) {
return false;
}
try {
return normalizedPath.startsWith(Path.of(prefix).normalize());
} catch (InvalidPathException ignored) {
// A malformed prefix can never contain a path, so it does not allow anything.
return false;
}
}
// ──────────────────────────── Permissions (Policy) ────────────────────────────
@@ -0,0 +1,72 @@
package io.github.hectorvent.floci.services.lambda;
import io.quarkus.test.junit.QuarkusTest;
import io.quarkus.test.junit.QuarkusTestProfile;
import io.quarkus.test.junit.TestProfile;
import io.restassured.response.ValidatableResponse;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import java.util.Map;
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.equalTo;
/**
* Hot-reload enabled with an allow-list: a path that only reaches the allowed directory through
* {@code ..}, or that merely shares its prefix, must be refused over the wire.
*/
@QuarkusTest
@TestProfile(LambdaHotReloadAllowListIntegrationTest.HotReloadAllowListProfile.class)
class LambdaHotReloadAllowListIntegrationTest {
private static int counter;
private static ValidatableResponse createFunction(String s3Key) {
String name = "hot-reload-allow-list-" + (++counter);
return given()
.contentType("application/json")
.body("""
{
"FunctionName": "%s",
"Runtime": "python3.12",
"Role": "arn:aws:iam::000000000000:role/r",
"Handler": "handler.handler",
"Code": { "S3Bucket": "hot-reload", "S3Key": "%s" }
}
""".formatted(name, s3Key))
.when()
.post("/2015-03-31/functions")
.then();
}
@ParameterizedTest
@ValueSource(strings = {
"/home/ci/code/../../",
"/home/ci/code/../../../etc",
"/home/ci/code-evil",
"/home/ci/code:/etc"
})
void createFunctionRejectsPathsOutsideTheAllowedDirectory(String s3Key) {
createFunction(s3Key)
.statusCode(400)
.body("__type", equalTo("InvalidParameterValueException"));
}
@ParameterizedTest
@ValueSource(strings = {"/home/ci/code", "/home/ci/code/app"})
void createFunctionAcceptsTheAllowedDirectoryAndItsChildren(String s3Key) {
createFunction(s3Key).statusCode(201);
}
public static final class HotReloadAllowListProfile implements QuarkusTestProfile {
@Override
public Map<String, String> getConfigOverrides() {
return Map.of(
"floci.services.lambda.hot-reload.allowed-paths", "/home/ci/code",
"quarkus.http.test-port", "4589",
"floci.port", "4589",
"floci.base-url", "http://localhost:4589");
}
}
}
@@ -13,6 +13,8 @@ import io.github.hectorvent.floci.services.lambda.zip.CodeStore;
import io.github.hectorvent.floci.services.lambda.zip.ZipExtractor;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import java.io.ByteArrayOutputStream;
import java.nio.charset.StandardCharsets;
@@ -958,6 +960,75 @@ class LambdaServiceTest {
assertTrue(ex.getMessage().contains("allowed"));
}
@ParameterizedTest
@ValueSource(strings = {
"/home/ci/code/../../",
"/home/ci/code/../../../etc",
"/home/ci/code/sub/../../..",
"/home/ci/code/./../secrets",
"/home/ci/code-evil",
"/home/ci/codex/my-fn",
"/home/ci"
})
void hotReload_allowListRejectsTraversalAndSiblingPrefixes(String s3Key) {
LambdaService svc = serviceWithHotReload(true, List.of("/home/ci/code"));
Map<String, Object> req = baseRequest("hr-escape");
req.put("Code", Map.of("S3Bucket", "hot-reload", "S3Key", s3Key));
AwsException ex = assertThrows(AwsException.class, () -> svc.createFunction(REGION, req), s3Key);
assertEquals("InvalidParameterValueException", ex.getErrorCode());
}
@Test
void hotReload_rejectsAColonInThePath() {
LambdaService svc = serviceWithHotReload(true, null);
Map<String, Object> req = baseRequest("hr-colon");
req.put("Code", Map.of("S3Bucket", "hot-reload", "S3Key", "/home/ci/code:/etc"));
AwsException ex = assertThrows(AwsException.class, () -> svc.createFunction(REGION, req));
assertEquals("InvalidParameterValueException", ex.getErrorCode());
}
@Test
void hotReload_allowListAcceptsThePrefixItselfAndItsChildren() {
LambdaService svc = serviceWithHotReload(true, List.of("/home/ci/code"));
Map<String, Object> exact = baseRequest("hr-exact");
exact.put("Code", Map.of("S3Bucket", "hot-reload", "S3Key", "/home/ci/code"));
assertEquals("/home/ci/code", svc.createFunction(REGION, exact).getHotReloadHostPath());
Map<String, Object> child = baseRequest("hr-child");
child.put("Code", Map.of("S3Bucket", "hot-reload", "S3Key", "/home/ci/code/app"));
assertEquals("/home/ci/code/app", svc.createFunction(REGION, child).getHotReloadHostPath());
}
@Test
void hotReload_storesTheNormalizedPathHandedToDocker() {
LambdaService svc = serviceWithHotReload(true, List.of("/home/ci/code"));
Map<String, Object> req = baseRequest("hr-normalized");
req.put("Code", Map.of("S3Bucket", "hot-reload", "S3Key", "/home/ci/code/app/../lib/"));
assertEquals("/home/ci/code/lib", svc.createFunction(REGION, req).getHotReloadHostPath());
}
@Test
void hotReload_traversalIsRejectedOnUpdateFunctionCodeToo() {
LambdaService svc = serviceWithHotReload(true, List.of("/home/ci/code"));
Map<String, Object> create = baseRequest("hr-update");
create.put("Code", Map.of("S3Bucket", "hot-reload", "S3Key", "/home/ci/code/app"));
svc.createFunction(REGION, create);
Map<String, Object> update = new HashMap<>();
update.put("S3Bucket", "hot-reload");
update.put("S3Key", "/home/ci/code/../../");
AwsException ex = assertThrows(AwsException.class,
() -> svc.updateFunctionCode(REGION, "hr-update", update));
assertEquals("InvalidParameterValueException", ex.getErrorCode());
}
@Test
void hotReload_happyPath_setsHostPathAndClearsCodeLocalPath() {
LambdaService svc = serviceWithHotReload(true, null);