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

@@ -44,6 +44,7 @@ import io.github.hectorvent.floci.services.apigatewayv2.model.Api;
import io.github.hectorvent.floci.services.apigatewayv2.model.Authorizer;
import io.github.hectorvent.floci.services.apigatewayv2.model.Route;
import io.github.hectorvent.floci.services.apigatewayv2.proxy.CloudMapInstanceResolver;
import io.github.hectorvent.floci.services.apigatewayv2.proxy.HttpApiForwardedHeaders;
import io.github.hectorvent.floci.services.apigatewayv2.websocket.ConnectionInfo;
import io.github.hectorvent.floci.services.apigatewayv2.websocket.WebSocketConnectionManager;
import io.github.hectorvent.floci.services.elbv2.ElbV2Service;
@@ -3001,13 +3002,16 @@ public class ApiGatewayExecuteController {
? Map.of() : objectMapper.convertValue(lambdaAuthorizerContext, MAP_TYPE);
String sourceIp = routeContext.sourceIp();
// $request.header.* mappings keep the caller's values; only what reaches the backend changes.
Map<String, List<String>> backendHeaders = HttpApiForwardedHeaders.apply(multiValueHeaders, sourceIp,
routeContext.localIp(), headers.getHeaderString("Host"), isSecureTransport(uriInfo) ? "https" : "http");
io.github.hectorvent.floci.services.apigatewayv2.proxy.RequestContext ctx =
new io.github.hectorvent.floci.services.apigatewayv2.proxy.RequestContext(
apiId, stageName, httpMethod, path,
pathParams.getOrDefault("proxy", ""), route.getRouteKey(),
UUID.randomUUID().toString(), sourceIp,
requestHeaders, queryParams, pathParams, body,
claims, authorizerContext, multiValueHeaders, multiValueQueryParams);
claims, authorizerContext, backendHeaders, multiValueQueryParams);
LOG.debugv("execute-api v2: {0} {1}/{2}{3} → HTTP_PROXY {4}",
httpMethod, apiId, stageName, path, effective.getIntegrationUri());
@@ -33,6 +33,11 @@ public class ApiGatewayExecuteRouteContext {
return context != null ? context.request().connection().remoteAddress().hostAddress() : "127.0.0.1";
}
String localIp() {
RoutingContext context = currentVertxRequest != null ? currentVertxRequest.getCurrent() : null;
return context != null ? context.request().connection().localAddress().hostAddress() : "127.0.0.1";
}
private final Instant requestTime = Instant.now();
private final String extendedRequestId = UUID.randomUUID().toString();
private String requestId;
@@ -0,0 +1,75 @@
package io.github.hectorvent.floci.services.apigatewayv2.proxy;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;
/**
* The forwarding headers an HTTP API sends an {@code HTTP_PROXY} backend: the caller's chain moves to an
* RFC 7239 {@code Forwarded} header and {@code X-Forwarded-For} names the gateway itself.
*
* <p>The API Gateway important notes say HTTP APIs translate {@code X-Forwarded-*} into {@code Forwarded}
* and append the egress IP, Host and protocol. They do not give the syntax or the {@code X-Forwarded-For}
* value, which follow third-party descriptions of observed behavior. Only {@code X-Forwarded-For} is
* translated here.
*/
public final class HttpApiForwardedHeaders {
private static final String FORWARDED = "Forwarded";
private static final String FORWARDED_FOR = "X-Forwarded-For";
private static final Pattern TOKEN = Pattern.compile("[A-Za-z0-9._-]+");
private HttpApiForwardedHeaders() {
}
/** Returns a copy of {@code headers} with both headers replaced. */
public static Map<String, List<String>> apply(Map<String, List<String>> headers, String clientIp,
String gatewayIp, String host, String proto) {
Map<String, List<String>> result = new LinkedHashMap<>();
List<String> chain = new ArrayList<>();
for (Map.Entry<String, List<String>> header : headers.entrySet()) {
if (FORWARDED_FOR.equalsIgnoreCase(header.getKey())) {
for (String line : header.getValue()) {
for (String entry : line.split(",")) {
if (!entry.isBlank()) {
chain.add(entry.trim());
}
}
}
} else if (!FORWARDED.equalsIgnoreCase(header.getKey())) {
result.put(header.getKey(), header.getValue());
}
}
chain.add(clientIp);
List<String> forElements = new ArrayList<>();
for (String node : chain) {
forElements.add("for=" + parameter(node(node)));
}
StringBuilder forwarded = new StringBuilder(String.join(",", forElements))
.append(";by=").append(parameter(node(gatewayIp)));
if (host != null && !host.isBlank()) {
forwarded.append(";host=").append(parameter(host));
}
forwarded.append(";proto=").append(proto);
result.put(FORWARDED, List.of(forwarded.toString()));
result.put(FORWARDED_FOR, List.of(gatewayIp));
return result;
}
private static String node(String address) {
boolean ipv6 = address.indexOf(':') != address.lastIndexOf(':') && !address.startsWith("[");
return ipv6 ? "[" + address + "]" : address;
}
/** RFC 7239 parameter values are tokens, so anything else, such as a port or an IPv6 node, is quoted. */
private static String parameter(String value) {
if (TOKEN.matcher(value).matches()) {
return value;
}
return "\"" + value.replace("\\", "\\\\").replace("\"", "\\\"") + "\"";
}
}