mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
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:
1 parent
bc9f41df6f
commit
847ad256f2
5 files changed
+186
-6
No files matched your search
+72
@@ -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);
|
||||
|
||||
Reference in new issue
Block a user