fix(apigateway): report the caller's address as sourceIp in HTTP API Lambda events (#8674)

This commit is contained in:
Tyler Webb authored and GitHub committed 2026-11-16 19:45:37 -04:00
1 parent 79c074759d
commit 47466ca6a8
3 files changed
+40 -3

No files matched your search

+4
View File
@@ -867,6 +867,10 @@ query names are case-sensitive. No configured sources produces an empty array
or string. A missing or empty configured value returns `401` without invoking
Lambda in either format. Repeated request values use each event's delivered
representation: last value in format `1.0`, comma-joined values in format `2.0`.
The authorizer event's source IP (`requestContext.http.sourceIp` in format `2.0`,
`requestContext.identity.sourceIp` in format `1.0`) and the Lambda proxy event's
`requestContext.http.sourceIp` are the address of the TCP connection to Floci, as in
`$context.identity.sourceIp`; a client's `X-Forwarded-For` header does not change them.
HTTP API `HTTP_PROXY` integrations can map values from a successful Lambda
authorizer's context into request headers, query strings, and paths. For a returned
@@ -3712,7 +3712,7 @@ public class ApiGatewayExecuteController {
ctx.put("domainName", AwsEndpoints.executeApiHost(apiId, arnRegion));
ctx.put("domainPrefix", apiId);
ObjectNode identity = ctx.putObject("identity");
identity.put("sourceIp", "127.0.0.1");
identity.put("sourceIp", routeContext.sourceIp());
identity.put("userAgent", headers.getHeaderString("User-Agent") != null
? headers.getHeaderString("User-Agent") : "");
return event;
@@ -3762,7 +3762,7 @@ public class ApiGatewayExecuteController {
http.put("method", httpMethod);
http.put("path", preservedPath);
http.put("protocol", "HTTP/1.1");
http.put("sourceIp", "127.0.0.1");
http.put("sourceIp", routeContext.sourceIp());
http.put("userAgent", headers.getHeaderString("User-Agent") != null
? headers.getHeaderString("User-Agent") : "");
@@ -4131,7 +4131,7 @@ public class ApiGatewayExecuteController {
http.put("method", httpMethod);
http.put("path", preservedPath);
http.put("protocol", "HTTP/1.1");
http.put("sourceIp", "127.0.0.1");
http.put("sourceIp", routeContext.sourceIp());
http.put("userAgent", headers.getHeaderString("User-Agent") != null
? headers.getHeaderString("User-Agent") : "");
@@ -32,6 +32,7 @@ import org.junit.jupiter.params.provider.ValueSource;
import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Stream;
import static org.junit.jupiter.api.Assertions.*;
@@ -118,6 +119,38 @@ class HttpApiAuthorizerPolicyTest {
assertPolicy("2.0", statements, 403);
}
@ParameterizedTest
@CsvSource({
"1.0, 203.0.113.42, /requestContext/identity/sourceIp",
"1.0, 2001:db8::42, /requestContext/identity/sourceIp",
"2.0, 203.0.113.42, /requestContext/http/sourceIp",
"2.0, 2001:db8::42, /requestContext/http/sourceIp"
})
void authorizerAndProxyEventsCarryTransportAddress(String version, String sourceIp, String authorizerField) {
when(remoteAddress.hostAddress()).thenReturn(sourceIp);
when(headers.getHeaderString("X-Forwarded-For")).thenReturn("198.51.100.42");
AtomicReference<JsonNode> authorizerEvent = new AtomicReference<>();
AtomicReference<JsonNode> backendEvent = new AtomicReference<>();
when(lambda.invokeTarget(eq("us-east-1"), eq("arn:aws:lambda:us-east-1:000000000000:function:" + "authorizer"), any(byte[].class), eq(InvocationType.RequestResponse)))
.thenAnswer(invocation -> {
authorizerEvent.set(mapper.readTree((byte[]) invocation.getArgument(2)));
return result("{\"principalId\":\"user\",\"policyDocument\":{\"Version\":\"2012-10-17\","
+ "\"Statement\":[{\"Effect\":\"Allow\",\"Action\":\"*\",\"Resource\":\"*\"}]}}");
});
when(lambda.invokeTarget(eq("us-east-1"), eq("arn:aws:lambda:us-east-1:000000000000:function:" + "backend"), any(byte[].class), eq(InvocationType.RequestResponse)))
.thenAnswer(invocation -> {
backendEvent.set(mapper.readTree((byte[]) invocation.getArgument(2)));
return result("{\"statusCode\":200,\"body\":\"allowed\"}");
});
try (Response response = controller(version, false).handleGet(headers, uri, "api", "test", "items")) {
assertEquals(200, response.getStatus(), "payload version " + version);
}
assertEquals(sourceIp, authorizerEvent.get().at(authorizerField).asText());
assertEquals(sourceIp, backendEvent.get().at("/requestContext/http/sourceIp").asText());
}
@ParameterizedTest
@CsvSource({
"1.0, 2001:db8::42, 403",