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

* fix(apigateway): project HTTP API v2 cookies

* test(apigateway): cover cookie and header coexistence
This commit is contained in:
Saatvik Arya authored and GitHub committed 2026-08-18 20:16:34 -04:00
1 parent 41953d8820
commit 48880aa79a
2 files changed
+79 -10

No files matched your search

@@ -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)
@@ -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"));
}
}
}