fix(apigatewayv2): pin websocket HTTP targets (#4341)

Resolve the plain http:// target of a WebSocket HTTP or HTTP_PROXY
integration once, reject metadata and link-local results, and connect to
the checked InetAddress so the name cannot be re-resolved between the
check and the connection.

The pinned request and its response parsing now live in a shared
PinnedHttpClient that HTTP_PROXY uses too, so both integrations keep the
same bounds: the 10 MB payload quota on the Content-Length, chunked and
close-delimited paths, rejected negative or oversized chunk sizes, and
the CRLF check after each chunk.

The Host header carries the target host and port only, without any
credentials the integration URI holds.
This commit is contained in:
Ben Younes authored and GitHub committed 2026-09-26 16:33:15 -05:00
1 parent fe55cf0d06
commit 5467506fca
4 files changed
+452 -204

No files matched your search

@@ -12,9 +12,15 @@ import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
@@ -25,6 +31,7 @@ class WebSocketIntegrationInvokerHttpTargetTest {
private WebSocketIntegrationInvoker invoker;
private HttpServer backend;
private final AtomicReference<String> receivedHost = new AtomicReference<>();
@BeforeEach
void setUp() throws IOException {
@@ -35,6 +42,7 @@ class WebSocketIntegrationInvokerHttpTargetTest {
mock(VtlTemplateEngine.class));
backend = HttpServer.create(new InetSocketAddress("127.0.0.1", 0), 0);
backend.createContext("/", exchange -> {
receivedHost.set(exchange.getRequestHeaders().getFirst("Host"));
byte[] body = "ok".getBytes(StandardCharsets.UTF_8);
exchange.sendResponseHeaders(200, body.length);
exchange.getResponseBody().write(body);
@@ -80,4 +88,81 @@ class WebSocketIntegrationInvokerHttpTargetTest {
assertEquals(200, result.statusCode());
assertEquals("ok", result.body());
}
@Test
void httpProxyConnectsToTheAddressItCheckedInsteadOfResolvingTheNameAgain() throws Exception {
assertConnectsToTheAddressItCheckedInsteadOfResolvingTheNameAgain("HTTP_PROXY");
}
@Test
void httpConnectsToTheAddressItCheckedInsteadOfResolvingTheNameAgain() throws Exception {
assertConnectsToTheAddressItCheckedInsteadOfResolvingTheNameAgain("HTTP");
}
private void assertConnectsToTheAddressItCheckedInsteadOfResolvingTheNameAgain(String integrationType)
throws Exception {
receivedHost.set(null);
AtomicInteger lookups = new AtomicInteger();
WebSocketIntegrationInvoker pinnedInvoker = new WebSocketIntegrationInvoker(
mock(LambdaService.class),
mock(AwsServiceRouter.class),
new ObjectMapper(),
mock(VtlTemplateEngine.class),
host -> {
if (lookups.incrementAndGet() == 1) {
return new InetAddress[] {InetAddress.getByName("127.0.0.1")};
}
return new InetAddress[] {InetAddress.getByName("169.254.169.254")};
});
String uri = "http://rebind.example.test:" + backend.getAddress().getPort() + "/";
Integration integration = new Integration();
integration.setIntegrationType(integrationType);
integration.setIntegrationUri(uri);
IntegrationResult result = pinnedInvoker.invoke("us-east-1", integration, "{}", Map.of(), Map.of(), Map.of());
assertEquals(200, result.statusCode());
assertEquals("ok", result.body());
assertEquals(1, lookups.get(), "the name must be resolved once, then the checked address used");
assertEquals("rebind.example.test:" + backend.getAddress().getPort(), receivedHost.get());
}
@Test
void hostHeaderCarriesOnlyTheHostAndPortOfAnIntegrationUriWithCredentials() {
receivedHost.set(null);
int port = backend.getAddress().getPort();
IntegrationResult result = invoke("HTTP_PROXY", "http://user:pass@127.0.0.1:" + port + "/");
assertEquals(200, result.statusCode());
assertEquals("127.0.0.1:" + port, receivedHost.get());
}
@Test
void responseOverTheTenMegabytePayloadQuotaIsRejected() throws Exception {
try (ServerSocket server = serveOversizedContentLength()) {
IntegrationResult result =
invoke("HTTP_PROXY", "http://127.0.0.1:" + server.getLocalPort() + "/");
assertEquals(500, result.statusCode());
assertNotNull(result.functionError());
assertTrue(result.functionError().contains("exceeds"), result.functionError());
}
}
/** A backend announcing a body larger than the integration payload quota. */
private static ServerSocket serveOversizedContentLength() throws IOException {
ServerSocket server = new ServerSocket(0, 1, InetAddress.getLoopbackAddress());
Thread.ofVirtual().start(() -> {
try (Socket socket = server.accept()) {
OutputStream out = socket.getOutputStream();
out.write(("HTTP/1.1 200 OK\r\nContent-Length: " + (10 * 1024 * 1024 + 1) + "\r\n\r\n")
.getBytes(StandardCharsets.ISO_8859_1));
out.flush();
} catch (IOException ignored) {
// The client gives up as soon as it reads the oversized Content-Length.
}
});
return server;
}
}