fix(apigatewayv2): answer an HTTP API's Lambda proxy body it cannot read with 500 (#6012)

* fix(apigatewayv2): answer an HTTP API's Lambda proxy body it cannot read with 500

Review of #5985: the catch at the end of buildProxyResponse still
answered an HTTP API with 502 and the function's output, for example
when isBase64Encoded is set on a body that is not base64.

Measured on HTTP APIs (eu-west-1), with payload formats 1.0 and 2.0:
API Gateway answers 500 with {"message":"Internal Server Error"} a body
that is an object or a list, an isBase64Encoded it cannot read as a
boolean, and a body said to be base64 that does not decode. It reads
isBase64Encoded's text as Go's strconv.ParseBool does: "True", "TRUE",
"t" and 1 are true, "F" and 0 false, and "tRuE", "yes", 2 and 1.5 are
not read. Floci read true, the text "true" and any integer but 0 as
true, so it relayed "True" and "t" bodies undecoded, "yes" ones as
plain text, decoded a body under 2, and answered an object body 200
with an empty body. A number or a boolean body is still its text.

The catch now answers an HTTP API with the same 500 for a result Floci
cannot parse at all; a REST API keeps its 502.

* refactor(apigatewayv2): decode an HTTP API's base64 body once

Review of #6012: the readability check decoded a base64 body that the
response then decoded again, a second decode and array for every
encoded reply. The catch at the end of buildProxyResponse already
answers an HTTP API with 500 and the generic message, so the check now
looks only at the body's shape, and a body said to be base64 that does
not decode fails where it is decoded and reaches that catch.
This commit is contained in:
Alessandro Lattao authored and GitHub committed 2026-10-19 16:40:26 -04:00
1 parent 65e35adf54
commit 28799ed352
5 files changed
+91 -12

No files matched your search

@@ -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<String> TRUE_TEXT = Set.of("1", "t", "T", "TRUE", "true", "True");
private static final Set<String> 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