diff --git a/src/main/java/io/github/hectorvent/floci/services/apigatewayv2/proxy/HttpProxyInvoker.java b/src/main/java/io/github/hectorvent/floci/services/apigatewayv2/proxy/HttpProxyInvoker.java index fa898e6bbd..02f08690a3 100644 --- a/src/main/java/io/github/hectorvent/floci/services/apigatewayv2/proxy/HttpProxyInvoker.java +++ b/src/main/java/io/github/hectorvent/floci/services/apigatewayv2/proxy/HttpProxyInvoker.java @@ -9,13 +9,9 @@ import javax.net.ssl.SSLParameters; import javax.net.ssl.TrustManager; import javax.net.ssl.X509TrustManager; -import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; -import java.io.OutputStream; import java.net.InetAddress; -import java.net.InetSocketAddress; -import java.net.Socket; import java.net.URI; import java.net.URLEncoder; import java.net.UnknownHostException; @@ -56,9 +52,6 @@ import java.util.StringJoiner; public class HttpProxyInvoker { private static final Logger LOG = Logger.getLogger(HttpProxyInvoker.class); - /** API Gateway's integration payload quota: 10 MB, not adjustable. */ - private static final int MAX_RESPONSE_BYTES = 10 * 1024 * 1024; - /** RFC 7230 hop-by-hop headers that must not be forwarded across proxies. */ private static final Set HOP_BY_HOP = Set.of( "connection", "keep-alive", "proxy-authenticate", "proxy-authorization", @@ -332,7 +325,7 @@ public class HttpProxyInvoker { if (finalUrl.startsWith("http://") && (hasHeader(builder, "Host") || isNamedHost(finalUrl))) { try { return invokeHttpPinned(finalUrl, method, builder, options.timeout()); - } catch (ResponseTooLargeException ignored) { + } catch (PinnedHttpClient.ResponseTooLargeException ignored) { return tooLargeResult(); } catch (Exception e) { LOG.warnv("HTTP_PROXY backend call failed: {0}", e.getMessage()); @@ -374,7 +367,7 @@ public class HttpProxyInvoker { clientFor(options).send(hrb.build(), HttpResponse.BodyHandlers.ofInputStream()); byte[] body; try (InputStream in = resp.body()) { - body = readBounded(in); + body = PinnedHttpClient.readBounded(in); } Map> respHeaders = new LinkedHashMap<>(); for (Map.Entry> e : resp.headers().map().entrySet()) { @@ -382,7 +375,7 @@ public class HttpProxyInvoker { respHeaders.put(e.getKey(), List.copyOf(e.getValue())); } return new ProxyResult(resp.statusCode(), respHeaders, body); - } catch (ResponseTooLargeException ignored) { + } catch (PinnedHttpClient.ResponseTooLargeException ignored) { return tooLargeResult(); } catch (Exception e) { LOG.warnv("HTTP_PROXY backend call failed: {0}", e.getMessage()); @@ -419,184 +412,25 @@ public class HttpProxyInvoker { ProxyRequestBuilder builder, Duration timeout) throws IOException { URI uri = URI.create(finalUrl); - int port = uri.getPort() == -1 ? 80 : uri.getPort(); - String path = uri.getRawPath(); - if (path == null || path.isBlank()) { - path = "/"; - } - if (uri.getRawQuery() != null) { - path += "?" + uri.getRawQuery(); - } - InetAddress[] targets = resolveNonMetadataTarget(uri.getHost()); - String hostHeader = firstHeader(builder, "Host"); - if (hostHeader == null) { - hostHeader = uri.getRawAuthority(); - } - try (Socket socket = new Socket()) { - socket.connect(new InetSocketAddress(targets[0], port), 10_000); - socket.setSoTimeout((int) Math.min(timeout.toMillis(), Integer.MAX_VALUE)); - OutputStream out = socket.getOutputStream(); - byte[] body = builder.body() == null ? new byte[0] : builder.body(); - StringBuilder request = new StringBuilder() - .append(method.toUpperCase(Locale.ROOT)).append(' ').append(path).append(" HTTP/1.1\r\n") - .append("Host: ").append(hostHeader).append("\r\n") - .append("Connection: close\r\n"); - for (Map.Entry> header : builder.headers().entrySet()) { - String name = header.getKey(); - String lower = name.toLowerCase(Locale.ROOT); - if (RESTRICTED.contains(lower) || lower.equals("connection")) { - continue; - } - for (String value : header.getValue()) { - request.append(name).append(": ").append(value).append("\r\n"); - } + Map> requestHeaders = new LinkedHashMap<>(); + for (Map.Entry> header : builder.headers().entrySet()) { + if (!RESTRICTED.contains(header.getKey().toLowerCase(Locale.ROOT))) { + requestHeaders.put(header.getKey(), header.getValue()); } - if (body.length > 0) { - request.append("Content-Length: ").append(body.length).append("\r\n"); - } - request.append("\r\n"); - out.write(request.toString().getBytes(StandardCharsets.ISO_8859_1)); - out.write(body); - out.flush(); - - return readRawHttpResponse(socket.getInputStream(), method); - } - } - - private static ProxyResult readRawHttpResponse(InputStream input, String method) throws IOException { - ByteArrayOutputStream headerBytes = new ByteArrayOutputStream(); - int previous3 = -1; - int previous2 = -1; - int previous1 = -1; - int current; - while ((current = input.read()) != -1) { - headerBytes.write(current); - if (previous3 == '\r' && previous2 == '\n' && previous1 == '\r' && current == '\n') { - break; - } - previous3 = previous2; - previous2 = previous1; - previous1 = current; } - String headersText = headerBytes.toString(StandardCharsets.ISO_8859_1); - String[] lines = headersText.split("\r\n"); - if (lines.length == 0 || !lines[0].startsWith("HTTP/")) { - throw new IOException("invalid HTTP response"); - } - String[] status = lines[0].split(" ", 3); - int statusCode = Integer.parseInt(status[1]); - Map> headers = new LinkedHashMap<>(); - String transferEncoding = null; - long contentLength = -1; - for (int i = 1; i < lines.length; i++) { - int separator = lines[i].indexOf(':'); - if (separator <= 0) { - continue; - } - String name = lines[i].substring(0, separator); - String value = lines[i].substring(separator + 1).trim(); - if (name.equalsIgnoreCase("Transfer-Encoding")) { - transferEncoding = value; - } - if (name.equalsIgnoreCase("Content-Length")) { - contentLength = Long.parseLong(value); - } - if (!HOP_BY_HOP.contains(name.toLowerCase(Locale.ROOT))) { - // Repeated header lines (Set-Cookie) accumulate rather than overwrite. - headers.computeIfAbsent(name, k -> new java.util.ArrayList<>()).add(value); - } - } - boolean bodyless = "HEAD".equalsIgnoreCase(method) || statusCode == 204 || statusCode == 304 - || (statusCode >= 100 && statusCode < 200); - byte[] body = bodyless - ? new byte[0] - : transferEncoding != null && transferEncoding.toLowerCase(Locale.ROOT).contains("chunked") - ? readChunkedBody(input) - : contentLength >= 0 ? readContentLength(input, contentLength) : readBounded(input); - return new ProxyResult(statusCode, headers, body); - } + PinnedHttpClient.Response response = PinnedHttpClient.send( + targets[0], uri, method, firstHeader(builder, "Host"), + requestHeaders, builder.body(), timeout); - private static byte[] readChunkedBody(InputStream input) throws IOException { - ByteArrayOutputStream body = new ByteArrayOutputStream(); - while (true) { - String sizeLine = readAsciiLine(input); - if (sizeLine == null) { - throw new IOException("unexpected end of chunked response"); - } - int extension = sizeLine.indexOf(';'); - int size = Integer.parseInt((extension >= 0 ? sizeLine.substring(0, extension) : sizeLine).trim(), 16); - if (size == 0) { - while (true) { - String trailer = readAsciiLine(input); - if (trailer == null || trailer.isEmpty()) { - return body.toByteArray(); - } - } - } - if (size < 0 || size > MAX_RESPONSE_BYTES - body.size()) { - throw new ResponseTooLargeException(); - } - body.write(input.readNBytes(size)); - expectCrlf(input); - } - } - - private static byte[] readContentLength(InputStream input, long contentLength) throws IOException { - if (contentLength > MAX_RESPONSE_BYTES) { - throw new ResponseTooLargeException(); - } - return input.readNBytes((int) contentLength); - } - - /** Reads to end of stream, failing once more than the payload quota has arrived. */ - private static byte[] readBounded(InputStream input) throws IOException { - byte[] body = input.readNBytes(MAX_RESPONSE_BYTES + 1); - if (body.length > MAX_RESPONSE_BYTES) { - throw new ResponseTooLargeException(); - } - return body; - } - - private static final class ResponseTooLargeException extends IOException { - ResponseTooLargeException() { - super("integration response exceeds " + MAX_RESPONSE_BYTES + " bytes"); - } - } - - private static String readAsciiLine(InputStream input) throws IOException { - ByteArrayOutputStream line = new ByteArrayOutputStream(); - while (true) { - int b = input.read(); - if (b == -1) { - return line.size() == 0 ? null : line.toString(StandardCharsets.ISO_8859_1); - } - if (b == '\n') { - return line.toString(StandardCharsets.ISO_8859_1); - } - if (b == '\r') { - int next = input.read(); - if (next == '\n') { - return line.toString(StandardCharsets.ISO_8859_1); - } - line.write(b); - if (next != -1) { - line.write(next); - } - continue; - } - line.write(b); - } - } - - private static void expectCrlf(InputStream input) throws IOException { - int cr = input.read(); - int lf = input.read(); - if (cr != '\r' || lf != '\n') { - throw new IOException("invalid chunked response"); + Map> respHeaders = new LinkedHashMap<>(); + for (Map.Entry> header : response.headers().entrySet()) { + if (HOP_BY_HOP.contains(header.getKey().toLowerCase(Locale.ROOT))) continue; + respHeaders.put(header.getKey(), List.copyOf(header.getValue())); } + return new ProxyResult(response.statusCode(), respHeaders, response.body()); } private static String buildFinalUrl(ProxyRequestBuilder builder) { @@ -615,7 +449,8 @@ public class HttpProxyInvoker { } private static ProxyResult tooLargeResult() { - LOG.warnv("HTTP_PROXY backend response exceeds the {0}-byte payload quota", MAX_RESPONSE_BYTES); + LOG.warnv("HTTP_PROXY backend response exceeds the {0}-byte payload quota", + PinnedHttpClient.MAX_RESPONSE_BYTES); return ProxyResult.withSingleValueHeaders(413, Map.of("Content-Type", "application/json"), "{\"message\":\"Request Entity Too Large\"}".getBytes(StandardCharsets.UTF_8)); diff --git a/src/main/java/io/github/hectorvent/floci/services/apigatewayv2/proxy/PinnedHttpClient.java b/src/main/java/io/github/hectorvent/floci/services/apigatewayv2/proxy/PinnedHttpClient.java new file mode 100644 index 0000000000..9d7e9e300c --- /dev/null +++ b/src/main/java/io/github/hectorvent/floci/services/apigatewayv2/proxy/PinnedHttpClient.java @@ -0,0 +1,280 @@ +package io.github.hectorvent.floci.services.apigatewayv2.proxy; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.net.InetAddress; +import java.net.InetSocketAddress; +import java.net.Socket; +import java.net.URI; +import java.nio.charset.StandardCharsets; +import java.time.Duration; +import java.util.ArrayList; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Locale; +import java.util.Map; +import java.util.Set; + +/** + * Sends one plain-HTTP request to an already checked {@link InetAddress} and reads the response + * off the socket. + * + *

The address is supplied by the caller, which resolves the integration host once and runs it + * through {@code SsrfProtection}. Connecting to that same address, rather than handing the name + * back to a client that would resolve it again, is what closes the DNS-rebinding window between + * the check and the connection. + * + *

Response framing is bounded on every path: the body stops at the API Gateway payload quota + * whether it arrives with a Content-Length, chunked, or delimited by the connection closing, chunk + * sizes are rejected when negative or oversized, and the CRLF that must follow each chunk is + * verified. Both HTTP_PROXY and the WebSocket integrations go through here so those limits stay in + * one place. + */ +public final class PinnedHttpClient { + + /** API Gateway's integration payload quota: 10 MB, not adjustable. */ + public static final int MAX_RESPONSE_BYTES = 10 * 1024 * 1024; + + private static final int CONNECT_TIMEOUT_MILLIS = 10_000; + private static final int DEFAULT_HTTP_PORT = 80; + private static final int MAX_STATUS_LINE_PARTS = 3; + + /** Written by this client itself, so a caller-supplied copy would duplicate the header. */ + private static final Set CLIENT_OWNED_HEADERS = + Set.of("host", "connection", "content-length"); + + /** Methods that carry no body, so an empty payload needs no Content-Length. */ + private static final Set BODYLESS_METHODS = + Set.of("GET", "HEAD", "OPTIONS", "DELETE", "TRACE"); + + private PinnedHttpClient() { + } + + /** + * A backend response, with the header names as the backend spelled them and repeated header + * lines kept separate. + */ + public record Response(int statusCode, Map> headers, byte[] body) { + } + + /** Raised once more than the payload quota has arrived, on any of the framing paths. */ + public static final class ResponseTooLargeException extends IOException { + public ResponseTooLargeException() { + super("integration response exceeds " + MAX_RESPONSE_BYTES + " bytes"); + } + } + + /** + * Connects to {@code address} and exchanges one request/response pair. + * + * @param address the checked address to connect to, never re-resolved from the URI + * @param target the integration URI, read for the port, path, query and default Host + * @param method the HTTP method to send + * @param hostHeader the Host header to advertise, or null to use the target's host and port + * @param headers extra request headers; Host, Connection and Content-Length are ignored + * because this client writes them itself + * @param body the request body, may be null or empty + * @param timeout how long to wait for the backend to respond + */ + public static Response send(InetAddress address, URI target, String method, String hostHeader, + Map> headers, byte[] body, Duration timeout) + throws IOException { + byte[] payload = body == null ? new byte[0] : body; + String upperMethod = method.toUpperCase(Locale.ROOT); + int port = target.getPort() == -1 ? DEFAULT_HTTP_PORT : target.getPort(); + + try (Socket socket = new Socket()) { + socket.connect(new InetSocketAddress(address, port), CONNECT_TIMEOUT_MILLIS); + socket.setSoTimeout((int) Math.min(timeout.toMillis(), Integer.MAX_VALUE)); + + OutputStream out = socket.getOutputStream(); + String requestHead = buildRequestHead( + target, upperMethod, + hostHeader == null ? hostAndPort(target) : hostHeader, + headers, payload.length); + out.write(requestHead.getBytes(StandardCharsets.ISO_8859_1)); + out.write(payload); + out.flush(); + + return readResponse(socket.getInputStream(), upperMethod); + } + } + + /** The authority to advertise as Host, without any {@code user:pass@} the URI carries. */ + private static String hostAndPort(URI target) { + String host = target.getHost(); + if (host == null) { + return target.getRawAuthority(); + } + return target.getPort() == -1 ? host : host + ":" + target.getPort(); + } + + private static String buildRequestHead(URI target, String method, String hostHeader, + Map> headers, int contentLength) { + String path = target.getRawPath(); + if (path == null || path.isBlank()) { + path = "/"; + } + if (target.getRawQuery() != null) { + path += "?" + target.getRawQuery(); + } + + StringBuilder request = new StringBuilder() + .append(method).append(' ').append(path).append(" HTTP/1.1\r\n") + .append("Host: ").append(hostHeader).append("\r\n") + .append("Connection: close\r\n"); + if (headers != null) { + for (Map.Entry> header : headers.entrySet()) { + if (CLIENT_OWNED_HEADERS.contains(header.getKey().toLowerCase(Locale.ROOT))) { + continue; + } + for (String value : header.getValue()) { + request.append(header.getKey()).append(": ").append(value).append("\r\n"); + } + } + } + if (contentLength > 0 || !BODYLESS_METHODS.contains(method)) { + request.append("Content-Length: ").append(contentLength).append("\r\n"); + } + return request.append("\r\n").toString(); + } + + private static Response readResponse(InputStream input, String method) throws IOException { + String headersText = readHead(input); + String[] lines = headersText.split("\r\n"); + if (lines.length == 0 || !lines[0].startsWith("HTTP/")) { + throw new IOException("invalid HTTP response"); + } + String[] status = lines[0].split(" ", MAX_STATUS_LINE_PARTS); + if (status.length < 2) { + throw new IOException("invalid HTTP status line"); + } + int statusCode = Integer.parseInt(status[1]); + + Map> headers = new LinkedHashMap<>(); + String transferEncoding = null; + long contentLength = -1; + for (int i = 1; i < lines.length; i++) { + int separator = lines[i].indexOf(':'); + if (separator <= 0) { + continue; + } + String name = lines[i].substring(0, separator); + String value = lines[i].substring(separator + 1).trim(); + if (name.equalsIgnoreCase("Transfer-Encoding")) { + transferEncoding = value; + } + if (name.equalsIgnoreCase("Content-Length")) { + contentLength = Long.parseLong(value); + } + // Repeated header lines (Set-Cookie) accumulate rather than overwrite. + headers.computeIfAbsent(name, k -> new ArrayList<>()).add(value); + } + + boolean bodyless = "HEAD".equals(method) || statusCode == 204 || statusCode == 304 + || (statusCode >= 100 && statusCode < 200); + byte[] body = bodyless + ? new byte[0] + : transferEncoding != null + && transferEncoding.toLowerCase(Locale.ROOT).contains("chunked") + ? readChunkedBody(input) + : contentLength >= 0 ? readContentLength(input, contentLength) : readBounded(input); + return new Response(statusCode, headers, body); + } + + /** Reads up to and including the blank line that ends the response head. */ + private static String readHead(InputStream input) throws IOException { + ByteArrayOutputStream headBytes = new ByteArrayOutputStream(); + int previous3 = -1; + int previous2 = -1; + int previous1 = -1; + int current; + while ((current = input.read()) != -1) { + headBytes.write(current); + if (previous3 == '\r' && previous2 == '\n' && previous1 == '\r' && current == '\n') { + break; + } + previous3 = previous2; + previous2 = previous1; + previous1 = current; + } + return headBytes.toString(StandardCharsets.ISO_8859_1); + } + + private static byte[] readChunkedBody(InputStream input) throws IOException { + ByteArrayOutputStream body = new ByteArrayOutputStream(); + while (true) { + String sizeLine = readAsciiLine(input); + if (sizeLine == null) { + throw new IOException("unexpected end of chunked response"); + } + int extension = sizeLine.indexOf(';'); + int size = Integer.parseInt( + (extension >= 0 ? sizeLine.substring(0, extension) : sizeLine).trim(), 16); + if (size == 0) { + while (true) { + String trailer = readAsciiLine(input); + if (trailer == null || trailer.isEmpty()) { + return body.toByteArray(); + } + } + } + if (size < 0 || size > MAX_RESPONSE_BYTES - body.size()) { + throw new ResponseTooLargeException(); + } + body.write(input.readNBytes(size)); + expectCrlf(input); + } + } + + private static byte[] readContentLength(InputStream input, long contentLength) throws IOException { + if (contentLength > MAX_RESPONSE_BYTES) { + throw new ResponseTooLargeException(); + } + return input.readNBytes((int) contentLength); + } + + /** Reads to end of stream, failing once more than the payload quota has arrived. */ + public static byte[] readBounded(InputStream input) throws IOException { + byte[] body = input.readNBytes(MAX_RESPONSE_BYTES + 1); + if (body.length > MAX_RESPONSE_BYTES) { + throw new ResponseTooLargeException(); + } + return body; + } + + private static String readAsciiLine(InputStream input) throws IOException { + ByteArrayOutputStream line = new ByteArrayOutputStream(); + while (true) { + int b = input.read(); + if (b == -1) { + return line.size() == 0 ? null : line.toString(StandardCharsets.ISO_8859_1); + } + if (b == '\n') { + return line.toString(StandardCharsets.ISO_8859_1); + } + if (b == '\r') { + int next = input.read(); + if (next == '\n') { + return line.toString(StandardCharsets.ISO_8859_1); + } + line.write(b); + if (next != -1) { + line.write(next); + } + continue; + } + line.write(b); + } + } + + private static void expectCrlf(InputStream input) throws IOException { + int cr = input.read(); + int lf = input.read(); + if (cr != '\r' || lf != '\n') { + throw new IOException("invalid chunked response"); + } + } +} diff --git a/src/main/java/io/github/hectorvent/floci/services/apigatewayv2/websocket/WebSocketIntegrationInvoker.java b/src/main/java/io/github/hectorvent/floci/services/apigatewayv2/websocket/WebSocketIntegrationInvoker.java index 7bdded4721..24c6d0ab29 100644 --- a/src/main/java/io/github/hectorvent/floci/services/apigatewayv2/websocket/WebSocketIntegrationInvoker.java +++ b/src/main/java/io/github/hectorvent/floci/services/apigatewayv2/websocket/WebSocketIntegrationInvoker.java @@ -6,6 +6,7 @@ import io.github.hectorvent.floci.core.common.SsrfProtection; import io.github.hectorvent.floci.services.apigateway.AwsServiceRouter; import io.github.hectorvent.floci.services.apigateway.VtlTemplateEngine; import io.github.hectorvent.floci.services.apigatewayv2.model.Integration; +import io.github.hectorvent.floci.services.apigatewayv2.proxy.PinnedHttpClient; import io.github.hectorvent.floci.services.lambda.LambdaArnUtils; import io.github.hectorvent.floci.services.lambda.LambdaService; import io.github.hectorvent.floci.services.lambda.model.InvocationType; @@ -15,13 +16,18 @@ import jakarta.enterprise.context.ApplicationScoped; import jakarta.inject.Inject; import org.jboss.logging.Logger; +import java.io.IOException; import java.net.InetAddress; import java.net.URI; import java.net.http.HttpClient; import java.net.http.HttpRequest; import java.net.http.HttpResponse; import java.nio.charset.StandardCharsets; +import java.time.Duration; import java.util.Collections; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; import java.util.Map; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -40,21 +46,41 @@ public class WebSocketIntegrationInvoker { */ private static final Pattern STAGE_VAR_PATTERN = Pattern.compile("\\$\\{stageVariables\\.([^}]+)}"); + private static final String APPLICATION_JSON = "application/json"; + private static final String CONTENT_TYPE_HEADER = "Content-Type"; + private static final String HTTP_SCHEME = "http"; + private static final Duration BACKEND_TIMEOUT = Duration.ofSeconds(10); private final LambdaService lambdaService; private final AwsServiceRouter serviceRouter; private final ObjectMapper objectMapper; private final VtlTemplateEngine vtlEngine; private final HttpClient httpClient; + private final AddressResolver addressResolver; @Inject public WebSocketIntegrationInvoker(LambdaService lambdaService, AwsServiceRouter serviceRouter, ObjectMapper objectMapper, VtlTemplateEngine vtlEngine) { + this(lambdaService, serviceRouter, objectMapper, vtlEngine, InetAddress::getAllByName); + } + + WebSocketIntegrationInvoker(LambdaService lambdaService, AwsServiceRouter serviceRouter, + ObjectMapper objectMapper, VtlTemplateEngine vtlEngine, + AddressResolver addressResolver) { this.lambdaService = lambdaService; this.serviceRouter = serviceRouter; this.objectMapper = objectMapper; this.vtlEngine = vtlEngine; this.httpClient = HttpClient.newHttpClient(); + this.addressResolver = addressResolver; + } + + @FunctionalInterface + interface AddressResolver { + InetAddress[] resolve(String host) throws IOException; + } + + private record HttpBackendResponse(int statusCode, String body) { } @PreDestroy @@ -155,11 +181,11 @@ public class WebSocketIntegrationInvoker { } // Extract response headers (used by $connect to propagate to upgrade response) if (responseNode.has("headers") && responseNode.get("headers").isObject()) { - responseHeaders = new java.util.HashMap<>(); - var headersNode = responseNode.get("headers"); - var fields = headersNode.fields(); + responseHeaders = new HashMap<>(); + JsonNode headersNode = responseNode.get("headers"); + Iterator> fields = headersNode.fields(); while (fields.hasNext()) { - var field = fields.next(); + Map.Entry field = fields.next(); responseHeaders.put(field.getKey(), field.getValue().asText()); } } @@ -244,15 +270,7 @@ public class WebSocketIntegrationInvoker { try { URI target = URI.create(uri); - SsrfProtection.rejectMetadataAddresses(InetAddress.getAllByName(target.getHost()), target.getHost()); - HttpRequest request = HttpRequest.newBuilder() - .uri(target) - .header("Content-Type", "application/json") - .POST(HttpRequest.BodyPublishers.ofString(eventJson, StandardCharsets.UTF_8)) - .build(); - - HttpResponse response = httpClient.send(request, HttpResponse.BodyHandlers.ofString()); - + HttpBackendResponse response = postToHttpEndpoint(target, eventJson); return new IntegrationResult(response.statusCode(), response.body(), null); } catch (Exception e) { LOG.warnv("HTTP_PROXY integration call failed: {0}", e.getMessage()); @@ -294,15 +312,7 @@ public class WebSocketIntegrationInvoker { try { URI target = URI.create(uri); - SsrfProtection.rejectMetadataAddresses(InetAddress.getAllByName(target.getHost()), target.getHost()); - HttpRequest request = HttpRequest.newBuilder() - .uri(target) - .header("Content-Type", "application/json") - .POST(HttpRequest.BodyPublishers.ofString(transformedPayload, StandardCharsets.UTF_8)) - .build(); - - HttpResponse response = httpClient.send(request, HttpResponse.BodyHandlers.ofString()); - + HttpBackendResponse response = postToHttpEndpoint(target, transformedPayload); int statusCode = response.statusCode(); String body = response.body(); @@ -319,6 +329,44 @@ public class WebSocketIntegrationInvoker { } } + private HttpBackendResponse postToHttpEndpoint(URI target, String payload) + throws IOException, InterruptedException { + if (HTTP_SCHEME.equalsIgnoreCase(target.getScheme())) { + return postToPinnedHttpEndpoint(target, payload); + } + + resolveNonMetadataTarget(target.getHost()); + HttpRequest request = HttpRequest.newBuilder() + .uri(target) + .header(CONTENT_TYPE_HEADER, APPLICATION_JSON) + .POST(HttpRequest.BodyPublishers.ofString(payload, StandardCharsets.UTF_8)) + .build(); + HttpResponse response = httpClient.send(request, HttpResponse.BodyHandlers.ofString()); + return new HttpBackendResponse(response.statusCode(), response.body()); + } + + private InetAddress[] resolveNonMetadataTarget(String host) throws IOException { + if (host == null || host.isBlank()) { + throw new IOException("integration URI has no host"); + } + return SsrfProtection.rejectMetadataAddresses(addressResolver.resolve(host), host); + } + + /** + * Sends the payload to the address the host resolved to, instead of letting a client resolve + * the name a second time. The shared {@link PinnedHttpClient} owns the request writing and the + * bounded response parsing, so HTTP_PROXY and the WebSocket integrations keep the same limits. + */ + private HttpBackendResponse postToPinnedHttpEndpoint(URI target, String payload) throws IOException { + InetAddress[] targets = resolveNonMetadataTarget(target.getHost()); + PinnedHttpClient.Response response = PinnedHttpClient.send( + targets[0], target, "POST", null, + Map.of(CONTENT_TYPE_HEADER, List.of(APPLICATION_JSON)), + payload.getBytes(StandardCharsets.UTF_8), BACKEND_TIMEOUT); + return new HttpBackendResponse( + response.statusCode(), new String(response.body(), StandardCharsets.UTF_8)); + } + /** * Handle a MOCK integration — no backend invocation. * Evaluates templateSelectionExpression against requestTemplates to determine status code, diff --git a/src/test/java/io/github/hectorvent/floci/services/apigatewayv2/websocket/WebSocketIntegrationInvokerHttpTargetTest.java b/src/test/java/io/github/hectorvent/floci/services/apigatewayv2/websocket/WebSocketIntegrationInvokerHttpTargetTest.java index aba9d36781..8926e21822 100644 --- a/src/test/java/io/github/hectorvent/floci/services/apigatewayv2/websocket/WebSocketIntegrationInvokerHttpTargetTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/apigatewayv2/websocket/WebSocketIntegrationInvokerHttpTargetTest.java @@ -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 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; + } }