Skip to content

armgit / floci

Public Imported from GitHub
0

Light, fluffy, and always free - The AWS Local Emulator alternative

fix(apigatewayv2): encode binary Lambda request bodies (#2203)

Langning Zhang28e9f6ed63e5
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 bd68459b5b..1bf2d79dd4 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
@@ -43,8 +43,10 @@ import java.util.ArrayList;
import java.util.Base64;
import java.util.HashMap;
import java.util.List;
+import java.util.Locale;
import java.util.Map;
import java.util.Optional;
+import java.util.Set;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import java.util.regex.Matcher;
@@ -66,6 +68,15 @@ public class ApiGatewayExecuteController {
private static final Logger LOG = Logger.getLogger(ApiGatewayExecuteController.class);
private static final TypeReference<Map<String, Object>> MAP_TYPE = new TypeReference<>() {};
+ private static final Set<String> V2_TEXT_CONTENT_TYPES = Set.of(
+ MediaType.TEXT_PLAIN,
+ MediaType.TEXT_HTML,
+ "text/csv",
+ MediaType.TEXT_XML,
+ MediaType.APPLICATION_JSON,
+ MediaType.APPLICATION_XML,
+ "application/javascript",
+ "application/graphql");
private final ApiGatewayService apiGatewayService;
private final ApiGatewayV2Service apiGatewayV2Service;
@@ -1915,8 +1926,11 @@ public class ApiGatewayExecuteController {
? headers.getHeaderString("User-Agent") : "");
if (body != null && body.length > 0) {
- event.put("body", new String(body));
- event.put("isBase64Encoded", false);
+ boolean isText = isV2TextContentType(headers.getHeaderString(HttpHeaders.CONTENT_TYPE));
+ event.put("body", isText
+ ? new String(body, StandardCharsets.UTF_8)
+ : Base64.getEncoder().encodeToString(body));
+ event.put("isBase64Encoded", !isText);
} else {
event.putNull("body");
event.put("isBase64Encoded", false);
@@ -1929,6 +1943,25 @@ public class ApiGatewayExecuteController {
}
}
+ private static boolean isV2TextContentType(String contentType) {
+ if (contentType == null || contentType.isBlank()) {
+ return false;
+ }
+
+ try {
+ MediaType mediaType = MediaType.valueOf(contentType);
+ String type = (mediaType.getType() + "/" + mediaType.getSubtype()).toLowerCase(Locale.ROOT);
+ if (mediaType.getParameters().isEmpty()) {
+ return V2_TEXT_CONTENT_TYPES.contains(type);
+ }
+ return (MediaType.TEXT_PLAIN.equals(type) || MediaType.APPLICATION_JSON.equals(type))
+ && mediaType.getParameters().size() == 1
+ && StandardCharsets.UTF_8.name().equalsIgnoreCase(mediaType.getParameters().get("charset"));
+ } catch (IllegalArgumentException e) {
+ return false;
+ }
+ }
+
private String jsonMessage(String message) {
return objectMapper.createObjectNode().put("message", message).toString();
}
diff --git a/src/test/java/io/github/hectorvent/floci/services/apigateway/BuildV2ProxyEventBodyEncodingTest.java b/src/test/java/io/github/hectorvent/floci/services/apigateway/BuildV2ProxyEventBodyEncodingTest.java
new file mode 100644
index 0000000000..fb568a3842
--- /dev/null
+++ b/src/test/java/io/github/hectorvent/floci/services/apigateway/BuildV2ProxyEventBodyEncodingTest.java
@@ -0,0 +1,113 @@
+package io.github.hectorvent.floci.services.apigateway;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import io.github.hectorvent.floci.core.common.RegionResolver;
+import jakarta.ws.rs.core.HttpHeaders;
+import jakarta.ws.rs.core.MultivaluedHashMap;
+import jakarta.ws.rs.core.UriInfo;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.Arguments;
+import org.junit.jupiter.params.provider.MethodSource;
+
+import java.net.URI;
+import java.nio.charset.StandardCharsets;
+import java.util.Base64;
+import java.util.stream.Stream;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.junit.jupiter.params.provider.Arguments.arguments;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+class BuildV2ProxyEventBodyEncodingTest {
+
+ private static final byte[] REQUEST_BODY = "hello world".getBytes(StandardCharsets.UTF_8);
+ private static final String TEXT_BODY = "hello world";
+ private static final String BASE64_BODY = Base64.getEncoder().encodeToString(REQUEST_BODY);
+
+ private ApiGatewayExecuteController controller;
+ private ObjectMapper objectMapper;
+ private HttpHeaders headers;
+ private UriInfo uriInfo;
+
+ @BeforeEach
+ void setUp() throws Exception {
+ RegionResolver regionResolver = mock(RegionResolver.class);
+ when(regionResolver.getAccountId()).thenReturn("000000000000");
+
+ headers = mock(HttpHeaders.class);
+ when(headers.getRequestHeaders()).thenReturn(new MultivaluedHashMap<>());
+ when(headers.getHeaderString("User-Agent")).thenReturn(null);
+
+ uriInfo = mock(UriInfo.class);
+ when(uriInfo.getQueryParameters()).thenReturn(new MultivaluedHashMap<>());
+ when(uriInfo.getRequestUri()).thenReturn(new URI("http://localhost:4566/test/echo"));
+
+ objectMapper = new ObjectMapper();
+ controller = new ApiGatewayExecuteController(
+ null, null, null,
+ regionResolver, objectMapper, null,
+ null, null, null, null
+ );
+ }
+
+ @ParameterizedTest(name = "{0}")
+ @MethodSource("bodyEncodingCases")
+ void serializesBodyAccordingToContentType(
+ String caseName,
+ String contentType,
+ byte[] requestBody,
+ String expectedBody,
+ boolean expectedBase64Encoded) throws Exception {
+ when(headers.getHeaderString(HttpHeaders.CONTENT_TYPE)).thenReturn(contentType);
+
+ String json = controller.buildV2ProxyEvent(
+ "POST", "/echo", "POST /echo",
+ "abc123", "$default", headers, uriInfo, requestBody, "req-1");
+
+ JsonNode event = objectMapper.readTree(json);
+ if (expectedBody == null) {
+ assertTrue(event.path("body").isNull());
+ } else {
+ assertEquals(expectedBody, event.path("body").asText());
+ }
+ assertEquals(expectedBase64Encoded, event.path("isBase64Encoded").asBoolean());
+ }
+
+ private static Stream<Arguments> bodyEncodingCases() {
+ return Stream.of(
+ arguments("text/plain", "text/plain", REQUEST_BODY, TEXT_BODY, false),
+ arguments("text/plain UTF-8", "text/plain; charset=utf-8", REQUEST_BODY, TEXT_BODY, false),
+ arguments("text/plain uppercase UTF-8", "TEXT/PLAIN; CHARSET=UTF-8", REQUEST_BODY, TEXT_BODY, false),
+ arguments("text/html", "text/html", REQUEST_BODY, TEXT_BODY, false),
+ arguments("text/csv", "text/csv", REQUEST_BODY, TEXT_BODY, false),
+ arguments("text/xml", "text/xml", REQUEST_BODY, TEXT_BODY, false),
+ arguments("application/json", "application/json", REQUEST_BODY, TEXT_BODY, false),
+ arguments("application/json UTF-8", "application/json; charset=utf-8", REQUEST_BODY, TEXT_BODY, false),
+ arguments("application/json uppercase UTF-8", "APPLICATION/JSON; CHARSET=UTF-8",
+ REQUEST_BODY, TEXT_BODY, false),
+ arguments("application/xml", "application/xml", REQUEST_BODY, TEXT_BODY, false),
+ arguments("application/javascript", "application/javascript", REQUEST_BODY, TEXT_BODY, false),
+ arguments("application/graphql", "application/graphql", REQUEST_BODY, TEXT_BODY, false),
+ arguments("missing Content-Type", null, REQUEST_BODY, BASE64_BODY, true),
+ arguments("text/plain ISO-8859-1", "text/plain; charset=iso-8859-1", REQUEST_BODY, BASE64_BODY, true),
+ arguments("application/x-www-form-urlencoded", "application/x-www-form-urlencoded",
+ REQUEST_BODY, BASE64_BODY, true),
+ arguments("application/octet-stream", "application/octet-stream", REQUEST_BODY, BASE64_BODY, true),
+ arguments("application/x-protobuf", "application/x-protobuf", REQUEST_BODY, BASE64_BODY, true),
+ arguments("application/zip", "application/zip", REQUEST_BODY, BASE64_BODY, true),
+ arguments("application/pdf", "application/pdf", REQUEST_BODY, BASE64_BODY, true),
+ arguments("multipart/form-data", "multipart/form-data; boundary=test", REQUEST_BODY, BASE64_BODY, true),
+ arguments("image/png", "image/png", REQUEST_BODY, BASE64_BODY, true),
+ arguments("image/jpeg", "image/jpeg", REQUEST_BODY, BASE64_BODY, true),
+ arguments("image/gif", "image/gif", REQUEST_BODY, BASE64_BODY, true),
+ arguments("audio/mpeg", "audio/mpeg", REQUEST_BODY, BASE64_BODY, true),
+ arguments("video/mp4", "video/mp4", REQUEST_BODY, BASE64_BODY, true),
+ arguments("custom content type", "weird/unknown", REQUEST_BODY, BASE64_BODY, true),
+ arguments("empty body", "application/octet-stream", new byte[0], null, false)
+ );
+ }
+}
diff --git a/src/test/java/io/github/hectorvent/floci/services/apigatewayv2/ApiGatewayV2BinaryPayloadIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/apigatewayv2/ApiGatewayV2BinaryPayloadIntegrationTest.java
new file mode 100644
index 0000000000..bc72467e9b
--- /dev/null
+++ b/src/test/java/io/github/hectorvent/floci/services/apigatewayv2/ApiGatewayV2BinaryPayloadIntegrationTest.java
@@ -0,0 +1,141 @@
+package io.github.hectorvent.floci.services.apigatewayv2;
+
+import io.quarkus.test.junit.QuarkusTest;
+import io.restassured.http.ContentType;
+import org.junit.jupiter.api.MethodOrderer;
+import org.junit.jupiter.api.Order;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.TestMethodOrder;
+
+import java.util.Base64;
+
+import static io.restassured.RestAssured.given;
+import static org.hamcrest.Matchers.equalTo;
+import static org.hamcrest.Matchers.notNullValue;
+
+@QuarkusTest
+@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
+class ApiGatewayV2BinaryPayloadIntegrationTest {
+
+ private static final String FUNCTION_NAME = "httpv2-binary-payload-fn";
+ private static final byte[] BINARY_BODY = {(byte) 0xff, (byte) 0xfe, (byte) 0xfd, (byte) 0xfa};
+
+ private static String apiId;
+ private static String integrationId;
+
+ @Test
+ @Order(1)
+ void setupLambdaAndHttpApi() throws Exception {
+ String zip = WebSocketTestSupport.createLambdaZip("""
+ exports.handler = async (event) => ({
+ statusCode: 200,
+ headers: { "Content-Type": "application/json" },
+ body: JSON.stringify({
+ body: event.body,
+ isBase64Encoded: event.isBase64Encoded
+ })
+ });
+ """);
+
+ given()
+ .contentType(ContentType.JSON)
+ .body("""
+ {"FunctionName":"%s","Runtime":"nodejs20.x","Role":"arn:aws:iam::000000000000:role/lambda-role","Handler":"index.handler","Timeout":30,"Code":{"ZipFile":"%s"}}
+ """.formatted(FUNCTION_NAME, zip))
+ .when().post("/2015-03-31/functions")
+ .then().statusCode(201);
+
+ apiId = given()
+ .contentType(ContentType.JSON)
+ .body("""
+ {"name":"binary-payload-test","protocolType":"HTTP"}
+ """)
+ .when().post("/v2/apis")
+ .then()
+ .statusCode(201)
+ .body("apiId", notNullValue())
+ .extract().path("apiId");
+
+ given()
+ .contentType(ContentType.JSON)
+ .body("""
+ {"stageName":"test"}
+ """)
+ .when().post("/v2/apis/" + apiId + "/stages")
+ .then().statusCode(201);
+
+ integrationId = given()
+ .contentType(ContentType.JSON)
+ .body("""
+ {"integrationType":"AWS_PROXY","integrationUri":"arn:aws:lambda:us-east-1:000000000000:function:%s/invocations","integrationMethod":"POST","payloadFormatVersion":"2.0"}
+ """.formatted(FUNCTION_NAME))
+ .when().post("/v2/apis/" + apiId + "/integrations")
+ .then()
+ .statusCode(201)
+ .extract().path("integrationId");
+
+ given()
+ .contentType(ContentType.JSON)
+ .body("""
+ {"routeKey":"POST /echo","target":"integrations/%s"}
+ """.formatted(integrationId))
+ .when().post("/v2/apis/" + apiId + "/routes")
+ .then().statusCode(201);
+
+ given()
+ .contentType(ContentType.JSON)
+ .body("{}")
+ .when().post("/2015-03-31/functions/" + FUNCTION_NAME + "/invocations")
+ .then().statusCode(200);
+ }
+
+ @Test
+ @Order(10)
+ void binaryRequestIsBase64EncodedInLambdaEvent() {
+ given()
+ .contentType("application/octet-stream")
+ .body(BINARY_BODY)
+ .when().post("/execute-api/" + apiId + "/test/echo")
+ .then()
+ .statusCode(200)
+ .body("body", equalTo(Base64.getEncoder().encodeToString(BINARY_BODY)))
+ .body("isBase64Encoded", equalTo(true));
+ }
+
+ @Test
+ @Order(11)
+ void textRequestRemainsPlainInLambdaEvent() {
+ given()
+ .contentType("text/plain; charset=utf-8")
+ .body("hello world")
+ .when().post("/execute-api/" + apiId + "/test/echo")
+ .then()
+ .statusCode(200)
+ .body("body", equalTo("hello world"))
+ .body("isBase64Encoded", equalTo(false));
+ }
+
+ @Test
+ @Order(12)
+ void jsonRequestRemainsPlainInLambdaEvent() {
+ String jsonBody = "{\"message\":\"hello\"}";
+
+ given()
+ .contentType(ContentType.JSON)
+ .body(jsonBody)
+ .when().post("/execute-api/" + apiId + "/test/echo")
+ .then()
+ .statusCode(200)
+ .body("body", equalTo(jsonBody))
+ .body("isBase64Encoded", equalTo(false));
+ }
+
+ @Test
+ @Order(999)
+ void cleanup() {
+ if (apiId != null) {
+ given().when().delete("/v2/apis/" + apiId);
+ }
+ given().when().delete("/2015-03-31/functions/" + FUNCTION_NAME);
+ }
+}