fix(elbv2): accept request headers up to 64 KB as AWS does (#7115)

* fix(elbv2): accept request headers up to 64 KB as AWS does

ALB listeners used Vert.x's default 8 KB maxHeaderSize, so a request with
several JWT cookies got 431 Request Header Fields Too Large. AWS ALBs accept
up to 64 KB of request headers. Raise the listener limit and the target proxy
client's response-header limit to match.

* fix(elbv2): apply the ALB response header and request line quotas

Size the proxy client for the 32 KB response header quota and raise the
listener's request line limit to 16 KB. The large-header test now stays
under the 16 KB single-header limit, sends a 10 KB query, and reads the
data plane's constants.
This commit is contained in:
Abhigya authored and GitHub committed 2026-10-31 17:40:19 -04:00
1 parent 10f54bb554
commit 8c475d664b
2 files changed
+62 -3

No files matched your search

@@ -86,6 +86,14 @@ public class ElbV2DataPlane {
"connection", "keep-alive", "transfer-encoding", "upgrade", "te", "trailers", "proxy-authorization", "proxy-authenticate"
);
private static final String PRESERVE_HOST_HEADER_ATTRIBUTE = "routing.http.preserve_host_header.enabled";
/*
* ALB HTTP header quotas: 64 KB for the request header block, 16 KB for the request line and
* 32 KB for the response header block. Vert.x defaults to 8 KB and 4 KB, so a browser carrying
* several JWT cookies got 431 from the listener and an oversized target response failed to proxy.
*/
static final int MAX_REQUEST_HEADER_SIZE = 64 * 1024;
static final int MAX_REQUEST_LINE_LENGTH = 16 * 1024;
static final int MAX_RESPONSE_HEADER_SIZE = 32 * 1024;
private static final String IDLE_TIMEOUT_ATTRIBUTE = "idle_timeout.timeout_seconds";
private static final long DEFAULT_IDLE_TIMEOUT_SECONDS = 60;
private static final int MAX_LAMBDA_BODY_BYTES = 1024 * 1024;
@@ -209,7 +217,8 @@ public class ElbV2DataPlane {
proxyClient = vertx.createHttpClient(new HttpClientOptions()
.setMaxPoolSize(100)
.setConnectTimeout(5000)
.setKeepAlive(true));
.setKeepAlive(true)
.setMaxHeaderSize(MAX_RESPONSE_HEADER_SIZE));
}
@PreDestroy
@@ -477,7 +486,9 @@ public class ElbV2DataPlane {
boolean secure = portIsSecure(port);
HttpServerOptions options = new HttpServerOptions()
.setHost("0.0.0.0")
.setPort(port);
.setPort(port)
.setMaxHeaderSize(MAX_REQUEST_HEADER_SIZE)
.setMaxInitialLineLength(MAX_REQUEST_LINE_LENGTH);
if (secure) {
state.tlsKeyManager = new ReloadingKeyManager(new ElbV2TlsKeyManager(vertx, tlsKeyCertOptions(port)));
options.setSsl(true)
@@ -9,10 +9,12 @@ import io.restassured.response.Response;
import io.vertx.core.Vertx;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.http.HttpClient;
import io.vertx.core.http.HttpClientOptions;
import io.vertx.core.http.HttpClientRequest;
import io.vertx.core.http.HttpClientResponse;
import io.vertx.core.http.HttpMethod;
import io.vertx.core.http.HttpServer;
import io.vertx.core.http.HttpServerOptions;
import io.vertx.core.http.RequestOptions;
import io.vertx.core.net.SocketAddress;
import jakarta.inject.Inject;
@@ -86,7 +88,9 @@ class ElbV2PreserveHostHeaderIntegrationTest {
@Test
void preservesHostHeaderWhenLoadBalancerAttributeIsEnabled() throws Exception {
HttpServer backend = vertx.createHttpServer()
HttpServer backend = vertx.createHttpServer(new HttpServerOptions()
.setMaxHeaderSize(ElbV2DataPlane.MAX_REQUEST_HEADER_SIZE)
.setMaxInitialLineLength(ElbV2DataPlane.MAX_REQUEST_LINE_LENGTH))
.requestHandler(request -> {
if ("/stream".equals(request.path())) {
request.body().onSuccess(body -> {
@@ -97,6 +101,13 @@ class ElbV2PreserveHostHeaderIntegrationTest {
request.response().write("stream-");
request.response().end(body.toString());
});
} else if ("/large-headers".equals(request.path())) {
String cookie = request.getHeader("Cookie");
request.response()
.putHeader("X-Received-Cookie-Length", String.valueOf(cookie.length()))
.putHeader("X-Received-Query-Length", String.valueOf(request.query().length()))
.putHeader("X-Large", cookie)
.end("ok");
} else {
request.response().end(request.getHeader("Host"));
}
@@ -116,6 +127,7 @@ class ElbV2PreserveHostHeaderIntegrationTest {
listenerArn = createListener(loadBalancerArn, targetGroupArn);
assertStreamsRequestAndResponse(loadBalancerArn);
assertForwardsLargeHeaders(loadBalancerArn);
assertForwardedHost("127.0.0.1:" + backend.actualPort());
enableHostHeaderPreservation(loadBalancerArn);
@@ -234,6 +246,42 @@ class ElbV2PreserveHostHeaderIntegrationTest {
}
}
private void assertForwardsLargeHeaders(String loadBalancerArn) throws Exception {
// Several JWT cookies on a shared parent domain exceed Vert.x's 8 KB default header block,
// while staying under the ALB's 16 KB single-header limit. The query passes Vert.x's 4 KB
// default request line.
String cookie = "a=" + "x".repeat(3600) + "; b=" + "y".repeat(3600) + "; c=" + "z".repeat(3600);
String query = "q=" + "q".repeat(10_000);
String dnsName = loadBalancerDnsName(loadBalancerArn);
HttpClient client = vertx.createHttpClient(new HttpClientOptions()
.setMaxHeaderSize(ElbV2DataPlane.MAX_RESPONSE_HEADER_SIZE));
try {
HttpClientRequest request = client
.request(new RequestOptions()
.setServer(SocketAddress.inetSocketAddress(LISTENER_PORT, "127.0.0.1"))
.setHost(dnsName)
.setPort(LISTENER_PORT)
.setMethod(HttpMethod.GET)
.setURI("/large-headers?" + query))
.toCompletionStage()
.toCompletableFuture()
.get(2, TimeUnit.SECONDS);
request.putHeader("Cookie", cookie);
HttpClientResponse response = request.send()
.toCompletionStage()
.toCompletableFuture()
.get(2, TimeUnit.SECONDS);
response.body().toCompletionStage().toCompletableFuture().get(2, TimeUnit.SECONDS);
assertEquals(200, response.statusCode());
assertEquals(String.valueOf(cookie.length()), response.getHeader("X-Received-Cookie-Length"));
assertEquals(String.valueOf(query.length()), response.getHeader("X-Received-Query-Length"));
assertEquals(cookie, response.getHeader("X-Large"));
} finally {
client.close();
}
}
private static String createLoadBalancer() {
return given()
.formParam("Action", "CreateLoadBalancer")