Skip to content

armgit / floci

Public Imported from GitHub
0

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

fix(apigateway): project HTTP API v2 cookies (#2391)

Saatvik Arya48880aa79ae9
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 b7983114e3..6607b7294e 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
@@ -401,7 +401,7 @@ public class ApiGatewayExecuteController {
try {
InvokeResult result = lambdaService.invoke(region, functionName, eventJson.getBytes(),
InvocationType.RequestResponse);
- return buildProxyResponse(result);
+ return buildProxyResponse(result, false);
} catch (AwsException e) {
if (e.getHttpStatus() == 404) {
return Response.status(404)
@@ -839,7 +839,7 @@ public class ApiGatewayExecuteController {
}
}
- Response buildProxyResponse(InvokeResult result) {
+ Response buildProxyResponse(InvokeResult result, boolean httpApiV2) {
if (result.getPayload() == null || result.getPayload().length == 0) {
return Response.status(result.getFunctionError() != null ? 502 : result.getStatusCode()).build();
}
@@ -860,6 +860,12 @@ public class ApiGatewayExecuteController {
if (e.getValue().isArray()) e.getValue().forEach(v -> builder.header(e.getKey(), v.asText()));
});
}
+ if (httpApiV2) {
+ JsonNode cookies = node.get("cookies");
+ if (cookies != null && cookies.isArray()) {
+ cookies.forEach(cookie -> builder.header(HttpHeaders.SET_COOKIE, cookie.asText()));
+ }
+ }
JsonNode bodyNode = node.get("body");
if (bodyNode != null && !bodyNode.isNull()) {
@@ -1419,7 +1425,7 @@ public class ApiGatewayExecuteController {
try {
InvokeResult result = lambdaService.invoke(region, functionName,
eventJson.getBytes(), InvocationType.RequestResponse);
- return buildProxyResponse(result);
+ return buildProxyResponse(result, true);
} catch (AwsException e) {
if (e.getHttpStatus() == 404) {
return Response.status(404)
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 ab8feb2ab6..33ede13a8c 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
@@ -132,7 +132,7 @@ class ApiGatewayExecuteControllerTest {
ApiGatewayExecuteController controller = controller(new ObjectMapper());
Response response = controller.buildProxyResponse(proxyPayload("""
{"statusCode":404,"headers":{"content-type":"application/problem+json"},"body":"{}"}
- """));
+ """), false);
assertEquals("application/problem+json", response.getMediaType().toString());
}
@@ -143,7 +143,7 @@ class ApiGatewayExecuteControllerTest {
ApiGatewayExecuteController controller = controller(new ObjectMapper());
Response response = controller.buildProxyResponse(proxyPayload("""
{"statusCode":200,"headers":{"Content-Type":"text/plain"},"body":"hi"}
- """));
+ """), false);
assertEquals("text/plain", response.getMediaType().toString());
}
@@ -152,7 +152,7 @@ class ApiGatewayExecuteControllerTest {
ApiGatewayExecuteController controller = controller(new ObjectMapper());
Response response = controller.buildProxyResponse(proxyPayload("""
{"statusCode":200,"headers":{"CONTENT-TYPE":"application/xml"},"body":"<a/>"}
- """));
+ """), false);
assertEquals("application/xml", response.getMediaType().toString());
}
@@ -161,7 +161,7 @@ class ApiGatewayExecuteControllerTest {
ApiGatewayExecuteController controller = controller(new ObjectMapper());
Response response = controller.buildProxyResponse(proxyPayload("""
{"statusCode":200,"headers":{"X-Other":"value"},"body":"{}"}
- """));
+ """), false);
assertEquals("application/json", response.getMediaType().toString());
}
@@ -172,7 +172,7 @@ class ApiGatewayExecuteControllerTest {
ApiGatewayExecuteController controller = controller(new ObjectMapper());
Response response = controller.buildProxyResponse(proxyPayload("""
{"statusCode":200,"headers":{"content-type":null},"body":"{}"}
- """));
+ """), false);
assertEquals("application/json", response.getMediaType().toString());
}
@@ -183,7 +183,7 @@ class ApiGatewayExecuteControllerTest {
ApiGatewayExecuteController controller = controller(new ObjectMapper());
Response response = controller.buildProxyResponse(proxyPayload("""
{"statusCode":200,"multiValueHeaders":{"content-type":["application/xml"]},"body":"<a/>"}
- """));
+ """), false);
assertEquals("application/xml", response.getMediaType().toString());
}
@@ -197,7 +197,7 @@ class ApiGatewayExecuteControllerTest {
"headers":{"Content-Type":"text/plain"},\
"multiValueHeaders":{"Content-Type":["application/xml"]},\
"body":"<a/>"}
- """));
+ """), false);
assertEquals("application/xml", response.getMediaType().toString());
}
@@ -306,4 +306,67 @@ class ApiGatewayExecuteControllerTest {
verify(apiGatewayService).resolveRestApiRegion("us-east-1", "restapi1");
verify(apiGatewayService).getRestApi("ap-southeast-2", "restapi1");
}
+
+ @Test
+ void projectsHttpApiV2CookiesAsRepeatedSetCookieHeadersAlongsideResponseHeaders() {
+ ObjectMapper objectMapper = new ObjectMapper();
+ ApiGatewayExecuteController controller = controller(objectMapper);
+ InvokeResult result = new InvokeResult(
+ 200,
+ null,
+ """
+ {
+ "statusCode": 200,
+ "headers": {
+ "X-Trace": "cookie-projection"
+ },
+ "cookies": [
+ "session=one; Path=/; HttpOnly",
+ "csrf=two; Path=/; Secure"
+ ]
+ }
+ """.getBytes(StandardCharsets.UTF_8),
+ null,
+ "request-id");
+
+ try (Response response = controller.buildProxyResponse(result, true)) {
+ assertEquals(
+ List.of("session=one; Path=/; HttpOnly", "csrf=two; Path=/; Secure"),
+ response.getStringHeaders().get(HttpHeaders.SET_COOKIE));
+ assertEquals("cookie-projection", response.getHeaderString("X-Trace"));
+ }
+ }
+
+ @Test
+ void ignoresHttpApiV2CookiesForRestApiV1Responses() {
+ ObjectMapper objectMapper = new ObjectMapper();
+ ApiGatewayExecuteController controller = controller(objectMapper);
+ InvokeResult result = new InvokeResult(
+ 200,
+ null,
+ """
+ {
+ "statusCode": 200,
+ "headers": {
+ "X-Trace": "rest-v1"
+ },
+ "multiValueHeaders": {
+ "Set-Cookie": [
+ "legacy=one; Path=/; HttpOnly",
+ "legacy=two; Path=/; Secure"
+ ]
+ },
+ "cookies": ["http-api-v2=ignored; Path=/"]
+ }
+ """.getBytes(StandardCharsets.UTF_8),
+ null,
+ "request-id");
+
+ try (Response response = controller.buildProxyResponse(result, false)) {
+ assertEquals(
+ List.of("legacy=one; Path=/; HttpOnly", "legacy=two; Path=/; Secure"),
+ response.getStringHeaders().get(HttpHeaders.SET_COOKIE));
+ assertEquals("rest-v1", response.getHeaderString("X-Trace"));
+ }
+ }
}