fix(apigatewayv2): send HTTP_PROXY backends the client address in Forwarded (#7281)

This commit is contained in:
Abhigya authored and GitHub committed 2026-11-02 10:46:57 -04:00
1 parent 33052fa101
commit ebd2b70b32
6 files changed
+176 -5

No files matched your search

@@ -25,6 +25,7 @@ import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.matchesPattern;
import static org.hamcrest.Matchers.nullValue;
import static org.hamcrest.Matchers.startsWith;
@@ -55,6 +56,7 @@ class ApiGatewayV2CloudMapIntegrationTest {
final HttpServer server;
final List<Received> received = new CopyOnWriteArrayList<>();
final List<String> rawRequestTargets = new CopyOnWriteArrayList<>();
final List<String> forwardingHeaders = new CopyOnWriteArrayList<>();
Stub(String name) throws IOException {
this.name = name;
@@ -62,6 +64,8 @@ class ApiGatewayV2CloudMapIntegrationTest {
server.createContext("/", exchange -> {
String body = new String(exchange.getRequestBody().readAllBytes(), StandardCharsets.UTF_8);
rawRequestTargets.add(exchange.getRequestURI().getRawPath());
forwardingHeaders.add(exchange.getRequestHeaders().getFirst("Forwarded")
+ "|" + exchange.getRequestHeaders().get("X-Forwarded-For"));
received.add(new Received(exchange.getRequestMethod(), exchange.getRequestURI().getPath(),
exchange.getRequestURI().getRawQuery(), body,
exchange.getRequestHeaders().getFirst("X-Probe")));
@@ -184,6 +188,21 @@ class ApiGatewayV2CloudMapIntegrationTest {
assertThat(stub.rawRequestTargets, contains("/orders"));
}
@Test
void instanceReceivesTheCallersChainInForwarded() throws IOException {
Stub stub = startStub("only");
String serviceId = createService(null);
register(serviceId, "i-1", stub, "");
String apiId = createApi(serviceArn(serviceId), "VPC_LINK", createVpcLink());
given().header("X-Forwarded-For", "198.51.100.7")
.when().get("/execute-api/" + apiId + "/$default/orders")
.then().statusCode(200);
assertThat(stub.forwardingHeaders.get(0), matchesPattern(
"for=198\\.51\\.100\\.7,for=[^;]+;by=[^;]+;host=\"?[^;]+;proto=http\\|\\[(?!198\\.51)[^,]+\\]"));
}
@Test
void serviceWithoutInstancesAnswers503() {
String serviceId = createService(null);
@@ -0,0 +1,60 @@
package io.github.hectorvent.floci.services.apigatewayv2.proxy;
import org.junit.jupiter.api.Test;
import java.util.List;
import java.util.Map;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
class HttpApiForwardedHeadersTest {
private static Map<String, List<String>> apply(Map<String, List<String>> headers) {
return HttpApiForwardedHeaders.apply(headers, "192.0.2.43", "10.0.0.1", "api.example.test", "https");
}
@Test
void callerWithoutAChainIsTheOnlyForElement() {
Map<String, List<String>> result = apply(Map.of());
assertEquals(List.of("for=192.0.2.43;by=10.0.0.1;host=api.example.test;proto=https"), result.get("Forwarded"));
assertEquals(List.of("10.0.0.1"), result.get("X-Forwarded-For"));
}
@Test
void callersChainMovesToForwardedAndClientComesLast() {
Map<String, List<String>> result = apply(Map.of("x-forwarded-for", List.of("198.51.100.7, 198.51.100.8", "198.51.100.9")));
assertEquals(List.of("for=198.51.100.7,for=198.51.100.8,for=198.51.100.9,for=192.0.2.43"
+ ";by=10.0.0.1;host=api.example.test;proto=https"), result.get("Forwarded"));
assertEquals(List.of("10.0.0.1"), result.get("X-Forwarded-For"));
assertFalse(result.containsKey("x-forwarded-for"));
}
@Test
void callersForwardedHeaderIsReplacedAndOtherHeadersAreKept() {
Map<String, List<String>> result = apply(Map.of("forwarded", List.of("for=203.0.113.1"), "X-Probe", List.of("kept")));
assertFalse(result.containsKey("forwarded"));
assertEquals(List.of("for=192.0.2.43;by=10.0.0.1;host=api.example.test;proto=https"), result.get("Forwarded"));
assertEquals(List.of("kept"), result.get("X-Probe"));
}
@Test
void ipv6NodesAndHostPortsAreQuoted() {
Map<String, List<String>> result = HttpApiForwardedHeaders.apply(Map.of(), "2001:db8::1", "::1",
"localhost:4566", "http");
assertEquals(List.of("for=\"[2001:db8::1]\";by=\"[::1]\";host=\"localhost:4566\";proto=http"),
result.get("Forwarded"));
}
@Test
void quotesInACallersEntryCannotBreakOutOfTheParameter() {
Map<String, List<String>> result = apply(Map.of("X-Forwarded-For", List.of("a\"b;by=evil")));
assertEquals(List.of("for=\"a\\\"b;by=evil\",for=192.0.2.43;by=10.0.0.1;host=api.example.test;proto=https"),
result.get("Forwarded"));
}
}