mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(apigatewayv2): cap HTTP_PROXY integration responses at the 10 MB payload quota (#4226)
HttpProxyInvoker read the whole integration response into memory with no limit. Stop reading once a response passes API Gateway's 10 MB payload quota and return 413 Request Entity Too Large, for Content-Length, chunked and close-delimited bodies alike. REST and HTTP APIs share this invoker, so both get the limit.
This commit is contained in:
1 parent
317e007942
commit
ea60f70ad2
3 files changed
+185
-6
No files matched your search
+134
@@ -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;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user