Light, fluffy, and always free - The AWS Local Emulator alternative
fix(elbv2): preserve repeated target response headers (#5628)
Mohith15f50cb68e1e
diff --git a/docs/services/elb.md b/docs/services/elb.mdindex 4ecfc2bf03..194c974204 100644--- a/docs/services/elb.md+++ b/docs/services/elb.md@@ -97,6 +97,7 @@ Floci supports Application Load Balancers (ALB) and Network Load Balancers (NLB)- `DeleteRule` is rejected with `OperationNotPermitted` for the default rule.- `DescribeSSLPolicies` returns a pre-seeded list of standard AWS SSL policies (`ELBSecurityPolicy-*`).- `DescribeAccountLimits` returns standard default limits (e.g., 50 load balancers per region, 100 target groups, etc.).+- Responses from HTTP targets preserve repeated header values, including multiple `Set-Cookie` headers. This also applies when a target rejects a WebSocket upgrade.- `routing.http.preserve_host_header.enabled` (default `false`) controls whether the original client Host header is forwarded to targets unchanged, or replaced with the target's `host:port`.- `RegisterTargets` rejects link-local and cloud instance-metadata addresses (`169.254.0.0/16`, `fe80::/10`, `fd00:ec2::254`) with `InvalidTarget`, and the load balancer and its health checks refuse to connect to them. Loopback and private addresses stay reachable so a load balancer can front a neighbouring container.diff --git a/src/main/java/io/github/hectorvent/floci/services/elbv2/ElbV2DataPlane.java b/src/main/java/io/github/hectorvent/floci/services/elbv2/ElbV2DataPlane.javaindex 04767cb5ba..a3f3481e59 100644--- a/src/main/java/io/github/hectorvent/floci/services/elbv2/ElbV2DataPlane.java+++ b/src/main/java/io/github/hectorvent/floci/services/elbv2/ElbV2DataPlane.java@@ -15,6 +15,7 @@ import io.github.hectorvent.floci.services.elbv2.model.TargetGroup;import io.github.hectorvent.floci.services.lambda.LambdaService;import io.github.hectorvent.floci.services.lambda.model.InvocationType;import io.github.hectorvent.floci.services.lambda.model.InvokeResult;+import io.vertx.core.MultiMap;import io.vertx.core.Vertx;import io.vertx.core.buffer.Buffer;import io.vertx.core.http.HttpClient;@@ -23,6 +24,7 @@ import io.vertx.core.http.HttpMethod;import io.vertx.core.http.HttpServer;import io.vertx.core.http.HttpServerOptions;import io.vertx.core.http.HttpServerRequest;+import io.vertx.core.http.HttpServerResponse;import io.vertx.core.http.HttpVersion;import io.vertx.core.http.RequestOptions;import io.vertx.core.net.NetSocket;@@ -39,6 +41,7 @@ import java.util.ArrayList;import java.util.Base64;import java.util.HashMap;import java.util.List;+import java.util.Locale;import java.util.Map;import java.util.concurrent.ConcurrentHashMap;import java.util.concurrent.atomic.AtomicBoolean;@@ -738,11 +741,7 @@ public class ElbV2DataPlane {}responseStarted.set(true);req.response().setStatusCode(resp.statusCode());- resp.headers().forEach(entry -> {- if (!HOP_BY_HOP_HEADERS.contains(entry.getKey().toLowerCase())) {- req.response().putHeader(entry.getKey(), entry.getValue());- }- });+ copyTargetResponseHeaders(resp.headers(), req.response());if (resp.getHeader("Content-Length") == null) {req.response().setChunked(true);}@@ -775,6 +774,14 @@ public class ElbV2DataPlane {});}+ private static void copyTargetResponseHeaders(MultiMap headers, HttpServerResponse response) {+ for (String name : headers.names()) {+ if (!HOP_BY_HOP_HEADERS.contains(name.toLowerCase(Locale.ROOT))) {+ response.putHeader(name, headers.getAll(name));+ }+ }+ }+/*** An ALB forwards a WebSocket handshake to an instance or ip target with its upgrade headers* intact, which an ordinary proxied request cannot do: {@code Connection} and {@code Upgrade}@@ -842,11 +849,7 @@ public class ElbV2DataPlane {clientReq.connect().onSuccess(resp -> {req.response().setStatusCode(resp.statusCode());- resp.headers().forEach(entry -> {- if (!HOP_BY_HOP_HEADERS.contains(entry.getKey().toLowerCase())) {- req.response().putHeader(entry.getKey(), entry.getValue());- }- });+ copyTargetResponseHeaders(resp.headers(), req.response());if (resp.statusCode() != 101) {req.resume();if (resp.getHeader("Content-Length") == null) {diff --git a/src/test/java/io/github/hectorvent/floci/services/elbv2/ElbV2PreserveHostHeaderIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/elbv2/ElbV2PreserveHostHeaderIntegrationTest.javaindex ddc2fb0735..06aa364f33 100644--- a/src/test/java/io/github/hectorvent/floci/services/elbv2/ElbV2PreserveHostHeaderIntegrationTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/elbv2/ElbV2PreserveHostHeaderIntegrationTest.java@@ -3,6 +3,7 @@ package io.github.hectorvent.floci.services.elbv2;import io.github.hectorvent.floci.testing.RealElbV2DataPlaneProfile;import io.quarkus.test.junit.QuarkusTest;import io.quarkus.test.junit.TestProfile;+import io.restassured.response.Response;import io.vertx.core.Vertx;import io.vertx.core.buffer.Buffer;import io.vertx.core.http.HttpClientRequest;@@ -20,6 +21,7 @@ import java.net.ServerSocket;import java.net.Socket;import java.nio.charset.StandardCharsets;import java.time.Duration;+import java.util.List;import java.util.concurrent.CompletableFuture;import java.util.concurrent.TimeUnit;@@ -41,6 +43,43 @@ class ElbV2PreserveHostHeaderIntegrationTest {@InjectVertx vertx;+ @Test+ void preservesRepeatedTargetResponseHeaders() throws Exception {+ HttpServer backend = vertx.createHttpServer().requestHandler(request -> {+ request.response().headers()+ .add("Set-Cookie", "a=1; Path=/")+ .add("Set-Cookie", "b=2; Path=/")+ .add("Set-Cookie", "c=3; Path=/")+ .add("X-Multi", "one")+ .add("X-Multi", "two")+ .add("Keep-Alive", "timeout=5");+ request.response().end("ok");+ }).listen(0, "127.0.0.1").toCompletionStage().toCompletableFuture().get(2, TimeUnit.SECONDS);+ String loadBalancerArn = null;+ String targetGroupArn = null;+ String listenerArn = null;+ try {+ loadBalancerArn = createLoadBalancer();+ targetGroupArn = createTargetGroup(backend.actualPort());+ registerTarget(targetGroupArn, backend.actualPort());+ listenerArn = createListener(loadBalancerArn, targetGroupArn);+ await().atMost(Duration.ofSeconds(3)).untilAsserted(() -> {+ Response response = given().baseUri("http://127.0.0.1").port(LISTENER_PORT).get("/headers");+ assertEquals(200, response.statusCode());+ assertEquals("ok", response.asString());+ assertEquals(List.of("a=1; Path=/", "b=2; Path=/", "c=3; Path=/"),+ response.headers().getValues("Set-Cookie"));+ assertEquals(List.of("one", "two"), response.headers().getValues("X-Multi"));+ assertEquals(List.of(), response.headers().getValues("Keep-Alive"));+ });+ } finally {+ deleteListener(listenerArn);+ deleteTargetGroup(targetGroupArn);+ deleteLoadBalancer(loadBalancerArn);+ backend.close().toCompletionStage().toCompletableFuture().get(2, TimeUnit.SECONDS);+ }+ }+@Testvoid preservesHostHeaderWhenLoadBalancerAttributeIsEnabled() throws Exception {HttpServer backend = vertx.createHttpServer()diff --git a/src/test/java/io/github/hectorvent/floci/services/elbv2/ElbV2WebSocketIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/elbv2/ElbV2WebSocketIntegrationTest.javaindex 9a3c9c1f90..32a1884a76 100644--- a/src/test/java/io/github/hectorvent/floci/services/elbv2/ElbV2WebSocketIntegrationTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/elbv2/ElbV2WebSocketIntegrationTest.java@@ -18,6 +18,7 @@ import org.junit.jupiter.api.BeforeEach;import org.junit.jupiter.api.Test;import java.time.Duration;+import java.util.List;import java.util.concurrent.CompletableFuture;import java.util.concurrent.ExecutionException;import java.util.concurrent.TimeUnit;@@ -61,7 +62,14 @@ class ElbV2WebSocketIntegrationTest {if (!"websocket".equalsIgnoreCase(request.getHeader("Upgrade"))) {request.response().setStatusCode(400).end("not an upgrade");} else if (!request.path().startsWith("/socket.io/")) {- request.response().setStatusCode(404).end();+ request.response().headers()+ .add("Set-Cookie", "a=1; Path=/")+ .add("Set-Cookie", "b=2; Path=/")+ .add("Set-Cookie", "c=3; Path=/")+ .add("X-Multi", "one")+ .add("X-Multi", "two")+ .add("Keep-Alive", "timeout=5");+ request.response().setStatusCode(404).end("upgrade refused");} else {String query = request.query();request.toWebSocket().onSuccess(socket -> socket.textMessageHandler(@@ -137,6 +145,11 @@ class ElbV2WebSocketIntegrationTest {UpgradeRejectedException rejected = assertInstanceOf(UpgradeRejectedException.class, failure.getCause());assertEquals(404, rejected.getStatus());+ assertEquals(List.of("a=1; Path=/", "b=2; Path=/", "c=3; Path=/"),+ rejected.getHeaders().getAll("Set-Cookie"));+ assertEquals(List.of("one", "two"), rejected.getHeaders().getAll("X-Multi"));+ assertEquals(List.of(), rejected.getHeaders().getAll("Keep-Alive"));+ assertEquals("upgrade refused", rejected.getBody().toString());}@Test