diff --git a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/ApiGatewayV2LambdaProxyResponseTest.java b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/ApiGatewayV2LambdaProxyResponseTest.java index 2853601a7e..0e0ec6dd51 100644 --- a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/ApiGatewayV2LambdaProxyResponseTest.java +++ b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/ApiGatewayV2LambdaProxyResponseTest.java @@ -26,7 +26,9 @@ import static org.assertj.core.api.Assertions.assertThat; * Measured on AWS (eu-west-1, 2026-10-19): a function that raises, a 1.0 result without a * statusCode, and a statusCode that is not a whole number from 100 to 599 are answered 500 with * {"message":"Internal Server Error"}; a statusCode given as text that spells one is taken; format - * 2.0 takes a result without a statusCode as a 200 whose body is the result itself. + * 2.0 takes a result without a statusCode as a 200 whose body is the result itself. A body that is + * an object, one said to be base64 that is not, and an isBase64Encoded of "yes" are answered 500 + * too, while "True" is read as true. */ @DisplayName("API Gateway V2 Lambda proxy responses") class ApiGatewayV2LambdaProxyResponseTest { @@ -109,7 +111,15 @@ class ApiGatewayV2LambdaProxyResponseTest { "2.0|highstatus|500|" + ERROR, "1.0|textstatus|201|x", "2.0|textstatus|201|x", - "2.0|nostatus|200|{\"body\":\"x\",\"headers\":{\"x-probe\":\"1\"}}" + "2.0|nostatus|200|{\"body\":\"x\",\"headers\":{\"x-probe\":\"1\"}}", + "1.0|badbase64|500|" + ERROR, + "2.0|badbase64|500|" + ERROR, + "1.0|bodyobject|500|" + ERROR, + "2.0|bodyobject|500|" + ERROR, + "1.0|base64yes|500|" + ERROR, + "2.0|base64yes|500|" + ERROR, + "1.0|base64capital|200|hello", + "2.0|base64capital|200|hello" }) void aResultIsAnsweredAsAwsReadsIt(String format, String mode, int status, String body) throws Exception { HttpResponse response = get(format, mode); diff --git a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/LambdaUtils.java b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/LambdaUtils.java index d8d132f5a7..1cdc39fbe6 100644 --- a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/LambdaUtils.java +++ b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/LambdaUtils.java @@ -337,7 +337,8 @@ public final class LambdaUtils { /** * ZIP containing a Node.js handler that returns the Lambda proxy result its {@code mode} path * parameter names: one that raises, one without a statusCode, one whose statusCode is not a - * number, one whose statusCode is text that spells one, and one outside 100 to 599. + * number, one whose statusCode is text that spells one, one outside 100 to 599, and bodies an HTTP + * API cannot read (an object, base64 that is not, an isBase64Encoded of "yes") or reads ("True"). */ public static byte[] proxyResponseZip() { String code = """ @@ -348,6 +349,10 @@ public final class LambdaUtils { if (mode === 'badstatus') return { statusCode: 'abc', body: 'x' }; if (mode === 'textstatus') return { statusCode: '201', body: 'x' }; if (mode === 'highstatus') return { statusCode: 600, body: 'x' }; + if (mode === 'badbase64') return { statusCode: 200, body: 'not base64!', isBase64Encoded: true }; + if (mode === 'bodyobject') return { statusCode: 200, body: { a: 1 } }; + if (mode === 'base64yes') return { statusCode: 200, body: 'aGVsbG8=', isBase64Encoded: 'yes' }; + if (mode === 'base64capital') return { statusCode: 200, body: 'aGVsbG8=', isBase64Encoded: 'True' }; return { statusCode: 200, body: 'ok' }; }; """; diff --git a/docs/services/api-gateway.md b/docs/services/api-gateway.md index 9635144a07..ecaeb4bfb2 100644 --- a/docs/services/api-gateway.md +++ b/docs/services/api-gateway.md @@ -659,8 +659,11 @@ this default hostname with `404 Not Found`, matching AWS HTTP API behavior. A Lambda proxy integration's response is read as AWS reads it (measured): a function that raises, a format 1.0 result that is not an object with a `statusCode`, and a `statusCode` that is not a whole number from 100 to 599 (text that spells one, such as `"201"`, is taken) are answered `500` with -`{"message":"Internal Server Error"}`. Format 2.0 takes a result without a `statusCode` as a `200` -whose body is the result itself. +`{"message":"Internal Server Error"}`, and so are a `body` that is an object or a list, an +`isBase64Encoded` that Go's `strconv.ParseBool` would not read (`"True"`, `"t"` and `1` are true, +`"yes"` and `2` are not read), and a `body` said to be base64 that does not decode. A number or a +boolean `body` is its text. Format 2.0 takes a result without a `statusCode` as a `200` whose body +is the result itself. An API with a `corsConfiguration` applies it as AWS does (measured). It ignores the CORS headers an integration returns, and sets its own on whatever answers a route: the integration, or API Gateway diff --git a/src/main/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayExecuteController.java b/src/main/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayExecuteController.java index 13db68e896..e5c338c672 100644 --- a/src/main/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayExecuteController.java +++ b/src/main/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayExecuteController.java @@ -1605,11 +1605,15 @@ public class ApiGatewayExecuteController { : result.getPayload(); return Response.status(200).entity(inferredBody).type(MediaType.APPLICATION_JSON).build(); } - if (httpApi && httpApiStatusCode(node) == null) { + Boolean base64 = httpApi ? httpApiBase64(node) : Boolean.valueOf(node.path("isBase64Encoded").asBoolean(false)); + if (httpApi && (httpApiStatusCode(node) == null || base64 == null + || node.path("body").isContainerNode())) { // Measured (eu-west-1, 2026-10-19): an HTTP API answers a response it cannot read // with 500 and its generic message, in format 1.0 a result that is not an object with // a statusCode, and in either format a statusCode that is not a whole number from 100 - // to 599, though text spelling one ("201") is taken. + // to 599, though text spelling one ("201") is taken, a body that is an object or a + // list, and an isBase64Encoded it cannot read as a boolean. A body it says is base64 + // that does not decode reaches the catch below. return httpApiError(500, "Internal Server Error"); } int statusCode = httpApi ? httpApiStatusCode(node) : node.path("statusCode").asInt(200); @@ -1637,8 +1641,7 @@ public class ApiGatewayExecuteController { JsonNode bodyNode = node.get("body"); if (bodyNode != null && !bodyNode.isNull()) { String bodyStr = bodyNode.asText(); - boolean isBase64 = node.path("isBase64Encoded").asBoolean(false); - byte[] bytes = isBase64 ? Base64.getDecoder().decode(bodyStr) : bodyStr.getBytes(); + byte[] bytes = base64 ? Base64.getDecoder().decode(bodyStr) : bodyStr.getBytes(); String ct = findHeaderIgnoreCase(multiHeaders, "Content-Type") .or(() -> findHeaderIgnoreCase(respHeaders, "Content-Type")) .orElse(MediaType.APPLICATION_JSON); @@ -1647,6 +1650,11 @@ public class ApiGatewayExecuteController { return builder.build(); } catch (Exception e) { LOG.warnv("Failed to parse Lambda response: {0}", e.getMessage()); + if (httpApi) { + // A result an HTTP API cannot parse, and a body said to be base64 that does not + // decode (measured), are answered as a response it cannot read. + return httpApiError(500, "Internal Server Error"); + } return Response.status(502).entity(result.getPayload()).type(MediaType.APPLICATION_JSON).build(); } } @@ -1657,6 +1665,25 @@ public class ApiGatewayExecuteController { } private static final Pattern WHOLE_STATUS = Pattern.compile("\\d{1,3}"); + private static final Set TRUE_TEXT = Set.of("1", "t", "T", "TRUE", "true", "True"); + private static final Set FALSE_TEXT = Set.of("0", "f", "F", "FALSE", "false", "False"); + + /** + * Whether an HTTP API reads a Lambda proxy response's body as base64, or null when it cannot read + * isBase64Encoded: its text read as Go's strconv.ParseBool does (measured: "True", "TRUE", "t" and + * 1 are true, "F" and 0 false, and "tRuE", "yes", 2 and 1.5 unreadable). Left out, it is false. + */ + private static Boolean httpApiBase64(JsonNode response) { + JsonNode flag = response.path("isBase64Encoded"); + if (flag.isMissingNode() || flag.isNull()) { + return false; + } + String text = flag.isContainerNode() ? "" : flag.asText(); + if (TRUE_TEXT.contains(text)) { + return true; + } + return FALSE_TEXT.contains(text) ? false : null; + } /** * The status an HTTP API takes from a Lambda proxy response, or null when it cannot read one: a diff --git a/src/test/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayExecuteControllerTest.java b/src/test/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayExecuteControllerTest.java index 71c52dcd8a..2f0aaf27fa 100644 --- a/src/test/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayExecuteControllerTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayExecuteControllerTest.java @@ -256,9 +256,11 @@ class ApiGatewayExecuteControllerTest { /** * Measured on HTTP APIs (eu-west-1, 2026-10-19), for payload formats 1.0 and 2.0: a 1.0 result * that is not an object with a statusCode, and a statusCode that is not a whole number from 100 - * to 599, are answered with 500 and the generic message; a statusCode given as text that spells - * one is taken, and headers that are not an object are left out. A function that raises is - * covered below. + * to 599, are answered with 500 and the generic message, and so are a body that is an object or a + * list, an isBase64Encoded that strconv.ParseBool would not read, and a body said to be base64 + * that does not decode; a statusCode given as text that spells one is taken, headers that are + * not an object are left out, and a number or a boolean body is its text. A function that raises + * is covered below. */ @ParameterizedTest(name = "{0} payload {1}") @MethodSource("httpApiProxyResponses") @@ -288,6 +290,38 @@ class ApiGatewayExecuteControllerTest { Arguments.of("{\"statusCode\":\"201\",\"body\":\"x\"}", "2.0", 201, "x"), Arguments.of("{\"statusCode\":201,\"headers\":\"x\",\"body\":\"x\"}", "1.0", 201, "x"), Arguments.of("{\"statusCode\":201,\"headers\":\"x\",\"body\":\"x\"}", "2.0", 201, "x"), + Arguments.of("{\"statusCode\":200,\"body\":\"not base64!\",\"isBase64Encoded\":true}", "1.0", 500, error), + Arguments.of("{\"statusCode\":200,\"body\":\"not base64!\",\"isBase64Encoded\":true}", "2.0", 500, error), + Arguments.of("{\"statusCode\":200,\"body\":{\"a\":1}}", "1.0", 500, error), + Arguments.of("{\"statusCode\":200,\"body\":{\"a\":1}}", "2.0", 500, error), + Arguments.of("{\"statusCode\":200,\"body\":[\"a\"]}", "1.0", 500, error), + Arguments.of("{\"statusCode\":200,\"body\":[\"a\"]}", "2.0", 500, error), + Arguments.of("{\"statusCode\":200,\"body\":\"aGVsbG8=\",\"isBase64Encoded\":\"yes\"}", "1.0", 500, error), + Arguments.of("{\"statusCode\":200,\"body\":\"aGVsbG8=\",\"isBase64Encoded\":\"yes\"}", "2.0", 500, error), + Arguments.of("{\"statusCode\":200,\"body\":\"aGVsbG8=\",\"isBase64Encoded\":\"tRuE\"}", "1.0", 500, error), + Arguments.of("{\"statusCode\":200,\"body\":\"aGVsbG8=\",\"isBase64Encoded\":\"tRuE\"}", "2.0", 500, error), + Arguments.of("{\"statusCode\":200,\"body\":\"aGVsbG8=\",\"isBase64Encoded\":2}", "1.0", 500, error), + Arguments.of("{\"statusCode\":200,\"body\":\"aGVsbG8=\",\"isBase64Encoded\":2}", "2.0", 500, error), + Arguments.of("{\"statusCode\":200,\"body\":\"aGVsbG8=\",\"isBase64Encoded\":1.5}", "1.0", 500, error), + Arguments.of("{\"statusCode\":200,\"body\":\"aGVsbG8=\",\"isBase64Encoded\":1.5}", "2.0", 500, error), + Arguments.of("{\"statusCode\":200,\"body\":\"aGVsbG8=\",\"isBase64Encoded\":true}", "1.0", 200, "hello"), + Arguments.of("{\"statusCode\":200,\"body\":\"aGVsbG8=\",\"isBase64Encoded\":true}", "2.0", 200, "hello"), + Arguments.of("{\"statusCode\":200,\"body\":\"aGVsbG8=\",\"isBase64Encoded\":\"True\"}", "1.0", 200, "hello"), + Arguments.of("{\"statusCode\":200,\"body\":\"aGVsbG8=\",\"isBase64Encoded\":\"True\"}", "2.0", 200, "hello"), + Arguments.of("{\"statusCode\":200,\"body\":\"aGVsbG8=\",\"isBase64Encoded\":\"TRUE\"}", "1.0", 200, "hello"), + Arguments.of("{\"statusCode\":200,\"body\":\"aGVsbG8=\",\"isBase64Encoded\":\"TRUE\"}", "2.0", 200, "hello"), + Arguments.of("{\"statusCode\":200,\"body\":\"aGVsbG8=\",\"isBase64Encoded\":\"t\"}", "1.0", 200, "hello"), + Arguments.of("{\"statusCode\":200,\"body\":\"aGVsbG8=\",\"isBase64Encoded\":\"t\"}", "2.0", 200, "hello"), + Arguments.of("{\"statusCode\":200,\"body\":\"aGVsbG8=\",\"isBase64Encoded\":1}", "1.0", 200, "hello"), + Arguments.of("{\"statusCode\":200,\"body\":\"aGVsbG8=\",\"isBase64Encoded\":1}", "2.0", 200, "hello"), + Arguments.of("{\"statusCode\":200,\"body\":\"aGVsbG8=\",\"isBase64Encoded\":\"F\"}", "1.0", 200, "aGVsbG8="), + Arguments.of("{\"statusCode\":200,\"body\":\"aGVsbG8=\",\"isBase64Encoded\":\"F\"}", "2.0", 200, "aGVsbG8="), + Arguments.of("{\"statusCode\":200,\"body\":\"aGVsbG8=\",\"isBase64Encoded\":0}", "1.0", 200, "aGVsbG8="), + Arguments.of("{\"statusCode\":200,\"body\":\"aGVsbG8=\",\"isBase64Encoded\":0}", "2.0", 200, "aGVsbG8="), + Arguments.of("{\"statusCode\":200,\"body\":5}", "1.0", 200, "5"), + Arguments.of("{\"statusCode\":200,\"body\":5}", "2.0", 200, "5"), + Arguments.of("{\"statusCode\":200,\"body\":true}", "1.0", 200, "true"), + Arguments.of("{\"statusCode\":200,\"body\":true}", "2.0", 200, "true"), Arguments.of("null", "2.0", 200, "null"), Arguments.of("5", "2.0", 200, "5")); }