From 78ec0bc6e7489e02456e0caaed9cc75af07655c5 Mon Sep 17 00:00:00 2001 From: Andre Kurait Date: Sat, 31 Oct 2026 19:16:10 -0500 Subject: [PATCH] fix(lambda): accept bounded X-Ray error cause headers (#7153) --- .../lambda/runtime/RuntimeApiServer.java | 4 ++ .../lambda/runtime/RuntimeApiServerTest.java | 56 ++++++++++++++++++- 2 files changed, 58 insertions(+), 2 deletions(-) diff --git a/src/main/java/io/github/hectorvent/floci/services/lambda/runtime/RuntimeApiServer.java b/src/main/java/io/github/hectorvent/floci/services/lambda/runtime/RuntimeApiServer.java index 5885a60f90..f7b81af1cf 100644 --- a/src/main/java/io/github/hectorvent/floci/services/lambda/runtime/RuntimeApiServer.java +++ b/src/main/java/io/github/hectorvent/floci/services/lambda/runtime/RuntimeApiServer.java @@ -73,6 +73,9 @@ public class RuntimeApiServer { private static final Logger LOG = Logger.getLogger(RuntimeApiServer.class); + // The Go runtime sends X-Ray error causes below 1 MiB, in addition to normal request headers. + private static final int MAX_RUNTIME_API_HEADER_SIZE = 1024 * 1024 + 16 * 1024; + private static final String RUNTIME_API_VERSION = "2018-06-01"; private static final String NEXT_PATH = "/" + RUNTIME_API_VERSION + "/runtime/invocation/next"; private static final String RESPONSE_PATH = "/" + RUNTIME_API_VERSION + "/runtime/invocation/:requestId/response"; @@ -613,6 +616,7 @@ public class RuntimeApiServer { if (started.isDone()) return; httpServer = vertx.createHttpServer(new HttpServerOptions() .setMaxFormAttributeSize(-1) + .setMaxHeaderSize(MAX_RUNTIME_API_HEADER_SIZE) // Real AWS's Runtime/Extensions API is plain HTTP/1.1. Vert.x defaults to // accepting an h2c upgrade, which the JDK HttpClient used in tests and by some // language runtimes opts into unless told otherwise. That multiplexes every diff --git a/src/test/java/io/github/hectorvent/floci/services/lambda/runtime/RuntimeApiServerTest.java b/src/test/java/io/github/hectorvent/floci/services/lambda/runtime/RuntimeApiServerTest.java index 226d11de17..e938a80b02 100644 --- a/src/test/java/io/github/hectorvent/floci/services/lambda/runtime/RuntimeApiServerTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/lambda/runtime/RuntimeApiServerTest.java @@ -11,6 +11,8 @@ import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Timeout; import org.junit.jupiter.api.condition.EnabledOnOs; import org.junit.jupiter.api.condition.OS; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; import java.io.IOException; import java.net.BindException; @@ -337,7 +339,52 @@ class RuntimeApiServerTest { assertErrorEndpointReportsUnhandled("req-plain-header", "ValueError"); } + @ParameterizedTest + @ValueSource(ints = {8192, 1024 * 1024 - 1}) + @Timeout(15) + void errorEndpoint_acceptsGoRuntimeXrayCauseHeaders(int headerBytes) throws Exception { + String cause = "{\"message\":\"" + "x".repeat(headerBytes - 14) + "\"}"; + assertEquals(headerBytes, cause.length()); + assertErrorEndpointReportsUnhandled("req-xray-header", "Runtime.UserException", cause); + } + + @Test + @Timeout(15) + void errorEndpoint_rejectsHeadersBeyondItsBudgetWithoutConsumingInvocation() throws Exception { + String requestId = "req-oversized-header"; + String payload = "{\"errorMessage\":\"original failure\",\"errorType\":\"X\"}"; + CompletableFuture resultFuture = new CompletableFuture<>(); + server.enqueue(new PendingInvocation(requestId, "{}".getBytes(), System.currentTimeMillis() + 60_000, + "arn:aws:lambda:us-east-1:000000000000:function:test", resultFuture)); + HttpResponse next = httpClient.send(HttpRequest.newBuilder() + .uri(URI.create("http://localhost:" + port + "/2018-06-01/runtime/invocation/next")) + .GET().build(), HttpResponse.BodyHandlers.ofString()); + assertEquals(200, next.statusCode()); + URI error = URI.create("http://localhost:" + port + "/2018-06-01/runtime/invocation/" + requestId + "/error"); + + HttpResponse rejected = httpClient.send(HttpRequest.newBuilder(error) + .header("Lambda-Runtime-Function-Xray-Error-Cause", "x".repeat(1024 * 1024 + 16 * 1024 + 1)) + .POST(HttpRequest.BodyPublishers.ofString(payload)).build(), HttpResponse.BodyHandlers.ofString()); + assertEquals(431, rejected.statusCode()); + assertFalse(resultFuture.isDone()); + + // The decoder closes the rejected connection; retry independently of when its EOF arrives. + try (HttpClient retryClient = HttpClient.newHttpClient()) { + HttpResponse retry = retryClient.send(HttpRequest.newBuilder(error) + .POST(HttpRequest.BodyPublishers.ofString(payload)).build(), HttpResponse.BodyHandlers.ofString()); + assertEquals(202, retry.statusCode()); + } + InvokeResult result = resultFuture.get(2, TimeUnit.SECONDS); + assertEquals("Unhandled", result.getFunctionError()); + assertEquals(payload, new String(result.getPayload())); + } + private void assertErrorEndpointReportsUnhandled(String requestId, String errorTypeHeader) throws Exception { + assertErrorEndpointReportsUnhandled(requestId, errorTypeHeader, null); + } + + private void assertErrorEndpointReportsUnhandled(String requestId, String errorTypeHeader, + String xrayCause) throws Exception { CompletableFuture resultFuture = new CompletableFuture<>(); PendingInvocation invocation = new PendingInvocation( requestId, "{}".getBytes(), System.currentTimeMillis() + 60_000, @@ -355,11 +402,16 @@ class RuntimeApiServerTest { if (errorTypeHeader != null) { errorRequest.header("Lambda-Runtime-Function-Error-Type", errorTypeHeader); } - httpClient.send(errorRequest.build(), HttpResponse.BodyHandlers.ofString()); + if (xrayCause != null) { + errorRequest.header("Lambda-Runtime-Function-Xray-Error-Cause", xrayCause); + } + HttpResponse ack = httpClient.send(errorRequest.build(), HttpResponse.BodyHandlers.ofString()); + assertEquals(202, ack.statusCode()); + assertEquals("OK", new JsonObject(ack.body()).getString("status")); InvokeResult result = resultFuture.get(2, TimeUnit.SECONDS); assertEquals("Unhandled", result.getFunctionError()); - assertTrue(new String(result.getPayload()).contains("boom")); + assertEquals("{\"errorMessage\":\"boom\",\"errorType\":\"X\"}", new String(result.getPayload())); } /**