Light, fluffy, and always free - The AWS Local Emulator alternative
fix(apigatewayv2): cap HTTP_PROXY integration responses at the 10 MB payload quota (#4226)
Niels Kootstraea60f70ad259
diff --git a/docs/services/api-gateway.md b/docs/services/api-gateway.mdindex d0e0ea1e1a..bf25e246b3 100644--- a/docs/services/api-gateway.md+++ b/docs/services/api-gateway.md@@ -252,6 +252,8 @@ A `MOCK` integration renders its request template and uses the `statusCode` it p`HTTP_PROXY` forwards the request to the integration's `uri` — with `{param}` placeholders resolved from the matched resource's path parameters — and relays the backend's status, headers and body unchanged. Per AWS, no request templates and no integration-response selection apply to `HTTP_PROXY`, so a backend `4xx`/`5xx` reaches the caller verbatim rather than being remapped. `integration.request.{header,querystring,path}.*` → `method.request.*` mappings are applied. Hop-by-hop headers (including `Host`) are stripped. An unreachable or failing backend yields `502`.+A backend response body larger than the 10 MB API Gateway payload quota yields `413` with `{"message":"Request Entity Too Large"}`. The same limit applies to HTTP API `HTTP_PROXY` integrations.+Passthrough keeps repeated values repeated, in both directions: `?tag=a&tag=b` reaches the backend as two `tag` parameters rather than one `tag=a,b`, a header sent twice arrives twice, and a backend that returns two `Set-Cookie` headers relays two to the caller. Comma-joining them would not be reversible, since a cookie's `Expires` attribute contains a comma of its own. An explicit `integration.request.header.X` or `integration.request.querystring.X` mapping overwrites, so it replaces any repeated inbound values with the single mapped one.`HTTP` (non-proxy) transforms in both directions instead: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.javaindex eedfe3816a..fa898e6bbd 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@@ -56,6 +56,9 @@ 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<String> HOP_BY_HOP = Set.of("connection", "keep-alive", "proxy-authenticate", "proxy-authorization",@@ -329,6 +332,8 @@ public class HttpProxyInvoker {if (finalUrl.startsWith("http://") && (hasHeader(builder, "Host") || isNamedHost(finalUrl))) {try {return invokeHttpPinned(finalUrl, method, builder, options.timeout());+ } catch (ResponseTooLargeException ignored) {+ return tooLargeResult();} catch (Exception e) {LOG.warnv("HTTP_PROXY backend call failed: {0}", e.getMessage());return errorResult("Bad Gateway: " + e.getMessage());@@ -365,14 +370,20 @@ public class HttpProxyInvoker {try {resolveNonMetadataTarget(hrb.build().uri().getHost());- HttpResponse<byte[]> resp =- clientFor(options).send(hrb.build(), HttpResponse.BodyHandlers.ofByteArray());+ HttpResponse<InputStream> resp =+ clientFor(options).send(hrb.build(), HttpResponse.BodyHandlers.ofInputStream());+ byte[] body;+ try (InputStream in = resp.body()) {+ body = readBounded(in);+ }Map<String, List<String>> respHeaders = new LinkedHashMap<>();for (Map.Entry<String, List<String>> e : resp.headers().map().entrySet()) {if (HOP_BY_HOP.contains(e.getKey().toLowerCase())) continue;respHeaders.put(e.getKey(), List.copyOf(e.getValue()));}- return new ProxyResult(resp.statusCode(), respHeaders, resp.body());+ return new ProxyResult(resp.statusCode(), respHeaders, body);+ } catch (ResponseTooLargeException ignored) {+ return tooLargeResult();} catch (Exception e) {LOG.warnv("HTTP_PROXY backend call failed: {0}", e.getMessage());return errorResult("Bad Gateway: " + e.getMessage());@@ -479,7 +490,7 @@ public class HttpProxyInvoker {int statusCode = Integer.parseInt(status[1]);Map<String, List<String>> headers = new LinkedHashMap<>();String transferEncoding = null;- int contentLength = -1;+ long contentLength = -1;for (int i = 1; i < lines.length; i++) {int separator = lines[i].indexOf(':');if (separator <= 0) {@@ -491,7 +502,7 @@ public class HttpProxyInvoker {transferEncoding = value;}if (name.equalsIgnoreCase("Content-Length")) {- contentLength = Integer.parseInt(value);+ contentLength = Long.parseLong(value);}if (!HOP_BY_HOP.contains(name.toLowerCase(Locale.ROOT))) {// Repeated header lines (Set-Cookie) accumulate rather than overwrite.@@ -504,7 +515,7 @@ public class HttpProxyInvoker {? new byte[0]: transferEncoding != null && transferEncoding.toLowerCase(Locale.ROOT).contains("chunked")? readChunkedBody(input)- : contentLength >= 0 ? input.readNBytes(contentLength) : input.readAllBytes();+ : contentLength >= 0 ? readContentLength(input, contentLength) : readBounded(input);return new ProxyResult(statusCode, headers, body);}@@ -525,11 +536,36 @@ public class HttpProxyInvoker {}}}+ 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) {@@ -578,6 +614,13 @@ public class HttpProxyInvoker {return base + "?" + sj;}+ private static ProxyResult tooLargeResult() {+ LOG.warnv("HTTP_PROXY backend response exceeds the {0}-byte payload quota", MAX_RESPONSE_BYTES);+ return ProxyResult.withSingleValueHeaders(413,+ Map.of("Content-Type", "application/json"),+ "{\"message\":\"Request Entity Too Large\"}".getBytes(StandardCharsets.UTF_8));+ }+private static ProxyResult errorResult(String message) {String body = "{\"message\":\"" + message.replace("\"", "\\\"") + "\"}";return ProxyResult.withSingleValueHeaders(502,diff --git a/src/test/java/io/github/hectorvent/floci/services/apigatewayv2/proxy/HttpProxyInvokerTest.java b/src/test/java/io/github/hectorvent/floci/services/apigatewayv2/proxy/HttpProxyInvokerTest.javaindex 71dc49088a..814cc22f46 100644--- a/src/test/java/io/github/hectorvent/floci/services/apigatewayv2/proxy/HttpProxyInvokerTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/apigatewayv2/proxy/HttpProxyInvokerTest.java@@ -8,8 +8,12 @@ import org.junit.jupiter.api.BeforeEach;import org.junit.jupiter.api.Test;import java.io.IOException;+import java.io.InputStream;+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.time.Duration;import java.util.LinkedHashMap;@@ -344,4 +348,134 @@ class HttpProxyInvokerTest {assertEquals(200, result.statusCode());assertEquals(0, result.body().length);}++ // API Gateway caps integration payloads at 10 MB and answers an oversized backend+ // response with 413 Request Entity Too Large.+ private static final int MAX_PAYLOAD_BYTES = 10 * 1024 * 1024;++ private void serveBody(int size, boolean chunked) {+ backend.removeContext("/");+ backend.createContext("/", exchange -> {+ exchange.sendResponseHeaders(200, chunked ? 0 : size);+ byte[] block = new byte[64 * 1024];+ try (var out = exchange.getResponseBody()) {+ for (int written = 0; written < size; written += block.length) {+ out.write(block, 0, Math.min(block.length, size - written));+ }+ } catch (IOException ignored) {+ // the proxy may stop reading once the limit is exceeded+ }+ });+ }++ private ProxyResult invokeLargeResponse(boolean overrideHost) {+ return invokeLargeResponse(backendPort, overrideHost);+ }++ private ProxyResult invokeLargeResponse(int port, boolean overrideHost) {+ Integration integration = httpProxyIntegration(+ "http://127.0.0.1:" + port + "/large",+ overrideHost ? Map.of("overwrite:header.Host", "lb.localhost.test") : null);+ RequestContext ctx = ctxFor("GET", "/wallet/large", "large",+ Map.of(), Map.of(), null, Map.of());+ return new HttpProxyInvoker().invoke(integration, ctx);+ }++ @Test+ void responseOfExactlyTenMegabytesIsReturned() {+ serveBody(MAX_PAYLOAD_BYTES, false);++ ProxyResult result = invokeLargeResponse(false);++ assertEquals(200, result.statusCode());+ assertEquals(MAX_PAYLOAD_BYTES, result.body().length);+ }++ @Test+ void responseOverTenMegabytesIsRejectedAsTooLarge() {+ serveBody(MAX_PAYLOAD_BYTES + 1, false);++ assertEquals(413, invokeLargeResponse(false).statusCode());+ }++ @Test+ void responseOverTenMegabytesIsRejectedWhenHostIsOverridden() {+ serveBody(MAX_PAYLOAD_BYTES + 1, false);++ assertEquals(413, invokeLargeResponse(true).statusCode());+ }++ @Test+ void chunkedResponseOverTenMegabytesIsRejectedWhenHostIsOverridden() {+ serveBody(MAX_PAYLOAD_BYTES + 1, true);++ assertEquals(413, invokeLargeResponse(true).statusCode());+ }++ @Test+ void responseOfExactlyTenMegabytesIsReturnedWhenHostIsOverridden() {+ serveBody(MAX_PAYLOAD_BYTES, false);++ ProxyResult result = invokeLargeResponse(true);++ assertEquals(200, result.statusCode());+ assertEquals(MAX_PAYLOAD_BYTES, result.body().length);+ }++ @Test+ void chunkedResponseOfExactlyTenMegabytesIsReturnedWhenHostIsOverridden() {+ serveBody(MAX_PAYLOAD_BYTES, true);++ ProxyResult result = invokeLargeResponse(true);++ assertEquals(200, result.statusCode());+ assertEquals(MAX_PAYLOAD_BYTES, result.body().length);+ }++ @Test+ void closeDelimitedResponseOfExactlyTenMegabytesIsReturnedWhenHostIsOverridden() throws Exception {+ try (ServerSocket server = serveCloseDelimitedBody(MAX_PAYLOAD_BYTES)) {+ ProxyResult result = invokeLargeResponse(server.getLocalPort(), true);++ assertEquals(200, result.statusCode());+ assertEquals(MAX_PAYLOAD_BYTES, result.body().length);+ }+ }++ @Test+ void closeDelimitedResponseOverTenMegabytesIsRejectedWhenHostIsOverridden() throws Exception {+ try (ServerSocket server = serveCloseDelimitedBody(MAX_PAYLOAD_BYTES + 1)) {+ assertEquals(413, invokeLargeResponse(server.getLocalPort(), true).statusCode());+ }+ }++ /** A backend that sends neither Content-Length nor chunked encoding and ends the body by closing. */+ private static ServerSocket serveCloseDelimitedBody(int size) throws IOException {+ ServerSocket server = new ServerSocket(0, 1, InetAddress.getLoopbackAddress());+ Thread thread = new Thread(() -> {+ try (Socket socket = server.accept()) {+ InputStream in = socket.getInputStream();+ int matched = 0;+ byte[] terminator = "\r\n\r\n".getBytes(StandardCharsets.US_ASCII);+ while (matched < terminator.length) {+ int b = in.read();+ if (b < 0) {+ return;+ }+ matched = b == terminator[matched] ? matched + 1 : (b == terminator[0] ? 1 : 0);+ }+ OutputStream out = socket.getOutputStream();+ out.write("HTTP/1.1 200 OK\r\nConnection: close\r\n\r\n".getBytes(StandardCharsets.US_ASCII));+ byte[] block = new byte[64 * 1024];+ for (int written = 0; written < size; written += block.length) {+ out.write(block, 0, Math.min(block.length, size - written));+ }+ } catch (IOException ignored) {+ // the proxy may stop reading once the limit is exceeded+ }+ });+ thread.setDaemon(true);+ thread.start();+ return server;+ }}